Skip to content

Commit 95b7569

Browse files
committed
[Clang] Change up algorithm
1 parent 2970041 commit 95b7569

File tree

3 files changed

+48
-91
lines changed

3 files changed

+48
-91
lines changed

clang/include/clang/Basic/AttributeCommonInfo.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,7 @@ class AttributeCommonInfo {
6767
IgnoredAttribute,
6868
UnknownAttribute,
6969
};
70-
enum Scope {
71-
SC_NONE,
72-
SC_CLANG,
73-
SC_GNU,
74-
SC_MSVC,
75-
SC_OMP,
76-
SC_HLSL,
77-
SC_GSL,
78-
SC_RISCV
79-
};
70+
enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV };
8071

8172
private:
8273
const IdentifierInfo *AttrName = nullptr;

clang/lib/Basic/Attributes.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "clang/Basic/ParsedAttrInfo.h"
1818
#include "clang/Basic/TargetInfo.h"
1919

20+
#include "llvm/ADT/StringMap.h"
21+
2022
using namespace clang;
2123

2224
static int hasAttributeImpl(AttributeCommonInfo::Syntax Syntax, StringRef Name,
@@ -153,18 +155,18 @@ std::string AttributeCommonInfo::getNormalizedFullName() const {
153155
normalizeName(getAttrName(), getScopeName(), getSyntax()));
154156
}
155157

156-
const std::map<StringRef, AttributeCommonInfo::Scope> ScopeMap = {
157-
{"", AttributeCommonInfo::SC_NONE},
158-
{"clang", AttributeCommonInfo::SC_CLANG},
159-
{"gnu", AttributeCommonInfo::SC_GNU},
160-
{"msvc", AttributeCommonInfo::SC_MSVC},
161-
{"omp", AttributeCommonInfo::SC_OMP},
162-
{"hlsl", AttributeCommonInfo::SC_HLSL},
163-
{"gsl", AttributeCommonInfo::SC_GSL},
164-
{"riscv", AttributeCommonInfo::SC_RISCV}};
158+
const llvm::StringMap<AttributeCommonInfo::Scope> ScopeMap = {
159+
{"", AttributeCommonInfo::Scope::NONE},
160+
{"clang", AttributeCommonInfo::Scope::CLANG},
161+
{"gnu", AttributeCommonInfo::Scope::GNU},
162+
{"msvc", AttributeCommonInfo::Scope::MSVC},
163+
{"omp", AttributeCommonInfo::Scope::OMP},
164+
{"hlsl", AttributeCommonInfo::Scope::HLSL},
165+
{"gsl", AttributeCommonInfo::Scope::GSL},
166+
{"riscv", AttributeCommonInfo::Scope::RISCV}};
165167

