[Driver] Fix implicit config files from prefixed symlinks
authorMartin Storsjo <martin@martin.st>
Wed, 25 Apr 2018 21:23:59 +0000 (21:23 +0000)
committerMartin Storsjo <martin@martin.st>
Wed, 25 Apr 2018 21:23:59 +0000 (21:23 +0000)
If -no-canonical-prefixes isn't used, the clang executable name used
is the one of the actual executable, not the name of the symlink that
the user invoked.

In these cases, the target prefix was overridden based on the clang
executable name. (On the other hand the implicit -target option
that such a symlink adds, is added as an actual command line parameter
in tools/driver/driver.cop, before resolving the symlink and finding
the actual clang executable.

Use the original ClangNameParts (set from argv[0] in
tools/driver/driver.cpp) if it seems to be initialized propery.

All existing tests of this feature used -no-canonical-prefixes
(possibly because it also makes the driver look in the directory
of the symlink instead of the directory of the executable); add
another one that uses --config-user-dir= to specify the directory
instead. (For actual users of such symlinks, outisde of the test
suite, the directory is probably the same for both.)

This makes this feature work more like what the documentation
describes.

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

llvm-svn: 330871

clang/include/clang/Driver/ToolChain.h
clang/lib/Driver/Driver.cpp
clang/test/Driver/config-file3.c

index b1f7338..9b90801 100644 (file)
@@ -78,6 +78,10 @@ struct ParsedClangName {
                   bool IsRegistered)
       : TargetPrefix(Target), ModeSuffix(Suffix), DriverMode(Mode),
         TargetIsValid(IsRegistered) {}
+
+  bool isEmpty() const {
+    return TargetPrefix.empty() && ModeSuffix.empty() && DriverMode == nullptr;
+  }
 };
 
 /// ToolChain - Access to tools for a single platform.
index ceb772c..932a54f 100644 (file)
@@ -129,7 +129,8 @@ Driver::Driver(StringRef ClangExecutable, StringRef DefaultTargetTriple,
 
 void Driver::ParseDriverMode(StringRef ProgramName,
                              ArrayRef<const char *> Args) {
-  ClangNameParts = ToolChain::getTargetAndModeFromProgramName(ProgramName);
+  if (ClangNameParts.isEmpty())
+    ClangNameParts = ToolChain::getTargetAndModeFromProgramName(ProgramName);
   setDriverModeFromOption(ClangNameParts.DriverMode);
 
   for (const char *ArgPtr : Args) {
index c1b523c..938411c 100644 (file)
 // FULL-NAME: -Wundefined-func-template
 // FULL-NAME-NOT: -Werror
 //
+//--- Invocation qqq-clang-g++ tries to find config file qqq-clang-g++.cfg even without -no-canonical-prefixes.
+// (As the clang executable and symlink are in different directories, this
+// requires specifying the path via --config-*-dir= though.)
+//
+// RUN: %T/testdmode/qqq-clang-g++ --config-system-dir= --config-user-dir=%T/testdmode -c %s -### 2>&1 | FileCheck %s -check-prefix SYMLINK
+//
+// SYMLINK: Configuration file: {{.*}}/testdmode/qqq-clang-g++.cfg
+//
 //--- File specified by --config overrides config inferred from clang executable.
 //
 // RUN: %T/testdmode/qqq-clang-g++ --config-system-dir=%S/Inputs/config --config-user-dir= --config i386-qqq -c -no-canonical-prefixes %s -### 2>&1 | FileCheck %s -check-prefix CHECK-EXPLICIT