Skip to content

Commit e9d454d

Browse files
committed
[Driver] Report warnings for unclaimed TargetSpecific options for assembler input
This patch amends D151590 to not error for unlaimed TargetSpecific options for `-x assembler` input files. This input type causes Driver to construct tools::ClangAs (-fintegrated-as) or other assemblers (e.g. tools::gnutools::Assembler) Their ConstructJobs methods, unlike Clang::ConstructJobs, claim very few options. If an option is unclaimed, it either leads to a -Wunused-command-line-argument warning or an error (if `TargetSpecific` is set): ``` % clang '-###' --target=aarch64 -mbranch-protection=bti -c a.s clang: error: unsupported option '-mbranch-protection=' for target 'aarch64' ``` It seems that downgrading the diagnostic to warning is most useful as many users use CFLAGS even for `.s` files: ``` clang --target=aarch64 -mbranch-protection=bti -S a.c clang --target=aarch64 -mbranch-protection=bti -c a.s ``` I decide not to suppress the warning so that -Wunused-command-line-argument lovers still get a warning, and help projects use proper ASFLAGS/CFLAGS/etc. Note: `-mbranch-protection=bti a.S` currently has no warning as `-x assembler-with-cpp` instructs clangDriver to select tools::Clang and claim most options. Revert D159010 to demonstrate that we emit a warning for -mfpmath= for `-x assembler` input. Modify my AIX cleanup cd18efb to add an err_drv_unsupported_opt_for_target. Reviewed By: thesamesam Differential Revision: https://reviews.llvm.org/D159173
1 parent dfa8a15 commit e9d454d

File tree

8 files changed

+31
-13
lines changed

8 files changed

