Skip to content

Commit

Permalink
[clang][parse] Move source range into ParsedAttibutesView
Browse files Browse the repository at this point in the history
Move the SourceRange from the old ParsedAttributesWithRange into
ParsedAttributesView, so we have source range information available
everywhere we use attributes.

This also removes ParsedAttributesWithRange (replaced by simply using
ParsedAttributes) and ParsedAttributesVieWithRange (replaced by using
ParsedAttributesView).

Differential Revision: https://reviews.llvm.org/D121201
  • Loading branch information
tbaederr committed Mar 24, 2022
1 parent 32012eb commit 711e3a5
Show file tree
Hide file tree
Showing 21 changed files with 261 additions and 369 deletions.
183 changes: 61 additions & 122 deletions clang/include/clang/Parse/Parser.h

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions clang/include/clang/Sema/DeclSpec.h
Expand Up @@ -368,7 +368,7 @@ class DeclSpec {
ExplicitSpecifier FS_explicit_specifier;

// attributes.
ParsedAttributesWithRange Attrs;
ParsedAttributes Attrs;

// Scope specifier for the type spec, if applicable.
CXXScopeSpec TypeScope;
Expand Down Expand Up @@ -786,7 +786,7 @@ class DeclSpec {
/// int __attribute__((may_alias)) __attribute__((aligned(16))) var;
/// \endcode
///
void addAttributes(ParsedAttributesView &AL) {
void addAttributes(const ParsedAttributesView &AL) {
Attrs.addAll(AL.begin(), AL.end());
}

Expand Down Expand Up @@ -2513,11 +2513,11 @@ class Declarator {
/// __attribute__((common,deprecated));
///
/// Also extends the range of the declarator.
void takeAttributes(ParsedAttributes &attrs, SourceLocation lastLoc) {
void takeAttributes(ParsedAttributes &attrs) {
Attrs.takeAllFrom(attrs);

if (!lastLoc.isInvalid())
SetRangeEnd(lastLoc);
if (attrs.Range.getEnd().isValid())
SetRangeEnd(attrs.Range.getEnd());
}

const ParsedAttributes &getAttributes() const { return Attrs; }
Expand Down
41 changes: 13 additions & 28 deletions clang/include/clang/Sema/ParsedAttr.h
Expand Up @@ -900,6 +900,7 @@ class ParsedAttributesView {
using SizeType = decltype(std::declval<VecTy>().size());

public:
SourceRange Range;
bool empty() const { return AttrList.empty(); }
SizeType size() const { return AttrList.size(); }
ParsedAttr &operator[](SizeType pos) { return *AttrList[pos]; }
Expand Down Expand Up @@ -998,22 +999,27 @@ class ParsedAttributes : public ParsedAttributesView {

AttributePool &getPool() const { return pool; }

void takeAllFrom(ParsedAttributes &attrs) {
addAll(attrs.begin(), attrs.end());
attrs.clearListOnly();
pool.takeAllFrom(attrs.pool);
void takeAllFrom(ParsedAttributes &Other) {
assert(&Other != this &&
"ParsedAttributes can't take attributes from itself");
addAll(Other.begin(), Other.end());
Other.clearListOnly();
pool.takeAllFrom(Other.pool);
}

void takeOneFrom(ParsedAttributes &Attrs, ParsedAttr *PA) {
Attrs.getPool().remove(PA);
Attrs.remove(PA);
void takeOneFrom(ParsedAttributes &Other, ParsedAttr *PA) {
assert(&Other != this &&
"ParsedAttributes can't take attribute from itself");
Other.getPool().remove(PA);
Other.remove(PA);
getPool().add(PA);
addAtEnd(PA);
}

void clear() {
clearListOnly();
pool.clear();
Range = SourceRange();
}

/// Add attribute with expression arguments.
Expand Down Expand Up @@ -1097,27 +1103,6 @@ class ParsedAttributes : public ParsedAttributesView {
mutable AttributePool pool;
};

struct ParsedAttributesWithRange : ParsedAttributes {
ParsedAttributesWithRange(AttributeFactory &factory)
: ParsedAttributes(factory) {}

void clear() {
ParsedAttributes::clear();
Range = SourceRange();
}

SourceRange Range;
};
struct ParsedAttributesViewWithRange : ParsedAttributesView {
ParsedAttributesViewWithRange() {}
void clearListOnly() {
ParsedAttributesView::clearListOnly();
Range = SourceRange();
}

SourceRange Range;
};

/// These constants match the enumerated choices of
/// err_attribute_argument_n_type and err_attribute_argument_type.
enum AttributeArgumentNType {
Expand Down
16 changes: 6 additions & 10 deletions clang/include/clang/Sema/Sema.h
Expand Up @@ -2867,8 +2867,7 @@ class Sema final {
void ActOnCXXForRangeDecl(Decl *D);
StmtResult ActOnCXXForRangeIdentifier(Scope *S, SourceLocation IdentLoc,
IdentifierInfo *Ident,
ParsedAttributes &Attrs,
SourceLocation AttrEnd);
ParsedAttributes &Attrs);
void SetDeclDeleted(Decl *dcl, SourceLocation DelLoc);
void SetDeclDefaulted(Decl *dcl, SourceLocation DefaultLoc);
void CheckStaticLocalForDllExport(VarDecl *VD);
Expand Down Expand Up @@ -4440,8 +4439,7 @@ class Sema final {

/// Process the attributes before creating an attributed statement. Returns
/// the semantic attributes that have been processed.
void ProcessStmtAttributes(Stmt *Stmt,
const ParsedAttributesWithRange &InAttrs,
void ProcessStmtAttributes(Stmt *Stmt, const ParsedAttributes &InAttrs,
SmallVectorImpl<const Attr *> &OutAttrs);

void WarnConflictingTypedMethods(ObjCMethodDecl *Method,
Expand Down Expand Up @@ -4783,7 +4781,7 @@ class Sema final {

StmtResult BuildAttributedStmt(SourceLocation AttrsLoc,
ArrayRef<const Attr *> Attrs, Stmt *SubStmt);
StmtResult ActOnAttributedStmt(const ParsedAttributesWithRange &AttrList,
StmtResult ActOnAttributedStmt(const ParsedAttributes &AttrList,
Stmt *SubStmt);

class ConditionResult;
Expand Down Expand Up @@ -7361,11 +7359,9 @@ class Sema final {
TypeSourceInfo *TInfo,
SourceLocation EllipsisLoc);

BaseResult ActOnBaseSpecifier(Decl *classdecl,
SourceRange SpecifierRange,
ParsedAttributes &Attrs,
bool Virtual, AccessSpecifier Access,
ParsedType basetype,
BaseResult ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange,
const ParsedAttributesView &Attrs, bool Virtual,
AccessSpecifier Access, ParsedType basetype,
SourceLocation BaseLoc,
SourceLocation EllipsisLoc);

Expand Down
13 changes: 6 additions & 7 deletions clang/lib/Parse/ParseCXXInlineMethods.cpp
Expand Up @@ -22,9 +22,9 @@ using namespace clang;
/// Declarator is a well formed C++ inline method definition. Now lex its body
/// and store its tokens for parsing after the C++ class is complete.
NamedDecl *Parser::ParseCXXInlineMethodDef(
AccessSpecifier AS, ParsedAttributes &AccessAttrs, ParsingDeclarator &D,
const ParsedTemplateInfo &TemplateInfo, const VirtSpecifiers &VS,
SourceLocation PureSpecLoc) {
AccessSpecifier AS, const ParsedAttributesView &AccessAttrs,
ParsingDeclarator &D, const ParsedTemplateInfo &TemplateInfo,
const VirtSpecifiers &VS, SourceLocation PureSpecLoc) {
assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) &&
"Current token not a '{', ':', '=', or 'try'!");
Expand Down Expand Up @@ -720,7 +720,6 @@ void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);

ParsedAttributes Attrs(AttrFactory);
SourceLocation endLoc;

if (LA.Decls.size() > 0) {
Decl *D = LA.Decls[0];
Expand All @@ -743,7 +742,7 @@ void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
}

ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, nullptr,
nullptr, SourceLocation(), ParsedAttr::AS_GNU,
nullptr);

Expand All @@ -752,7 +751,7 @@ void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
} else {
// If there are multiple decls, then the decl cannot be within the
// function scope.
ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, nullptr,
nullptr, SourceLocation(), ParsedAttr::AS_GNU,
nullptr);
}
Expand Down Expand Up @@ -796,7 +795,7 @@ void Parser::ParseLexedPragma(LateParsedPragma &LP) {
case tok::annot_attr_openmp:
case tok::annot_pragma_openmp: {
AccessSpecifier AS = LP.getAccessSpecifier();
ParsedAttributesWithRange Attrs(AttrFactory);
ParsedAttributes Attrs(AttrFactory);
(void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
break;
}
Expand Down

0 comments on commit 711e3a5

Please sign in to comment.