[PATCH] D94265: [clangd] Search compiler in PATH for system include extractionIf the compiler is specified by its name, search it in the systemPATH instead of the command directory: this is what the build system would do.

Quentin Chateau via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 7 13:56:54 PST 2021


qchateau created this revision.
qchateau added a reviewer: sammccall.
Herald added subscribers: usaxena95, kadircet, arphaman.
qchateau requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D94265

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
@@ -136,24 +136,21 @@
 }
 
 llvm::Optional<DriverInfo>
-extractSystemIncludesAndTarget(PathRef Driver, llvm::StringRef Lang,
+extractSystemIncludesAndTarget(llvm::StringRef Driver, llvm::StringRef Lang,
                                llvm::ArrayRef<std::string> CommandLine,
                                const llvm::Regex &QueryDriverRegex) {
   trace::Span Tracer("Extract system includes and target");
   SPAN_ATTACH(Tracer, "driver", Driver);
   SPAN_ATTACH(Tracer, "lang", Lang);
 
-  if (!QueryDriverRegex.match(Driver)) {
-    vlog("System include extraction: not allowed driver {0}", Driver);
+  auto DriverPath = llvm::sys::findProgramByName(Driver);
+  if (auto Error = DriverPath.getError()) {
+    elog("System include extraction: {0} - {1}", Error.message(), Driver);
     return llvm::None;
   }
 
-  if (!llvm::sys::fs::exists(Driver)) {
-    elog("System include extraction: {0} does not exist.", Driver);
-    return llvm::None;
-  }
-  if (!llvm::sys::fs::can_execute(Driver)) {
-    elog("System include extraction: {0} is not executable.", Driver);
+  if (!QueryDriverRegex.match(*DriverPath)) {
+    vlog("System include extraction: not allowed driver {0}", *DriverPath);
     return llvm::None;
   }
 
@@ -171,8 +168,8 @@
   llvm::Optional<llvm::StringRef> Redirects[] = {
       {""}, {""}, llvm::StringRef(StdErrPath)};
 
-  llvm::SmallVector<llvm::StringRef> Args = {Driver, "-E", "-x",
-                                             Lang,   "-",  "-v"};
+  llvm::SmallVector<llvm::StringRef> Args = {*DriverPath, "-E", "-x",
+                                             Lang,        "-",  "-v"};
 
   // These flags will be preserved
   const llvm::StringRef FlagsToPreserve[] = {
@@ -203,8 +200,8 @@
     }
   }
 
-  if (int RC = llvm::sys::ExecuteAndWait(Driver, Args, /*Env=*/llvm::None,
-                                         Redirects)) {
+  if (int RC = llvm::sys::ExecuteAndWait(*DriverPath, Args,
+                                         /*Env=*/llvm::None, Redirects)) {
     elog("System include extraction: driver execution failed with return code: "
          "{0}. Args: ['{1}']",
          llvm::to_string(RC), llvm::join(Args, "', '"));
@@ -224,7 +221,7 @@
     return llvm::None;
   log("System includes extractor: successfully executed {0}\n\tgot includes: "
       "\"{1}\"\n\tgot target: \"{2}\"",
-      Driver, llvm::join(Info->SystemIncludes, ", "), Info->Target);
+      *DriverPath, llvm::join(Info->SystemIncludes, ", "), Info->Target);
   return Info;
 }
 
@@ -332,7 +329,6 @@
     }
 
     llvm::SmallString<128> Driver(Cmd->CommandLine.front());
-    llvm::sys::fs::make_absolute(Cmd->Directory, Driver);
 
     if (auto Info =
             QueriedDrivers.get(/*Key=*/(Driver + ":" + Lang).str(), [&] {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94265.315234.patch
Type: text/x-patch
Size: 2994 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210107/4a08776c/attachment.bin>


More information about the cfe-commits mailing list