Skip to content

Commit 8f73c44

Browse files
committed
Revert "[clang-tidy] Reworked enum options handling(again)"
This reverts commit b9306fd and follow-up 42a5158. It seems to build check-clang-tools on macOS, see comments on https://reviews.llvm.org/D82188
1 parent 80e15b4 commit 8f73c44

19 files changed

+139
-164
lines changed

clang-tools-extra/clang-tidy/ClangTidyCheck.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void ClangTidyCheck::OptionsView::store(ClangTidyOptions::OptionMap &Options,
158158
}
159159

160160
llvm::Expected<int64_t> ClangTidyCheck::OptionsView::getEnumInt(
161-
StringRef LocalName, ArrayRef<std::pair<int64_t, StringRef>> Mapping,
161+
StringRef LocalName, ArrayRef<std::pair<StringRef, int64_t>> Mapping,
162162
bool CheckGlobal, bool IgnoreCase) {
163163
auto Iter = CheckOptions.find((NamePrefix + LocalName).str());
164164
if (CheckGlobal && Iter == CheckOptions.end())
@@ -171,19 +171,19 @@ llvm::Expected<int64_t> ClangTidyCheck::OptionsView::getEnumInt(
171171
unsigned EditDistance = -1;
172172
for (const auto &NameAndEnum : Mapping) {
173173
if (IgnoreCase) {
174-
if (Value.equals_lower(NameAndEnum.second))
175-
return NameAndEnum.first;
176-
} else if (Value.equals(NameAndEnum.second)) {
177-
return NameAndEnum.first;
178-
} else if (Value.equals_lower(NameAndEnum.second)) {
179-
Closest = NameAndEnum.second;
174+
if (Value.equals_lower(NameAndEnum.first))
175+
return NameAndEnum.second;
176+
} else if (Value.equals(NameAndEnum.first)) {
177+
return NameAndEnum.second;
178+
} else if (Value.equals_lower(NameAndEnum.first)) {
179+
Closest = NameAndEnum.first;
180180
EditDistance = 0;
181181
continue;
182182
}
183-
unsigned Distance = Value.edit_distance(NameAndEnum.second);
183+
unsigned Distance = Value.edit_distance(NameAndEnum.first);
184184
if (Distance < EditDistance) {
185185
EditDistance = Distance;
186-
Closest = NameAndEnum.second;
186+
Closest = NameAndEnum.first;
187187
}
188188
}
189189
if (EditDistance < 3)

clang-tools-extra/clang-tidy/ClangTidyCheck.h

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ class CompilerInstance;
2727

2828
namespace tidy {
2929

30-
/// This class should be specialized by any enum type that needs to be converted
31-
/// to and from an \ref llvm::StringRef.
32-
template <class T> struct OptionEnumMapping {
33-
// Specializations of this struct must implement this function.
34-
static ArrayRef<std::pair<T, StringRef>> getEnumMapping() = delete;
35-
};
36-
3730
template <typename T> class OptionError : public llvm::ErrorInfo<T> {
3831
std::error_code convertToErrorCode() const override {
3932
return llvm::inconvertibleErrorCode();
@@ -320,80 +313,77 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
320313
}
321314

322315
/// Read a named option from the ``Context`` and parse it as an
323-
/// enum type ``T``.
316+
/// enum type ``T`` using the \p Mapping provided. If \p IgnoreCase is set,
317+
/// it will search the mapping ignoring the case.
324318
///
325319
/// Reads the option with the check-local name \p LocalName from the
326320
/// ``CheckOptions``. If the corresponding key is not present, returns a
327321
/// ``MissingOptionError``. If the key can't be parsed as a ``T`` returns a
328322
/// ``UnparseableEnumOptionError``.
329-
///
330-
/// \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
331-
/// supply the mapping required to convert between ``T`` and a string.
332323
template <typename T>
333324
std::enable_if_t<std::is_enum<T>::value, llvm::Expected<T>>
334-
get(StringRef LocalName, bool IgnoreCase = false) {
335-
if (llvm::Expected<int64_t> ValueOr =
336-
getEnumInt(LocalName, typeEraseMapping<T>(), false, IgnoreCase))
325+
get(StringRef LocalName, ArrayRef<std::pair<StringRef, T>> Mapping,
326+
bool IgnoreCase = false) {
327+
if (llvm::Expected<int64_t> ValueOr = getEnumInt(
328+
LocalName, typeEraseMapping(Mapping), false, IgnoreCase))
337329
return static_cast<T>(*ValueOr);
338330
else
339331
return std::move(ValueOr.takeError());
340332
}
341333

342334
/// Read a named option from the ``Context`` and parse it as an
343-
/// enum type ``T``.
335+
/// enum type ``T`` using the \p Mapping provided. If \p IgnoreCase is set,
336+
/// it will search the mapping ignoring the case.
344337
///
345338
/// Reads the option with the check-local name \p LocalName from the
346339
/// ``CheckOptions``. If the corresponding key is not present or it can't be
347340
/// parsed as a ``T``, returns \p Default.
348-
///
349-
/// \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
350-
/// supply the mapping required to convert between ``T`` and a string.
351341
template <typename T>
352342
std::enable_if_t<std::is_enum<T>::value, T>
353-
get(StringRef LocalName, T Default, bool IgnoreCase = false) {
354-
if (auto ValueOr = get<T>(LocalName, IgnoreCase))
343+
get(StringRef LocalName, ArrayRef<std::pair<StringRef, T>> Mapping,
344+
T Default, bool IgnoreCase = false) {
345+
if (auto ValueOr = get(LocalName, Mapping, IgnoreCase))
355346
return *ValueOr;
356347
else
357348
logErrToStdErr(ValueOr.takeError());
358349
return Default;
359350
}
360351

361352
/// Read a named option from the ``Context`` and parse it as an
362-
/// enum type ``T``.
353+
/// enum type ``T`` using the \p Mapping provided. If \p IgnoreCase is set,
354+
/// it will search the mapping ignoring the case.
363355
///
364356
/// Reads the option with the check-local name \p LocalName from local or
365357
/// global ``CheckOptions``. Gets local option first. If local is not
366358
/// present, falls back to get global option. If global option is not
367359
/// present either, returns a ``MissingOptionError``. If the key can't be
368360
/// parsed as a ``T`` returns a ``UnparseableEnumOptionError``.
369-
///
370-
/// \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
371-
/// supply the mapping required to convert between ``T`` and a string.
372361
template <typename T>
373362
std::enable_if_t<std::is_enum<T>::value, llvm::Expected<T>>
374363
getLocalOrGlobal(StringRef LocalName,
364+
ArrayRef<std::pair<StringRef, T>> Mapping,
375365
bool IgnoreCase = false) {
376-
if (llvm::Expected<int64_t> ValueOr =
377-
getEnumInt(LocalName, typeEraseMapping<T>(), true, IgnoreCase))
366+
if (llvm::Expected<int64_t> ValueOr = getEnumInt(
367+
LocalName, typeEraseMapping(Mapping), true, IgnoreCase))
378368
return static_cast<T>(*ValueOr);
379369
else
380370
return std::move(ValueOr.takeError());
381371
}
382372

383373
/// Read a named option from the ``Context`` and parse it as an
384-
/// enum type ``T``.
374+
/// enum type ``T`` using the \p Mapping provided. If \p IgnoreCase is set,
375+
/// it will search the mapping ignoring the case.
385376
///
386377
/// Reads the option with the check-local name \p LocalName from local or
387378
/// global ``CheckOptions``. Gets local option first. If local is not
388379
/// present, falls back to get global option. If global option is not
389380
/// present either or it can't be parsed as a ``T``, returns \p Default.
390-
///
391-
/// \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
392-
/// supply the mapping required to convert between ``T`` and a string.
393381
template <typename T>
394382
std::enable_if_t<std::is_enum<T>::value, T>
395-
getLocalOrGlobal(StringRef LocalName, T Default, bool IgnoreCase = false) {
396-
if (auto ValueOr = getLocalOrGlobal<T>(LocalName, IgnoreCase))
383+
getLocalOrGlobal(StringRef LocalName,
384+
ArrayRef<std::pair<StringRef, T>> Mapping, T Default,
385+
bool IgnoreCase = false) {
386+
if (auto ValueOr = getLocalOrGlobal(LocalName, Mapping, IgnoreCase))
397387
return *ValueOr;
398388
else
399389
logErrToStdErr(ValueOr.takeError());
@@ -411,40 +401,34 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
411401
int64_t Value) const;
412402

413403
/// Stores an option with the check-local name \p LocalName as the string
414-
/// representation of the Enum \p Value to \p Options.
415-
///
416-
/// \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
417-
/// supply the mapping required to convert between ``T`` and a string.
404+
/// representation of the Enum \p Value using the \p Mapping to \p Options.
418405
template <typename T>
419406
std::enable_if_t<std::is_enum<T>::value>
420-
store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, T Value) {
421-
ArrayRef<std::pair<T, StringRef>> Mapping =
422-
OptionEnumMapping<T>::getEnumMapping();
407+
store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, T Value,
408+
ArrayRef<std::pair<StringRef, T>> Mapping) {
423409
auto Iter = llvm::find_if(
424-
Mapping, [&](const std::pair<T, StringRef> &NameAndEnum) {
425-
return NameAndEnum.first == Value;
410+
Mapping, [&](const std::pair<StringRef, T> &NameAndEnum) {
411+
return NameAndEnum.second == Value;
426412
});
427413
assert(Iter != Mapping.end() && "Unknown Case Value");
428-
store(Options, LocalName, Iter->second);
414+
store(Options, LocalName, Iter->first);
429415
}
430416

431417
private:
432-
using NameAndValue = std::pair<int64_t, StringRef>;
418+
using NameAndValue = std::pair<StringRef, int64_t>;
433419

434420
llvm::Expected<int64_t> getEnumInt(StringRef LocalName,
435421
ArrayRef<NameAndValue> Mapping,
436422
bool CheckGlobal, bool IgnoreCase);
437423

438424
template <typename T>
439425
std::enable_if_t<std::is_enum<T>::value, std::vector<NameAndValue>>
440-
typeEraseMapping() {
441-
ArrayRef<std::pair<T, StringRef>> Mapping =
442-
OptionEnumMapping<T>::getEnumMapping();
426+
typeEraseMapping(ArrayRef<std::pair<StringRef, T>> Mapping) {
443427
std::vector<NameAndValue> Result;
444428
Result.reserve(Mapping.size());
445429
for (auto &MappedItem : Mapping) {
446-
Result.emplace_back(static_cast<int64_t>(MappedItem.first),
447-
MappedItem.second);
430+
Result.emplace_back(MappedItem.first,
431+
static_cast<int64_t>(MappedItem.second));
448432
}
449433
return Result;
450434
}

clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ StringFindStartswithCheck::StringFindStartswithCheck(StringRef Name,
2727
StringLikeClasses(utils::options::parseStringList(
2828
Options.get("StringLikeClasses", "::std::basic_string"))),
2929
IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
30+
utils::IncludeSorter::getMapping(),
3031
utils::IncludeSorter::IS_LLVM)),
3132
AbseilStringsMatchHeader(
3233
Options.get("AbseilStringsMatchHeader", "absl/strings/match.h")) {}
@@ -121,7 +122,8 @@ void StringFindStartswithCheck::storeOptions(
121122
ClangTidyOptions::OptionMap &Opts) {
122123
Options.store(Opts, "StringLikeClasses",
123124
utils::options::serializeStringList(StringLikeClasses));
124-
Options.store(Opts, "IncludeStyle", IncludeStyle);
125+
Options.store(Opts, "IncludeStyle", IncludeStyle,
126+
utils::IncludeSorter::getMapping());
125127
Options.store(Opts, "AbseilStringsMatchHeader", AbseilStringsMatchHeader);
126128
}
127129

clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ InitVariablesCheck::InitVariablesCheck(StringRef Name,
2525
ClangTidyContext *Context)
2626
: ClangTidyCheck(Name, Context),
2727
IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
28+
utils::IncludeSorter::getMapping(),
2829
utils::IncludeSorter::IS_LLVM)),
2930
MathHeader(Options.get("MathHeader", "math.h")) {}
3031

3132
void InitVariablesCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
32-
Options.store(Opts, "IncludeStyle", IncludeStyle);
33+
Options.store(Opts, "IncludeStyle", IncludeStyle,
34+
utils::IncludeSorter::getMapping());
3335
Options.store(Opts, "MathHeader", MathHeader);
3436
}
3537

clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ProBoundsConstantArrayIndexCheck::ProBoundsConstantArrayIndexCheck(
2222
StringRef Name, ClangTidyContext *Context)
2323
: ClangTidyCheck(Name, Context), GslHeader(Options.get("GslHeader", "")),
2424
IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
25+
utils::IncludeSorter::getMapping(),
2526
utils::IncludeSorter::IS_LLVM)) {}
2627

2728
void ProBoundsConstantArrayIndexCheck::storeOptions(

clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,6 @@ using namespace llvm;
2828

2929
namespace clang {
3030
namespace tidy {
31-
32-
template <> struct OptionEnumMapping<modernize::Confidence::Level> {
33-
static llvm::ArrayRef<std::pair<modernize::Confidence::Level, StringRef>>
34-
getEnumMapping() {
35-
static constexpr std::pair<modernize::Confidence::Level, StringRef>
36-
Mapping[] = {{modernize::Confidence::CL_Reasonable, "reasonable"},
37-
{modernize::Confidence::CL_Safe, "safe"},
38-
{modernize::Confidence::CL_Risky, "risky"}};
39-
return makeArrayRef(Mapping);
40-
}
41-
};
42-
43-
template <> struct OptionEnumMapping<modernize::VariableNamer::NamingStyle> {
44-
static llvm::ArrayRef<
45-
std::pair<modernize::VariableNamer::NamingStyle, StringRef>>
46-
getEnumMapping() {
47-
static constexpr std::pair<modernize::VariableNamer::NamingStyle, StringRef>
48-
Mapping[] = {{modernize::VariableNamer::NS_CamelCase, "CamelCase"},
49-
{modernize::VariableNamer::NS_CamelBack, "camelBack"},
50-
{modernize::VariableNamer::NS_LowerCase, "lower_case"},
51-
{modernize::VariableNamer::NS_UpperCase, "UPPER_CASE"}};
52-
return makeArrayRef(Mapping);
53-
}
54-
};
55-
5631
namespace modernize {
5732

5833
static const char LoopNameArray[] = "forLoopArray";
@@ -69,6 +44,25 @@ static const char EndVarName[] = "endVar";
6944
static const char DerefByValueResultName[] = "derefByValueResult";
7045
static const char DerefByRefResultName[] = "derefByRefResult";
7146

47+
static ArrayRef<std::pair<StringRef, Confidence::Level>>
48+
getConfidenceMapping() {
49+
static constexpr std::pair<StringRef, Confidence::Level> Mapping[] = {
50+
{"reasonable", Confidence::CL_Reasonable},
51+
{"safe", Confidence::CL_Safe},
52+
{"risky", Confidence::CL_Risky}};
53+
return makeArrayRef(Mapping);
54+
}
55+
56+
static ArrayRef<std::pair<StringRef, VariableNamer::NamingStyle>>
57+
getStyleMapping() {
58+
static constexpr std::pair<StringRef, VariableNamer::NamingStyle> Mapping[] =
59+
{{"CamelCase", VariableNamer::NS_CamelCase},
60+
{"camelBack", VariableNamer::NS_CamelBack},
61+
{"lower_case", VariableNamer::NS_LowerCase},
62+
{"UPPER_CASE", VariableNamer::NS_UpperCase}};
63+
return makeArrayRef(Mapping);
64+
}
65+
7266
// shared matchers
7367
static const TypeMatcher AnyType() { return anything(); }
7468

@@ -483,13 +477,15 @@ LoopConvertCheck::RangeDescriptor::RangeDescriptor()
483477
LoopConvertCheck::LoopConvertCheck(StringRef Name, ClangTidyContext *Context)
484478
: ClangTidyCheck(Name, Context), TUInfo(new TUTrackingInfo),
485479
MaxCopySize(Options.get("MaxCopySize", 16ULL)),
486-
MinConfidence(Options.get("MinConfidence", Confidence::CL_Reasonable)),
487-
NamingStyle(Options.get("NamingStyle", VariableNamer::NS_CamelCase)) {}
480+
MinConfidence(Options.get("MinConfidence", getConfidenceMapping(),
481+
Confidence::CL_Reasonable)),
482+
NamingStyle(Options.get("NamingStyle", getStyleMapping(),
483+
VariableNamer::NS_CamelCase)) {}
488484

489485
void LoopConvertCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
490486
Options.store(Opts, "MaxCopySize", std::to_string(MaxCopySize));
491-
Options.store(Opts, "MinConfidence", MinConfidence);
492-
Options.store(Opts, "NamingStyle", NamingStyle);
487+
Options.store(Opts, "MinConfidence", MinConfidence, getConfidenceMapping());
488+
Options.store(Opts, "NamingStyle", NamingStyle, getStyleMapping());
493489
}
494490

495491
void LoopConvertCheck::registerMatchers(MatchFinder *Finder) {

clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name, ClangTidyContext *Context,
4545
StringRef MakeSmartPtrFunctionName)
4646
: ClangTidyCheck(Name, Context),
4747
IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
48+
utils::IncludeSorter::getMapping(),
4849
utils::IncludeSorter::IS_LLVM)),
4950
MakeSmartPtrFunctionHeader(
5051
Options.get("MakeSmartPtrFunctionHeader", StdMemoryHeader)),
@@ -53,7 +54,8 @@ MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name, ClangTidyContext *Context,
5354
IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
5455

5556
void MakeSmartPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
56-
Options.store(Opts, "IncludeStyle", IncludeStyle);
57+
Options.store(Opts, "IncludeStyle", IncludeStyle,
58+
utils::IncludeSorter::getMapping());
5759
Options.store(Opts, "MakeSmartPtrFunctionHeader", MakeSmartPtrFunctionHeader);
5860
Options.store(Opts, "MakeSmartPtrFunction", MakeSmartPtrFunctionName);
5961
Options.store(Opts, "IgnoreMacros", IgnoreMacros);

clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,13 @@ collectParamDecls(const CXXConstructorDecl *Ctor,
121121
PassByValueCheck::PassByValueCheck(StringRef Name, ClangTidyContext *Context)
122122
: ClangTidyCheck(Name, Context),
123123
IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
124+
utils::IncludeSorter::getMapping(),
124125
utils::IncludeSorter::IS_LLVM)),
125126
ValuesOnly(Options.get("ValuesOnly", false)) {}
126127

127128
void PassByValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
128-
Options.store(Opts, "IncludeStyle", IncludeStyle);
129+
Options.store(Opts, "IncludeStyle", IncludeStyle,
130+
utils::IncludeSorter::getMapping());
129131
Options.store(Opts, "ValuesOnly", ValuesOnly);
130132
}
131133

clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ ReplaceAutoPtrCheck::ReplaceAutoPtrCheck(StringRef Name,
7575
ClangTidyContext *Context)
7676
: ClangTidyCheck(Name, Context),
7777
IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
78+
utils::IncludeSorter::getMapping(),
7879
utils::IncludeSorter::IS_LLVM)) {}
7980

