Skip to content
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

[Clang][Driver] Warn about -c/-S with -fsyntax-only #98607

Merged
merged 3 commits into from
Jul 22, 2024

Conversation

chestnykh
Copy link
Contributor

Emit warning that -S and/or -c arguments are not used if -fsyntax-only is also passed to clang
addPGOAndCoverageFlags is not the right place
to produce this warning
Now -fsyntax-only -c/-S combination handles like -fsyntax-only -E in BuildJobs() driver function

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

llvmbot commented Jul 12, 2024

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Author: Dmitriy Chestnykh (chestnykh)

Changes

Emit warning that -S and/or -c arguments are not used if -fsyntax-only is also passed to clang
addPGOAndCoverageFlags is not the right place
to produce this warning
Now -fsyntax-only -c/-S combination handles like -fsyntax-only -E in BuildJobs() driver function


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

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/Clang.cpp (-4)
  • (added) clang/test/Driver/warn-fsyntax-only-c-S.c (+9)
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 4a674f67b8e1b..4440725ccd923 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -794,10 +794,6 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
       Args.hasArg(options::OPT_coverage))
     FProfileDir = Args.getLastArg(options::OPT_fprofile_dir);
 
-  // TODO: Don't claim -c/-S to warn about -fsyntax-only -c/-S, -E -c/-S,
-  // like we warn about -fsyntax-only -E.
-  (void)(Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S));
-
   // Put the .gcno and .gcda files (if needed) next to the primary output file,
   // or fall back to a file in the current directory for `clang -c --coverage
   // d/a.c` in the absence of -o.
diff --git a/clang/test/Driver/warn-fsyntax-only-c-S.c b/clang/test/Driver/warn-fsyntax-only-c-S.c
new file mode 100644
index 0000000000000..bb1cd5fd1183c
--- /dev/null
+++ b/clang/test/Driver/warn-fsyntax-only-c-S.c
@@ -0,0 +1,9 @@
+// RUN: %clang -fsyntax-only -S %s 2>&1 | FileCheck %s --check-prefix=CHECK-ASM
+// RUN: %clang -fsyntax-only -c %s 2>&1 | FileCheck %s --check-prefix=CHECK-OBJ
+// RUN: %clang -fsyntax-only -S -c %s 2>&1 | FileCheck %s --check-prefix=CHECK-BOTH
+
+// CHECK-ASM: warning: argument unused during compilation: '-S' [-Wunused-command-line-argument]
+// CHECK-OBJ: warning: argument unused during compilation: '-c' [-Wunused-command-line-argument]
+
+// CHECK-BOTH: warning: argument unused during compilation: '-S' [-Wunused-command-line-argument]
+// CHECK-NEXT: warning: argument unused during compilation: '-c' [-Wunused-command-line-argument]

@chestnykh
Copy link
Contributor Author

CC: @shafik @ChuanqiXu9

Emit warning that `-S` and/or `-c` arguments are not used
if `-fsyntax-only` is also passed to clang
`addPGOAndCoverageFlags` is not the right place
to produce this warning
Now `-fsyntax-only -c/-S` combination handles like
`-fsyntax-only -E` in `BuildJobs()` driver function
@@ -794,10 +794,6 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
Args.hasArg(options::OPT_coverage))
FProfileDir = Args.getLastArg(options::OPT_fprofile_dir);

// TODO: Don't claim -c/-S to warn about -fsyntax-only -c/-S, -E -c/-S,
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for catching this. I left this in a07b135 to be cleaned up later but I forgot to remove this TODO.

@@ -0,0 +1,9 @@
// RUN: %clang -fsyntax-only -S %s 2>&1 | FileCheck %s --check-prefix=CHECK-ASM
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps just warn-fsyntax-only.c

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed

// CHECK-OBJ: warning: argument unused during compilation: '-c' [-Wunused-command-line-argument]

// CHECK-BOTH: warning: argument unused during compilation: '-S' [-Wunused-command-line-argument]
// CHECK-NEXT: warning: argument unused during compilation: '-c' [-Wunused-command-line-argument]
Copy link
Member

Choose a reason for hiding this comment

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

CHECK-NEXT is not used by any FileCheck command

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added -fsyntax-only -E case. -c -S i think is like duplication of -c and -S test cases

