Skip to content

Commit e8743c0

Browse files
committed
Const-initialize ParsedAttrInfos
Gets rid of a 150k static initializer (Release clang)
1 parent 3ab3f3c commit e8743c0

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

clang/examples/Attribute/Attribute.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ struct ExampleAttrInfo : public ParsedAttrInfo {
2828
OptArgs = 1;
2929
// GNU-style __attribute__(("example")) and C++-style [[example]] and
3030
// [[plugin::example]] supported.
31-
Spellings.push_back({ParsedAttr::AS_GNU, "example"});
32-
Spellings.push_back({ParsedAttr::AS_CXX11, "example"});
33-
Spellings.push_back({ParsedAttr::AS_CXX11, "plugin::example"});
31+
static constexpr Spelling S[] = {{ParsedAttr::AS_GNU, "example"},
32+
{ParsedAttr::AS_CXX11, "example"},
33+
{ParsedAttr::AS_CXX11, "plugin::example"}};
34+
Spellings = S;
3435
}
3536

3637
bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr,

clang/include/clang/Sema/ParsedAttr.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ struct ParsedAttrInfo {
6363
/// The syntaxes supported by this attribute and how they're spelled.
6464
struct Spelling {
6565
AttributeCommonInfo::Syntax Syntax;
66-
std::string NormalizedFullName;
66+
const char *NormalizedFullName;
6767
};
68-
std::vector<Spelling> Spellings;
68+
ArrayRef<Spelling> Spellings;
6969

70-
ParsedAttrInfo(AttributeCommonInfo::Kind AttrKind =
71-
AttributeCommonInfo::NoSemaHandlerAttribute)
70+
constexpr ParsedAttrInfo(AttributeCommonInfo::Kind AttrKind =
71+
AttributeCommonInfo::NoSemaHandlerAttribute)
7272
: AttrKind(AttrKind), NumArgs(0), OptArgs(0), HasCustomParsing(0),
7373
IsTargetSpecific(0), IsType(0), IsStmt(0), IsKnownToGCC(0),
7474
IsSupportedByPragmaAttribute(0) {}

clang/lib/Sema/ParsedAttr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const ParsedAttrInfo &ParsedAttrInfo::get(const AttributeCommonInfo &A) {
115115
return *AttrInfoMap[A.getParsedKind()];
116116

117117
// If this is an ignored attribute then return an appropriate ParsedAttrInfo.
118-
static ParsedAttrInfo IgnoredParsedAttrInfo(
118+
static const ParsedAttrInfo IgnoredParsedAttrInfo(
119119
AttributeCommonInfo::IgnoredAttribute);
120120
if (A.getParsedKind() == AttributeCommonInfo::IgnoredAttribute)
121121
return IgnoredParsedAttrInfo;
@@ -140,7 +140,7 @@ const ParsedAttrInfo &ParsedAttrInfo::get(const AttributeCommonInfo &A) {
140140
return *Ptr;
141141

142142
// If we failed to find a match then return a default ParsedAttrInfo.
143-
static ParsedAttrInfo DefaultParsedAttrInfo(
143+
static const ParsedAttrInfo DefaultParsedAttrInfo(
144144
AttributeCommonInfo::UnknownAttribute);
145145
return DefaultParsedAttrInfo;
146146
}

clang/utils/TableGen/ClangAttrEmitter.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3719,8 +3719,27 @@ void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
37193719
// ParsedAttr.cpp.
37203720
const std::string &AttrName = I->first;
37213721
const Record &Attr = *I->second;
3722-
OS << "struct ParsedAttrInfo" << I->first << " : public ParsedAttrInfo {\n";
3723-
OS << " ParsedAttrInfo" << I->first << "() {\n";
3722+
auto Spellings = GetFlattenedSpellings(Attr);
3723+
if (!Spellings.empty()) {
3724+
OS << "static constexpr ParsedAttrInfo::Spelling " << I->first
3725+
<< "Spellings[] = {\n";
3726+
for (const auto &S : Spellings) {
3727+
const std::string &RawSpelling = S.name();
3728+
std::string Spelling;
3729+
if (!S.nameSpace().empty())
3730+
Spelling += S.nameSpace() + "::";
3731+
if (S.variety() == "GNU")
3732+
Spelling += NormalizeGNUAttrSpelling(RawSpelling);
3733+
else
3734+
Spelling += RawSpelling;
3735+
OS << " {AttributeCommonInfo::AS_" << S.variety();
3736+
OS << ", \"" << Spelling << "\"},\n";
3737+
}
3738+
OS << "};\n";
3739+
}
3740+
OS << "struct ParsedAttrInfo" << I->first
3741+
<< " final : public ParsedAttrInfo {\n";
3742+
OS << " constexpr ParsedAttrInfo" << I->first << "() {\n";
37243743
OS << " AttrKind = ParsedAttr::AT_" << AttrName << ";\n";
37253744
emitArgInfo(Attr, OS);
37263745
OS << " HasCustomParsing = ";
@@ -3736,18 +3755,8 @@ void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
37363755
OS << IsKnownToGCC(Attr) << ";\n";
37373756
OS << " IsSupportedByPragmaAttribute = ";
37383757
OS << PragmaAttributeSupport.isAttributedSupported(*I->second) << ";\n";
3739-
for (const auto &S : GetFlattenedSpellings(Attr)) {
3740-
const std::string &RawSpelling = S.name();
3741-
std::string Spelling;
3742-
if (!S.nameSpace().empty())
3743-
Spelling += S.nameSpace() + "::";
3744-
if (S.variety() == "GNU")
3745-
Spelling += NormalizeGNUAttrSpelling(RawSpelling);
3746-
else
3747-
Spelling += RawSpelling;
3748-
OS << " Spellings.push_back({AttributeCommonInfo::AS_" << S.variety();
3749-
OS << ",\"" << Spelling << "\"});\n";
3750-
}
3758+
if (!Spellings.empty())
3759+
OS << " Spellings = " << I->first << "Spellings;\n";
37513760
OS << " }\n";
37523761
GenerateAppertainsTo(Attr, OS);
37533762
GenerateLangOptRequirements(Attr, OS);

0 commit comments

Comments
 (0)