Skip to content

Commit a42bb8b

Browse files
mcinallyCameron McInallyshafik
authored
[Driver] Move CommonArgs to a location visible by the Frontend Drivers (#142800)
This patch moves the CommonArgs utilities into a location visible by the Frontend Drivers, so that the Frontend Drivers may share option parsing code with the Compiler Driver. This is useful when the Frontend Drivers would like to verify that their incoming options are well-formed and also not reinvent the option parsing wheel. We already see code in the Clang/Flang Drivers that is parsing and verifying its incoming options. E.g. OPT_ffp_contract. This option is parsed in the Compiler Driver, Clang Driver, and Flang Driver, all with slightly different parsing code. It would be nice if the Frontend Drivers were not required to duplicate this Compiler Driver code. That way there is no/low maintenance burden on keeping all these parsing functions in sync. Along those lines, the Frontend Drivers will now have a useful mechanism to verify their incoming options are well-formed. Currently, the Frontend Drivers trust that the Compiler Driver is not passing back junk in some cases. The Language Drivers may even accept junk with no error at all. E.g.: `clang -cc1 -mprefer-vector-width=junk test.c' With this patch, we'll now be able to tighten up incomming options to the Frontend drivers in a lightweight way. --------- Co-authored-by: Cameron McInally <[email protected]> Co-authored-by: Shafik Yaghmour <[email protected]>
1 parent 347186b commit a42bb8b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+95
-89
lines changed

clang/lib/Driver/ToolChains/CommonArgs.h renamed to clang/include/clang/Driver/CommonArgs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ void handleVectorizeLoopsArgs(const llvm::opt::ArgList &Args,
270270
/// Enable -fslp-vectorize based on the optimization level selected.
271271
void handleVectorizeSLPArgs(const llvm::opt::ArgList &Args,
272272
llvm::opt::ArgStringList &CmdArgs);
273+
274+
// Parse -mprefer-vector-width=. Return the Value string if well-formed.
275+
// Otherwise, return an empty string and issue a diagnosic message if needed.
276+
StringRef ParseMPreferVectorWidthOption(clang::DiagnosticsEngine &Diags,
277+
const llvm::opt::ArgList &Args);
278+
273279
} // end namespace tools
274280
} // end namespace driver
275281
} // end namespace clang

clang/lib/Driver/MultilibBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "clang/Driver/MultilibBuilder.h"
10-
#include "ToolChains/CommonArgs.h"
10+
#include "clang/Driver/CommonArgs.h"
1111
#include "llvm/ADT/StringMap.h"
1212
#include "llvm/Support/Path.h"
1313
#include "llvm/Support/Regex.h"

clang/lib/Driver/ToolChain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
#include "ToolChains/Arch/ARM.h"
1212
#include "ToolChains/Arch/RISCV.h"
1313
#include "ToolChains/Clang.h"
14-
#include "ToolChains/CommonArgs.h"
1514
#include "ToolChains/Flang.h"
1615
#include "ToolChains/InterfaceStubs.h"
1716
#include "clang/Basic/ObjCRuntime.h"
1817
#include "clang/Basic/Sanitizers.h"
1918
#include "clang/Config/config.h"
2019
#include "clang/Driver/Action.h"
20+
#include "clang/Driver/CommonArgs.h"
2121
#include "clang/Driver/Driver.h"
2222
#include "clang/Driver/InputInfo.h"
2323
#include "clang/Driver/Job.h"

clang/lib/Driver/ToolChains/AIX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "AIX.h"
10-
#include "CommonArgs.h"
10+
#include "clang/Driver/CommonArgs.h"
1111
#include "clang/Driver/Compilation.h"
1212
#include "clang/Driver/Options.h"
1313
#include "clang/Driver/SanitizerArgs.h"

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "AMDGPU.h"
10-
#include "CommonArgs.h"
1110
#include "clang/Basic/TargetID.h"
1211
#include "clang/Config/config.h"
12+
#include "clang/Driver/CommonArgs.h"
1313
#include "clang/Driver/Compilation.h"
1414
#include "clang/Driver/InputInfo.h"
1515
#include "clang/Driver/Options.h"

clang/lib/Driver/ToolChains/AVR.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "AVR.h"
10-
#include "CommonArgs.h"
10+
#include "clang/Driver/CommonArgs.h"
1111
#include "clang/Driver/Compilation.h"
1212
#include "clang/Driver/InputInfo.h"
1313
#include "clang/Driver/Options.h"

clang/lib/Driver/ToolChains/Arch/AArch64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "AArch64.h"
10-
#include "../CommonArgs.h"
10+
#include "clang/Driver/CommonArgs.h"
1111
#include "clang/Driver/Driver.h"
1212
#include "clang/Driver/Options.h"
1313
#include "llvm/Option/ArgList.h"

clang/lib/Driver/ToolChains/Arch/LoongArch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#include "LoongArch.h"
1010
#include "../Clang.h"
11-
#include "ToolChains/CommonArgs.h"
1211
#include "clang/Basic/DiagnosticDriver.h"
12+
#include "clang/Driver/CommonArgs.h"
1313
#include "clang/Driver/Driver.h"
1414
#include "clang/Driver/Options.h"
1515
#include "llvm/TargetParser/Host.h"

clang/lib/Driver/ToolChains/Arch/Mips.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Mips.h"
10-
#include "ToolChains/CommonArgs.h"
10+
#include "clang/Driver/CommonArgs.h"
1111
#include "clang/Driver/Driver.h"
1212
#include "clang/Driver/Options.h"
1313
#include "llvm/ADT/StringSwitch.h"

clang/lib/Driver/ToolChains/Arch/PPC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "PPC.h"
10-
#include "ToolChains/CommonArgs.h"
10+
#include "clang/Driver/CommonArgs.h"
1111
#include "clang/Driver/Driver.h"
1212
#include "clang/Driver/Options.h"
1313
#include "llvm/ADT/StringSwitch.h"

clang/lib/Driver/ToolChains/Arch/RISCV.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "RISCV.h"
1010
#include "../Clang.h"
11-
#include "ToolChains/CommonArgs.h"
11+
#include "clang/Driver/CommonArgs.h"
1212
#include "clang/Driver/Driver.h"
1313
#include "clang/Driver/Options.h"
1414
#include "llvm/Option/ArgList.h"

clang/lib/Driver/ToolChains/BareMetal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#include "BareMetal.h"
1010

11-
#include "CommonArgs.h"
1211
#include "Gnu.h"
12+
#include "clang/Driver/CommonArgs.h"
1313
#include "clang/Driver/InputInfo.h"
1414

1515
#include "Arch/ARM.h"

clang/lib/Driver/ToolChains/CSKYToolChain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "CSKYToolChain.h"
10-
#include "CommonArgs.h"
10+
#include "clang/Driver/CommonArgs.h"
1111
#include "clang/Driver/Compilation.h"
1212
#include "clang/Driver/InputInfo.h"
1313
#include "clang/Driver/Options.h"

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "Arch/RISCV.h"
1515
#include "Arch/Sparc.h"
1616
#include "Arch/SystemZ.h"
17-
#include "CommonArgs.h"
1817
#include "Hexagon.h"
1918
#include "PS4CPU.h"
2019
#include "clang/Basic/CLWarnings.h"
@@ -26,6 +25,7 @@
2625
#include "clang/Basic/Version.h"
2726
#include "clang/Config/config.h"
2827
#include "clang/Driver/Action.h"
28+
#include "clang/Driver/CommonArgs.h"
2929
#include "clang/Driver/Distro.h"
3030
#include "clang/Driver/InputInfo.h"
3131
#include "clang/Driver/Options.h"
@@ -264,27 +264,6 @@ static void ParseMRecip(const Driver &D, const ArgList &Args,
264264
OutStrings.push_back(Args.MakeArgString(Out));
265265
}
266266

267-
/// The -mprefer-vector-width option accepts either a positive integer
268-
/// or the string "none".
269-
static void ParseMPreferVectorWidth(const Driver &D, const ArgList &Args,
270-
ArgStringList &CmdArgs) {
271-
Arg *A = Args.getLastArg(options::OPT_mprefer_vector_width_EQ);
272-
if (!A)
273-
return;
274-
275-
StringRef Value = A->getValue();
276-
if (Value == "none") {
277-
CmdArgs.push_back("-mprefer-vector-width=none");
278-
} else {
279-
unsigned Width;
280-
if (Value.getAsInteger(10, Width)) {
281-
D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value;
282-
return;
283-
}
284-
CmdArgs.push_back(Args.MakeArgString("-mprefer-vector-width=" + Value));
285-
}
286-
}
287-
288267
static bool
289268
shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
290269
const llvm::Triple &Triple) {
@@ -7607,7 +7586,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
76077586

76087587
handleVectorizeLoopsArgs(Args, CmdArgs);
76097588
handleVectorizeSLPArgs(Args, CmdArgs);
7610-
ParseMPreferVectorWidth(D, Args, CmdArgs);
7589+
7590+
StringRef VecWidth = ParseMPreferVectorWidthOption(D.getDiags(), Args);
7591+
if (!VecWidth.empty())
7592+
CmdArgs.push_back(Args.MakeArgString("-mprefer-vector-width=" + VecWidth));
76117593

76127594
Args.AddLastArg(CmdArgs, options::OPT_fshow_overloads_EQ);
76137595
Args.AddLastArg(CmdArgs,

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "CommonArgs.h"
9+
#include "clang/Driver/CommonArgs.h"
1010
#include "Arch/AArch64.h"
1111
#include "Arch/ARM.h"
1212
#include "Arch/CSKY.h"
@@ -3167,3 +3167,25 @@ void tools::handleInterchangeLoopsArgs(const ArgList &Args,
31673167
options::OPT_fno_loop_interchange, EnableInterchange))
31683168
CmdArgs.push_back("-floop-interchange");
31693169
}
3170+
3171+
// Parse -mprefer-vector-width=. Return the Value string if well-formed.
3172+
// Otherwise, return an empty string and issue a diagnosic message if needed.
3173+
StringRef tools::ParseMPreferVectorWidthOption(clang::DiagnosticsEngine &Diags,
3174+
const llvm::opt::ArgList &Args) {
3175+
Arg *A = Args.getLastArg(clang::driver::options::OPT_mprefer_vector_width_EQ);
3176+
if (!A)
3177+
return "";
3178+
3179+
StringRef Value = A->getValue();
3180+
unsigned Width LLVM_ATTRIBUTE_UNINITIALIZED;
3181+
3182+
// Only "none" and Integer values are accepted by
3183+
// -mprefer-vector-width=<value>.
3184+
if (Value != "none" && Value.getAsInteger(10, Width)) {
3185+
Diags.Report(clang::diag::err_drv_invalid_value)
3186+
<< A->getOption().getName() << Value;
3187+
return "";
3188+
}
3189+
3190+
return Value;
3191+
}

clang/lib/Driver/ToolChains/CrossWindows.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "CrossWindows.h"
10-
#include "CommonArgs.h"
10+
#include "clang/Driver/CommonArgs.h"
1111
#include "clang/Driver/Compilation.h"
1212
#include "clang/Driver/Driver.h"
1313
#include "clang/Driver/Options.h"

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Cuda.h"
10-
#include "CommonArgs.h"
1110
#include "clang/Basic/Cuda.h"
1211
#include "clang/Config/config.h"
12+
#include "clang/Driver/CommonArgs.h"
1313
#include "clang/Driver/Compilation.h"
1414
#include "clang/Driver/Distro.h"
1515
#include "clang/Driver/Driver.h"

clang/lib/Driver/ToolChains/Cygwin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Cygwin.h"
10-
#include "CommonArgs.h"
1110
#include "clang/Config/config.h"
11+
#include "clang/Driver/CommonArgs.h"
1212
#include "clang/Driver/Driver.h"
1313
#include "clang/Driver/Options.h"
1414
#include "llvm/Support/Path.h"

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
#include "Darwin.h"
1010
#include "Arch/ARM.h"
11-
#include "CommonArgs.h"
1211
#include "clang/Basic/AlignedAllocation.h"
1312
#include "clang/Basic/ObjCRuntime.h"
1413
#include "clang/Config/config.h"
14+
#include "clang/Driver/CommonArgs.h"
1515
#include "clang/Driver/Compilation.h"
1616
#include "clang/Driver/Driver.h"
1717
#include "clang/Driver/Options.h"

clang/lib/Driver/ToolChains/DragonFly.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "DragonFly.h"
10-
#include "CommonArgs.h"
10+
#include "clang/Driver/CommonArgs.h"
1111
#include "clang/Driver/Compilation.h"
1212
#include "clang/Driver/Driver.h"
1313
#include "clang/Driver/Options.h"

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
#include "Flang.h"
1010
#include "Arch/RISCV.h"
11-
#include "CommonArgs.h"
1211

1312
#include "clang/Basic/CodeGenOptions.h"
13+
#include "clang/Driver/CommonArgs.h"
1414
#include "clang/Driver/Options.h"
1515
#include "llvm/Frontend/Debug/Options.h"
1616
#include "llvm/Support/Path.h"

clang/lib/Driver/ToolChains/FreeBSD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include "Arch/ARM.h"
1111
#include "Arch/Mips.h"
1212
#include "Arch/Sparc.h"
13-
#include "CommonArgs.h"
1413
#include "clang/Config/config.h"
14+
#include "clang/Driver/CommonArgs.h"
1515
#include "clang/Driver/Compilation.h"
1616
#include "clang/Driver/Options.h"
1717
#include "clang/Driver/SanitizerArgs.h"

clang/lib/Driver/ToolChains/Fuchsia.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Fuchsia.h"
10-
#include "CommonArgs.h"
1110
#include "clang/Config/config.h"
11+
#include "clang/Driver/CommonArgs.h"
1212
#include "clang/Driver/Compilation.h"
1313
#include "clang/Driver/Driver.h"
1414
#include "clang/Driver/MultilibBuilder.h"

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include "Arch/RISCV.h"
1616
#include "Arch/Sparc.h"
1717
#include "Arch/SystemZ.h"
18-
#include "CommonArgs.h"
1918
#include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
19+
#include "clang/Driver/CommonArgs.h"
2020
#include "clang/Driver/Compilation.h"
2121
#include "clang/Driver/Driver.h"
2222
#include "clang/Driver/MultilibBuilder.h"

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
#include "HIPAMD.h"
1010
#include "AMDGPU.h"
11-
#include "CommonArgs.h"
1211
#include "HIPUtility.h"
1312
#include "SPIRV.h"
1413
#include "clang/Basic/Cuda.h"
14+
#include "clang/Driver/CommonArgs.h"
1515
#include "clang/Driver/Compilation.h"
1616
#include "clang/Driver/Driver.h"
1717
#include "clang/Driver/InputInfo.h"

clang/lib/Driver/ToolChains/HIPSPV.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "HIPSPV.h"
10-
#include "CommonArgs.h"
1110
#include "HIPUtility.h"
11+
#include "clang/Driver/CommonArgs.h"
1212
#include "clang/Driver/Compilation.h"
1313
#include "clang/Driver/Driver.h"
1414
#include "clang/Driver/InputInfo.h"

clang/lib/Driver/ToolChains/HIPUtility.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "HIPUtility.h"
10-
#include "CommonArgs.h"
10+
#include "clang/Driver/CommonArgs.h"
1111
#include "clang/Driver/Compilation.h"
1212
#include "clang/Driver/Options.h"
1313
#include "llvm/ADT/StringExtras.h"

clang/lib/Driver/ToolChains/HLSL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "HLSL.h"
10-
#include "CommonArgs.h"
10+
#include "clang/Driver/CommonArgs.h"
1111
#include "clang/Driver/Compilation.h"
1212
#include "clang/Driver/Job.h"
1313
#include "llvm/ADT/StringSwitch.h"

clang/lib/Driver/ToolChains/Haiku.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Haiku.h"
10-
#include "CommonArgs.h"
1110
#include "clang/Config/config.h"
11+
#include "clang/Driver/CommonArgs.h"
1212
#include "clang/Driver/Compilation.h"
1313
#include "clang/Driver/SanitizerArgs.h"
1414
#include "llvm/Support/Path.h"

clang/lib/Driver/ToolChains/Hexagon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Hexagon.h"
10-
#include "CommonArgs.h"
10+
#include "clang/Driver/CommonArgs.h"
1111
#include "clang/Driver/Compilation.h"
1212
#include "clang/Driver/Driver.h"
1313
#include "clang/Driver/InputInfo.h"

clang/lib/Driver/ToolChains/Hurd.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Hurd.h"
10-
#include "CommonArgs.h"
1110
#include "clang/Config/config.h"
11+
#include "clang/Driver/CommonArgs.h"
1212
#include "clang/Driver/Driver.h"
1313
#include "clang/Driver/Options.h"
1414
#include "llvm/Support/Path.h"

clang/lib/Driver/ToolChains/InterfaceStubs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "InterfaceStubs.h"
10-
#include "CommonArgs.h"
10+
#include "clang/Driver/CommonArgs.h"
1111
#include "clang/Driver/Compilation.h"
1212
#include "llvm/Support/Path.h"
1313

0 commit comments

Comments
 (0)