diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td --- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td +++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td @@ -47,6 +47,9 @@ def warn_fe_backend_unsupported_fp_exceptions : Warning< "overriding currently unsupported use of floating point exceptions " "on this target">, InGroup; +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; 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 --- 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 @@ const std::vector &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 --- /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]