Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2024
Merged

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

merged 1 commit into from
Nov 29, 2024

Conversation

paparodeo
Copy link
Contributor

@paparodeo paparodeo commented Nov 16, 2024

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.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Nov 16, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 16, 2024

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Author: Reno Dakota (paparodeo)

Changes

Fixes: #116278

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.


Full diff: https://github.com/llvm/llvm-project/pull/116476.diff

2 Files Affected:

  • (modified) clang/lib/Driver/Driver.cpp (+6-7)
  • (modified) clang/test/Driver/unsupported-option.c (+10)
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 93e85f7dffe35a..8e784a7b130ac3 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4064,17 +4064,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;
@@ -4129,10 +4130,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

@paparodeo paparodeo changed the title [Clang][Driver] report unsupported option error regardless of argument order [Clang][Driver] report unsupported option error when link + compile in same process Nov 24, 2024
@Sirraide Sirraide requested a review from MaskRay November 25, 2024 00:36
// 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalize this diagnostic

…t 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.
@MaskRay MaskRay merged commit cbf495f into llvm:main Nov 29, 2024
4 of 6 checks passed
Copy link

@paparodeo Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@paparodeo paparodeo deleted the clang-unsupported-option branch November 29, 2024 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang] clang -mpopcnt hello.c <link flag> fails to generate warning when file precedes link flag
3 participants