Skip to content

Commit b6d4d51

Browse files
committed
[clang] Specify attribute syntax & spelling with a single argument
When constructing an attribute, the syntactic form was specified using two arguments: an attribute-independent syntax type and an attribute-specific spelling index. This patch replaces them with a single argument. In most cases, that's done using a new Form class that combines the syntax and spelling into a single object. This has the minor benefit of removing a couple of constructors. But the main purpose is to allow additional information to be stored as well, beyond just the syntax and spelling enums. In the case of the attribute-specific Create and CreateImplicit functions, the patch instead uses the attribute-specific spelling enum. This helps to ensure that the syntax and spelling are consistent with each other and with the Attr.td definition. If a Create or CreateImplicit caller specified a syntax and a spelling, the patch drops the syntax argument and keeps the spelling. If the caller instead specified only a syntax (so that the spelling was SpellingNotCalculated), the patch simply drops the syntax argument. There were two cases of the latter: TargetVersion and Weak. TargetVersionAttrs were created with GNU syntax, which matches their definition in Attr.td, but which is also the default. WeakAttrs were created with Pragma syntax, which does not match their definition in Attr.td. Dropping the argument switches them to AS_GNU too (to match [GCC<"weak">]). Differential Revision: https://reviews.llvm.org/D148102
1 parent e841d50 commit b6d4d51

16 files changed

+192
-204
lines changed

clang/include/clang/Basic/AttributeCommonInfo.h

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -80,50 +80,58 @@ class AttributeCommonInfo {
8080
static constexpr unsigned SpellingNotCalculated = 0xf;
8181

8282
public:
83-
AttributeCommonInfo(const IdentifierInfo *AttrName,
84-
const IdentifierInfo *ScopeName, SourceRange AttrRange,
85-
SourceLocation ScopeLoc, Syntax SyntaxUsed)
86-
: AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
87-
ScopeLoc(ScopeLoc),
88-
AttrKind(getParsedKind(AttrName, ScopeName, SyntaxUsed)),
89-
SyntaxUsed(SyntaxUsed), SpellingIndex(SpellingNotCalculated) {}
83+
/// Combines information about the source-code form of an attribute,
84+
/// including its syntax and spelling.
85+
class Form {
86+
public:
87+
constexpr Form(Syntax SyntaxUsed,
88+
unsigned SpellingIndex = SpellingNotCalculated)
89+
: SyntaxUsed(SyntaxUsed), SpellingIndex(SpellingIndex) {}
90+
91+
Syntax getSyntax() const { return Syntax(SyntaxUsed); }
92+
unsigned getSpellingIndex() const { return SpellingIndex; }
93+
94+
private:
95+
unsigned SyntaxUsed : 4;
96+
unsigned SpellingIndex : 4;
97+
};
9098

9199
AttributeCommonInfo(const IdentifierInfo *AttrName,
92100
const IdentifierInfo *ScopeName, SourceRange AttrRange,
93-
SourceLocation ScopeLoc, Kind AttrKind, Syntax SyntaxUsed)
101+
SourceLocation ScopeLoc, Form FormUsed)
94102
: AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
95-
ScopeLoc(ScopeLoc), AttrKind(AttrKind), SyntaxUsed(SyntaxUsed),
96-
SpellingIndex(SpellingNotCalculated) {}
103+
ScopeLoc(ScopeLoc),
104+
AttrKind(getParsedKind(AttrName, ScopeName, FormUsed.getSyntax())),
105+
SyntaxUsed(FormUsed.getSyntax()),
106+
SpellingIndex(FormUsed.getSpellingIndex()) {}
97107

98108
AttributeCommonInfo(const IdentifierInfo *AttrName,
99109
const IdentifierInfo *ScopeName, SourceRange AttrRange,
100-
SourceLocation ScopeLoc, Kind AttrKind, Syntax SyntaxUsed,
101-
unsigned Spelling)
110+
SourceLocation ScopeLoc, Kind AttrKind, Form FormUsed)
102111
: AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
103-
ScopeLoc(ScopeLoc), AttrKind(AttrKind), SyntaxUsed(SyntaxUsed),
104-
SpellingIndex(Spelling) {}
112+
ScopeLoc(ScopeLoc), AttrKind(AttrKind),
113+
SyntaxUsed(FormUsed.getSyntax()),
114+
SpellingIndex(FormUsed.getSpellingIndex()) {}
105115

106116
AttributeCommonInfo(const IdentifierInfo *AttrName, SourceRange AttrRange,
107-
Syntax SyntaxUsed)
117+
Form FormUsed)
108118
: AttrName(AttrName), ScopeName(nullptr), AttrRange(AttrRange),
109-
ScopeLoc(), AttrKind(getParsedKind(AttrName, ScopeName, SyntaxUsed)),
110-
SyntaxUsed(SyntaxUsed), SpellingIndex(SpellingNotCalculated) {}
111-
112-
AttributeCommonInfo(SourceRange AttrRange, Kind K, Syntax SyntaxUsed)
113-
: AttrName(nullptr), ScopeName(nullptr), AttrRange(AttrRange), ScopeLoc(),
114-
AttrKind(K), SyntaxUsed(SyntaxUsed),
115-
SpellingIndex(SpellingNotCalculated) {}
119+
ScopeLoc(),
120+
AttrKind(getParsedKind(AttrName, ScopeName, FormUsed.getSyntax())),
121+
SyntaxUsed(FormUsed.getSyntax()),
122+
SpellingIndex(FormUsed.getSpellingIndex()) {}
116123

