[clang-tidy] Use StringSwitch in a bunch of places. NFCI.

This commit is contained in:
Benjamin Kramer 2020-04-26 17:23:30 +02:00
parent 3f10f1a5c7
commit c758181525
3 changed files with 28 additions and 36 deletions

View File

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

View File

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

View File

@ -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) {