- Rename to warn-fsyntax-only.c
- Add `-fsyntax-only -E` testcase
- Remove `-c -S` case because we really don't need it
@chestnykh chestnykh requested a review from MaskRay July 21, 2024 12:21
@chestnykh chestnykh merged commit 1efcc53 into llvm:main Jul 22, 2024
7 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 22, 2024

LLVM Buildbot has detected a new failure on builder llvm-clang-x86_64-sie-ubuntu-fast running on sie-linux-worker while building clang at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/144/builds/2918

Here is the relevant piece of the build log for the reference:

Step 6 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'Clang :: Driver/warn-fsyntax-only.c' FAILED ********************
Exit Code: 2

Command Output (stderr):
--
RUN: at line 1: /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/clang -fsyntax-only -E /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/Driver/warn-fsyntax-only.c 2>&1 | /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/FileCheck /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/Driver/warn-fsyntax-only.c --check-prefix=CHECK-PP
+ /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/clang -fsyntax-only -E /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/Driver/warn-fsyntax-only.c
+ /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/FileCheck /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/Driver/warn-fsyntax-only.c --check-prefix=CHECK-PP
RUN: at line 2: /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/clang -fsyntax-only -S /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/Driver/warn-fsyntax-only.c 2>&1 | /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/FileCheck /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/Driver/warn-fsyntax-only.c --check-prefix=CHECK-ASM
+ /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/clang -fsyntax-only -S /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/Driver/warn-fsyntax-only.c
+ /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/FileCheck /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/Driver/warn-fsyntax-only.c --check-prefix=CHECK-ASM
FileCheck error: '<stdin>' is empty.
FileCheck command line:  /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/FileCheck /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/Driver/warn-fsyntax-only.c --check-prefix=CHECK-ASM

--

********************


@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 22, 2024

LLVM Buildbot has detected a new failure on builder llvm-clang-x86_64-sie-win running on sie-win-worker while building clang at step 7 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/46/builds/2069

Here is the relevant piece of the build log for the reference:

Step 7 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'Clang :: Driver/warn-fsyntax-only.c' FAILED ********************
Exit Code: 2

Command Output (stdout):
--
# RUN: at line 1
z:\b\llvm-clang-x86_64-sie-win\build\bin\clang.exe -fsyntax-only -E Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver\warn-fsyntax-only.c 2>&1 | z:\b\llvm-clang-x86_64-sie-win\build\bin\filecheck.exe Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver\warn-fsyntax-only.c --check-prefix=CHECK-PP
# executed command: 'z:\b\llvm-clang-x86_64-sie-win\build\bin\clang.exe' -fsyntax-only -E 'Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver\warn-fsyntax-only.c'
# executed command: 'z:\b\llvm-clang-x86_64-sie-win\build\bin\filecheck.exe' 'Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver\warn-fsyntax-only.c' --check-prefix=CHECK-PP
# RUN: at line 2
z:\b\llvm-clang-x86_64-sie-win\build\bin\clang.exe -fsyntax-only -S Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver\warn-fsyntax-only.c 2>&1 | z:\b\llvm-clang-x86_64-sie-win\build\bin\filecheck.exe Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver\warn-fsyntax-only.c --check-prefix=CHECK-ASM
# executed command: 'z:\b\llvm-clang-x86_64-sie-win\build\bin\clang.exe' -fsyntax-only -S 'Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver\warn-fsyntax-only.c'
# executed command: 'z:\b\llvm-clang-x86_64-sie-win\build\bin\filecheck.exe' 'Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver\warn-fsyntax-only.c' --check-prefix=CHECK-ASM
# .---command stderr------------
# | FileCheck error: '<stdin>' is empty.
# | FileCheck command line:  z:\b\llvm-clang-x86_64-sie-win\build\bin\filecheck.exe Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Driver\warn-fsyntax-only.c --check-prefix=CHECK-ASM
# `-----------------------------
# error: command failed with exit status: 2

--

********************


@dyung
Copy link
Collaborator

dyung commented Jul 23, 2024

@chestnykh Can you take a look at the bot failures and revert if you need time to investigate so we can get the bots back to green?

