Skip to content

Commit 3bc43d3

Browse files
committed
Repurpose DriverArgs to MacroDefines
1 parent dfc40b7 commit 3bc43d3

File tree

7 files changed

+118
-113
lines changed

7 files changed

+118
-113
lines changed

clang/include/clang/Driver/Multilib.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ class MultilibSet {
164164
const_iterator end() const { return Multilibs.end(); }
165165

166166
/// Process custom flags from \p Flags and returns an expanded flags list and
167-
/// a list of extra compilation arguments.
167+
/// a list of macro defines.
168168
/// Returns a pair where:
169169
/// - first: the new flags list including custom flags after processing.
170-
/// - second: the extra compilation arguments to be fed to the driver.
170+
/// - second: the extra macro defines to be fed to the driver.
171171
std::pair<Multilib::flags_list, SmallVector<StringRef>>
172172
processCustomFlags(const Driver &D, const Multilib::flags_list &Flags) const;
173173

clang/include/clang/Driver/ToolChain.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,10 @@ class ToolChain {
686686
/// Add warning options that need to be passed to cc1 for this target.
687687
virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const;
688688

689-
// Get the list of extra driver arguments strings requested by the multilib
689+
// Get the list of extra macro defines requested by the multilib
690690
// configuration.
691691
virtual SmallVector<std::string>
692-
getMultilibDriverArgsStr(llvm::opt::ArgList &Args) const {
692+
getMultilibMacroDefinesStr(llvm::opt::ArgList &Args) const {
693693
return {};
694694
};
695695

clang/lib/Driver/Driver.cpp

Lines changed: 81 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,9 +1253,7 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
12531253
bool HasConfigFile = !ContainsError && (CfgOptions.get() != nullptr);
12541254

12551255
// All arguments, from both config file and command line.
1256-
auto UArgs = std::make_unique<InputArgList>(std::move(
1257-
HasConfigFile ? std::move(*CfgOptions) : std::move(*CLOptions)));
1258-
InputArgList &Args = *UArgs;
1256+
InputArgList Args = std::move(HasConfigFile ? *CfgOptions : *CLOptions);
12591257

12601258
if (HasConfigFile)
12611259
for (auto *Opt : *CLOptions) {
@@ -1289,6 +1287,52 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
12891287
}
12901288
}
12911289

1290+
// Check for working directory option before accessing any files
1291+
if (Arg *WD = Args.getLastArg(options::OPT_working_directory))
1292+
if (VFS->setCurrentWorkingDirectory(WD->getValue()))
1293+
Diag(diag::err_drv_unable_to_set_working_directory) << WD->getValue();
1294+
1295+
// Check for missing include directories.
1296+
if (!Diags.isIgnored(diag::warn_missing_include_dirs, SourceLocation())) {
1297+
for (auto IncludeDir : Args.getAllArgValues(options::OPT_I_Group)) {
1298+
if (!VFS->exists(IncludeDir))
1299+
Diag(diag::warn_missing_include_dirs) << IncludeDir;
1300+
}
1301+
}
1302+
1303+
// FIXME: This stuff needs to go into the Compilation, not the driver.
1304+
bool CCCPrintPhases;
1305+
1306+
// -canonical-prefixes, -no-canonical-prefixes are used very early in main.
1307+
Args.ClaimAllArgs(options::OPT_canonical_prefixes);
1308+
Args.ClaimAllArgs(options::OPT_no_canonical_prefixes);
1309+
1310+
// f(no-)integated-cc1 is also used very early in main.
1311+
Args.ClaimAllArgs(options::OPT_fintegrated_cc1);
1312+
Args.ClaimAllArgs(options::OPT_fno_integrated_cc1);
1313+
1314+
// Ignore -pipe.
1315+
Args.ClaimAllArgs(options::OPT_pipe);
1316+
1317+
// Extract -ccc args.
1318+
//
1319+
// FIXME: We need to figure out where this behavior should live. Most of it
1320+
// should be outside in the client; the parts that aren't should have proper
1321+
// options, either by introducing new ones or by overloading gcc ones like -V
1322+
// or -b.
1323+
CCCPrintPhases = Args.hasArg(options::OPT_ccc_print_phases);
1324+
CCCPrintBindings = Args.hasArg(options::OPT_ccc_print_bindings);
1325+
if (const Arg *A = Args.getLastArg(options::OPT_ccc_gcc_name))
1326+
CCCGenericGCCName = A->getValue();
1327+
1328+
// Process -fproc-stat-report options.
1329+
if (const Arg *A = Args.getLastArg(options::OPT_fproc_stat_report_EQ)) {
1330+
CCPrintProcessStats = true;
1331+
CCPrintStatReportFilename = A->getValue();
1332+
}
1333+
if (Args.hasArg(options::OPT_fproc_stat_report))
1334+
CCPrintProcessStats = true;
1335+
12921336
// FIXME: TargetTriple is used by the target-prefixed calls to as/ld
12931337
// and getToolChain is const.
12941338
if (IsCLMode()) {
@@ -1341,79 +1385,6 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
13411385
TargetTriple = A->getValue();
13421386
if (const Arg *A = Args.getLastArg(options::OPT_ccc_install_dir))
13431387
Dir = Dir = A->getValue();
1344-
if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ))
1345-
SysRoot = A->getValue();
1346-
if (const Arg *A = Args.getLastArg(options::OPT_resource_dir))
1347-
ResourceDir = A->getValue();
1348-
if (const Arg *A = Args.getLastArg(options::OPT__dyld_prefix_EQ))
1349-
DyldPrefix = A->getValue();
1350-
1351-
setLTOMode(Args);
1352-
1353-
// Owned by the host.
1354-
const ToolChain &TC =
1355-
getToolChain(Args, computeTargetTriple(*this, TargetTriple, Args));
1356-
1357-
SmallVector<std::string> MultilibDriverArgsStr =
1358-
TC.getMultilibDriverArgsStr(Args);
1359-
SmallVector<const char *> MLArgsChar(
1360-
llvm::map_range(MultilibDriverArgsStr, [&Args](const auto &S) {
1361-
return Args.MakeArgString(S);
1362-
}));
1363-
bool MLContainsError;
1364-
auto MultilibDriverArgList = std::make_unique<InputArgList>(
1365-
ParseArgStrings(MLArgsChar, /*UseDriverMode=*/false, MLContainsError));
1366-
if (!MLContainsError)
1367-
for (auto *Opt : *MultilibDriverArgList) {
1368-
appendOneArg(Args, Opt, nullptr);
1369-
}
1370-
1371-
// Check for working directory option before accessing any files
1372-
if (Arg *WD = Args.getLastArg(options::OPT_working_directory))
1373-
if (VFS->setCurrentWorkingDirectory(WD->getValue()))
1374-
Diag(diag::err_drv_unable_to_set_working_directory) << WD->getValue();
1375-
1376-
// Check for missing include directories.
1377-
if (!Diags.isIgnored(diag::warn_missing_include_dirs, SourceLocation())) {
1378-
for (auto IncludeDir : Args.getAllArgValues(options::OPT_I_Group)) {
1379-
if (!VFS->exists(IncludeDir))
1380-
Diag(diag::warn_missing_include_dirs) << IncludeDir;
1381-
}
1382-
}
1383-
1384-
// FIXME: This stuff needs to go into the Compilation, not the driver.
1385-
bool CCCPrintPhases;
1386-
1387-
// -canonical-prefixes, -no-canonical-prefixes are used very early in main.
1388-
Args.ClaimAllArgs(options::OPT_canonical_prefixes);
1389-
Args.ClaimAllArgs(options::OPT_no_canonical_prefixes);
1390-
1391-
// f(no-)integated-cc1 is also used very early in main.
1392-
Args.ClaimAllArgs(options::OPT_fintegrated_cc1);
1393-
Args.ClaimAllArgs(options::OPT_fno_integrated_cc1);
1394-
1395-
// Ignore -pipe.
1396-
Args.ClaimAllArgs(options::OPT_pipe);
1397-
1398-
// Extract -ccc args.
1399-
//
1400-
// FIXME: We need to figure out where this behavior should live. Most of it
1401-
// should be outside in the client; the parts that aren't should have proper
1402-
// options, either by introducing new ones or by overloading gcc ones like -V
1403-
// or -b.
1404-
CCCPrintPhases = Args.hasArg(options::OPT_ccc_print_phases);
1405-
CCCPrintBindings = Args.hasArg(options::OPT_ccc_print_bindings);
1406-
if (const Arg *A = Args.getLastArg(options::OPT_ccc_gcc_name))
1407-
CCCGenericGCCName = A->getValue();
1408-
1409-
// Process -fproc-stat-report options.
1410-
if (const Arg *A = Args.getLastArg(options::OPT_fproc_stat_report_EQ)) {
1411-
CCPrintProcessStats = true;
1412-
CCPrintStatReportFilename = A->getValue();
1413-
}
1414-
if (Args.hasArg(options::OPT_fproc_stat_report))
1415-
CCPrintProcessStats = true;
1416-
14171388
for (const Arg *A : Args.filtered(options::OPT_B)) {
14181389
A->claim();
14191390
PrefixDirs.push_back(A->getValue(0));
@@ -1428,6 +1399,13 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
14281399
CompilerPath = Split.second;
14291400
}
14301401
}
1402+
if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ))
1403+
SysRoot = A->getValue();
1404+
if (const Arg *A = Args.getLastArg(options::OPT__dyld_prefix_EQ))
1405+
DyldPrefix = A->getValue();
1406+
1407+
if (const Arg *A = Args.getLastArg(options::OPT_resource_dir))
1408+
ResourceDir = A->getValue();
14311409

