[PATCH] D116395: [Clang] Emit warning for -x option without effects

Qiu Chaofan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 29 23:10:42 PST 2021


qiucf created this revision.
qiucf added reviewers: rsmith, hans, thakis, awarzynski, brad, phosek.
qiucf requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Things might be confusing for people not familiar with how this option works. Add this warning like GCC.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116395

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


Index: clang/test/Driver/redundant-args.c
===================================================================
--- clang/test/Driver/redundant-args.c
+++ clang/test/Driver/redundant-args.c
@@ -1,2 +1,4 @@
-// RUN: %clang -target x86_64-apple-darwin10 \
-// RUN:   -Werror -x c -x c -fsyntax-only %s
+// RUN: %clang -target x86_64-apple-darwin10 -Werror -x c -x c -fsyntax-only %s
+// RUN: %clang -target x86_64-apple-darwin10 %s -### -x c 2>&1 | FileCheck %s
+
+// CHECK: warning: '-x c' after last input file has no effect
Index: clang/lib/Driver/Driver.cpp
===================================================================
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -2269,6 +2269,7 @@
     assert(!Args.hasArg(options::OPT_x) && "-x and /TC or /TP is not allowed");
   }
 
+  size_t InputsBeforeOptX = 0;
   for (Arg *A : Args) {
     if (A->getOption().getKind() == Option::InputClass) {
       const char *Value = A->getValue();
@@ -2387,6 +2388,7 @@
       InputTypeArg = A;
       InputType = types::lookupTypeForTypeSpecifier(A->getValue());
       A->claim();
+      InputsBeforeOptX = Inputs.size();
 
       // Follow gcc behavior and treat as linker input for invalid -x
       // options. Its not clear why we shouldn't just revert to unknown; but
@@ -2411,6 +2413,8 @@
     Arg *A = MakeInputArg(Args, Opts, "-");
     Inputs.push_back(std::make_pair(types::TY_C, A));
   }
+  if (Inputs.size() == InputsBeforeOptX && InputTypeArg != nullptr)
+    Diag(diag::warn_drv_ignored_option_x) << InputTypeArg->getValue(0);
 }
 
 namespace {
Index: clang/include/clang/Basic/DiagnosticDriverKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -568,6 +568,9 @@
   "'%0' does not support '-%1'; flag ignored">,
   InGroup<OptionIgnored>;
 
+def warn_drv_ignored_option_x : Warning<
+  "'-x %0' after last input file has no effect">, InGroup<OptionIgnored>;
+
 def warn_drv_darwin_sdk_invalid_settings : Warning<
   "SDK settings were ignored as 'SDKSettings.json' could not be parsed">,
   InGroup<DiagGroup<"darwin-sdk-settings">>;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116395.396614.patch
Type: text/x-patch
Size: 2199 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211230/711b3114/attachment-0001.bin>


More information about the cfe-commits mailing list