dyung added a commit that referenced this pull request Jul 23, 2024
sgundapa pushed a commit to sgundapa/upstream_effort that referenced this pull request Jul 23, 2024
Emit warning that `-S` and/or `-c` arguments are not used if
`-fsyntax-only` is also passed to clang
`addPGOAndCoverageFlags` is not the right place
to produce this warning
Now `-fsyntax-only -c/-S` combination handles like `-fsyntax-only -E` in
`BuildJobs()` driver function
MaskRay added a commit that referenced this pull request Jul 24, 2024
Remove the TODO I left in commit
a07b135.

We will now warn about `-c/-S` with `-fsyntax-only`. This relands #98607
with a specific target triple.
@mstorsjo
Copy link
Member

FWIW, in addition to -c in combination with -fsyntax-only, this now also warns for -c in combination with -E.

That sounds like a reasonable thing to do to me, so it's probably fine, but it'd be nice to have that aspect acknowledged as one intended behaviour change as well.

mstorsjo added a commit to mstorsjo/gas-preprocessor that referenced this pull request Jul 25, 2024
A command like "cc -c -E" is tautological; the -c is ignored, when
we explicitly specify that we want to preprocess only.

Since
llvm/llvm-project@6461e53
and llvm/llvm-project#98607, Clang now
warns about the unused "-c" argument in this case.

We already did omit the "-c" argument when preprocessing
(with cl.exe) for armasm, but do this for other cases as well.
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Summary:
Emit warning that `-S` and/or `-c` arguments are not used if
`-fsyntax-only` is also passed to clang
`addPGOAndCoverageFlags` is not the right place
to produce this warning
Now `-fsyntax-only -c/-S` combination handles like `-fsyntax-only -E` in
`BuildJobs()` driver function

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251113
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Summary:
Remove the TODO I left in commit
a07b135.

We will now warn about `-c/-S` with `-fsyntax-only`. This relands #98607
with a specific target triple.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60250579
@MaskRay
Copy link
Member

MaskRay commented Jul 29, 2024

FWIW, in addition to -c in combination with -fsyntax-only, this now also warns for -c in combination with -E.

That sounds like a reasonable thing to do to me, so it's probably fine, but it'd be nice to have that aspect acknowledged as one intended behaviour change as well.

Thanks for pointing this out. Yes, the warning for -c -E seems desired. It's a bit difficult to analyze what combinations lead to warnings due to our claim code scattering all over the driver....

mstorsjo added a commit to FFmpeg/gas-preprocessor that referenced this pull request Jul 29, 2024
A command like "cc -c -E" is tautological; the -c is ignored, when
we explicitly specify that we want to preprocess only.

Since
llvm/llvm-project@6461e53
and llvm/llvm-project#98607, Clang now
warns about the unused "-c" argument in this case.

We already did omit the "-c" argument when preprocessing
(with cl.exe) for armasm, but do this for other cases as well.
MaskRay added a commit that referenced this pull request Jul 30, 2024
compile_commands.json entries often have -c. When adding -fsyntax-only,
we should remove -c to prevent the following warning:

```
% clang -c -fsyntax-only a.c
clang: warning: argument unused during compilation: '-c' [-Wunused-command-line-argument]
```

Previously, -c and -S were inappropriately claimed in
`addPGOAndCoverageFlags` (see the workaround added by commit
a07b135). Now the workaround have been
removed (#98607). (clangDriver reports a -Wunused-command-line-argument
diagnostic for each unclaimed option.)

Fix #100909

Pull Request: #101103
Harini0924 pushed a commit to Harini0924/llvm-project that referenced this pull request Aug 1, 2024
Remove the TODO I left in commit
a07b135.

We will now warn about `-c/-S` with `-fsyntax-only`. This relands llvm#98607
with a specific target triple.
banach-space pushed a commit to banach-space/llvm-project that referenced this pull request Aug 7, 2024
compile_commands.json entries often have -c. When adding -fsyntax-only,
we should remove -c to prevent the following warning:

```
% clang -c -fsyntax-only a.c
clang: warning: argument unused during compilation: '-c' [-Wunused-command-line-argument]
```

Previously, -c and -S were inappropriately claimed in
`addPGOAndCoverageFlags` (see the workaround added by commit
a07b135). Now the workaround have been
removed (llvm#98607). (clangDriver reports a -Wunused-command-line-argument
diagnostic for each unclaimed option.)

Fix llvm#100909

Pull Request: llvm#101103
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.

6 participants