Skip to content

Conversation

@chinmaydd
Copy link
Contributor

No description provided.

…scope

Change-Id: Iacfabcfd3a1cedeb7c864b3b59bad88b2177e7a8
@chinmaydd chinmaydd requested review from erichkeane and nikic November 8, 2024 03:06
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 8, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 8, 2024

@llvm/pr-subscribers-clang

Author: Chinmay Deshpande (chinmaydd)

Changes

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

2 Files Affected:

  • (modified) clang/include/clang/Basic/AttributeCommonInfo.h (+1-1)
  • (modified) clang/lib/Basic/Attributes.cpp (+16-18)
diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h b/clang/include/clang/Basic/AttributeCommonInfo.h
index 11c64547721739..350154859ccdec 100644
--- a/clang/include/clang/Basic/AttributeCommonInfo.h
+++ b/clang/include/clang/Basic/AttributeCommonInfo.h
@@ -67,7 +67,7 @@ class AttributeCommonInfo {
     IgnoredAttribute,
     UnknownAttribute,
   };
-  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV };
+  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV, INVALID };
 
 private:
   const IdentifierInfo *AttrName = nullptr;
diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp
index 2d18fb3f9d5bb2..a2ffaa147f3ef7 100644
--- a/clang/lib/Basic/Attributes.cpp
+++ b/clang/lib/Basic/Attributes.cpp
@@ -18,6 +18,7 @@
 #include "clang/Basic/TargetInfo.h"
 
 #include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringSwitch.h"
 
 using namespace clang;
 
@@ -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);
+
+  return ParsedScope;
 }
 
 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 nit, else LGTM.

.Case("riscv", AttributeCommonInfo::Scope::RISCV)
.Default(AttributeCommonInfo::Scope::INVALID);

assert(ParsedScope != AttributeCommonInfo::Scope::INVALID);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

See This

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @erichkeane and @nikic. This looks quite clean now :)

Change-Id: I99a618d8127680cb91146e1a82e0e0b6c0717888
@chinmaydd chinmaydd merged commit dbad941 into llvm:main Nov 8, 2024
8 checks passed
@chinmaydd chinmaydd deleted the chinmaydd/sswitch branch November 8, 2024 21:27
Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants