]> granicus.if.org Git - clang/commitdiff
[Driver] Do a PATH lookup if needed when using -no-canonical-prefixes
authorPetr Hosek <phosek@chromium.org>
Fri, 16 Jun 2017 22:40:18 +0000 (22:40 +0000)
committerPetr Hosek <phosek@chromium.org>
Fri, 16 Jun 2017 22:40:18 +0000 (22:40 +0000)
When -no-canonical-prefixes option is used and argv0 contains only
a program name, we need to do a PATH lookup to get an executable path,
otherwise the return value won't be a valid path and any subsequent
uses of it (e.g. when invoking -cc1) will fail with an error.

This patch fixes PR9576.

Differential Revision: https://reviews.llvm.org/D34290

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@305600 91177308-0d34-0410-b5e6-96231b3b80d8

tools/driver/driver.cpp

index 4bd3b228d0ff6b13142ad7adfea25417bd09f84d..af25d95980218f1fe9ecd2dc2d3ef7ab88b62a6f 100644 (file)
@@ -53,8 +53,15 @@ using namespace clang::driver;
 using namespace llvm::opt;
 
 std::string GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
-  if (!CanonicalPrefixes)
-    return Argv0;
+  if (!CanonicalPrefixes) {
+    SmallString<128> ExecutablePath(Argv0);
+    // Do a PATH lookup if Argv0 isn't a valid path.
+    if (!llvm::sys::fs::exists(ExecutablePath))
+      if (llvm::ErrorOr<std::string> P =
+              llvm::sys::findProgramByName(ExecutablePath))
+        ExecutablePath = *P;
+    return ExecutablePath.str();
+  }
 
   // This just needs to be some symbol in the binary; C++ doesn't
   // allow taking the address of ::main however.