[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)
https://github.com/chinmaydd closed https://github.com/llvm/llvm-project/pull/115414 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)
https://github.com/chinmaydd updated https://github.com/llvm/llvm-project/pull/115414 >From ce67d44d55ef488bac9428b4c7b78e37ea8471f6 Mon Sep 17 00:00:00 2001 From: Chinmay Deshpande Date: Thu, 7 Nov 2024 22:05:03 -0500 Subject: [PATCH 1/2] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope Change-Id: Iacfabcfd3a1cedeb7c864b3b59bad88b2177e7a8 --- .../include/clang/Basic/AttributeCommonInfo.h | 2 +- clang/lib/Basic/Attributes.cpp| 34 +-- 2 files changed, 17 insertions(+), 19 deletions(-) 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 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 &Element, - StringRef Value) { return Element.first < Value; }); - assert(It != std::end(ScopeList) && It->first == ScopeName); - - return It->second; + AttributeCommonInfo::Scope ParsedScope = + llvm::StringSwitch(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 { >From f90efc1a9bbdbaf351e594e07d7c74cd7fa8662c Mon Sep 17 00:00:00 2001 From: Chinmay Deshpande Date: Fri, 8 Nov 2024 13:24:00 -0500 Subject: [PATCH 2/2] [NFC][Clang] Address comments Change-Id: I99a618d8127680cb91146e1a82e0e0b6c0717888 --- .../include/clang/Basic/AttributeCommonInfo.h | 2 +- clang/lib/Basic/Attributes.cpp| 24 +++ clang/test/CodeGenCXX/split-stacks.cpp| 2 +- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h b/clang/include/clang/Basic/AttributeCommonInfo.h index 350154859ccdec..11c64547721739 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, INVALID }; + enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV }; private: const IdentifierInfo *AttrName = nullptr; diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp index a2ffaa147f3ef7..6904bce3ac51ec 100644 --- a/clang/lib/Basic/Attributes.cpp +++ b/clang/lib/Basic/Attributes.cpp @@ -158,21 +158,15 @@ std::string AttributeCommonInfo::getNormalizedFullName() const { AttributeCommonInfo::Scope getScopeFromNormalizedScopeName(StringRef ScopeName) { - AttributeCommonInfo::Scope ParsedScope = - llvm::StringSwitch(ScopeName) - .Case("", AttributeCommonInfo::Scope::NONE) - .Case("clang", AttributeCommonInfo::Scope::CLANG) - .Case("gnu", AttributeCommonInfo::Scope::GNU) - .Case("gsl", AttributeCommonInfo::Scope::GSL) - .
[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)
@@ -155,26 +156,23 @@ std::string AttributeCommonInfo::getNormalizedFullName() const { normalizeName(getAttrName(), getScopeName(), getSyntax())); } -// Sorted list of attribute scope names -static constexpr std::pair 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 &Element, - StringRef Value) { return Element.first < Value; }); - assert(It != std::end(ScopeList) && It->first == ScopeName); - - return It->second; + AttributeCommonInfo::Scope ParsedScope = + llvm::StringSwitch(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); chinmaydd wrote: Thanks @erichkeane and @nikic. This looks quite clean now :) https://github.com/llvm/llvm-project/pull/115414 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)
https://github.com/erichkeane approved this pull request. https://github.com/llvm/llvm-project/pull/115414 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)
https://github.com/chinmaydd updated https://github.com/llvm/llvm-project/pull/115414 >From ce67d44d55ef488bac9428b4c7b78e37ea8471f6 Mon Sep 17 00:00:00 2001 From: Chinmay Deshpande Date: Thu, 7 Nov 2024 22:05:03 -0500 Subject: [PATCH 1/2] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope Change-Id: Iacfabcfd3a1cedeb7c864b3b59bad88b2177e7a8 --- .../include/clang/Basic/AttributeCommonInfo.h | 2 +- clang/lib/Basic/Attributes.cpp| 34 +-- 2 files changed, 17 insertions(+), 19 deletions(-) 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 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 &Element, - StringRef Value) { return Element.first < Value; }); - assert(It != std::end(ScopeList) && It->first == ScopeName); - - return It->second; + AttributeCommonInfo::Scope ParsedScope = + llvm::StringSwitch(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 { >From a64632f9b3253a181c2a00ca44c4009736617191 Mon Sep 17 00:00:00 2001 From: Chinmay Deshpande Date: Fri, 8 Nov 2024 13:24:00 -0500 Subject: [PATCH 2/2] [NFC][Clang] Address comments Change-Id: I99a618d8127680cb91146e1a82e0e0b6c0717888 --- .../include/clang/Basic/AttributeCommonInfo.h | 2 +- clang/lib/Basic/Attributes.cpp| 24 +++ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h b/clang/include/clang/Basic/AttributeCommonInfo.h index 350154859ccdec..11c64547721739 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, INVALID }; + enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV }; private: const IdentifierInfo *AttrName = nullptr; diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp index a2ffaa147f3ef7..6904bce3ac51ec 100644 --- a/clang/lib/Basic/Attributes.cpp +++ b/clang/lib/Basic/Attributes.cpp @@ -158,21 +158,15 @@ std::string AttributeCommonInfo::getNormalizedFullName() const { AttributeCommonInfo::Scope getScopeFromNormalizedScopeName(StringRef ScopeName) { - AttributeCommonInfo::Scope ParsedScope = - llvm::StringSwitch(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) -
[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)
https://github.com/chinmaydd updated https://github.com/llvm/llvm-project/pull/115414 >From ce67d44d55ef488bac9428b4c7b78e37ea8471f6 Mon Sep 17 00:00:00 2001 From: Chinmay Deshpande Date: Thu, 7 Nov 2024 22:05:03 -0500 Subject: [PATCH 1/2] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope Change-Id: Iacfabcfd3a1cedeb7c864b3b59bad88b2177e7a8 --- .../include/clang/Basic/AttributeCommonInfo.h | 2 +- clang/lib/Basic/Attributes.cpp| 34 +-- 2 files changed, 17 insertions(+), 19 deletions(-) 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 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 &Element, - StringRef Value) { return Element.first < Value; }); - assert(It != std::end(ScopeList) && It->first == ScopeName); - - return It->second; + AttributeCommonInfo::Scope ParsedScope = + llvm::StringSwitch(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 { >From c70b5653d0903f0a8d3e27f0dff3e1c95298b207 Mon Sep 17 00:00:00 2001 From: Chinmay Deshpande Date: Fri, 8 Nov 2024 13:24:00 -0500 Subject: [PATCH 2/2] [NFC][Clang] Address comments Change-Id: I99a618d8127680cb91146e1a82e0e0b6c0717888 --- .../include/clang/Basic/AttributeCommonInfo.h | 2 +- clang/lib/Basic/Attributes.cpp| 24 +++ clang/test/CodeGenCXX/split-stacks.cpp| 2 +- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h b/clang/include/clang/Basic/AttributeCommonInfo.h index 350154859ccdec..11c64547721739 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, INVALID }; + enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV }; private: const IdentifierInfo *AttrName = nullptr; diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp index a2ffaa147f3ef7..6904bce3ac51ec 100644 --- a/clang/lib/Basic/Attributes.cpp +++ b/clang/lib/Basic/Attributes.cpp @@ -158,21 +158,15 @@ std::string AttributeCommonInfo::getNormalizedFullName() const { AttributeCommonInfo::Scope getScopeFromNormalizedScopeName(StringRef ScopeName) { - AttributeCommonInfo::Scope ParsedScope = - llvm::StringSwitch(ScopeName) - .Case("", AttributeCommonInfo::Scope::NONE) - .Case("clang", AttributeCommonInfo::Scope::CLANG) - .Case("gnu", AttributeCommonInfo::Scope::GNU) - .Case("gsl", AttributeCommonInfo::Scope::GSL) - .
[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)
https://github.com/erichkeane edited https://github.com/llvm/llvm-project/pull/115414 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)
@@ -155,26 +156,23 @@ std::string AttributeCommonInfo::getNormalizedFullName() const { normalizeName(getAttrName(), getScopeName(), getSyntax())); } -// Sorted list of attribute scope names -static constexpr std::pair 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 &Element, - StringRef Value) { return Element.first < Value; }); - assert(It != std::end(ScopeList) && It->first == ScopeName); - - return It->second; + AttributeCommonInfo::Scope ParsedScope = + llvm::StringSwitch(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 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)
https://github.com/erichkeane commented: 1 nit, else LGTM. https://github.com/llvm/llvm-project/pull/115414 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)
https://github.com/nikic approved this pull request. LGTM https://github.com/llvm/llvm-project/pull/115414 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)
llvmbot wrote: @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 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 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 &Element, - StringRef Value) { return Element.first < Value; }); - assert(It != std::end(ScopeList) && It->first == ScopeName); - - return It->second; + AttributeCommonInfo::Scope ParsedScope = + llvm::StringSwitch(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 { `` https://github.com/llvm/llvm-project/pull/115414 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)
https://github.com/chinmaydd created https://github.com/llvm/llvm-project/pull/115414 None >From ce67d44d55ef488bac9428b4c7b78e37ea8471f6 Mon Sep 17 00:00:00 2001 From: Chinmay Deshpande Date: Thu, 7 Nov 2024 22:05:03 -0500 Subject: [PATCH] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope Change-Id: Iacfabcfd3a1cedeb7c864b3b59bad88b2177e7a8 --- .../include/clang/Basic/AttributeCommonInfo.h | 2 +- clang/lib/Basic/Attributes.cpp| 34 +-- 2 files changed, 17 insertions(+), 19 deletions(-) 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 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 &Element, - StringRef Value) { return Element.first < Value; }); - assert(It != std::end(ScopeList) && It->first == ScopeName); - - return It->second; + AttributeCommonInfo::Scope ParsedScope = + llvm::StringSwitch(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 { ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits