[clang] a79995c - [Driver] Allow warning for unclaimed TargetSpecific options

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 16 08:32:29 PDT 2023


Author: Fangrui Song
Date: 2023-06-16T08:32:25-07:00
New Revision: a79995ca6004082774a87f7a58ab6be5343364b7

URL: https://github.com/llvm/llvm-project/commit/a79995ca6004082774a87f7a58ab6be5343364b7
DIFF: https://github.com/llvm/llvm-project/commit/a79995ca6004082774a87f7a58ab6be5343364b7.diff

LOG: [Driver] Allow warning for unclaimed TargetSpecific options

For unclaimed target-agnostic options, we can apply clang_ignored_gcc_optimization_f_Group
to accept but warn about them.
```
% clang -c -fexpensive-optimizations a.c
clang: warning: optimization flag '-fexpensive-optimizations' is not supported [-Wignored-optimization-argument]
```

For an unclaimed target-specific option, one target may want to accept but warn
about it. Add `llvm::opt::Arg::IgnoredTargetSpecific` to support this warning
need.

Close https://github.com/llvm/llvm-project/issues/63282

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D152856

Added: 
    

Modified: 
    clang/include/clang/Driver/Options.td
    clang/lib/Driver/Driver.cpp
    clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
    clang/lib/Driver/ToolChains/MinGW.cpp
    llvm/include/llvm/Option/Arg.h
    llvm/lib/Option/Arg.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 06f02a05b7f13..87df2266bd7e3 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3555,7 +3555,6 @@ def mqdsp6_compat : Flag<["-"], "mqdsp6-compat">, Group<m_Group>, Flags<[NoXarch
 def m64 : Flag<["-"], "m64">, Group<m_Group>, Flags<[NoXarchOption, CoreOption]>;
 def maix64 : Flag<["-"], "maix64">, Group<m_Group>, Flags<[NoXarchOption]>;
 def mx32 : Flag<["-"], "mx32">, Group<m_Group>, Flags<[NoXarchOption, CoreOption]>;
-def mabi_EQ : Joined<["-"], "mabi=">, Group<m_Group>;
 def miamcu : Flag<["-"], "miamcu">, Group<m_Group>, Flags<[NoXarchOption, CoreOption]>,
   HelpText<"Use Intel MCU ABI">;
 def mno_iamcu : Flag<["-"], "mno-iamcu">, Group<m_Group>, Flags<[NoXarchOption, CoreOption]>;
@@ -3564,6 +3563,7 @@ def malign_loops_EQ : Joined<["-"], "malign-loops=">, Group<clang_ignored_m_Grou
 def malign_jumps_EQ : Joined<["-"], "malign-jumps=">, Group<clang_ignored_m_Group>;
 
 let Flags = [TargetSpecific] in {
+def mabi_EQ : Joined<["-"], "mabi=">, Group<m_Group>;
 def malign_branch_EQ : CommaJoined<["-"], "malign-branch=">, Group<m_Group>,
   HelpText<"Specify types of branches to align">;
 def malign_branch_boundary_EQ : Joined<["-"], "malign-branch-boundary=">, Group<m_Group>,
@@ -3651,9 +3651,7 @@ def malign_double : Flag<["-"], "malign-double">, Group<m_Group>, Flags<[CC1Opti
 let Flags = [TargetSpecific] in {
 def mfloat_abi_EQ : Joined<["-"], "mfloat-abi=">, Group<m_Group>, Values<"soft,softfp,hard">;
 def mfpmath_EQ : Joined<["-"], "mfpmath=">, Group<m_Group>;
-} // let Flags = [TargetSpecific]
 def mfpu_EQ : Joined<["-"], "mfpu=">, Group<m_Group>;
-let Flags = [TargetSpecific] in {
 def mhwdiv_EQ : Joined<["-"], "mhwdiv=">, Group<m_Group>;
 def mhwmult_EQ : Joined<["-"], "mhwmult=">, Group<m_Group>;
 } // let Flags = [TargetSpecific]

diff  --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 9fc62be357a60..4c282241d2b25 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4892,7 +4892,8 @@ void Driver::BuildJobs(Compilation &C) const {
       // In clang-cl, don't mention unknown arguments here since they have
       // already been warned about.
       if (!IsCLMode() || !A->getOption().matches(options::OPT_UNKNOWN)) {
-        if (A->getOption().hasFlag(options::TargetSpecific)) {
+        if (A->getOption().hasFlag(options::TargetSpecific) &&
+            !A->isIgnoredTargetSpecific()) {
           Diag(diag::err_drv_unsupported_opt_for_target)
               << A->getSpelling() << getTargetTriple();
         } else {

diff  --git a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
index 44c7472f60a60..fe0d5cf149302 100644
--- a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
@@ -139,4 +139,10 @@ void loongarch::getLoongArchTargetFeatures(const Driver &D,
   // or the alias -m[no-]strict-align.
   AddTargetFeature(Args, Features, options::OPT_munaligned_access,
                    options::OPT_mno_unaligned_access, "ual");
+
+  // Accept but warn about these TargetSpecific options.
+  if (Arg *A = Args.getLastArgNoClaim(options::OPT_mabi_EQ))
+    A->ignoreTargetSpecific();
+  if (Arg *A = Args.getLastArgNoClaim(options::OPT_mfpu_EQ))
+    A->ignoreTargetSpecific();
 }

diff  --git a/clang/lib/Driver/ToolChains/MinGW.cpp b/clang/lib/Driver/ToolChains/MinGW.cpp
index 947abb9b02b51..6ce36f84a6d0c 100644
--- a/clang/lib/Driver/ToolChains/MinGW.cpp
+++ b/clang/lib/Driver/ToolChains/MinGW.cpp
@@ -701,6 +701,9 @@ void toolchains::MinGW::addClangTargetOptions(
           << A->getSpelling() << GuardArgs;
     }
   }
+
+  if (Arg *A = DriverArgs.getLastArgNoClaim(options::OPT_mthreads))
+    A->ignoreTargetSpecific();
 }
 
 void toolchains::MinGW::AddClangCXXStdlibIncludeArgs(

diff  --git a/llvm/include/llvm/Option/Arg.h b/llvm/include/llvm/Option/Arg.h
index 4be254ccdab44..5a718438bf4a3 100644
--- a/llvm/include/llvm/Option/Arg.h
+++ b/llvm/include/llvm/Option/Arg.h
@@ -47,11 +47,17 @@ class Arg {
   /// ArgList.
   unsigned Index;
 
-  /// Was this argument used to effect compilation?
+  /// Was this argument used to affect compilation?
   ///
-  /// This is used for generating "argument unused" diagnostics.
+  /// This is used to generate an "argument unused" warning (without
+  /// clang::driver::options::TargetSpecific) or "unsupported option" error
+  /// (with TargetSpecific).
   mutable unsigned Claimed : 1;
 
+  /// Used by an unclaimed option with the TargetSpecific flag. If set, report
+  /// an "argument unused" warning instead of an "unsupported option" error.
+  unsigned IgnoredTargetSpecific : 1;
+
   /// Does this argument own its values?
   mutable unsigned OwnsValues : 1;
 
@@ -93,6 +99,7 @@ class Arg {
   const Arg &getBaseArg() const {
     return BaseArg ? *BaseArg : *this;
   }
+  Arg &getBaseArg() { return BaseArg ? const_cast<Arg &>(*BaseArg) : *this; }
   void setBaseArg(const Arg *BaseArg) { this->BaseArg = BaseArg; }
 
   /// Args are converted to their unaliased form.  For args that originally
@@ -104,10 +111,15 @@ class Arg {
   void setOwnsValues(bool Value) const { OwnsValues = Value; }
 
   bool isClaimed() const { return getBaseArg().Claimed; }
-
-  /// Set the Arg claimed bit.
   void claim() const { getBaseArg().Claimed = true; }
 
+  bool isIgnoredTargetSpecific() const {
+    return getBaseArg().IgnoredTargetSpecific;
+  }
+  void ignoreTargetSpecific() {
+    getBaseArg().IgnoredTargetSpecific = true;
+  }
+
   unsigned getNumValues() const { return Values.size(); }
 
   const char *getValue(unsigned N = 0) const {

diff  --git a/llvm/lib/Option/Arg.cpp b/llvm/lib/Option/Arg.cpp
index 2da32bfacf306..48d173accdac2 100644
--- a/llvm/lib/Option/Arg.cpp
+++ b/llvm/lib/Option/Arg.cpp
@@ -20,19 +20,19 @@ using namespace llvm::opt;
 
 Arg::Arg(const Option Opt, StringRef S, unsigned Index, const Arg *BaseArg)
     : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
-      OwnsValues(false) {}
+      IgnoredTargetSpecific(false), OwnsValues(false) {}
 
 Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0,
          const Arg *BaseArg)
     : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
-      OwnsValues(false) {
+      IgnoredTargetSpecific(false), OwnsValues(false) {
   Values.push_back(Value0);
 }
 
 Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0,
          const char *Value1, const Arg *BaseArg)
     : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
-      OwnsValues(false) {
+      IgnoredTargetSpecific(false), OwnsValues(false) {
   Values.push_back(Value0);
   Values.push_back(Value1);
 }


        


More information about the cfe-commits mailing list