[PATCH] D73453: Preserve -nostdinc and --sysroot when calling query driver

Tobias Pisani via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 27 02:15:39 PST 2020


topisani created this revision.
topisani added reviewers: kadircet, klimek.
Herald added subscribers: cfe-commits, usaxena95, arphaman, jkorous, ilya-biryukov.
Herald added a project: clang.

Solves this issue: https://github.com/clangd/clangd/issues/157

This is my first contribution to an llvm project, so I hope I'm doing it right!


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D73453

Files:
  clang-tools-extra/clangd/QueryDriverDatabase.cpp


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<llvm::StringRef> PreservedArgs,
+                      llvm::Regex &QueryDriverRegex) {
   trace::Span Tracer("Extract system includes");
   SPAN_ATTACH(Tracer, "driver", Driver);
   SPAN_ATTACH(Tracer, "lang", Lang);
@@ -120,14 +121,15 @@
   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"};
+  llvm::copy(PreservedArgs, std::back_inserter(Args));
 
   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}. Driver: {1}, args: [{2}]",
+         llvm::to_string(RC), Driver, llvm::join(Args, ", "));
     return {};
   }
 
@@ -218,12 +220,20 @@
       return Cmd;
 
     llvm::StringRef Lang;
+    llvm::SmallVector<llvm::StringRef, 4> PreservedArgs;
     for (size_t I = 0, E = Cmd->CommandLine.size(); I < E; ++I) {
       llvm::StringRef Arg = Cmd->CommandLine[I];
       if (Arg == "-x" && I + 1 < E)
         Lang = Cmd->CommandLine[I + 1];
       else if (Arg.startswith("-x"))
         Lang = Arg.drop_front(2).trim();
+      else if (Arg == "-nostdinc")
+        PreservedArgs.push_back(Arg);
+      else if (Arg == "--sysroot" && I + 1 < E) {
+        PreservedArgs.push_back(Arg);
+        PreservedArgs.push_back(Cmd->CommandLine[I + 1]);
+      } else if (Arg.startswith("--sysroot="))
+        PreservedArgs.push_back(Arg);
     }
     if (Lang.empty()) {
       llvm::StringRef Ext = llvm::sys::path::extension(File).trim('.');
@@ -247,8 +257,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, PreservedArgs, QueryDriverRegex);
     }
 
     return addSystemIncludes(*Cmd, SystemIncludes);
@@ -278,7 +288,7 @@
   if (QueryDriverGlobs.empty())
     return Base;
   return std::make_unique<QueryDriverDatabase>(QueryDriverGlobs,
-                                                std::move(Base));
+                                               std::move(Base));
 }
 
 } // namespace clangd


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73453.240502.patch
Type: text/x-patch
Size: 3269 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200127/4a6bbb18/attachment.bin>


More information about the cfe-commits mailing list