14321410
if (const Arg *A = Args.getLastArg(options::OPT_save_temps_EQ)) {
14331411
SaveTemps = llvm::StringSwitch<SaveTempsMode>(A->getValue())
@@ -1447,6 +1425,8 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
14471425
Offload = OffloadHostDevice;
14481426
}
14491427

1428+
setLTOMode(Args);
1429+
14501430
// Process -fembed-bitcode= flags.
14511431
if (Arg *A = Args.getLastArg(options::OPT_fembed_bitcode_EQ)) {
14521432
StringRef Name = A->getValue();
@@ -1500,6 +1480,31 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
15001480
}
15011481
}
15021482

1483+
std::unique_ptr<llvm::opt::InputArgList> UArgs =
1484+
std::make_unique<InputArgList>(std::move(Args));
1485+
1486+
// Owned by the host.
1487+
const ToolChain &TC =
1488+
getToolChain(*UArgs, computeTargetTriple(*this, TargetTriple, *UArgs));
1489+
1490+
{
1491+
SmallVector<std::string> MultilibMacroDefinesStr =
1492+
TC.getMultilibMacroDefinesStr(*UArgs);
1493+
SmallVector<const char *> MLMacroDefinesChar(
1494+
llvm::map_range(MultilibMacroDefinesStr, [&UArgs](const auto &S) {
1495+
return UArgs->MakeArgString(Twine("-D") + Twine(S));
1496+
}));
1497+
bool MLContainsError;
1498+
auto MultilibMacroDefineList =
1499+
std::make_unique<InputArgList>(ParseArgStrings(
1500+
MLMacroDefinesChar, /*UseDriverMode=*/false, MLContainsError));
1501+
if (!MLContainsError) {
1502+
for (auto *Opt : *MultilibMacroDefineList) {
1503+
appendOneArg(*UArgs, Opt, nullptr);
1504+
}
1505+
}
1506+
}
1507+
15031508
// Perform the default argument translations.
15041509
DerivedArgList *TranslatedArgs = TranslateInputArgs(*UArgs);
15051510

