[clang-tools-extra] c758181 - [clang-tidy] Use StringSwitch in a bunch of places. NFCI.
Benjamin Kramer via cfe-commits
cfe-commits at lists.llvm.org
Sun Apr 26 08:31:51 PDT 2020
Author: Benjamin Kramer
Date: 2020-04-26T17:24:47+02:00
New Revision: c758181525cbd4a603d6fd518ed33d9f324a7009
URL: https://github.com/llvm/llvm-project/commit/c758181525cbd4a603d6fd518ed33d9f324a7009
DIFF: https://github.com/llvm/llvm-project/commit/c758181525cbd4a603d6fd518ed33d9f324a7009.diff
LOG: [clang-tidy] Use StringSwitch in a bunch of places. NFCI.
Added:
Modified:
clang-tools-extra/clang-tidy/abseil/DurationFactoryScaleCheck.cpp
clang-tools-extra/clang-tidy/modernize/DeprecatedIosBaseAliasesCheck.cpp
clang-tools-extra/clang-tidy/portability/SIMDIntrinsicsCheck.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/abseil/DurationFactoryScaleCheck.cpp b/clang-tools-extra/clang-tidy/abseil/DurationFactoryScaleCheck.cpp
index b58288500dcf..0e537fc3cc4e 100644
--- a/clang-tools-extra/clang-tidy/abseil/DurationFactoryScaleCheck.cpp
+++ b/clang-tools-extra/clang-tidy/abseil/DurationFactoryScaleCheck.cpp
@@ -23,19 +23,14 @@ namespace abseil {
// `FactoryName`, return `None`.
static llvm::Optional<DurationScale>
getScaleForFactory(llvm::StringRef FactoryName) {
- static const std::unordered_map<std::string, DurationScale> ScaleMap(
- {{"Nanoseconds", DurationScale::Nanoseconds},
- {"Microseconds", DurationScale::Microseconds},
- {"Milliseconds", DurationScale::Milliseconds},
- {"Seconds", DurationScale::Seconds},
- {"Minutes", DurationScale::Minutes},
- {"Hours", DurationScale::Hours}});
-
- auto ScaleIter = ScaleMap.find(std::string(FactoryName));
- if (ScaleIter == ScaleMap.end())
- return llvm::None;
-
- return ScaleIter->second;
+ return llvm::StringSwitch<llvm::Optional<DurationScale>>(FactoryName)
+ .Case("Nanoseconds", DurationScale::Nanoseconds)
+ .Case("Microseconds", DurationScale::Microseconds)
+ .Case("Milliseconds", DurationScale::Milliseconds)
+ .Case("Seconds", DurationScale::Seconds)
+ .Case("Minutes", DurationScale::Minutes)
+ .Case("Hours", DurationScale::Hours)
+ .Default(llvm::None);
}
// Given either an integer or float literal, return its value.
diff --git a/clang-tools-extra/clang-tidy/modernize/DeprecatedIosBaseAliasesCheck.cpp b/clang-tools-extra/clang-tidy/modernize/DeprecatedIosBaseAliasesCheck.cpp
index 15eb2e88285c..2eef9837e11c 100644
--- a/clang-tools-extra/clang-tidy/modernize/DeprecatedIosBaseAliasesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/DeprecatedIosBaseAliasesCheck.cpp
@@ -21,10 +21,13 @@ static constexpr std::array<StringRef, 5> DeprecatedTypes = {
"::std::ios_base::seek_dir", "::std::ios_base::streamoff",
"::std::ios_base::streampos"};
-static const llvm::StringMap<StringRef> ReplacementTypes = {
- {"io_state", "iostate"},
- {"open_mode", "openmode"},
- {"seek_dir", "seekdir"}};
+static llvm::Optional<const char *> getReplacementType(StringRef Type) {
+ return llvm::StringSwitch<llvm::Optional<const char *>>(Type)
+ .Case("io_state", "iostate")
+ .Case("open_mode", "openmode")
+ .Case("seek_dir", "seekdir")
+ .Default(llvm::None);
+}
void DeprecatedIosBaseAliasesCheck::registerMatchers(MatchFinder *Finder) {
auto IoStateDecl = typedefDecl(hasAnyName(DeprecatedTypes)).bind("TypeDecl");
@@ -40,14 +43,14 @@ void DeprecatedIosBaseAliasesCheck::check(
const auto *Typedef = Result.Nodes.getNodeAs<TypedefDecl>("TypeDecl");
StringRef TypeName = Typedef->getName();
- bool HasReplacement = ReplacementTypes.count(TypeName);
+ auto Replacement = getReplacementType(TypeName);
const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("TypeLoc");
SourceLocation IoStateLoc = TL->getBeginLoc();
// Do not generate fixits for matches depending on template arguments and
// macro expansions.
- bool Fix = HasReplacement && !TL->getType()->isDependentType();
+ bool Fix = Replacement && !TL->getType()->isDependentType();
if (IoStateLoc.isMacroID()) {
IoStateLoc = SM.getSpellingLoc(IoStateLoc);
Fix = false;
@@ -55,8 +58,8 @@ void DeprecatedIosBaseAliasesCheck::check(
SourceLocation EndLoc = IoStateLoc.getLocWithOffset(TypeName.size() - 1);
- if (HasReplacement) {
- auto FixName = ReplacementTypes.lookup(TypeName);
+ if (Replacement) {
+ auto FixName = *Replacement;
auto Builder = diag(IoStateLoc, "'std::ios_base::%0' is deprecated; use "
"'std::ios_base::%1' instead")
<< TypeName << FixName;
diff --git a/clang-tools-extra/clang-tidy/portability/SIMDIntrinsicsCheck.cpp b/clang-tools-extra/clang-tidy/portability/SIMDIntrinsicsCheck.cpp
index d567f524a6d1..db9cc8c5a08c 100644
--- a/clang-tools-extra/clang-tidy/portability/SIMDIntrinsicsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/portability/SIMDIntrinsicsCheck.cpp
@@ -43,21 +43,15 @@ static StringRef TrySuggestPPC(StringRef Name) {
if (!Name.consume_front("vec_"))
return {};
- static const llvm::StringMap<StringRef> Mapping{
- // [simd.alg]
- {"max", "$std::max"},
- {"min", "$std::min"},
-
- // [simd.binary]
- {"add", "operator+ on $simd objects"},
- {"sub", "operator- on $simd objects"},
- {"mul", "operator* on $simd objects"},
- };
-
- auto It = Mapping.find(Name);
- if (It != Mapping.end())
- return It->second;
- return {};
+ return llvm::StringSwitch<StringRef>(Name)
+ // [simd.alg]
+ .Case("max", "$std::max")
+ .Case("min", "$std::min")
+ // [simd.binary]
+ .Case("add", "operator+ on $simd objects")
+ .Case("sub", "operator- on $simd objects")
+ .Case("mul", "operator* on $simd objects")
+ .Default({});
}
static StringRef TrySuggestX86(StringRef Name) {
More information about the cfe-commits
mailing list