[PATCH] D121683: Emit a warning if -xc/-xc++ is after the last input file

Yi Kong via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 15 10:08:31 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG1c1a4b9556db: [clang][driver] Emit a warning if -xc/-xc++ is after the last input file (authored by kongyi).
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121683/new/

https://reviews.llvm.org/D121683

Files:
  clang/include/clang/Basic/DiagnosticDriverKinds.td
  clang/lib/Driver/Driver.cpp
  clang/test/Driver/x-args.c


Index: clang/test/Driver/x-args.c
===================================================================
--- /dev/null
+++ clang/test/Driver/x-args.c
@@ -0,0 +1,7 @@
+// RUN: %clang -fsyntax-only -Werror -xc %s
+// RUN: %clang -fsyntax-only -Werror %s -xc %s
+
+// RUN: %clang -fsyntax-only %s -xc++ -fsyntax-only 2>&1 | FileCheck %s
+// RUN: %clang -fsyntax-only -xc %s -xc++ -fsyntax-only 2>&1 | FileCheck %s
+// RUN: %clang -fsyntax-only %s -xc %s -xc++ -fsyntax-only 2>&1 | FileCheck %s
+// CHECK: ‘-x c++’ after last input file has no effect
\ No newline at end of file
Index: clang/lib/Driver/Driver.cpp
===================================================================
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -2308,6 +2308,15 @@
     assert(!Args.hasArg(options::OPT_x) && "-x and /TC or /TP is not allowed");
   }
 
+  // Warn -x after last input file has no effect
+  {
+    Arg *LastXArg = Args.getLastArgNoClaim(options::OPT_x);
+    Arg *LastInputArg = Args.getLastArgNoClaim(options::OPT_INPUT);
+    if (LastInputArg->getIndex() < LastXArg->getIndex()) {
+      Diag(clang::diag::warn_drv_unused_x) << LastXArg->getValue();
+    }
+  }
+
   for (Arg *A : Args) {
     if (A->getOption().getKind() == Option::InputClass) {
       const char *Value = A->getValue();
Index: clang/include/clang/Basic/DiagnosticDriverKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -365,6 +365,9 @@
 def warn_drv_unused_argument : Warning<
   "argument unused during compilation: '%0'">,
   InGroup<UnusedCommandLineArgument>;
+def warn_drv_unused_x : Warning<
+  "‘-x %0’ after last input file has no effect">,
+  InGroup<UnusedCommandLineArgument>;
 def warn_drv_empty_joined_argument : Warning<
   "joined argument expects additional value: '%0'">,
   InGroup<UnusedCommandLineArgument>;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121683.415486.patch
Type: text/x-patch
Size: 1960 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220315/d6af3892/attachment.bin>


More information about the cfe-commits mailing list