clang/lib/Driver/Multilib.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ std::pair<Multilib::flags_list, SmallVector<StringRef>>
154154
MultilibSet::processCustomFlags(const Driver &D,
155155
const Multilib::flags_list &Flags) const {
156156
Multilib::flags_list Result;
157-
SmallVector<StringRef> CompilationArgs;
157+
SmallVector<StringRef> MacroDefines;
158158

159159
// Custom flag values detected in the flags list
160160
SmallVector<const custom_flag::ValueDetail *> ClaimedCustomFlagValues;
@@ -193,9 +193,9 @@ MultilibSet::processCustomFlags(const Driver &D,
193193
if (!TriggeredCustomFlagDecls.insert(CustomFlagValue->Decl).second)
194194
continue;
195195
Result.push_back(std::string(custom_flag::Prefix) + CustomFlagValue->Name);
196-
if (CustomFlagValue->DriverArgs)
197-
CompilationArgs.append(CustomFlagValue->DriverArgs->begin(),
198-
CustomFlagValue->DriverArgs->end());
196+
if (CustomFlagValue->MacroDefines)
197+
MacroDefines.append(CustomFlagValue->MacroDefines->begin(),
198+
CustomFlagValue->MacroDefines->end());
199199
}
200200

201201
// Detect flag declarations with no value passed in. Select default value.
@@ -205,28 +205,28 @@ MultilibSet::processCustomFlags(const Driver &D,
205205
custom_flag::ValueDetail &CustomFlagValue =
206206
Decl->ValueList[*Decl->DefaultValueIdx];
207207
Result.push_back(std::string(custom_flag::Prefix) + CustomFlagValue.Name);
208-
if (CustomFlagValue.DriverArgs)
209-
CompilationArgs.append(CustomFlagValue.DriverArgs->begin(),
210-
CustomFlagValue.DriverArgs->end());
208+
if (CustomFlagValue.MacroDefines)
209+
MacroDefines.append(CustomFlagValue.MacroDefines->begin(),
210+
CustomFlagValue.MacroDefines->end());
211211
}
212212

213213
DiagnoseUnclaimedMultilibCustomFlags(D, UnclaimedCustomFlagValueStrs,
214214
CustomFlagDecls);
215215

216-
return {Result, CompilationArgs};
216+
return {Result, MacroDefines};
217217
}
218218

