[clang] cbf495f - [Clang][Driver] report unsupported option error when link + compile in same process (#116476)

via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 29 12:16:08 PST 2024


Author: Reno Dakota
Date: 2024-11-29T12:16:04-08:00
New Revision: cbf495fd12ba08c3a8e10dbcd482df1bf80736ed

URL: https://github.com/llvm/llvm-project/commit/cbf495fd12ba08c3a8e10dbcd482df1bf80736ed
DIFF: https://github.com/llvm/llvm-project/commit/cbf495fd12ba08c3a8e10dbcd482df1bf80736ed.diff

LOG: [Clang][Driver] report unsupported option error when link + compile in same process (#116476)

Fixes: https://github.com/llvm/llvm-project/issues/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.

Added: 
    

Modified: 
    clang/lib/Driver/Driver.cpp
    clang/test/Driver/unsupported-option.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index ad14b5c9b6dc80..fdf21e35356ad8 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4063,17 +4063,18 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
     YcArg = YuArg = nullptr;
   }
 
-  unsigned LastPLSize = 0;
+  bool LinkOnly = phases::Link == FinalPhase && Inputs.size() > 0;
   for (auto &I : Inputs) {
     types::ID InputType = I.first;
     const Arg *InputArg = I.second;
 
     auto PL = types::getCompilationPhases(InputType);
-    LastPLSize = PL.size();
+
+    phases::ID InitialPhase = PL[0];
+    LinkOnly = LinkOnly && phases::Link == InitialPhase && PL.size() == 1;
 
     // If the first step comes after the final phase we are doing as part of
     // this compilation, warn the user about it.
-    phases::ID InitialPhase = PL[0];
     if (InitialPhase > FinalPhase) {
       if (InputArg->isClaimed())
         continue;
@@ -4128,10 +4129,8 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
     }
   }
 
-  // If we are linking, claim any options which are obviously only used for
-  // compilation.
-  // FIXME: Understand why the last Phase List length is used here.
-  if (FinalPhase == phases::Link && LastPLSize == 1) {
+  // Claim any options which are obviously only used for compilation.
+  if (LinkOnly) {
     Args.ClaimAllArgs(options::OPT_CompileOnly_Group);
     Args.ClaimAllArgs(options::OPT_cl_compile_Group);
   }

diff  --git a/clang/test/Driver/unsupported-option.c b/clang/test/Driver/unsupported-option.c
index 6be531d9df7de9..af836cf0033741 100644
--- a/clang/test/Driver/unsupported-option.c
+++ b/clang/test/Driver/unsupported-option.c
@@ -17,3 +17,13 @@
 // RUN: not %clang -fprofile-sample-use=code.prof --target=powerpc-ibm-aix %s 2>&1 | \
 // RUN: FileCheck %s --check-prefix=AIX-PROFILE-SAMPLE
 // AIX-PROFILE-SAMPLE: error: unsupported option '-fprofile-sample-use=' for target
+
+// -mhtm is unsupported on x86_64. Test that using it in 
diff erent command
+// line permutations generates an `unsupported option` error.
+// RUN: not %clang --target=x86_64 -### %s -mhtm 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=UNSUP_OPT
+// RUN: not %clang --target=x86_64 -### %s -mhtm -lc 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=UNSUP_OPT
+// RUN: not %clang --target=x86_64 -### -mhtm -lc %s 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=UNSUP_OPT
+// UNSUP_OPT: error: unsupported option


        


More information about the cfe-commits mailing list