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

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 29 12:15:22 PST 2024


https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/116476

>From 49a748517c19adc10e9c65b548642a468de2d65e Mon Sep 17 00:00:00 2001
From: Reno Dakota <paparodeo at proton.me>
Date: Sat, 16 Nov 2024 05:57:40 +0000
Subject: [PATCH] [Clang][Driver] report unsupported option error regardless of
 argument order

This change updates clang to report unsupported option errors regardless
of the command line argument order.

When clang 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.
---
 clang/lib/Driver/Driver.cpp            | 13 ++++++-------
 clang/test/Driver/unsupported-option.c | 10 ++++++++++
 2 files changed, 16 insertions(+), 7 deletions(-)

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 different 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