[cfe-dev] A patch for adding getTypeSpecStartLoc() to TagDecl.

Douglas Gregor dgregor at apple.com
Mon Jul 20 13:32:37 PDT 2009


On Jul 16, 2009, at 6:58 AM, Enea Zaffanella wrote:

> Enea Zaffanella wrote:
>> Hello.
>> Please find attached a patch that adds method
>>  SourceLocation getTypeSpecStartLoc() const;
>> to class clang::FieldDecl.
>
> Here is another patch, along the same lines as the one above and  
> serving similar purposes, that adds this info to clang::TagDecl  
> (i.e., the base class for enum/struct/union/class declarations). In  
> this case the method will return the location for the corresponding  
> tag keyword.

It's useful to have the location of the tag keyword (class-key in C++)  
in TagDecl and its derived classes, but getTypeSpecStartLoc() is the  
wrong name for this functionality because there is no type specifier  
in one of these declarations. I think this patch is fine if the name  
is changed to, e.g., getTagKeywordLoc().

	- Doug

> Cheers,
> Enea Zaffanella.
> Index: include/clang/AST/DeclCXX.h
> ===================================================================
> --- include/clang/AST/DeclCXX.h	(revision 75902)
> +++ include/clang/AST/DeclCXX.h	(working copy)
> @@ -335,7 +335,8 @@
>
> protected:
>   CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
> -                SourceLocation L, IdentifierInfo *Id);
> +                SourceLocation L, IdentifierInfo *Id,
> +                SourceLocation TSSL = SourceLocation());
>
>   ~CXXRecordDecl();
>
> @@ -350,6 +351,7 @@
>
>   static CXXRecordDecl *Create(ASTContext &C, TagKind TK,  
> DeclContext *DC,
>                                SourceLocation L, IdentifierInfo *Id,
> +                               SourceLocation TSSL = SourceLocation 
> (),
>                                CXXRecordDecl* PrevDecl=0,
>                                bool DelayTypeCreation = false);
>
> Index: include/clang/AST/Decl.h
> ===================================================================
> --- include/clang/AST/Decl.h	(revision 75902)
> +++ include/clang/AST/Decl.h	(working copy)
> @@ -1300,12 +1300,14 @@
>   /// this points to the TypedefDecl. Used for mangling.
>   TypedefDecl *TypedefForAnonDecl;
>
> +  SourceLocation TypeSpecStartLoc;
>   SourceLocation RBraceLoc;
>
> protected:
>   TagDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
> -          IdentifierInfo *Id)
> -    : TypeDecl(DK, DC, L, Id), DeclContext(DK), TypedefForAnonDecl 
> (0) {
> +          IdentifierInfo *Id, SourceLocation TSSL = SourceLocation())
> +    : TypeDecl(DK, DC, L, Id), DeclContext(DK), TypedefForAnonDecl 
> (0),
> +      TypeSpecStartLoc(TSSL) {
>     assert((DK != Enum || TK == TK_enum) &&"EnumDecl not matched  
> with TK_enum");
>     TagDeclKind = TK;
>     IsDefinition = false;
> @@ -1315,6 +1317,9 @@
>   SourceLocation getRBraceLoc() const { return RBraceLoc; }
>   void setRBraceLoc(SourceLocation L) { RBraceLoc = L; }
>
> +  SourceLocation getTypeSpecStartLoc() const { return  
> TypeSpecStartLoc; }
> +  void setTypeSpecStartLoc(SourceLocation TSSL) { TypeSpecStartLoc  
> = TSSL; }
> +
>   virtual SourceRange getSourceRange() const;
>
>   /// isDefinition - Return true if this decl has its body specified.
> @@ -1400,14 +1405,14 @@
>   EnumDecl *InstantiatedFrom;
>
>   EnumDecl(DeclContext *DC, SourceLocation L,
> -           IdentifierInfo *Id)
> -    : TagDecl(Enum, TK_enum, DC, L, Id), InstantiatedFrom(0) {
> +           IdentifierInfo *Id, SourceLocation TSSL)
> +    : TagDecl(Enum, TK_enum, DC, L, Id, TSSL), InstantiatedFrom(0) {
>       IntegerType = QualType();
>     }
> public:
>   static EnumDecl *Create(ASTContext &C, DeclContext *DC,
>                           SourceLocation L, IdentifierInfo *Id,
> -                          EnumDecl *PrevDecl);
> +                          SourceLocation TSSL, EnumDecl *PrevDecl);
>
>   virtual void Destroy(ASTContext& C);
>
> @@ -1473,12 +1478,13 @@
>
> protected:
>   RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
> -             SourceLocation L, IdentifierInfo *Id);
> +             SourceLocation L, IdentifierInfo *Id, SourceLocation  
> TSSL);
>   virtual ~RecordDecl();
>
> public:
>   static RecordDecl *Create(ASTContext &C, TagKind TK, DeclContext  
> *DC,
>                             SourceLocation L, IdentifierInfo *Id,
> +                            SourceLocation TSSL = SourceLocation(),
>                             RecordDecl* PrevDecl = 0);
>
>   virtual void Destroy(ASTContext& C);
> Index: lib/Frontend/PCHReaderDecl.cpp
> ===================================================================
> --- lib/Frontend/PCHReaderDecl.cpp	(revision 75902)
> +++ lib/Frontend/PCHReaderDecl.cpp	(working copy)
> @@ -117,6 +117,7 @@
>   TD->setTypedefForAnonDecl(
>                     cast_or_null<TypedefDecl>(Reader.GetDecl(Record 
> [Idx++])));
>   TD->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
> +  TD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record 
> [Idx++]));
> }
>
> void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
> @@ -608,11 +609,11 @@
>     D = TypedefDecl::Create(*Context, 0, SourceLocation(), 0,  
> QualType());
>     break;
>   case pch::DECL_ENUM:
> -    D = EnumDecl::Create(*Context, 0, SourceLocation(), 0, 0);
> +    D = EnumDecl::Create(*Context, 0, SourceLocation(), 0,  
> SourceLocation(), 0);
>     break;
>   case pch::DECL_RECORD:
>     D = RecordDecl::Create(*Context, TagDecl::TK_struct, 0,  
> SourceLocation(),
> -                           0, 0);
> +                           0, SourceLocation(), 0);
>     break;
>   case pch::DECL_ENUM_CONSTANT:
>     D = EnumConstantDecl::Create(*Context, 0, SourceLocation(), 0,  
> QualType(),
> Index: lib/Frontend/PCHWriterDecl.cpp
> ===================================================================
> --- lib/Frontend/PCHWriterDecl.cpp	(revision 75902)
> +++ lib/Frontend/PCHWriterDecl.cpp	(working copy)
> @@ -113,6 +113,7 @@
>   Record.push_back(D->isDefinition());
>   Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
>   Writer.AddSourceLocation(D->getRBraceLoc(), Record);
> +  Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
> }
>
> void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
> Index: lib/Sema/SemaTemplateInstantiateDecl.cpp
> ===================================================================
> --- lib/Sema/SemaTemplateInstantiateDecl.cpp	(revision 75902)
> +++ lib/Sema/SemaTemplateInstantiateDecl.cpp	(working copy)
> @@ -217,6 +217,7 @@
> Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
>   EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
>                                     D->getLocation(), D- 
> >getIdentifier(),
> +                                    D->getTypeSpecStartLoc(),
>                                     /*PrevDecl=*/0);
>   Enum->setInstantiationOfMemberEnum(D);
>   Enum->setAccess(D->getAccess());
> @@ -284,7 +285,8 @@
>
>   CXXRecordDecl *Record
>     = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
> -                            D->getLocation(), D->getIdentifier(),  
> PrevDecl);
> +                            D->getLocation(), D->getIdentifier(),
> +                            D->getTypeSpecStartLoc(), PrevDecl);
>   Record->setImplicit(D->isImplicit());
>   Record->setAccess(D->getAccess());
>   if (!D->isInjectedClassName())
> Index: lib/Sema/SemaDecl.cpp
> ===================================================================
> --- lib/Sema/SemaDecl.cpp	(revision 75902)
> +++ lib/Sema/SemaDecl.cpp	(working copy)
> @@ -3706,7 +3706,7 @@
>   if (Kind == TagDecl::TK_enum) {
>     // FIXME: Tag decls should be chained to any simultaneous  
> vardecls, e.g.:
>     // enum X { A, B, C } D;    D should chain to X.
> -    New = EnumDecl::Create(Context, SearchDC, Loc, Name,
> +    New = EnumDecl::Create(Context, SearchDC, Loc, Name, KWLoc,
>                            cast_or_null<EnumDecl>(PrevDecl));
>     // If this is an undefined enum, warn.
>     if (TK != TK_Definition && !Invalid)  {
> @@ -3721,10 +3721,10 @@
>     // struct X { int A; } D;    D should chain to X.
>     if (getLangOptions().CPlusPlus)
>       // FIXME: Look for a way to use RecordDecl for simple structs.
> -      New = CXXRecordDecl::Create(Context, Kind, SearchDC, Loc, Name,
> +      New = CXXRecordDecl::Create(Context, Kind, SearchDC, Loc,  
> Name, KWLoc,
>                                   cast_or_null<CXXRecordDecl> 
> (PrevDecl));
>     else
> -      New = RecordDecl::Create(Context, Kind, SearchDC, Loc, Name,
> +      New = RecordDecl::Create(Context, Kind, SearchDC, Loc, Name,  
> KWLoc,
>                                cast_or_null<RecordDecl>(PrevDecl));
>   }
>
> @@ -3826,7 +3826,9 @@
>       CXXRecordDecl *InjectedClassName
>         = CXXRecordDecl::Create(Context, Record->getTagKind(),
>                                 CurContext, Record->getLocation(),
> -                                Record->getIdentifier(), Record);
> +                                Record->getIdentifier(),
> +                                Record->getTypeSpecStartLoc(),
> +                                Record);
>       InjectedClassName->setImplicit();
>       InjectedClassName->setAccess(AS_public);
>       if (ClassTemplateDecl *Template = Record- 
> >getDescribedClassTemplate())
> Index: lib/Sema/SemaTemplate.cpp
> ===================================================================
> --- lib/Sema/SemaTemplate.cpp	(revision 75902)
> +++ lib/Sema/SemaTemplate.cpp	(working copy)
> @@ -522,7 +522,7 @@
>   // declaration!
>
>   CXXRecordDecl *NewClass =
> -    CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc,  
> Name,
> +    CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc,  
> Name, KWLoc,
>                           PrevClassTemplate?
>                             PrevClassTemplate->getTemplatedDecl() : 0,
>                           /*DelayTypeCreation=*/true);
> Index: lib/AST/Decl.cpp
> ===================================================================
> --- lib/AST/Decl.cpp	(revision 75902)
> +++ lib/AST/Decl.cpp	(working copy)
> @@ -182,9 +182,9 @@
> }
>
> EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,  
> SourceLocation L,
> -                           IdentifierInfo *Id,
> +                           IdentifierInfo *Id, SourceLocation TSSL,
>                            EnumDecl *PrevDecl) {
> -  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
> +  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, TSSL);
>   C.getTypeDeclType(Enum, PrevDecl);
>   return Enum;
> }
> @@ -730,8 +730,8 @@
> // 
> = 
> = 
> = 
> ----------------------------------------------------------------------= 
> ==//
>
> RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,  
> SourceLocation L,
> -                       IdentifierInfo *Id)
> -  : TagDecl(DK, TK, DC, L, Id) {
> +                       IdentifierInfo *Id, SourceLocation TSSL)
> +  : TagDecl(DK, TK, DC, L, Id, TSSL) {
>   HasFlexibleArrayMember = false;
>   AnonymousStructOrUnion = false;
>   HasObjectMember = false;
> @@ -740,9 +740,9 @@
>
> RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK,  
> DeclContext *DC,
>                                SourceLocation L, IdentifierInfo *Id,
> -                               RecordDecl* PrevDecl) {
> +                               SourceLocation TSSL, RecordDecl*  
> PrevDecl) {
>
> -  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
> +  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, TSSL);
>   C.getTypeDeclType(R, PrevDecl);
>   return R;
> }
> Index: lib/AST/DeclCXX.cpp
> ===================================================================
> --- lib/AST/DeclCXX.cpp	(revision 75902)
> +++ lib/AST/DeclCXX.cpp	(working copy)
> @@ -24,8 +24,9 @@
> // 
> = 
> = 
> = 
> ----------------------------------------------------------------------= 
> ==//
>
> CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
> -                             SourceLocation L, IdentifierInfo *Id)
> -  : RecordDecl(K, TK, DC, L, Id),
> +                             SourceLocation L, IdentifierInfo *Id,
> +                             SourceLocation TSSL)
> +  : RecordDecl(K, TK, DC, L, Id, TSSL),
>     UserDeclaredConstructor(false), UserDeclaredCopyConstructor 
> (false),
>     UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
>     Aggregate(true), PlainOldData(true), Polymorphic(false), Abstract 
> (false),
> @@ -36,9 +37,10 @@
>
> CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK,  
> DeclContext *DC,
>                                      SourceLocation L,  
> IdentifierInfo *Id,
> +                                     SourceLocation TSSL,
>                                      CXXRecordDecl* PrevDecl,
>                                      bool DelayTypeCreation) {
> -  CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id);
> +  CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L,  
> Id, TSSL);
>   if (!DelayTypeCreation)
>     C.getTypeDeclType(R, PrevDecl);
>   return R;
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list