Skip to content

Commit

Permalink
[clang] Specify attribute syntax & spelling with a single argument
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rsandifo-arm committed Apr 13, 2023
1 parent e841d50 commit b6d4d51
Show file tree
Hide file tree
Showing 16 changed files with 192 additions and 204 deletions.
58 changes: 33 additions & 25 deletions clang/include/clang/Basic/AttributeCommonInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,50 +80,58 @@ class AttributeCommonInfo {
static constexpr unsigned SpellingNotCalculated = 0xf;

public:
AttributeCommonInfo(const IdentifierInfo *AttrName,
const IdentifierInfo *ScopeName, SourceRange AttrRange,
SourceLocation ScopeLoc, Syntax SyntaxUsed)
: AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
ScopeLoc(ScopeLoc),
AttrKind(getParsedKind(AttrName, ScopeName, SyntaxUsed)),
SyntaxUsed(SyntaxUsed), SpellingIndex(SpellingNotCalculated) {}
/// Combines information about the source-code form of an attribute,
/// including its syntax and spelling.
class Form {
public:
constexpr Form(Syntax SyntaxUsed,
unsigned SpellingIndex = SpellingNotCalculated)
: SyntaxUsed(SyntaxUsed), SpellingIndex(SpellingIndex) {}

Syntax getSyntax() const { return Syntax(SyntaxUsed); }
unsigned getSpellingIndex() const { return SpellingIndex; }

private:
unsigned SyntaxUsed : 4;
unsigned SpellingIndex : 4;
};

AttributeCommonInfo(const IdentifierInfo *AttrName,
const IdentifierInfo *ScopeName, SourceRange AttrRange,
SourceLocation ScopeLoc, Kind AttrKind, Syntax SyntaxUsed)
SourceLocation ScopeLoc, Form FormUsed)
: AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
ScopeLoc(ScopeLoc), AttrKind(AttrKind), SyntaxUsed(SyntaxUsed),
SpellingIndex(SpellingNotCalculated) {}
ScopeLoc(ScopeLoc),
AttrKind(getParsedKind(AttrName, ScopeName, FormUsed.getSyntax())),
SyntaxUsed(FormUsed.getSyntax()),
SpellingIndex(FormUsed.getSpellingIndex()) {}

AttributeCommonInfo(const IdentifierInfo *AttrName,
const IdentifierInfo *ScopeName, SourceRange AttrRange,
SourceLocation ScopeLoc, Kind AttrKind, Syntax SyntaxUsed,
unsigned Spelling)
SourceLocation ScopeLoc, Kind AttrKind, Form FormUsed)
: AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
ScopeLoc(ScopeLoc), AttrKind(AttrKind), SyntaxUsed(SyntaxUsed),
SpellingIndex(Spelling) {}
ScopeLoc(ScopeLoc), AttrKind(AttrKind),
SyntaxUsed(FormUsed.getSyntax()),
SpellingIndex(FormUsed.getSpellingIndex()) {}

AttributeCommonInfo(const IdentifierInfo *AttrName, SourceRange AttrRange,
Syntax SyntaxUsed)
Form FormUsed)
: AttrName(AttrName), ScopeName(nullptr), AttrRange(AttrRange),
ScopeLoc(), AttrKind(getParsedKind(AttrName, ScopeName, SyntaxUsed)),
SyntaxUsed(SyntaxUsed), SpellingIndex(SpellingNotCalculated) {}

AttributeCommonInfo(SourceRange AttrRange, Kind K, Syntax SyntaxUsed)
: AttrName(nullptr), ScopeName(nullptr), AttrRange(AttrRange), ScopeLoc(),
AttrKind(K), SyntaxUsed(SyntaxUsed),
SpellingIndex(SpellingNotCalculated) {}
ScopeLoc(),
AttrKind(getParsedKind(AttrName, ScopeName, FormUsed.getSyntax())),
SyntaxUsed(FormUsed.getSyntax()),
SpellingIndex(FormUsed.getSpellingIndex()) {}

