1From 7ef5ed98cc6666f64db2f155ded2077ce038e1e4 Mon Sep 17 00:00:00 2001
2From: Reno Dakota <paparodeo@proton.me>
3Date: Sat, 16 Nov 2024 05:57:40 +0000
4Subject: [PATCH] [Clang][Driver] report unsupported option error regardless of
5 argument order
6
7This change updates clang to report unsupported option errors regardless
8of the command line argument order.
9
10When clang with a source file and without `-c` it will both compile and
11link. When an unsupported option is also part of the command line clang
12should generated an error. However, if the source file name comes before
13an object file, eg: `-lc`, the error is ignored.
14
15```
16$ clang --target=x86_64 -lc hello.c -mhtm
17clang: error: unsupported option '-mhtm' for target 'x86_64'
18$ echo $?
191
20```
21
22but if `-lc` comes after `hello.c` the error is dropped
23
24```
25$ clang --target=x86_64 hello.c -mhtm -lc
26$ echo $?
270
28```
29
30after this change clang will report the error regardless of the command
31line argument order.
32---
33 clang/lib/Driver/Driver.cpp | 13 ++++++-------
34 clang/test/Driver/unsupported-option.c | 10 ++++++++++
35 2 files changed, 16 insertions(+), 7 deletions(-)
36
37diff --git a/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
38index 93e85f7dffe35a..8e784a7b130ac3 100644
39--- a/lib/Driver/Driver.cpp
40+++ b/lib/Driver/Driver.cpp
41@@ -4064,17 +4064,18 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
42 YcArg = YuArg = nullptr;
43 }
44
45- unsigned LastPLSize = 0;
46+ bool LinkOnly = phases::Link == FinalPhase && Inputs.size() > 0;
47 for (auto &I : Inputs) {
48 types::ID InputType = I.first;
49 const Arg *InputArg = I.second;
50
51 auto PL = types::getCompilationPhases(InputType);
52- LastPLSize = PL.size();
53+
54+ phases::ID InitialPhase = PL[0];
55+ LinkOnly = LinkOnly && phases::Link == InitialPhase && PL.size() == 1;
56
57 // If the first step comes after the final phase we are doing as part of
58 // this compilation, warn the user about it.
59- phases::ID InitialPhase = PL[0];
60 if (InitialPhase > FinalPhase) {
61 if (InputArg->isClaimed())
62 continue;
63@@ -4129,10 +4130,8 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
64 }
65 }
66
67- // If we are linking, claim any options which are obviously only used for
68- // compilation.
69- // FIXME: Understand why the last Phase List length is used here.
70- if (FinalPhase == phases::Link && LastPLSize == 1) {
71+ // claim any options which are obviously only used for compilation.
72+ if (LinkOnly) {
73 Args.ClaimAllArgs(options::OPT_CompileOnly_Group);
74 Args.ClaimAllArgs(options::OPT_cl_compile_Group);
75 }