Skip to content

Commit cbf495f

Browse files
authored
[Clang][Driver] report unsupported option error when link + compile in same process (#116476)
Fixes: #116278 This change updates clang to report unsupported option errors regardless of the command line argument order. When clang is invoked with a source file and without `-c` it will both compile and link. When an unsupported option is also part of the command line clang should generated an error. However, if the source file name comes before an object file, eg: `-lc`, the error is ignored. ``` $ clang --target=x86_64 -lc hello.c -mhtm clang: error: unsupported option '-mhtm' for target 'x86_64' $ echo $? 1 ``` but if `-lc` comes after `hello.c` the error is dropped ``` $ clang --target=x86_64 hello.c -mhtm -lc $ echo $? 0 ``` after this change clang will report the error regardless of the command line argument order.
1 parent 2de2e7a commit cbf495f

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4063,17 +4063,18 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
40634063
YcArg = YuArg = nullptr;
40644064
}
40654065

4066-
unsigned LastPLSize = 0;
4066+
bool LinkOnly = phases::Link == FinalPhase && Inputs.size() > 0;
40674067
for (auto &I : Inputs) {
40684068
types::ID InputType = I.first;
40694069
const Arg *InputArg = I.second;
40704070

40714071
auto PL = types::getCompilationPhases(InputType);
4072-
LastPLSize = PL.size();
4072+
4073+
phases::ID InitialPhase = PL[0];
4074+
LinkOnly = LinkOnly && phases::Link == InitialPhase && PL.size() == 1;
40734075

40744076
// If the first step comes after the final phase we are doing as part of
40754077
// this compilation, warn the user about it.
4076-
phases::ID InitialPhase = PL[0];
40774078
if (InitialPhase > FinalPhase) {
40784079
if (InputArg->isClaimed())
40794080
continue;
@@ -4128,10 +4129,8 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
41284129
}
41294130
}
41304131

4131-
// If we are linking, claim any options which are obviously only used for
4132-
// compilation.
4133-
// FIXME: Understand why the last Phase List length is used here.
4134-
if (FinalPhase == phases::Link && LastPLSize == 1) {
4132+
// Claim any options which are obviously only used for compilation.
4133+
if (LinkOnly) {
41354134
Args.ClaimAllArgs(options::OPT_CompileOnly_Group);
41364135
Args.ClaimAllArgs(options::OPT_cl_compile_Group);
41374136
}

clang/test/Driver/unsupported-option.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@
1717
// RUN: not %clang -fprofile-sample-use=code.prof --target=powerpc-ibm-aix %s 2>&1 | \
1818
// RUN: FileCheck %s --check-prefix=AIX-PROFILE-SAMPLE
1919
// AIX-PROFILE-SAMPLE: error: unsupported option '-fprofile-sample-use=' for target
20+
21+
// -mhtm is unsupported on x86_64. Test that using it in different command
22+
// line permutations generates an `unsupported option` error.
23+
// RUN: not %clang --target=x86_64 -### %s -mhtm 2>&1 \
24+
// RUN: | FileCheck %s -check-prefix=UNSUP_OPT
25+
// RUN: not %clang --target=x86_64 -### %s -mhtm -lc 2>&1 \
26+
// RUN: | FileCheck %s -check-prefix=UNSUP_OPT
27+
// RUN: not %clang --target=x86_64 -### -mhtm -lc %s 2>&1 \
28+
// RUN: | FileCheck %s -check-prefix=UNSUP_OPT
29+
// UNSUP_OPT: error: unsupported option

0 commit comments

Comments
 (0)