AttributeCommonInfo(SourceRange AttrRange, Kind K, Syntax SyntaxUsed,
unsigned Spelling)
AttributeCommonInfo(SourceRange AttrRange, Kind K, Form FormUsed)
: AttrName(nullptr), ScopeName(nullptr), AttrRange(AttrRange), ScopeLoc(),
AttrKind(K), SyntaxUsed(SyntaxUsed), SpellingIndex(Spelling) {}
AttrKind(K), SyntaxUsed(FormUsed.getSyntax()),
SpellingIndex(FormUsed.getSpellingIndex()) {}

AttributeCommonInfo(AttributeCommonInfo &&) = default;
AttributeCommonInfo(const AttributeCommonInfo &) = default;

Kind getParsedKind() const { return Kind(AttrKind); }
Syntax getSyntax() const { return Syntax(SyntaxUsed); }
Form getForm() const { return Form(getSyntax(), SpellingIndex); }
const IdentifierInfo *getAttrName() const { return AttrName; }
SourceLocation getLoc() const { return AttrRange.getBegin(); }
SourceRange getRange() const { return AttrRange; }
Expand Down
18 changes: 9 additions & 9 deletions clang/include/clang/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -2762,7 +2762,7 @@ class Parser : public CodeCompletionHandler {
ParseAttributeArgsCommon(IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
ParsedAttributes &Attrs, SourceLocation *EndLoc,
IdentifierInfo *ScopeName, SourceLocation ScopeLoc,
ParsedAttr::Syntax Syntax);
ParsedAttr::Form Form);

enum ParseAttrKindMask {
PAKM_GNU = 1 << 0,
Expand Down Expand Up @@ -2823,14 +2823,14 @@ class Parser : public CodeCompletionHandler {
SourceLocation AttrNameLoc,
ParsedAttributes &Attrs, SourceLocation *EndLoc,
IdentifierInfo *ScopeName, SourceLocation ScopeLoc,
ParsedAttr::Syntax Syntax, Declarator *D);
ParsedAttr::Form Form, Declarator *D);
IdentifierLoc *ParseIdentifierLoc();

unsigned
ParseClangAttributeArgs(IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
ParsedAttributes &Attrs, SourceLocation *EndLoc,
IdentifierInfo *ScopeName, SourceLocation ScopeLoc,
ParsedAttr::Syntax Syntax);
ParsedAttr::Form Form);

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

std::optional<AvailabilitySpec> ParseAvailabilitySpec();
ExprResult ParseAvailabilityCheckExpr(SourceLocation StartLoc);
Expand All @@ -2960,38 +2960,38 @@ class Parser : public CodeCompletionHandler {
SourceLocation *EndLoc,
IdentifierInfo *ScopeName,
SourceLocation ScopeLoc,
ParsedAttr::Syntax Syntax);
ParsedAttr::Form Form);

void ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
SourceLocation ObjCBridgeRelatedLoc,
ParsedAttributes &Attrs,
SourceLocation *EndLoc,
IdentifierInfo *ScopeName,
SourceLocation ScopeLoc,
ParsedAttr::Syntax Syntax);
ParsedAttr::Form Form);

void ParseSwiftNewTypeAttribute(IdentifierInfo &AttrName,
SourceLocation AttrNameLoc,
ParsedAttributes &Attrs,
SourceLocation *EndLoc,
IdentifierInfo *ScopeName,
SourceLocation ScopeLoc,
ParsedAttr::Syntax Syntax);
ParsedAttr::Form Form);

void ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
SourceLocation AttrNameLoc,
ParsedAttributes &Attrs,
SourceLocation *EndLoc,
IdentifierInfo *ScopeName,
SourceLocation ScopeLoc,
ParsedAttr::Syntax Syntax);
ParsedAttr::Form Form);

void ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
SourceLocation AttrNameLoc,
ParsedAttributes &Attrs,
IdentifierInfo *ScopeName,
SourceLocation ScopeLoc,
ParsedAttr::Syntax Syntax);
ParsedAttr::Form Form);

void ParseTypeofSpecifier(DeclSpec &DS);
SourceLocation ParseDecltypeSpecifier(DeclSpec &DS);
Expand Down

0 comments on commit b6d4d51

Please sign in to comment.