[clang-tools-extra] 9dff9ec - [clang-tidy] Change checks that take enum configurations to use a new access method.
Nathan James via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 7 12:04:39 PDT 2020
Author: Nathan James
Date: 2020-04-07T20:04:31+01:00
New Revision: 9dff9ecdd113ca57b8c548ebcdb14121bfd1840c
URL: https://github.com/llvm/llvm-project/commit/9dff9ecdd113ca57b8c548ebcdb14121bfd1840c
DIFF: https://github.com/llvm/llvm-project/commit/9dff9ecdd113ca57b8c548ebcdb14121bfd1840c.diff
LOG: [clang-tidy] Change checks that take enum configurations to use a new access method.
Summary: Change all checks that take enums as configuration to use enum specific methods in `ClangTidyCheck::OptionsView`.
Reviewers: aaron.ballman, alexfh
Reviewed By: aaron.ballman
Subscribers: wuzish, nemanjai, kbarton, arphaman, xazax.hun, cfe-commits
Tags: #clang, #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D76606
Added:
clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-case-violation.cpp
Modified:
clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp
clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.cpp
clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.cpp
clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.cpp
clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp
clang-tools-extra/clang-tidy/utils/IncludeSorter.h
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
index 3f0ca4adb54c..513714e99cb8 100644
--- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
+++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
@@ -26,8 +26,9 @@ StringFindStartswithCheck::StringFindStartswithCheck(StringRef Name,
: ClangTidyCheck(Name, Context),
StringLikeClasses(utils::options::parseStringList(
Options.get("StringLikeClasses", "::std::basic_string"))),
- IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
- Options.getLocalOrGlobal("IncludeStyle", "llvm"))),
+ IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::getMapping(),
+ utils::IncludeSorter::IS_LLVM)),
AbseilStringsMatchHeader(
Options.get("AbseilStringsMatchHeader", "absl/strings/match.h")) {}
@@ -121,8 +122,8 @@ void StringFindStartswithCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "StringLikeClasses",
utils::options::serializeStringList(StringLikeClasses));
- Options.store(Opts, "IncludeStyle",
- utils::IncludeSorter::toString(IncludeStyle));
+ Options.store(Opts, "IncludeStyle", IncludeStyle,
+ utils::IncludeSorter::getMapping());
Options.store(Opts, "AbseilStringsMatchHeader", AbseilStringsMatchHeader);
}
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
index f616efb1db6b..a5756ff634bb 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
@@ -24,8 +24,9 @@ AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
InitVariablesCheck::InitVariablesCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
- Options.getLocalOrGlobal("IncludeStyle", "llvm"))),
+ IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::getMapping(),
+ utils::IncludeSorter::IS_LLVM)),
MathHeader(Options.get("MathHeader", "math.h")) {}
void InitVariablesCheck::registerMatchers(MatchFinder *Finder) {
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
index 56a8a8140271..b48511287f88 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
@@ -21,8 +21,9 @@ namespace cppcoreguidelines {
ProBoundsConstantArrayIndexCheck::ProBoundsConstantArrayIndexCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context), GslHeader(Options.get("GslHeader", "")),
- IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
- Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {}
+ IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::getMapping(),
+ utils::IncludeSorter::IS_LLVM)) {}
void ProBoundsConstantArrayIndexCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
index fefa56ae5ef5..ee6234704ff5 100644
--- a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
@@ -14,6 +14,7 @@
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
@@ -43,6 +44,25 @@ static const char EndVarName[] = "endVar";
static const char DerefByValueResultName[] = "derefByValueResult";
static const char DerefByRefResultName[] = "derefByRefResult";
+static ArrayRef<std::pair<StringRef, Confidence::Level>>
+getConfidenceMapping() {
+ static constexpr std::pair<StringRef, Confidence::Level> Mapping[] = {
+ {"reasonable", Confidence::CL_Reasonable},
+ {"safe", Confidence::CL_Safe},
+ {"risky", Confidence::CL_Risky}};
+ return makeArrayRef(Mapping);
+}
+
+static ArrayRef<std::pair<StringRef, VariableNamer::NamingStyle>>
+getStyleMapping() {
+ static constexpr std::pair<StringRef, VariableNamer::NamingStyle> Mapping[] =
+ {{"CamelCase", VariableNamer::NS_CamelCase},
+ {"camelBack", VariableNamer::NS_CamelBack},
+ {"lower_case", VariableNamer::NS_LowerCase},
+ {"UPPER_CASE", VariableNamer::NS_UpperCase}};
+ return makeArrayRef(Mapping);
+}
+
// shared matchers
static const TypeMatcher AnyType() { return anything(); }
@@ -457,26 +477,15 @@ LoopConvertCheck::RangeDescriptor::RangeDescriptor()
LoopConvertCheck::LoopConvertCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context), TUInfo(new TUTrackingInfo),
MaxCopySize(std::stoull(Options.get("MaxCopySize", "16"))),
- MinConfidence(StringSwitch<Confidence::Level>(
- Options.get("MinConfidence", "reasonable"))
- .Case("safe", Confidence::CL_Safe)
- .Case("risky", Confidence::CL_Risky)
- .Default(Confidence::CL_Reasonable)),
- NamingStyle(StringSwitch<VariableNamer::NamingStyle>(
- Options.get("NamingStyle", "CamelCase"))
- .Case("camelBack", VariableNamer::NS_CamelBack)
- .Case("lower_case", VariableNamer::NS_LowerCase)
- .Case("UPPER_CASE", VariableNamer::NS_UpperCase)
- .Default(VariableNamer::NS_CamelCase)) {}
+ MinConfidence(Options.get("MinConfidence", getConfidenceMapping(),
+ Confidence::CL_Reasonable)),
+ NamingStyle(Options.get("NamingStyle", getStyleMapping(),
+ VariableNamer::NS_CamelCase)) {}
void LoopConvertCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "MaxCopySize", std::to_string(MaxCopySize));
- SmallVector<std::string, 3> Confs{"risky", "reasonable", "safe"};
- Options.store(Opts, "MinConfidence", Confs[static_cast<int>(MinConfidence)]);
-
- SmallVector<std::string, 4> Styles{"camelBack", "CamelCase", "lower_case",
- "UPPER_CASE"};
- Options.store(Opts, "NamingStyle", Styles[static_cast<int>(NamingStyle)]);
+ Options.store(Opts, "MinConfidence", MinConfidence, getConfidenceMapping());
+ Options.store(Opts, "NamingStyle", NamingStyle, getStyleMapping());
}
void LoopConvertCheck::registerMatchers(MatchFinder *Finder) {
diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
index 39410d6d0c14..c49f7665d490 100644
--- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
@@ -41,12 +41,12 @@ std::string GetNewExprName(const CXXNewExpr *NewExpr,
const char MakeSmartPtrCheck::PointerType[] = "pointerType";
-MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name,
- ClangTidyContext* Context,
+MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name, ClangTidyContext *Context,
StringRef MakeSmartPtrFunctionName)
: ClangTidyCheck(Name, Context),
- IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
- Options.getLocalOrGlobal("IncludeStyle", "llvm"))),
+ IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::getMapping(),
+ utils::IncludeSorter::IS_LLVM)),
MakeSmartPtrFunctionHeader(
Options.get("MakeSmartPtrFunctionHeader", StdMemoryHeader)),
MakeSmartPtrFunctionName(
@@ -54,7 +54,8 @@ MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name,
IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
void MakeSmartPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle", utils::IncludeSorter::toString(IncludeStyle));
+ Options.store(Opts, "IncludeStyle", IncludeStyle,
+ utils::IncludeSorter::getMapping());
Options.store(Opts, "MakeSmartPtrFunctionHeader", MakeSmartPtrFunctionHeader);
Options.store(Opts, "MakeSmartPtrFunction", MakeSmartPtrFunctionName);
Options.store(Opts, "IgnoreMacros", IgnoreMacros);
diff --git a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
index c29f8c949b33..315ee29c738a 100644
--- a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
@@ -120,13 +120,14 @@ collectParamDecls(const CXXConstructorDecl *Ctor,
PassByValueCheck::PassByValueCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
- Options.getLocalOrGlobal("IncludeStyle", "llvm"))),
+ IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::getMapping(),
+ utils::IncludeSorter::IS_LLVM)),
ValuesOnly(Options.get("ValuesOnly", 0) != 0) {}
void PassByValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle",
- utils::IncludeSorter::toString(IncludeStyle));
+ Options.store(Opts, "IncludeStyle", IncludeStyle,
+ utils::IncludeSorter::getMapping());
Options.store(Opts, "ValuesOnly", ValuesOnly);
}
diff --git a/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp
index c9fcefdc7b10..ade2bec3a98b 100644
--- a/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp
@@ -74,12 +74,13 @@ AST_MATCHER(Decl, isFromStdNamespace) {
ReplaceAutoPtrCheck::ReplaceAutoPtrCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
- Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {}
+ IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::getMapping(),
+ utils::IncludeSorter::IS_LLVM)) {}
void ReplaceAutoPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle",
- utils::IncludeSorter::toString(IncludeStyle));
+ Options.store(Opts, "IncludeStyle", IncludeStyle,
+ utils::IncludeSorter::getMapping());
}
void ReplaceAutoPtrCheck::registerMatchers(MatchFinder *Finder) {
diff --git a/clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.cpp b/clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.cpp
index 115615417335..e7c2bdb256e1 100644
--- a/clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.cpp
@@ -23,8 +23,9 @@ namespace modernize {
ReplaceRandomShuffleCheck::ReplaceRandomShuffleCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
- Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {}
+ IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::getMapping(),
+ utils::IncludeSorter::IS_LLVM)) {}
void ReplaceRandomShuffleCheck::registerMatchers(MatchFinder *Finder) {
const auto Begin = hasArgument(0, expr());
@@ -48,8 +49,8 @@ void ReplaceRandomShuffleCheck::registerPPCallbacks(
void ReplaceRandomShuffleCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle",
- utils::IncludeSorter::toString(IncludeStyle));
+ Options.store(Opts, "IncludeStyle", IncludeStyle,
+ utils::IncludeSorter::getMapping());
}
void ReplaceRandomShuffleCheck::check(const MatchFinder::MatchResult &Result) {
diff --git a/clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.cpp b/clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.cpp
index b852734b8caf..853e07f6001b 100644
--- a/clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.cpp
@@ -23,8 +23,9 @@ namespace performance {
MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
- Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {}
+ IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::getMapping(),
+ utils::IncludeSorter::IS_LLVM)) {}
void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
@@ -94,8 +95,8 @@ void MoveConstructorInitCheck::registerPPCallbacks(
}
void MoveConstructorInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle",
- utils::IncludeSorter::toString(IncludeStyle));
+ Options.store(Opts, "IncludeStyle", IncludeStyle,
+ utils::IncludeSorter::getMapping());
}
} // namespace performance
diff --git a/clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.cpp b/clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.cpp
index 01d0e03942bc..1d7ee4baae93 100644
--- a/clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.cpp
@@ -31,8 +31,9 @@ AST_MATCHER_P(Type, isBuiltinType, BuiltinType::Kind, Kind) {
TypePromotionInMathFnCheck::TypePromotionInMathFnCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
- Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {}
+ IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::getMapping(),
+ utils::IncludeSorter::IS_LLVM)) {}
void TypePromotionInMathFnCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
@@ -43,8 +44,8 @@ void TypePromotionInMathFnCheck::registerPPCallbacks(
void TypePromotionInMathFnCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle",
- utils::IncludeSorter::toString(IncludeStyle));
+ Options.store(Opts, "IncludeStyle", IncludeStyle,
+ utils::IncludeSorter::getMapping());
}
void TypePromotionInMathFnCheck::registerMatchers(MatchFinder *Finder) {
diff --git a/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp b/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
index 6182be47c783..5b71d8996dab 100644
--- a/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
@@ -67,8 +67,9 @@ bool isExplicitTemplateSpecialization(const FunctionDecl &Function) {
UnnecessaryValueParamCheck::UnnecessaryValueParamCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
- Options.getLocalOrGlobal("IncludeStyle", "llvm"))),
+ IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::getMapping(),
+ utils::IncludeSorter::IS_LLVM)),
AllowedTypes(
utils::options::parseStringList(Options.get("AllowedTypes", ""))) {}
@@ -175,8 +176,8 @@ void UnnecessaryValueParamCheck::registerPPCallbacks(
void UnnecessaryValueParamCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle",
- utils::IncludeSorter::toString(IncludeStyle));
+ Options.store(Opts, "IncludeStyle", IncludeStyle,
+ utils::IncludeSorter::getMapping());
Options.store(Opts, "AllowedTypes",
utils::options::serializeStringList(AllowedTypes));
}
diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
index 45cf87bccfc3..478122a42d21 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -11,8 +11,10 @@
#include "clang/AST/CXXInheritance.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/Support/Debug.h"
+#include "llvm/Support/Error.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/Regex.h"
@@ -97,6 +99,16 @@ static StringRef const StyleNames[] = {
#undef NAMING_KEYS
// clang-format on
+static constexpr std::pair<StringRef, IdentifierNamingCheck::CaseType>
+ Mapping[] = {
+ {"aNy_CasE", IdentifierNamingCheck::CT_AnyCase},
+ {"lower_case", IdentifierNamingCheck::CT_LowerCase},
+ {"UPPER_CASE", IdentifierNamingCheck::CT_UpperCase},
+ {"camelBack", IdentifierNamingCheck::CT_CamelBack},
+ {"CamelCase", IdentifierNamingCheck::CT_CamelCase},
+ {"Camel_Snake_Case", IdentifierNamingCheck::CT_CamelSnakeCase},
+ {"camel_Snake_Back", IdentifierNamingCheck::CT_CamelSnakeBack}};
+
namespace {
/// Callback supplies macros to IdentifierNamingCheck::checkMacro
class IdentifierNamingCheckPPCallbacks : public PPCallbacks {
@@ -129,26 +141,26 @@ IdentifierNamingCheck::IdentifierNamingCheck(StringRef Name,
: RenamerClangTidyCheck(Name, Context),
IgnoreFailedSplit(Options.get("IgnoreFailedSplit", 0)),
IgnoreMainLikeFunctions(Options.get("IgnoreMainLikeFunctions", 0)) {
- auto const fromString = [](StringRef Str) {
- return llvm::StringSwitch<llvm::Optional<CaseType>>(Str)
- .Case("aNy_CasE", CT_AnyCase)
- .Case("lower_case", CT_LowerCase)
- .Case("UPPER_CASE", CT_UpperCase)
- .Case("camelBack", CT_CamelBack)
- .Case("CamelCase", CT_CamelCase)
- .Case("Camel_Snake_Case", CT_CamelSnakeCase)
- .Case("camel_Snake_Back", CT_CamelSnakeBack)
- .Default(llvm::None);
- };
for (auto const &Name : StyleNames) {
- auto const caseOptional =
- fromString(Options.get((Name + "Case").str(), ""));
+ auto CaseOptional = [&]() -> llvm::Optional<CaseType> {
+ auto ValueOr = Options.get((Name + "Case").str(), makeArrayRef(Mapping));
+ if (ValueOr)
+ return *ValueOr;
+ llvm::logAllUnhandledErrors(
+ llvm::handleErrors(ValueOr.takeError(),
+ [](const MissingOptionError &) -> llvm::Error {
+ return llvm::Error::success();
+ }),
+ llvm::errs(), "warning: ");
+ return llvm::None;
+ }();
+
auto prefix = Options.get((Name + "Prefix").str(), "");
auto postfix = Options.get((Name + "Suffix").str(), "");
- if (caseOptional || !prefix.empty() || !postfix.empty()) {
- NamingStyles.push_back(NamingStyle(caseOptional, prefix, postfix));
+ if (CaseOptional || !prefix.empty() || !postfix.empty()) {
+ NamingStyles.push_back(NamingStyle(CaseOptional, prefix, postfix));
} else {
NamingStyles.push_back(llvm::None);
}
@@ -158,32 +170,11 @@ IdentifierNamingCheck::IdentifierNamingCheck(StringRef Name,
IdentifierNamingCheck::~IdentifierNamingCheck() = default;
void IdentifierNamingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
- auto const toString = [](CaseType Type) {
- switch (Type) {
- case CT_AnyCase:
- return "aNy_CasE";
- case CT_LowerCase:
- return "lower_case";
- case CT_CamelBack:
- return "camelBack";
- case CT_UpperCase:
- return "UPPER_CASE";
- case CT_CamelCase:
- return "CamelCase";
- case CT_CamelSnakeCase:
- return "Camel_Snake_Case";
- case CT_CamelSnakeBack:
- return "camel_Snake_Back";
- }
-
- llvm_unreachable("Unknown Case Type");
- };
-
for (size_t i = 0; i < SK_Count; ++i) {
if (NamingStyles[i]) {
if (NamingStyles[i]->Case) {
Options.store(Opts, (StyleNames[i] + "Case").str(),
- toString(*NamingStyles[i]->Case));
+ *NamingStyles[i]->Case, llvm::makeArrayRef(Mapping));
}
Options.store(Opts, (StyleNames[i] + "Prefix").str(),
NamingStyles[i]->Prefix);
diff --git a/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp b/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp
index 9d792272ab94..a344fefdee11 100644
--- a/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp
+++ b/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp
@@ -275,13 +275,11 @@ std::vector<FixItHint> IncludeSorter::GetEdits() {
return Fixes;
}
-IncludeSorter::IncludeStyle
-IncludeSorter::parseIncludeStyle(const std::string &Value) {
- return Value == "llvm" ? IS_LLVM : IS_Google;
-}
-
-StringRef IncludeSorter::toString(IncludeStyle Style) {
- return Style == IS_LLVM ? "llvm" : "google";
+llvm::ArrayRef<std::pair<StringRef, IncludeSorter::IncludeStyle>>
+IncludeSorter::getMapping() {
+ static constexpr std::pair<StringRef, IncludeSorter::IncludeStyle> Mapping[] =
+ {{"llvm", IS_LLVM}, {"google", IS_Google}};
+ return makeArrayRef(Mapping);
}
} // namespace utils
diff --git a/clang-tools-extra/clang-tidy/utils/IncludeSorter.h b/clang-tools-extra/clang-tidy/utils/IncludeSorter.h
index 9c07274a000a..db8eadcc5615 100644
--- a/clang-tools-extra/clang-tidy/utils/IncludeSorter.h
+++ b/clang-tools-extra/clang-tidy/utils/IncludeSorter.h
@@ -25,11 +25,7 @@ class IncludeSorter {
/// Supported include styles.
enum IncludeStyle { IS_LLVM = 0, IS_Google = 1 };
- /// Converts "llvm" to ``IS_LLVM``, otherwise returns ``IS_Google``.
- static IncludeStyle parseIncludeStyle(const std::string &Value);
-
- /// Converts ``IncludeStyle`` to string representation.
- static StringRef toString(IncludeStyle Style);
+ static ArrayRef<std::pair<StringRef, IncludeStyle>> getMapping();
/// The classifications of inclusions, in the order they should be sorted.
enum IncludeKinds {
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-case-violation.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-case-violation.cpp
new file mode 100644
index 000000000000..454999ed7c25
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-case-violation.cpp
@@ -0,0 +1,15 @@
+// RUN: clang-tidy %s -checks=readability-identifier-naming \
+// RUN: -config="{CheckOptions: [\
+// RUN: {key: readability-identifier-naming.FunctionCase, value: camelback}, \
+// RUN: {key: readability-identifier-naming.VariableCase, value: camelBack}, \
+// RUN: {key: readability-identifier-naming.ClassCase, value: UUPER_CASE}, \
+// RUN: {key: readability-identifier-naming.StructCase, value: CAMEL}, \
+// RUN: {key: readability-identifier-naming.EnumCase, value: AnY_cASe}, \
+// RUN: ]}" 2>&1 | FileCheck %s --implicit-check-not warning
+
+// CHECK-DAG: warning: invalid configuration value 'camelback' for option 'readability-identifier-naming.FunctionCase'; did you mean 'camelBack'?{{$}}
+// CHECK-DAG: warning: invalid configuration value 'UUPER_CASE' for option 'readability-identifier-naming.ClassCase'; did you mean 'UPPER_CASE'?{{$}}
+// Don't try to suggest an alternative for 'CAMEL'
+// CHECK-DAG: warning: invalid configuration value 'CAMEL' for option 'readability-identifier-naming.StructCase'{{$}}
+// This fails on the EditDistance, but as it matches ignoring case suggest the correct value
+// CHECK-DAG: warning: invalid configuration value 'AnY_cASe' for option 'readability-identifier-naming.EnumCase'; did you mean 'aNy_CasE'?{{$}}
More information about the cfe-commits
mailing list