Skip to content

Commit ce67d44

Browse files
committed
[NFC][Clang] Use StringSwitch instead of array for parsing attribute scope
Change-Id: Iacfabcfd3a1cedeb7c864b3b59bad88b2177e7a8
1 parent 36d757f commit ce67d44

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

clang/include/clang/Basic/AttributeCommonInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class AttributeCommonInfo {
6767
IgnoredAttribute,
6868
UnknownAttribute,
6969
};
70-
enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV };
70+
enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV, INVALID };
7171

7272
private:
7373
const IdentifierInfo *AttrName = nullptr;

clang/lib/Basic/Attributes.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "clang/Basic/TargetInfo.h"
1919

2020
#include "llvm/ADT/StringMap.h"
21+
#include "llvm/ADT/StringSwitch.h"
2122

2223
using namespace clang;
2324

@@ -155,26 +156,23 @@ std::string AttributeCommonInfo::getNormalizedFullName() const {
155156
normalizeName(getAttrName(), getScopeName(), getSyntax()));
156157
}
157158

158-
// Sorted list of attribute scope names
159-
static constexpr std::pair<StringRef, AttributeCommonInfo::Scope> ScopeList[] =
160-
{{"", AttributeCommonInfo::Scope::NONE},
161-
{"clang", AttributeCommonInfo::Scope::CLANG},
162-
{"gnu", AttributeCommonInfo::Scope::GNU},
163-
{"gsl", AttributeCommonInfo::Scope::GSL},
164-
{"hlsl", AttributeCommonInfo::Scope::HLSL},
165-
{"msvc", AttributeCommonInfo::Scope::MSVC},
166-
{"omp", AttributeCommonInfo::Scope::OMP},
167-
{"riscv", AttributeCommonInfo::Scope::RISCV}};
168-
169159
AttributeCommonInfo::Scope
170160
getScopeFromNormalizedScopeName(StringRef ScopeName) {
171-
auto It = std::lower_bound(
172-
std::begin(ScopeList), std::end(ScopeList), ScopeName,
173-
[](const std::pair<StringRef, AttributeCommonInfo::Scope> &Element,
174-
StringRef Value) { return Element.first < Value; });
175-
assert(It != std::end(ScopeList) && It->first == ScopeName);
176-
177-
return It->second;
161+
AttributeCommonInfo::Scope ParsedScope =
162+
llvm::StringSwitch<AttributeCommonInfo::Scope>(ScopeName)
163+
.Case("", AttributeCommonInfo::Scope::NONE)
164+
.Case("clang", AttributeCommonInfo::Scope::CLANG)
165+
.Case("gnu", AttributeCommonInfo::Scope::GNU)
166+
.Case("gsl", AttributeCommonInfo::Scope::GSL)
167+
.Case("hlsl", AttributeCommonInfo::Scope::HLSL)
168+
.Case("msvc", AttributeCommonInfo::Scope::MSVC)
169+
.Case("omp", AttributeCommonInfo::Scope::OMP)
170+
.Case("riscv", AttributeCommonInfo::Scope::RISCV)
171+
.Default(AttributeCommonInfo::Scope::INVALID);
172+
173+
assert(ParsedScope != AttributeCommonInfo::Scope::INVALID);
174+
175+
return ParsedScope;
178176
}
179177

180178
unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {

0 commit comments

Comments
 (0)