117-
AttributeCommonInfo(SourceRange AttrRange, Kind K, Syntax SyntaxUsed,
118-
unsigned Spelling)
124+
AttributeCommonInfo(SourceRange AttrRange, Kind K, Form FormUsed)
119125
: AttrName(nullptr), ScopeName(nullptr), AttrRange(AttrRange), ScopeLoc(),
120-
AttrKind(K), SyntaxUsed(SyntaxUsed), SpellingIndex(Spelling) {}
126+
AttrKind(K), SyntaxUsed(FormUsed.getSyntax()),
127+
SpellingIndex(FormUsed.getSpellingIndex()) {}
121128

122129
AttributeCommonInfo(AttributeCommonInfo &&) = default;
123130
AttributeCommonInfo(const AttributeCommonInfo &) = default;
124131

125132
Kind getParsedKind() const { return Kind(AttrKind); }
126133
Syntax getSyntax() const { return Syntax(SyntaxUsed); }
134+
Form getForm() const { return Form(getSyntax(), SpellingIndex); }
127135
const IdentifierInfo *getAttrName() const { return AttrName; }
128136
SourceLocation getLoc() const { return AttrRange.getBegin(); }
129137
SourceRange getRange() const { return AttrRange; }

clang/include/clang/Parse/Parser.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2762,7 +2762,7 @@ class Parser : public CodeCompletionHandler {
27622762
ParseAttributeArgsCommon(IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
27632763
ParsedAttributes &Attrs, SourceLocation *EndLoc,
27642764
IdentifierInfo *ScopeName, SourceLocation ScopeLoc,
2765-
ParsedAttr::Syntax Syntax);
2765+
ParsedAttr::Form Form);
27662766

27672767
enum ParseAttrKindMask {
27682768
PAKM_GNU = 1 << 0,
@@ -2823,14 +2823,14 @@ class Parser : public CodeCompletionHandler {
28232823
SourceLocation AttrNameLoc,
28242824
ParsedAttributes &Attrs, SourceLocation *EndLoc,
28252825
IdentifierInfo *ScopeName, SourceLocation ScopeLoc,
2826-
ParsedAttr::Syntax Syntax, Declarator *D);
2826+
ParsedAttr::Form Form, Declarator *D);
28272827
IdentifierLoc *ParseIdentifierLoc();
28282828

28292829
unsigned
28302830
ParseClangAttributeArgs(IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
28312831
ParsedAttributes &Attrs, SourceLocation *EndLoc,
28322832
IdentifierInfo *ScopeName, SourceLocation ScopeLoc,
2833-
ParsedAttr::Syntax Syntax);
2833+
ParsedAttr::Form Form);
28342834

28352835
void ReplayOpenMPAttributeTokens(CachedTokens &OpenMPTokens) {
28362836
// If parsing the attributes found an OpenMP directive, emit those tokens
@@ -2949,7 +2949,7 @@ class Parser : public CodeCompletionHandler {
29492949
SourceLocation *endLoc,
29502950
IdentifierInfo *ScopeName,
29512951
SourceLocation ScopeLoc,
2952-
ParsedAttr::Syntax Syntax);
2952+
ParsedAttr::Form Form);
29532953

29542954
std::optional<AvailabilitySpec> ParseAvailabilitySpec();
29552955
ExprResult ParseAvailabilityCheckExpr(SourceLocation StartLoc);
@@ -2960,38 +2960,38 @@ class Parser : public CodeCompletionHandler {
29602960
SourceLocation *EndLoc,
29612961
IdentifierInfo *ScopeName,
29622962
SourceLocation ScopeLoc,
2963-
ParsedAttr::Syntax Syntax);
2963+
ParsedAttr::Form Form);
29642964

29652965
void ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
29662966
SourceLocation ObjCBridgeRelatedLoc,
29672967
ParsedAttributes &Attrs,
29682968
SourceLocation *EndLoc,
29692969
IdentifierInfo *ScopeName,
29702970
SourceLocation ScopeLoc,
2971-
ParsedAttr::Syntax Syntax);
2971+
ParsedAttr::Form Form);
29722972

29732973
void ParseSwiftNewTypeAttribute(IdentifierInfo &AttrName,
29742974
SourceLocation AttrNameLoc,
29752975
ParsedAttributes &Attrs,
29762976
SourceLocation *EndLoc,
29772977
IdentifierInfo *ScopeName,
29782978
SourceLocation ScopeLoc,
2979-
ParsedAttr::Syntax Syntax);
2979+
ParsedAttr::Form Form);
29802980

29812981
void ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
29822982
SourceLocation AttrNameLoc,
29832983
ParsedAttributes &Attrs,
29842984
SourceLocation *EndLoc,
29852985
IdentifierInfo *ScopeName,
29862986
SourceLocation ScopeLoc,
2987-
ParsedAttr::Syntax Syntax);
2987+
ParsedAttr::Form Form);
29882988

29892989
void ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
29902990
SourceLocation AttrNameLoc,
29912991
ParsedAttributes &Attrs,
29922992
IdentifierInfo *ScopeName,
29932993
SourceLocation ScopeLoc,
2994-
ParsedAttr::Syntax Syntax);
2994+
ParsedAttr::Form Form);
29952995

29962996
void ParseTypeofSpecifier(DeclSpec &DS);
29972997
SourceLocation ParseDecltypeSpecifier(DeclSpec &DS);

0 commit comments

Comments
 (0)