8081
void ReplaceAutoPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
81-
Options.store(Opts, "IncludeStyle", IncludeStyle);
82+
Options.store(Opts, "IncludeStyle", IncludeStyle,
83+
utils::IncludeSorter::getMapping());
8284
}
8385

8486
void ReplaceAutoPtrCheck::registerMatchers(MatchFinder *Finder) {

clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ ReplaceRandomShuffleCheck::ReplaceRandomShuffleCheck(StringRef Name,
2424
ClangTidyContext *Context)
2525
: ClangTidyCheck(Name, Context),
2626
IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
27+
utils::IncludeSorter::getMapping(),
2728
utils::IncludeSorter::IS_LLVM)) {}
2829

2930
void ReplaceRandomShuffleCheck::registerMatchers(MatchFinder *Finder) {
@@ -51,7 +52,8 @@ void ReplaceRandomShuffleCheck::registerPPCallbacks(
5152

5253
void ReplaceRandomShuffleCheck::storeOptions(
5354
ClangTidyOptions::OptionMap &Opts) {
54-
Options.store(Opts, "IncludeStyle", IncludeStyle);
55+
Options.store(Opts, "IncludeStyle", IncludeStyle,
56+
utils::IncludeSorter::getMapping());
5557
}
5658

5759
void ReplaceRandomShuffleCheck::check(const MatchFinder::MatchResult &Result) {

0 commit comments

Comments
 (0)