1This code injects an LLVM modulemap for glibc and libstdc++ by overriding
2specific VFS paths. In order to do that, it needs to know the actual locations
3of glibc and libstdc++, but it only searches `-sysroot` and fails. Here we
4patch it to also consider `-idirafter` and `-isystem` as added by cc-wrapper.
5
6--- a/lib/ClangImporter/ClangIncludePaths.cpp
7+++ b/lib/ClangImporter/ClangIncludePaths.cpp
8@@ -120,6 +120,7 @@ static clang::driver::Driver createClangDriver(const ASTContext &ctx) {
9 /// \return a path without dots (`../`, './').
10 static llvm::Optional<Path>
11 findFirstIncludeDir(const llvm::opt::InputArgList &args,
12+ const llvm::opt::ArgList &DriverArgs,
13 const ArrayRef<const char *> expectedFileNames) {
14 // C++ stdlib paths are added as `-internal-isystem`.
15 std::vector<std::string> includeDirs =
16@@ -128,6 +129,14 @@ findFirstIncludeDir(const llvm::opt::InputArgList &args,
17 llvm::append_range(includeDirs,
18 args.getAllArgValues(
19 clang::driver::options::OPT_internal_externc_isystem));
20+ // Nix adds the C stdlib include path using `-idirafter`.
21+ llvm::append_range(includeDirs,
22+ DriverArgs.getAllArgValues(
23+ clang::driver::options::OPT_idirafter));
24+ // Nix adds the C++ stdlib include path using `-isystem`.
25+ llvm::append_range(includeDirs,
26+ DriverArgs.getAllArgValues(
27+ clang::driver::options::OPT_isystem));
28
29 for (const auto &includeDir : includeDirs) {
30 Path dir(includeDir);
31@@ -193,7 +202,7 @@ getGlibcFileMapping(ASTContext &ctx) {
32 // Ideally we would check that all of the headers referenced from the
33 // modulemap are present.
34 Path glibcDir;
35- if (auto dir = findFirstIncludeDir(parsedIncludeArgs,
36+ if (auto dir = findFirstIncludeDir(parsedIncludeArgs, clangDriverArgs,
37 {"inttypes.h", "unistd.h", "stdint.h"})) {
38 glibcDir = dir.value();
39 } else {
40@@ -251,7 +260,7 @@ getLibStdCxxFileMapping(ASTContext &ctx) {
41 auto parsedStdlibArgs = parseClangDriverArgs(clangDriver, stdlibArgStrings);
42
43 Path cxxStdlibDir;
44- if (auto dir = findFirstIncludeDir(parsedStdlibArgs,
45+ if (auto dir = findFirstIncludeDir(parsedStdlibArgs, clangDriverArgs,
46 {"cstdlib", "string", "vector"})) {
47 cxxStdlibDir = dir.value();
48 } else {