topisani updated this revision to Diff 240643.
topisani marked an inline comment as done.
topisani added a comment.

Addressed CR comments

I'm sure about the tests, I basically just added to the one test that already 
exists that it passes one of each "type" of preservable argument.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73453/new/

https://reviews.llvm.org/D73453

Files:
  clang-tools-extra/clangd/QueryDriverDatabase.cpp
  clang-tools-extra/clangd/test/system-include-extractor.test

Index: clang-tools-extra/clangd/test/system-include-extractor.test
===================================================================
--- clang-tools-extra/clangd/test/system-include-extractor.test
+++ clang-tools-extra/clangd/test/system-include-extractor.test
@@ -7,6 +7,9 @@
 # %temp_dir%/my/dir2 as include search paths.
 # RUN: echo '#!/bin/bash' >> %t.dir/my_driver.sh
 # RUN: echo '[ "$0" = "%t.dir/my_driver.sh" ] || exit' >> %t.dir/my_driver.sh
+# RUN: echo '[[ " $* " =~ " -nostdinc " ]] || exit' >> %t.dir/my_driver.sh
+# RUN: echo '[[ " $* " =~ " --sysroot /my/sysroot/path " ]] || exit' >> %t.dir/my_driver.sh
+# RUN: echo '[[ " $* " =~ " -isysroot=/isysroot " ]] || exit' >> %t.dir/my_driver.sh
 # RUN: echo 'echo line to ignore >&2' >> %t.dir/my_driver.sh
 # RUN: echo 'echo -e "#include <...> search starts here:\r" >&2' >> %t.dir/my_driver.sh
 # RUN: echo 'echo %t.dir/my/dir/ >&2' >> %t.dir/my_driver.sh
@@ -22,7 +25,7 @@
 
 # Generate a compile_commands.json that will query the mock driver we've
 # created. Which should add a.h and b.h into include search path.
-# RUN: echo '[{"directory": "%/t.dir", "command": "%/t.dir/my_driver.sh the-file.cpp", "file": "the-file.cpp"}]' > %t.dir/compile_commands.json
+# RUN: echo '[{"directory": "%/t.dir", "command": "%/t.dir/my_driver.sh the-file.cpp -nostdinc --sysroot /my/sysroot/path -isysroot=/isysroot", "file": "the-file.cpp"}]' > %t.dir/compile_commands.json
 
 # RUN: sed -e "s|INPUT_DIR|%/t.dir|g" %s > %t.test.1
 # On Windows, we need the URI in didOpen to look like "uri":"file:///C:/..."
Index: clang-tools-extra/clangd/QueryDriverDatabase.cpp
===================================================================
--- clang-tools-extra/clangd/QueryDriverDatabase.cpp
+++ clang-tools-extra/clangd/QueryDriverDatabase.cpp
@@ -85,9 +85,10 @@
   return SystemIncludes;
 }
 
-std::vector<std::string> extractSystemIncludes(PathRef Driver,
-                                               llvm::StringRef Lang,
-                                               llvm::Regex &QueryDriverRegex) {
+std::vector<std::string>
+extractSystemIncludes(PathRef Driver, llvm::StringRef Lang,
+                      llvm::ArrayRef<std::string> CommandLine,
+                      llvm::Regex &QueryDriverRegex) {
   trace::Span Tracer("Extract system includes");
   SPAN_ATTACH(Tracer, "driver", Driver);
   SPAN_ATTACH(Tracer, "lang", Lang);
@@ -120,14 +121,37 @@
   llvm::Optional<llvm::StringRef> Redirects[] = {
       {""}, {""}, llvm::StringRef(StdErrPath)};
 
-  // Should we also preserve flags like "-sysroot", "-nostdinc" ?
-  const llvm::StringRef Args[] = {Driver, "-E", "-x", Lang, "-", "-v"};
+  llvm::SmallVector<llvm::StringRef, 12> Args = {Driver, "-E", "-x",
+                                                 Lang,   "-",  "-v"};
+
+  // These flags will be preserved
+  const llvm::StringRef FlagsToPreserve[] = {
+      "-nostdinc", "--no-standard-includes", "-nostdinc++", "-nobuiltininc"};
+  // This and the following argument will be preserved
+  const llvm::StringRef ArgsToPreserve[] = {"--sysroot", "-isysroot"};
+  // Flags starting with these strings will be preserved
+  const llvm::StringRef FlagPrefixesToPreserve[] = {"--sysroot=", "-isysroot="};
+
+  for (size_t I = 0, E = CommandLine.size(); I < E; ++I) {
+    llvm::StringRef Arg = CommandLine[I];
+    if (llvm::any_of(FlagsToPreserve, [&Arg](auto &&S) { return S == Arg; })) {
+      Args.push_back(Arg);
+    } else if (llvm::any_of(ArgsToPreserve,
+                            [&Arg](auto &&S) { return S == Arg; }) &&
+               I + 1 < E) {
+      Args.push_back(Arg);
+      Args.push_back(CommandLine[I + 1]);
+    } else if (llvm::any_of(FlagPrefixesToPreserve,
+                            [&Arg](auto &&S) { return Arg.startswith(S); })) {
+      Args.push_back(Arg);
+    }
+  }
 
   if (int RC = llvm::sys::ExecuteAndWait(Driver, Args, /*Env=*/llvm::None,
                                          Redirects)) {
     elog("System include extraction: driver execution failed with return code: "
-         "{0}",
-         llvm::to_string(RC));
+         "{0}. Args: ['{1}']",
+         llvm::to_string(RC), llvm::join(Args, "', '"));
     return {};
   }
 
@@ -247,8 +271,8 @@
       if (It != DriverToIncludesCache.end())
         SystemIncludes = It->second;
       else
-        DriverToIncludesCache[Key] = SystemIncludes =
-            extractSystemIncludes(Key.first, Key.second, QueryDriverRegex);
+        DriverToIncludesCache[Key] = SystemIncludes = extractSystemIncludes(
+            Key.first, Key.second, Cmd->CommandLine, QueryDriverRegex);
     }
 
     return addSystemIncludes(*Cmd, SystemIncludes);
@@ -278,7 +302,7 @@
   if (QueryDriverGlobs.empty())
     return Base;
   return std::make_unique<QueryDriverDatabase>(QueryDriverGlobs,
-                                                std::move(Base));
+                                               std::move(Base));
 }
 
 } // namespace clangd
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to