From: Argyrios Kyrtzidis Date: Tue, 13 Sep 2011 16:05:53 +0000 (+0000) Subject: Record the full source range of an attribute. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ffcc3105d223899740e79f3f8199f3881df4d1de;p=clang Record the full source range of an attribute. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@139599 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Attr.h b/include/clang/AST/Attr.h index 17f0218a8b..7d57cd883f 100644 --- a/include/clang/AST/Attr.h +++ b/include/clang/AST/Attr.h @@ -57,7 +57,7 @@ namespace clang { /// Attr - This represents one attribute. class Attr { private: - SourceLocation Loc; + SourceRange Range; unsigned AttrKind : 16; protected: @@ -85,8 +85,8 @@ public: } protected: - Attr(attr::Kind AK, SourceLocation L) - : Loc(L), AttrKind(AK), Inherited(false) {} + Attr(attr::Kind AK, SourceRange R) + : Range(R), AttrKind(AK), Inherited(false) {} public: @@ -94,8 +94,9 @@ public: return static_cast(AttrKind); } - SourceLocation getLocation() const { return Loc; } - void setLocation(SourceLocation L) { Loc = L; } + SourceLocation getLocation() const { return Range.getBegin(); } + SourceRange getRange() const { return Range; } + void setRange(SourceRange R) { Range = R; } bool isInherited() const { return Inherited; } diff --git a/include/clang/Sema/AttributeList.h b/include/clang/Sema/AttributeList.h index 674e13a3df..a44f6fae33 100644 --- a/include/clang/Sema/AttributeList.h +++ b/include/clang/Sema/AttributeList.h @@ -56,7 +56,7 @@ private: IdentifierInfo *AttrName; IdentifierInfo *ScopeName; IdentifierInfo *ParmName; - SourceLocation AttrLoc; + SourceRange AttrRange; SourceLocation ScopeLoc; SourceLocation ParmLoc; @@ -114,13 +114,13 @@ private: size_t allocated_size() const; - AttributeList(IdentifierInfo *attrName, SourceLocation attrLoc, + AttributeList(IdentifierInfo *attrName, SourceRange attrRange, IdentifierInfo *scopeName, SourceLocation scopeLoc, IdentifierInfo *parmName, SourceLocation parmLoc, Expr **args, unsigned numArgs, bool declspec, bool cxx0x) : AttrName(attrName), ScopeName(scopeName), ParmName(parmName), - AttrLoc(attrLoc), ScopeLoc(scopeLoc), ParmLoc(parmLoc), + AttrRange(attrRange), ScopeLoc(scopeLoc), ParmLoc(parmLoc), NumArgs(numArgs), DeclspecAttribute(declspec), CXX0XAttribute(cxx0x), Invalid(false), IsAvailability(false), NextInPosition(0), NextInPool(0) { @@ -128,7 +128,7 @@ private: AttrKind = getKind(getName()); } - AttributeList(IdentifierInfo *attrName, SourceLocation attrLoc, + AttributeList(IdentifierInfo *attrName, SourceRange attrRange, IdentifierInfo *scopeName, SourceLocation scopeLoc, IdentifierInfo *parmName, SourceLocation parmLoc, const AvailabilityChange &introduced, @@ -137,7 +137,7 @@ private: SourceLocation unavailable, bool declspec, bool cxx0x) : AttrName(attrName), ScopeName(scopeName), ParmName(parmName), - AttrLoc(attrLoc), ScopeLoc(scopeLoc), ParmLoc(parmLoc), + AttrRange(attrRange), ScopeLoc(scopeLoc), ParmLoc(parmLoc), NumArgs(0), DeclspecAttribute(declspec), CXX0XAttribute(cxx0x), Invalid(false), IsAvailability(true), UnavailableLoc(unavailable), NextInPosition(0), NextInPool(0) { @@ -268,7 +268,8 @@ public: }; IdentifierInfo *getName() const { return AttrName; } - SourceLocation getLoc() const { return AttrLoc; } + SourceLocation getLoc() const { return AttrRange.getBegin(); } + SourceRange getRange() const { return AttrRange; } bool hasScope() const { return ScopeName; } IdentifierInfo *getScopeName() const { return ScopeName; } @@ -459,21 +460,21 @@ public: if (Head) Factory.reclaimPool(Head); } - AttributeList *create(IdentifierInfo *attrName, SourceLocation attrLoc, + AttributeList *create(IdentifierInfo *attrName, SourceRange attrRange, IdentifierInfo *scopeName, SourceLocation scopeLoc, IdentifierInfo *parmName, SourceLocation parmLoc, Expr **args, unsigned numArgs, bool declspec = false, bool cxx0x = false) { void *memory = allocate(sizeof(AttributeList) + numArgs * sizeof(Expr*)); - return add(new (memory) AttributeList(attrName, attrLoc, + return add(new (memory) AttributeList(attrName, attrRange, scopeName, scopeLoc, parmName, parmLoc, args, numArgs, declspec, cxx0x)); } - AttributeList *create(IdentifierInfo *attrName, SourceLocation attrLoc, + AttributeList *create(IdentifierInfo *attrName, SourceRange attrRange, IdentifierInfo *scopeName, SourceLocation scopeLoc, IdentifierInfo *parmName, SourceLocation parmLoc, const AvailabilityChange &introduced, @@ -482,7 +483,7 @@ public: SourceLocation unavailable, bool declspec = false, bool cxx0x = false) { void *memory = allocate(AttributeFactory::AvailabilityAllocSize); - return add(new (memory) AttributeList(attrName, attrLoc, + return add(new (memory) AttributeList(attrName, attrRange, scopeName, scopeLoc, parmName, parmLoc, introduced, deprecated, obsoleted, @@ -585,19 +586,19 @@ public: AttributeList *&getListRef() { return list; } - AttributeList *addNew(IdentifierInfo *attrName, SourceLocation attrLoc, + AttributeList *addNew(IdentifierInfo *attrName, SourceRange attrRange, IdentifierInfo *scopeName, SourceLocation scopeLoc, IdentifierInfo *parmName, SourceLocation parmLoc, Expr **args, unsigned numArgs, bool declspec = false, bool cxx0x = false) { AttributeList *attr = - pool.create(attrName, attrLoc, scopeName, scopeLoc, parmName, parmLoc, + pool.create(attrName, attrRange, scopeName, scopeLoc, parmName, parmLoc, args, numArgs, declspec, cxx0x); add(attr); return attr; } - AttributeList *addNew(IdentifierInfo *attrName, SourceLocation attrLoc, + AttributeList *addNew(IdentifierInfo *attrName, SourceRange attrRange, IdentifierInfo *scopeName, SourceLocation scopeLoc, IdentifierInfo *parmName, SourceLocation parmLoc, const AvailabilityChange &introduced, @@ -606,7 +607,7 @@ public: SourceLocation unavailable, bool declspec = false, bool cxx0x = false) { AttributeList *attr = - pool.create(attrName, attrLoc, scopeName, scopeLoc, parmName, parmLoc, + pool.create(attrName, attrRange, scopeName, scopeLoc, parmName, parmLoc, introduced, deprecated, obsoleted, unavailable, declspec, cxx0x); add(attr); diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 197e446fd3..16c25a4790 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -195,8 +195,8 @@ void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, if (Tok.is(tok::r_paren)) { // __attribute__(( mode(byte) )) - ConsumeParen(); // ignore the right paren loc for now - Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, + SourceLocation RParen = ConsumeParen(); + Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, ParmName, ParmLoc, 0, 0); } else if (Tok.is(tok::comma)) { ConsumeToken(); @@ -219,20 +219,21 @@ void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, ConsumeToken(); // Eat the comma, move to the next argument } if (ArgExprsOk && Tok.is(tok::r_paren)) { - ConsumeParen(); // ignore the right paren loc for now - Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, + SourceLocation RParen = ConsumeParen(); + Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size()); } } } else { // not an identifier switch (Tok.getKind()) { - case tok::r_paren: + case tok::r_paren: { // parse a possibly empty comma separated list of expressions // __attribute__(( nonnull() )) - ConsumeParen(); // ignore the right paren loc for now - Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, + SourceLocation RParen = ConsumeParen(); + Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, 0, SourceLocation(), 0, 0); break; + } case tok::kw_char: case tok::kw_wchar_t: case tok::kw_char16_t: @@ -248,16 +249,16 @@ void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, case tok::kw_double: case tok::kw_void: case tok::kw_typeof: { - AttributeList *attr - = Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, - 0, SourceLocation(), 0, 0); - if (attr->getKind() == AttributeList::AT_IBOutletCollection) - Diag(Tok, diag::err_iboutletcollection_builtintype); // If it's a builtin type name, eat it and expect a rparen // __attribute__(( vec_type_hint(char) )) - ConsumeToken(); + SourceLocation EndLoc = ConsumeToken(); if (Tok.is(tok::r_paren)) - ConsumeParen(); + EndLoc = ConsumeParen(); + AttributeList *attr + = Attrs.addNew(AttrName, SourceRange(AttrNameLoc, EndLoc), 0, + AttrNameLoc, 0, SourceLocation(), 0, 0); + if (attr->getKind() == AttributeList::AT_IBOutletCollection) + Diag(Tok, diag::err_iboutletcollection_builtintype); break; } default: @@ -281,8 +282,8 @@ void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, } // Match the ')'. if (ArgExprsOk && Tok.is(tok::r_paren)) { - ConsumeParen(); // ignore the right paren loc for now - Attrs.addNew(AttrName, AttrNameLoc, 0, + SourceLocation RParen = ConsumeParen(); + Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, 0, SourceLocation(), ArgExprs.take(), ArgExprs.size()); } @@ -695,7 +696,7 @@ void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, } // Record this attribute - attrs.addNew(&Availability, AvailabilityLoc, + attrs.addNew(&Availability, SourceRange(AvailabilityLoc, RParenLoc), 0, SourceLocation(), Platform, PlatformLoc, Changes[Introduced], diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 2f2e280f71..fc8583229b 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -1998,7 +1998,7 @@ static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { if (NoThrowAttr *Existing = D->getAttr()) { if (Existing->getLocation().isInvalid()) - Existing->setLocation(Attr.getLoc()); + Existing->setRange(Attr.getRange()); } else { D->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context)); } @@ -2013,7 +2013,7 @@ static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { if (ConstAttr *Existing = D->getAttr()) { if (Existing->getLocation().isInvalid()) - Existing->setLocation(Attr.getLoc()); + Existing->setRange(Attr.getRange()); } else { D->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context)); } @@ -2393,7 +2393,7 @@ static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { // If we don't have a valid location for this attribute, adopt the // location. if (f->getLocation().isInvalid()) - f->setLocation(Attr.getLoc()); + f->setRange(Attr.getRange()); return; } }