+31
-13
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4917,6 +4917,12 @@ void Driver::BuildJobs(Compilation &C) const {
49174917
(void)C.getArgs().hasArg(options::OPT_driver_mode);
49184918
(void)C.getArgs().hasArg(options::OPT_rsp_quoting);
49194919

4920+
bool HasAssembleJob = llvm::any_of(C.getJobs(), [](auto &J) {
4921+
// Match ClangAs and other derived assemblers of Tool. ClangAs uses a
4922+
// longer ShortName "clang integrated assembler" while other assemblers just
4923+
// use "assembler".
4924+
return strstr(J.getCreator().getShortName(), "assembler");
4925+
});
49204926
for (Arg *A : C.getArgs()) {
49214927
// FIXME: It would be nice to be able to send the argument to the
49224928
// DiagnosticsEngine, so that extra values, position, and so on could be
@@ -4946,7 +4952,7 @@ void Driver::BuildJobs(Compilation &C) const {
49464952
// already been warned about.
49474953
if (!IsCLMode() || !A->getOption().matches(options::OPT_UNKNOWN)) {
49484954
if (A->getOption().hasFlag(options::TargetSpecific) &&
4949-
!A->isIgnoredTargetSpecific()) {
4955+
!A->isIgnoredTargetSpecific() && !HasAssembleJob) {
49504956
Diag(diag::err_drv_unsupported_opt_for_target)
49514957
<< A->getSpelling() << getTargetTriple();
49524958
} else {

clang/lib/Driver/ToolChains/AIX.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
3030
const InputInfoList &Inputs,
3131
const ArgList &Args,
3232
const char *LinkingOutput) const {
33+
const Driver &D = getToolChain().getDriver();
3334
ArgStringList CmdArgs;
3435

3536
const bool IsArch32Bit = getToolChain().getTriple().isArch32Bit();
@@ -38,6 +39,11 @@ void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
3839
if (!IsArch32Bit && !IsArch64Bit)
3940
llvm_unreachable("Unsupported bit width value.");
4041

42+
if (Arg *A = C.getArgs().getLastArg(options::OPT_G)) {
43+
D.Diag(diag::err_drv_unsupported_opt_for_target)
44+
<< A->getSpelling() << D.getTargetTriple();
45+
}
46+
4147
// Specify the mode in which the as(1) command operates.
4248
if (IsArch32Bit) {
4349
CmdArgs.push_back("-a32");

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,7 @@ std::string x86::getX86TargetCPU(const Driver &D, const ArgList &Args,
118118

119119
void x86::getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple,
120120
const ArgList &Args,
121-
std::vector<StringRef> &Features, bool ForAS) {
122-
if (ForAS) {
123-
// Some target-specific options are only handled in AddX86TargetArgs, which
124-
// is not called by ClangAs::ConstructJob. Claim them here.
125-
Args.claimAllArgs(options::OPT_mfpmath_EQ);
126-
}
127-
121+
std::vector<StringRef> &Features) {
128122
// Claim and report unsupported -mabi=. Note: we don't support "sysv_abi" or
129123
// "ms_abi" as default function attributes.
130124
if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mabi_EQ)) {

clang/lib/Driver/ToolChains/Arch/X86.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ std::string getX86TargetCPU(const Driver &D, const llvm::opt::ArgList &Args,
2626

2727
void getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple,
2828
const llvm::opt::ArgList &Args,
29-
std::vector<llvm::StringRef> &Features, bool ForAS);
29+
std::vector<llvm::StringRef> &Features);
3030

3131
} // end namespace x86
3232
} // end namespace target

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ void tools::getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
528528
break;
529529
case llvm::Triple::x86:
530530
case llvm::Triple::x86_64:
531-
x86::getX86TargetFeatures(D, Triple, Args, Features, ForAS);
531+
x86::getX86TargetFeatures(D, Triple, Args, Features);
532532
break;
533533
case llvm::Triple::hexagon:
534534
hexagon::getHexagonTargetFeatures(D, Triple, Args, Features);

clang/test/Driver/aarch64-target-as-march.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
// MULTIPLE-VALUES-NOT: "-target-feature" "+v8.2a
3636

3737
/// march to compiler and assembler, we choose the one suited to the input file type
38-
// RUN: not %clang --target=aarch64-linux-gnueabi -### -c -Wa,-march=armv8.3-a -march=armv8.4-a %s 2>&1 | \
38+
// RUN: %clang --target=aarch64-linux-gnueabi -### -c -Wa,-march=armv8.3-a -march=armv8.4-a %s 2>&1 | \
3939
// RUN: FileCheck --check-prefix=TARGET-FEATURE-3 %s
4040
// RUN: %clang --target=aarch64-linux-gnueabi -### -c -Wa,-march=armv8.3-a -march=armv8.4-a \
4141
// RUN: %S/Inputs/wildcard1.c 2>&1 | FileCheck --check-prefix=TARGET-FEATURE-4 %s

clang/test/Driver/target-specific.s

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// Check that we report a warning instead of an error for target-specific compilation only options.
2+
// RUN: %clang -### --target=aarch64 -faddrsig -mbranch-protection=standard -c %s 2>&1 | FileCheck %s
3+
// RUN: %clang -### --target=aarch64 -faddrsig -mbranch-protection=standard -c -fno-integrated-as %s 2>&1 | FileCheck %s
4+
5+
/// Report a warning if we perform the link phase.
6+
// RUN: %clang -### --target=aarch64 -faddrsig -mbranch-protection=standard %s 2>&1 | FileCheck %s
7+
8+
// CHECK: warning: argument unused during compilation: '-faddrsig'
9+
// CHECK: warning: argument unused during compilation: '-mbranch-protection=standard'
10+
11+
/// assembler-with-cpp claims compile only options. Ideally we should emit a warning.
12+
// RUN: %clang -### -Werror --target=aarch64 -c -faddrsig -mbranch-protection=standard -x assembler-with-cpp %s

clang/test/Driver/x86-mfpmath.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %clang -### -c --target=x86_64 -mfpmath=sse %s 2>&1 | FileCheck %s
22
// CHECK: "-mfpmath" "sse"
33

4-
/// Don't warn for assembler input.
5-
// RUN: %clang -### -Werror -c --target=x86_64 -mfpmath=sse -x assembler %s 2>&1 | FileCheck /dev/null --implicit-check-not='"-mfpmath"'
4+
// RUN: %clang -### -c --target=x86_64 -mfpmath=sse -x assembler %s 2>&1 | FileCheck %s --check-prefix=WARN
5+
// WARN: warning: argument unused during compilation: '-mfpmath=sse'

0 commit comments

Comments
 (0)