[clang] 910ad36 - [Clang] Improve diagnostics about the invalid target feature.

via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 24 19:33:42 PDT 2022


Author: wangliushuai
Date: 2022-09-25T10:27:08+08:00
New Revision: 910ad36e1a2592915b32941844cf089442972d7a

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

LOG: [Clang] Improve diagnostics about the invalid target feature.

Clang with debug builds will crash when run with empty target feature input.
And the warning message is a little bit confusing. This patch adds an empty
check and a new diagnostic to illustrate where goes wrong.

Reviewed By: MaskRay, aaron.ballman

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

Added: 
    clang/test/Frontend/invalid-target-feature.c

Modified: 
    clang/include/clang/Basic/DiagnosticFrontendKinds.td
    clang/lib/Basic/TargetInfo.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 98ac3915a391c..8a4b69c757aaa 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -47,6 +47,9 @@ def warn_fe_backend_unsupported_fp_rounding : Warning<
 def warn_fe_backend_unsupported_fp_exceptions : Warning<
     "overriding currently unsupported use of floating point exceptions "
     "on this target">, InGroup<UnsupportedFPOpt>;
+def warn_fe_backend_invalid_feature_flag : Warning<
+    "feature flag '%0' must start with either '+' to enable the feature or '-'"
+    " to disable it; flag ignored">, InGroup<InvalidCommandLineArgument>;
 
 def err_incompatible_fp_eval_method_options : Error<
     "option 'ffp-eval-method' cannot be used with option "

diff  --git a/clang/lib/Basic/TargetInfo.cpp b/clang/lib/Basic/TargetInfo.cpp
index 5dacdeb81d7cd..96138f36bd16c 100644
--- a/clang/lib/Basic/TargetInfo.cpp
+++ b/clang/lib/Basic/TargetInfo.cpp
@@ -14,6 +14,7 @@
 #include "clang/Basic/AddressSpaces.h"
 #include "clang/Basic/CharInfo.h"
 #include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/DiagnosticFrontend.h"
 #include "clang/Basic/LangOptions.h"
 #include "llvm/ADT/APFloat.h"
 #include "llvm/ADT/STLExtras.h"
@@ -494,9 +495,13 @@ bool TargetInfo::initFeatureMap(
     const std::vector<std::string> &FeatureVec) const {
   for (const auto &F : FeatureVec) {
     StringRef Name = F;
+    if (Name.empty())
+      continue;
     // Apply the feature via the target.
-    bool Enabled = Name[0] == '+';
-    setFeatureEnabled(Features, Name.substr(1), Enabled);
+    if (Name[0] != '+' && Name[0] != '-')
+      Diags.Report(diag::warn_fe_backend_invalid_feature_flag) << Name;
+    else
+      setFeatureEnabled(Features, Name.substr(1), Name[0] == '+');
   }
   return true;
 }

diff  --git a/clang/test/Frontend/invalid-target-feature.c b/clang/test/Frontend/invalid-target-feature.c
new file mode 100644
index 0000000000000..dba25f4e307f1
--- /dev/null
+++ b/clang/test/Frontend/invalid-target-feature.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -target-feature '' -target-feature m %s 2>&1 \
+// RUN:    | FileCheck -check-prefix=NO_PREFIX %s
+
+// NO_PREFIX: warning: feature flag 'm' must start with either '+' to enable the feature or '-' to disable it; flag ignored [-Winvalid-command-line-argument]
+
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -target-feature '' -target-feature ' n' %s 2>&1 \
+// RUN:    | FileCheck -check-prefix=NULL_PREFIX %s
+
+// NULL_PREFIX: warning: feature flag ' n' must start with either '+' to enable the feature or '-' to disable it; flag ignored [-Winvalid-command-line-argument]


        


More information about the cfe-commits mailing list