Skip to content

Commit a7d2409

Browse files
committed
[clang] [Driver] Do not transform explicit --config filename
Disable transformations (e.g. attempting to replace target architecture) in the config filename that is passed explicitly via `--config`. This behavior is surprising and confusing -- if user passes an explicit config filename, Clang should use it as is. The transformations are still applied when the name is deduced from filename. Update the tests accordingly. This primarily ensures that full filename with .cfg suffix is passed to --config (appending `.cfg` implicitly is not documented, and would collide with use of filenames with other suffixes). The config-file2.c suite is removed entirely as it tested the transformations on the argument to --config. However, the aspects of that that were not tested as part of config-file3.c are now added there (based on config filename deduced from executable). This change streamlines the code in Driver::loadConfigFile(), opening the possibility of further changes, including support for handling multiple --config options and refactoring of filename deduction. Differential Revision: https://reviews.llvm.org/D134208
1 parent 1ea43e6 commit a7d2409

File tree

5 files changed

+42
-89
lines changed

5 files changed

+42
-89
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,6 @@ bool Driver::readConfigFile(StringRef FileName) {
964964

965965
bool Driver::loadConfigFile() {
966966
std::string CfgFileName;
967-
bool FileSpecifiedExplicitly = false;
968967

969968
// Process options that change search path for config files.
970969
if (CLOptions) {
@@ -988,7 +987,11 @@ bool Driver::loadConfigFile() {
988987
}
989988
}
990989

990+
// Prepare list of directories where config file is searched for.
991+
StringRef CfgFileSearchDirs[] = {UserConfigDir, SystemConfigDir, Dir};
992+
991993
// First try to find config file specified in command line.
994+
llvm::SmallString<128> CfgFilePath;
992995
if (CLOptions) {
993996
std::vector<std::string> ConfigFiles =
994997
CLOptions->getAllArgValues(options::OPT_config);
@@ -1020,7 +1023,18 @@ bool Driver::loadConfigFile() {
10201023
return readConfigFile(CfgFilePath);
10211024
}
10221025

1023-
FileSpecifiedExplicitly = true;
1026+
// Look for the configuration file in the usual locations.
1027+
if (searchForFile(CfgFilePath, CfgFileSearchDirs, CfgFileName, getVFS()))
1028+
return readConfigFile(CfgFilePath);
1029+
1030+
// Report error but only if config file was specified explicitly, by
1031+
// option --config. If it was deduced from executable name, it is not an
1032+
// error.
1033+
Diag(diag::err_drv_config_file_not_found) << CfgFileName;
1034+
for (const StringRef &SearchDir : CfgFileSearchDirs)
1035+
if (!SearchDir.empty())
1036+
Diag(diag::note_drv_config_file_searched_in) << SearchDir;
1037+
return true;
10241038
}
10251039
}
10261040

@@ -1059,8 +1073,8 @@ bool Driver::loadConfigFile() {
10591073
// Get architecture name from config file name like 'i386.cfg' or
10601074
// 'armv7l-clang.cfg'.
10611075
// Check if command line options changes effective triple.
1062-
llvm::Triple EffectiveTriple = computeTargetTriple(*this,
1063-
CfgTriple.getTriple(), *CLOptions);
1076+
llvm::Triple EffectiveTriple =
1077+
computeTargetTriple(*this, CfgTriple.getTriple(), *CLOptions);
10641078
if (CfgTriple.getArch() != EffectiveTriple.getArch()) {
10651079
FixedConfigFile = EffectiveTriple.getArchName();
10661080
FixedArchPrefixLen = FixedConfigFile.size();
@@ -1071,11 +1085,7 @@ bool Driver::loadConfigFile() {
10711085
}
10721086
}
10731087

1074-
// Prepare list of directories where config file is searched for.
1075-
StringRef CfgFileSearchDirs[] = {UserConfigDir, SystemConfigDir, Dir};
1076-
10771088
// Try to find config file. First try file with corrected architecture.
1078-
llvm::SmallString<128> CfgFilePath;
10791089
if (!FixedConfigFile.empty()) {
10801090
if (searchForFile(CfgFilePath, CfgFileSearchDirs, FixedConfigFile,
10811091
getVFS()))
@@ -1101,16 +1111,8 @@ bool Driver::loadConfigFile() {
11011111
return readConfigFile(CfgFilePath);
11021112
}
11031113

1104-
// Report error but only if config file was specified explicitly, by option
1105-
// --config. If it was deduced from executable name, it is not an error.
1106-
if (FileSpecifiedExplicitly) {
1107-
Diag(diag::err_drv_config_file_not_found) << CfgFileName;
1108-
for (const StringRef &SearchDir : CfgFileSearchDirs)
1109-
if (!SearchDir.empty())
1110-
Diag(diag::note_drv_config_file_searched_in) << SearchDir;
1111-
return true;
1112-
}
1113-
1114+
// If we were unable to find a config file deduced from executable name,
1115+
// do not report an error.
11141116
return false;
11151117
}
11161118

clang/test/Driver/config-file-errs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@
2424

2525
//--- Argument of '--config' must exist somewhere in well-known directories, if it is specified by bare name.
2626
//
27-
// RUN: not %clang --config-system-dir= --config-user-dir= --config nonexistent-config-file 2>&1 | FileCheck %s -check-prefix CHECK-NOTFOUND0
27+
// RUN: not %clang --config-system-dir= --config-user-dir= --config nonexistent-config-file.cfg 2>&1 | FileCheck %s -check-prefix CHECK-NOTFOUND0
2828
// CHECK-NOTFOUND0: configuration file 'nonexistent-config-file.cfg' cannot be found
2929
// CHECK-NOTFOUND0-NEXT: was searched for in the directory:
3030
// CHECK-NOTFOUND0-NOT: was searched for in the directory:
3131
//
32-
// RUN: not %clang --config-system-dir= --config-user-dir=%S/Inputs/config2 --config nonexistent-config-file 2>&1 | FileCheck %s -check-prefix CHECK-NOTFOUND1
32+
// RUN: not %clang --config-system-dir= --config-user-dir=%S/Inputs/config2 --config nonexistent-config-file.cfg 2>&1 | FileCheck %s -check-prefix CHECK-NOTFOUND1
3333
// CHECK-NOTFOUND1: configuration file 'nonexistent-config-file.cfg' cannot be found
3434
// CHECK-NOTFOUND1-NEXT: was searched for in the directory: {{.*}}/Inputs/config2
3535
// CHECK-NOTFOUND1-NEXT: was searched for in the directory:
3636
// CHECK-NOTFOUND1-NOT: was searched for in the directory:
3737
//
38-
// RUN: not %clang --config-system-dir=%S/Inputs/config --config-user-dir= --config nonexistent-config-file 2>&1 | FileCheck %s -check-prefix CHECK-NOTFOUND2
38+
// RUN: not %clang --config-system-dir=%S/Inputs/config --config-user-dir= --config nonexistent-config-file.cfg 2>&1 | FileCheck %s -check-prefix CHECK-NOTFOUND2
3939
// CHECK-NOTFOUND2: configuration file 'nonexistent-config-file.cfg' cannot be found
4040
// CHECK-NOTFOUND2-NEXT: was searched for in the directory: {{.*}}/Inputs/config
4141
// CHECK-NOTFOUND2-NEXT: was searched for in the directory:
4242
// CHECK-NOTFOUND2-NOT: was searched for in the directory:
4343
//
44-
// RUN: not %clang --config-system-dir=%S/Inputs/config --config-user-dir=%S/Inputs/config2 --config nonexistent-config-file 2>&1 | FileCheck %s -check-prefix CHECK-NOTFOUND3
44+
// RUN: not %clang --config-system-dir=%S/Inputs/config --config-user-dir=%S/Inputs/config2 --config nonexistent-config-file.cfg 2>&1 | FileCheck %s -check-prefix CHECK-NOTFOUND3
4545
// CHECK-NOTFOUND3: configuration file 'nonexistent-config-file.cfg' cannot be found
4646
// CHECK-NOTFOUND3-NEXT: was searched for in the directory: {{.*}}/Inputs/config2
4747
// CHECK-NOTFOUND3-NEXT: was searched for in the directory: {{.*}}/Inputs/config

clang/test/Driver/config-file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@
6868

6969
//--- User directory is searched first.
7070
//
71-
// RUN: %clang --config-system-dir=%S/Inputs/config --config-user-dir=%S/Inputs/config2 --config config-4 -S %s -o /dev/null -v 2>&1 | FileCheck %s -check-prefix CHECK-PRECEDENCE
71+
// RUN: %clang --config-system-dir=%S/Inputs/config --config-user-dir=%S/Inputs/config2 --config config-4.cfg -S %s -o /dev/null -v 2>&1 | FileCheck %s -check-prefix CHECK-PRECEDENCE
7272
// CHECK-PRECEDENCE: Configuration file: {{.*}}Inputs{{.}}config2{{.}}config-4.cfg
7373
// CHECK-PRECEDENCE: -Wall
7474

7575

7676
//--- Duplicate --config options are allowed if the value is the same
77-
// RUN: %clang --config-system-dir=%S/Inputs/config --config-user-dir=%S/Inputs/config2 --config config-4 --config config-4 -S %s -o /dev/null -v 2>&1 | FileCheck %s -check-prefix CHECK-SAME-CONFIG
77+
// RUN: %clang --config-system-dir=%S/Inputs/config --config-user-dir=%S/Inputs/config2 --config config-4.cfg --config config-4.cfg -S %s -o /dev/null -v 2>&1 | FileCheck %s -check-prefix CHECK-SAME-CONFIG
7878
// CHECK-SAME-CONFIG: Configuration file: {{.*}}Inputs{{.}}config2{{.}}config-4.cfg

clang/test/Driver/config-file2.c

Lines changed: 0 additions & 51 deletions
This file was deleted.

clang/test/Driver/config-file3.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
//
3838
//--- File specified by --config overrides config inferred from clang executable.
3939
//
40-
// 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
40+
// RUN: %t/testdmode/qqq-clang-g++ --config-system-dir=%S/Inputs/config --config-user-dir= --config i386-qqq.cfg -c -no-canonical-prefixes -### %s 2>&1 | FileCheck %s -check-prefix CHECK-EXPLICIT
4141
//
4242
// CHECK-EXPLICIT: Configuration file: {{.*}}/Inputs/config/i386-qqq.cfg
4343

@@ -77,27 +77,24 @@
7777
// RUN: ln -s %clang %t/testreload/x86_64-clang-g++
7878
// RUN: echo "-Wundefined-func-template" > %t/testreload/i386-clang-g++.cfg
7979
// RUN: echo "-Werror" > %t/testreload/i386.cfg
80+
// RUN: echo "-Wall" > %t/testreload/x86_64-clang-g++.cfg
8081
// RUN: %t/testreload/x86_64-clang-g++ --config-system-dir= --config-user-dir= -c -m32 -no-canonical-prefixes -### %s 2>&1 | FileCheck %s -check-prefix CHECK-RELOAD
8182
//
8283
// CHECK-RELOAD: Configuration file: {{.*}}/testreload/i386-clang-g++.cfg
8384
// CHECK-RELOAD: -Wundefined-func-template
8485
// CHECK-RELOAD-NOT: -Werror
86+
// CHECK-RELOAD-NOT: -Wall
8587

86-
//--- If config file is specified by --config and its name does not start with architecture, it is used without reloading.
87-
//
88-
// RUN: %t/testreload/x86_64-clang-g++ --config-system-dir=%S/Inputs --config-user-dir= --config config-3 -c -m32 -no-canonical-prefixes -### %s 2>&1 | FileCheck %s -check-prefix CHECK-RELOAD1a
89-
//
90-
// CHECK-RELOAD1a: Configuration file: {{.*}}/Inputs/config-3.cfg
91-
//
92-
// RUN: %t/testreload/x86_64-clang-g++ --config-system-dir=%S/Inputs --config-user-dir= --config config-3 -c --target=i386 -no-canonical-prefixes -### %s 2>&1 | FileCheck %s -check-prefix CHECK-RELOAD1b
93-
//
94-
// CHECK-RELOAD1b: Configuration file: {{.*}}/Inputs/config-3.cfg
88+
//--- Same for -target in place of -m32.
89+
// RUN: %t/testreload/x86_64-clang-g++ --config-system-dir= --config-user-dir= -c -target i386 -no-canonical-prefixes -### %s 2>&1 | FileCheck %s -check-prefix CHECK-RELOAD
9590

96-
//--- If config file is specified by --config and its name starts with architecture, it is reloaded.
97-
//
98-
// RUN: %t/testreload/x86_64-clang-g++ --config-system-dir=%S/Inputs/config --config-user-dir= --config x86_64-qqq -c -m32 -no-canonical-prefixes -### %s 2>&1 | FileCheck %s -check-prefix CHECK-RELOAD1c
91+
//--- `-target i386 -m64` should load the 64-bit config.
92+
// RUN: %t/testreload/x86_64-clang-g++ --config-system-dir= --config-user-dir= -c -target i386 -m64 -no-canonical-prefixes -### %s 2>&1 | FileCheck %s -check-prefix CHECK-RELOAD1a
9993
//
100-
// CHECK-RELOAD1c: Configuration file: {{.*}}/Inputs/config/i386-qqq.cfg
94+
// CHECK-RELOAD1a: Configuration file: {{.*}}/testreload/x86_64-clang-g++.cfg
95+
// CHECK-RELOAD1a: -Wall
96+
// CHECK-RELOAD1a-NOT: -Werror
97+
// CHECK-RELOAD1a-NOT: -Wundefined-func-template
10198

10299
//--- x86_64-clang-g++ tries to find config i386.cfg if i386-clang-g++.cfg is not found.
103100
//
@@ -107,4 +104,9 @@
107104
// CHECK-RELOAD1d: Configuration file: {{.*}}/testreload/i386.cfg
108105
// CHECK-RELOAD1d: -Werror
109106
// CHECK-RELOAD1d-NOT: -Wundefined-func-template
107+
// CHECK-RELOAD1d-NOT: -Wall
110108

109+
//--- x86_64-clang-g++ uses x86_64-clang-g++.cfg if i386*.cfg are not found.
110+
//
111+
// RUN: rm %t/testreload/i386.cfg
112+
// RUN: %t/testreload/x86_64-clang-g++ --config-system-dir= --config-user-dir= -c -m32 -no-canonical-prefixes -### %s 2>&1 | FileCheck %s -check-prefix CHECK-RELOAD1a

0 commit comments

Comments
 (0)