219219
bool MultilibSet::select(
220220
const Driver &D, const Multilib::flags_list &Flags,
221221
llvm::SmallVectorImpl<Multilib> &Selected,
222-
llvm::SmallVector<StringRef> *CustomFlagCompilationArgs) const {
223-
auto [FlagsWithCustom, CFCompilationArgs] = processCustomFlags(D, Flags);
222+
llvm::SmallVector<StringRef> *CustomFlagMacroDefines) const {
223+
auto [FlagsWithCustom, CFMacroDefines] = processCustomFlags(D, Flags);
224224
llvm::StringSet<> FlagSet(expandFlags(FlagsWithCustom));
225225
Selected.clear();
226226
bool AnyErrors = false;
227227

228-
if (CustomFlagCompilationArgs)
229-
*CustomFlagCompilationArgs = std::move(CFCompilationArgs);
228+
if (CustomFlagMacroDefines)
229+
*CustomFlagMacroDefines = std::move(CFMacroDefines);
230230

231231
// Decide which multilibs we're going to select at all.
232232
llvm::DenseSet<StringRef> ExclusiveGroupsSelected;

clang/lib/Driver/ToolChains/BareMetal.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ static void
187187
findMultilibsFromYAML(const ToolChain &TC, const Driver &D,
188188
StringRef MultilibPath, const ArgList &Args,
189189
DetectedMultilibs &Result,
190-
SmallVector<StringRef> &CustomFlagsCompilationArgs) {
190+
SmallVector<StringRef> &CustomFlagsMacroDefines) {
191191
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MB =
192192
D.getVFS().getBufferForFile(MultilibPath);
193193
if (!MB)
@@ -199,7 +199,7 @@ findMultilibsFromYAML(const ToolChain &TC, const Driver &D,
199199
return;
200200
Result.Multilibs = ErrorOrMultilibSet.get();
201201
if (Result.Multilibs.select(D, Flags, Result.SelectedMultilibs,
202-
&CustomFlagsCompilationArgs))
202+
&CustomFlagsMacroDefines))
203203
return;
204204
D.Diag(clang::diag::warn_drv_missing_multilib) << llvm::join(Flags, " ");
205205
std::stringstream ss;
@@ -258,13 +258,13 @@ void BareMetal::findMultilibs(const Driver &D, const llvm::Triple &Triple,
258258
// If multilib.yaml is found, update sysroot so it doesn't use a target
259259
// specific suffix
260260
SysRoot = computeBaseSysRoot(D, /*IncludeTriple=*/false);
261-
SmallVector<StringRef> CustomFlagDriverArgs;
261+
SmallVector<StringRef> CustomFlagMacroDefines;
262262
findMultilibsFromYAML(*this, D, *MultilibPath, Args, Result,
263-
CustomFlagDriverArgs);
263+
CustomFlagMacroDefines);
264264
SelectedMultilibs = Result.SelectedMultilibs;
265265
Multilibs = Result.Multilibs;
266-
MultilibDriverArgs.append(CustomFlagDriverArgs.begin(),
267-
CustomFlagDriverArgs.end());
266+
MultilibMacroDefines.append(CustomFlagMacroDefines.begin(),
267+
CustomFlagMacroDefines.end());
268268
} else if (isRISCVBareMetal(Triple)) {
269269
if (findRISCVMultilibs(D, Triple, Args, Result)) {
270270
SelectedMultilibs = Result.SelectedMultilibs;
@@ -606,6 +606,6 @@ SanitizerMask BareMetal::getSupportedSanitizers() const {
606606
}
607607

608608
SmallVector<std::string>
609-
BareMetal::getMultilibDriverArgsStr(llvm::opt::ArgList &Args) const {
610-
return MultilibDriverArgs;
611-
}
609+
BareMetal::getMultilibMacroDefinesStr(llvm::opt::ArgList &Args) const {
610+
return MultilibMacroDefines;
611+
}

clang/lib/Driver/ToolChains/BareMetal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public ToolChain {
7575
SanitizerMask getSupportedSanitizers() const override;
7676

7777
SmallVector<std::string>
78-
getMultilibDriverArgsStr(llvm::opt::ArgList &Args) const override;
78+
getMultilibMacroDefinesStr(llvm::opt::ArgList &Args) const override;
7979

8080
private:
8181
using OrderedMultilibs =
@@ -84,7 +84,7 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public ToolChain {
8484

8585
std::string SysRoot;
8686

87-
SmallVector<std::string> MultilibDriverArgs;
87+
SmallVector<std::string> MultilibMacroDefines;
8888
};
8989

9090
} // namespace toolchains

clang/test/Driver/baremetal-multilib-custom-flags.yaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@
3737
# CHECK-PRINT-MULTI-LIB: arm-none-eabi/multithreaded/thumb/v8-m.main/nofp;@-target=thumbv8m.main-unknown-none-eabi@mfpu=none@fmultilib-flag=multithreaded
3838

3939
# RUN: %clang --target=arm-none-eabi --multi-lib-config=%s -x c %s -fmultilib-flag=no-multithreaded -### -o /dev/null 2>&1 \
40-
# RUN: | FileCheck --check-prefix=CHECK-DRIVERARGS-NOMULTI %s
41-
# CHECK-DRIVERARGS-NOMULTI: "-D" "__SINGLE_THREAD__"
40+
# RUN: | FileCheck --check-prefix=CHECK-MACRODEFINES-NOMULTI %s
41+
# CHECK-MACRODEFINES-NOMULTI: "-D" "__SINGLE_THREAD__"
4242

4343
# RUN: %clang --target=arm-none-eabi --multi-lib-config=%s -x c %s -fmultilib-flag=io-semihosting -### -o /dev/null 2>&1 \
44-
# RUN: | FileCheck --check-prefix=CHECK-DRIVERARGS-IO-SEMIHOSTING %s
45-
# CHECK-DRIVERARGS-IO-SEMIHOSTING: "-D" "SEMIHOSTING"
44+
# RUN: | FileCheck --check-prefix=CHECK-MACRODEFINES-IO-SEMIHOSTING %s
45+
# CHECK-MACRODEFINES-IO-SEMIHOSTING: "-D" "SEMIHOSTING"
4646

4747
# RUN: %clang --target=arm-none-eabi --multi-lib-config=%s -x c %s -fmultilib-flag=io-linux-syscalls -### -o /dev/null 2>&1 \
48-
# RUN: | FileCheck --check-prefix=CHECK-DRIVERARGS-IO-LINUX %s
49-
# CHECK-DRIVERARGS-IO-LINUX: "-U" "SEMIHOSTING"
50-
# CHECK-DRIVERARGS-IO-LINUX-SAME: "-D" "LINUX_SYSCALLS"
48+
# RUN: | FileCheck --check-prefix=CHECK-MACRODEFINES-IO-LINUX %s
49+
# CHECK-MACRODEFINES-IO-LINUX: "-D" "LINUX_SYSCALLS"
50+
# CHECK-MACRODEFINES-IO-LINUX-SAME: "-D" "HOSTED"
5151

5252
---
5353
MultilibVersion: 1.0
@@ -68,14 +68,14 @@ Flags:
6868
- Name: multithreading
6969
Values:
7070
- Name: no-multithreaded
71-
DriverArgs: [-D__SINGLE_THREAD__]
71+
MacroDefines: [__SINGLE_THREAD__]
7272
- Name: multithreaded
7373
Default: no-multithreaded
7474
- Name: io
7575
Values:
7676
- Name: io-none
7777
- Name: io-semihosting
78-
DriverArgs: [-DSEMIHOSTING]
78+
MacroDefines: [SEMIHOSTING]
7979
- Name: io-linux-syscalls
80-
DriverArgs: [-USEMIHOSTING, -DLINUX_SYSCALLS]
80+
MacroDefines: [LINUX_SYSCALLS, HOSTED]
8181
Default: io-none

0 commit comments

Comments
 (0)