[clang] [Clang][Driver] Warn about `-c/-S` with `-fsyntax-only` (PR #98607)

Dmitriy Chestnykh via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 14 23:34:26 PDT 2024


https://github.com/chestnykh updated https://github.com/llvm/llvm-project/pull/98607

>From ae43fc67b7d9db848ec8c4ecbcab1db6af7fcb01 Mon Sep 17 00:00:00 2001
From: Dmitry Chestnykh <dm.chestnykh at gmail.com>
Date: Fri, 12 Jul 2024 12:25:16 +0300
Subject: [PATCH 1/2] [Clang][Driver] Warn about `-c/-S` with `-fsyntax-only`

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
---
 clang/lib/Driver/ToolChains/Clang.cpp     | 4 ----
 clang/test/Driver/warn-fsyntax-only-c-S.c | 9 +++++++++
 2 files changed, 9 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/Driver/warn-fsyntax-only-c-S.c

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]

>From 607a43381a39b4a6e18a247e7a1ab9ca87690bd5 Mon Sep 17 00:00:00 2001
From: Dmitry Chestnykh <dm.chestnykh at gmail.com>
Date: Mon, 15 Jul 2024 09:35:40 +0300
Subject: [PATCH 2/2] [Clang][Driver] Refactor test

- Rename to warn-fsyntax-only.c
- Add `-fsyntax-only -E` testcase
- Remove `-c -S` case because we really don't need it
---
 .../Driver/{warn-fsyntax-only-c-S.c => warn-fsyntax-only.c}  | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
 rename clang/test/Driver/{warn-fsyntax-only-c-S.c => warn-fsyntax-only.c} (55%)

diff --git a/clang/test/Driver/warn-fsyntax-only-c-S.c b/clang/test/Driver/warn-fsyntax-only.c
similarity index 55%
rename from clang/test/Driver/warn-fsyntax-only-c-S.c
rename to clang/test/Driver/warn-fsyntax-only.c
index bb1cd5fd1183c..633aebfc54d5d 100644
--- a/clang/test/Driver/warn-fsyntax-only-c-S.c
+++ b/clang/test/Driver/warn-fsyntax-only.c
@@ -1,9 +1,8 @@
+// RUN: %clang -fsyntax-only -E %s 2>&1 | FileCheck %s --check-prefix=CHECK-PP
 // 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-PP:  warning: argument unused during compilation: '-fsyntax-only' [-Wunused-command-line-argument]
 // 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]



More information about the cfe-commits mailing list