166168
AttributeCommonInfo::Scope
167-
getScopeFromNormalizedScopeName(const StringRef ScopeName) {
169+
getScopeFromNormalizedScopeName(StringRef ScopeName) {
168170
auto It = ScopeMap.find(ScopeName);
169171
if (It == ScopeMap.end()) {
170172
llvm_unreachable("Unknown normalized scope name. Shouldn't get here");

clang/utils/TableGen/ClangAttrEmitter.cpp

Lines changed: 35 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3854,91 +3854,55 @@ void EmitClangAttrSpellingListIndex(const RecordKeeper &Records,
38543854
continue;
38553855
}
38563856

3857-
bool HasSingleUniqueSpellingName = true;
3858-
StringMap<std::vector<const FlattenedSpelling *>> SpellingMap;
3859-
3860-
StringRef FirstName = Spellings.front().name();
3861-
for (const auto &S : Spellings) {
3862-
StringRef Name = S.name();
3863-
if (Name != FirstName)
3864-
HasSingleUniqueSpellingName = false;
3865-
SpellingMap[Name].push_back(&S);
3866-
}
3867-
3868-
// If parsed attribute has only one possible spelling name, only compare
3869-
// syntax and scope.
3870-
if (HasSingleUniqueSpellingName) {
3871-
for (const auto &[Idx, S] : enumerate(SpellingMap[FirstName])) {
3872-
OS << " if (getSyntax() == AttributeCommonInfo::AS_" << S->variety()
3857+
std::vector<StringRef> Names;
3858+
llvm::transform(Spellings, std::back_inserter(Names),
3859+
[](const FlattenedSpelling &FS) { return FS.name(); });
3860+
llvm::sort(Names);
3861+
Names.erase(llvm::unique(Names), Names.end());
3862+
3863+
for (const auto &[Idx, FS] : enumerate(Spellings)) {
3864+
if (Names.size() == 1) {
3865+
OS << " if (getSyntax() == AttributeCommonInfo::AS_" << FS.variety()
38733866
<< " && ComputedScope == ";
38743867

3875-
if (S->nameSpace() == "")
3876-
OS << "AttributeCommonInfo::SC_NONE";
3868+
if (FS.nameSpace() == "")
3869+
OS << "AttributeCommonInfo::Scope::NONE";
38773870
else
3878-
OS << "AttributeCommonInfo::SC_" + S->nameSpace().upper();
3871+
OS << "AttributeCommonInfo::Scope::" + FS.nameSpace().upper();
38793872

38803873
OS << ")\n"
38813874
<< " return " << Idx << ";\n";
3882-
}
3883-
} else {
3884-
size_t Idx = 0;
3885-
StringMap<bool> Completed;
3886-
for (const auto &Spell : Spellings) {
3887-
if (Completed.contains(Spell.name()))
3888-
continue;
3875+
} else {
3876+
SmallVector<StringRef, 6> SameLenNames;
3877+
llvm::copy_if(
3878+
Names, std::back_inserter(SameLenNames),
3879+
[&](StringRef N) { return N.size() == FS.name().size(); });
3880+
3881+
if (SameLenNames.size() == 1) {
3882+
OS << " if (Name.size() == " << FS.name().size()
3883+
<< " && getSyntax() == AttributeCommonInfo::AS_" << FS.variety()
3884+
<< " && ComputedScope == ";
38893885

3890-
const std::vector<const FlattenedSpelling *> &Cases =
3891-
SpellingMap[Spell.name()];
3892-
3893-
if (Cases.size() > 1) {
3894-
// For names with multiple possible cases, emit an enclosing if such
3895-
// that the name is compared against only once. Eg:
3896-
//
3897-
// if (Name == "always_inline") {
3898-
// if (getSyntax() == AttributeCommonInfo::AS_GNU &&
3899-
// ComputedScope == AttributeCommonInfo::SC_None)
3900-
// return 0;
3901-
// ...
3902-
// }
3903-
OS << " if (Name == \"" << Spell.name() << "\") {\n";
3904-
for (const auto &S : SpellingMap[Spell.name()]) {
3905-
OS << " if (getSyntax() == AttributeCommonInfo::AS_"
3906-
<< S->variety() << " && ComputedScope == ";
3907-
3908-
if (S->nameSpace() == "")
3909-
OS << "AttributeCommonInfo::SC_NONE";
3910-
else
3911-
OS << "AttributeCommonInfo::SC_" + S->nameSpace().upper();
3912-
3913-
OS << ")\n"
3914-
<< " return " << Idx << ";\n";
3915-
Idx++;
3916-
}
3917-
OS << " }\n";
3886+
if (FS.nameSpace() == "")
3887+
OS << "AttributeCommonInfo::Scope::NONE";
3888+
else
3889+
OS << "AttributeCommonInfo::Scope::" + FS.nameSpace().upper();
3890+
3891+
OS << ")\n"
3892+
<< " return " << Idx << ";\n";
39183893
} else {
3919-
// If there is only possible case for the spelling name, no need of
3920-
// enclosing if. Eg.
3921-
//
3922-
// if (Name == "__forceinline" &&
3923-
// getSyntax() == AttributeCommonInfo::AS_Keyword
3924-
// && ComputedScope == AttributeCommonInfo::SC_NONE)
3925-
// return 5;
3926-
const FlattenedSpelling *S = Cases.front();
3927-
OS << " if (Name == \"" << Spell.name() << "\""
3928-
<< " && getSyntax() == AttributeCommonInfo::AS_" << S->variety()
3894+
OS << " if (Name == \"" << FS.name() << "\""
3895+
<< " && getSyntax() == AttributeCommonInfo::AS_" << FS.variety()
39293896
<< " && ComputedScope == ";
39303897

3931-
std::string ScopeStr = "AttributeCommonInfo::SC_";
3932-
if (S->nameSpace() == "")
3933-
OS << "AttributeCommonInfo::SC_NONE";
3898+
if (FS.nameSpace() == "")
3899+
OS << "AttributeCommonInfo::Scope::NONE";
39343900
else
3935-
OS << "AttributeCommonInfo::SC_" + S->nameSpace().upper();
3901+
OS << "AttributeCommonInfo::Scope::" + FS.nameSpace().upper();
39363902

39373903
OS << ")\n"
3938-
<< " return " << Idx << ";\n";
3939-
Idx++;
3904+
<< " return " << Idx << ";\n";
39403905
}
3941-
Completed[Spell.name()] = true;
39423906
}
39433907
}
39443908

0 commit comments

Comments
 (0)