[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 8 05:59:47 PST 2024


================
@@ -155,26 +156,23 @@ std::string AttributeCommonInfo::getNormalizedFullName() const {
       normalizeName(getAttrName(), getScopeName(), getSyntax()));
 }
 
-// Sorted list of attribute scope names
-static constexpr std::pair<StringRef, AttributeCommonInfo::Scope> ScopeList[] =
-    {{"", AttributeCommonInfo::Scope::NONE},
-     {"clang", AttributeCommonInfo::Scope::CLANG},
-     {"gnu", AttributeCommonInfo::Scope::GNU},
-     {"gsl", AttributeCommonInfo::Scope::GSL},
-     {"hlsl", AttributeCommonInfo::Scope::HLSL},
-     {"msvc", AttributeCommonInfo::Scope::MSVC},
-     {"omp", AttributeCommonInfo::Scope::OMP},
-     {"riscv", AttributeCommonInfo::Scope::RISCV}};
-
 AttributeCommonInfo::Scope
 getScopeFromNormalizedScopeName(StringRef ScopeName) {
-  auto It = std::lower_bound(
-      std::begin(ScopeList), std::end(ScopeList), ScopeName,
-      [](const std::pair<StringRef, AttributeCommonInfo::Scope> &Element,
-         StringRef Value) { return Element.first < Value; });
-  assert(It != std::end(ScopeList) && It->first == ScopeName);
-
-  return It->second;
+  AttributeCommonInfo::Scope ParsedScope =
+      llvm::StringSwitch<AttributeCommonInfo::Scope>(ScopeName)
+          .Case("", AttributeCommonInfo::Scope::NONE)
+          .Case("clang", AttributeCommonInfo::Scope::CLANG)
+          .Case("gnu", AttributeCommonInfo::Scope::GNU)
+          .Case("gsl", AttributeCommonInfo::Scope::GSL)
+          .Case("hlsl", AttributeCommonInfo::Scope::HLSL)
+          .Case("msvc", AttributeCommonInfo::Scope::MSVC)
+          .Case("omp", AttributeCommonInfo::Scope::OMP)
+          .Case("riscv", AttributeCommonInfo::Scope::RISCV)
+          .Default(AttributeCommonInfo::Scope::INVALID);
+
+  assert(ParsedScope != AttributeCommonInfo::Scope::INVALID);
----------------
erichkeane wrote:

Remove `.Default` and the `assert`.  The `operator R` has an assert in it to do this for you.  

[See This](https://llvm.org/doxygen/StringSwitch_8h_source.html#l00188)


https://github.com/llvm/llvm-project/pull/115414


More information about the cfe-commits mailing list