[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
This commit is contained in:
wangliushuai 2022-09-25 10:16:30 +08:00
parent 5358968e13
commit 910ad36e1a
3 changed files with 19 additions and 2 deletions

View File

@ -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 "

View File

@ -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;
}

View File

@ -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]