From 753944f52184ddf75318b5d3f1fd6a7a84587f60 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Fri, 15 Jul 2016 18:11:33 +0000 Subject: [PATCH] [AST] Keep track of the left brace source location of a tag decl. This is useful for source modification tools. There will be a follow-up commit using it. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@275590 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Decl.h | 6 +++--- include/clang/Sema/Sema.h | 6 +++--- lib/AST/Decl.cpp | 1 + lib/AST/DeclTemplate.cpp | 2 +- lib/Parse/ParseDecl.cpp | 8 +++----- lib/Parse/ParseDeclCXX.cpp | 3 +-- lib/Sema/SemaDecl.cpp | 8 ++++---- lib/Sema/SemaTemplate.cpp | 2 +- lib/Sema/SemaTemplateInstantiate.cpp | 2 +- lib/Sema/SemaTemplateInstantiateDecl.cpp | 4 +--- lib/Serialization/ASTReaderDecl.cpp | 4 ++-- lib/Serialization/ASTWriter.cpp | 2 +- lib/Serialization/ASTWriterDecl.cpp | 4 +++- 13 files changed, 25 insertions(+), 27 deletions(-) diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index db13e25979..109036f958 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -2781,7 +2781,7 @@ protected: /// the TU. unsigned IsCompleteDefinitionRequired : 1; private: - SourceLocation RBraceLoc; + SourceRange BraceRange; // A struct representing syntactic qualifier info, // to be used for the (uncommon) case of out-of-line declarations. @@ -2843,8 +2843,8 @@ public: using redeclarable_base::getMostRecentDecl; using redeclarable_base::isFirstDecl; - SourceLocation getRBraceLoc() const { return RBraceLoc; } - void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } + SourceRange getBraceRange() const { return BraceRange; } + void setBraceRange(SourceRange R) { BraceRange = R; } /// getInnerLocStart - Return SourceLocation representing start of source /// range ignoring outer template declarations. diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 6c2c86d657..4f2f7300ae 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -2039,7 +2039,7 @@ public: /// ActOnTagFinishDefinition - Invoked once we have finished parsing /// the definition of a tag (enumeration, class, struct, or union). void ActOnTagFinishDefinition(Scope *S, Decl *TagDecl, - SourceLocation RBraceLoc); + SourceRange BraceRange); void ActOnTagFinishSkippedDefinition(SkippedDefinitionContext Context); @@ -2076,8 +2076,8 @@ public: SourceLocation IdLoc, IdentifierInfo *Id, AttributeList *Attrs, SourceLocation EqualLoc, Expr *Val); - void ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc, - SourceLocation RBraceLoc, Decl *EnumDecl, + void ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange, + Decl *EnumDecl, ArrayRef Elements, Scope *S, AttributeList *Attr); diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 46427abea8..d1e8d25ea0 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -3525,6 +3525,7 @@ SourceLocation TagDecl::getOuterLocStart() const { } SourceRange TagDecl::getSourceRange() const { + SourceLocation RBraceLoc = BraceRange.getEnd(); SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); return SourceRange(getOuterLocStart(), E); } diff --git a/lib/AST/DeclTemplate.cpp b/lib/AST/DeclTemplate.cpp index c4cc79f1a5..37943cdd5b 100644 --- a/lib/AST/DeclTemplate.cpp +++ b/lib/AST/DeclTemplate.cpp @@ -778,7 +778,7 @@ ClassTemplateSpecializationDecl::getSourceRange() const { getSpecializationKind() == TSK_ExplicitInstantiationDefinition); if (getExternLoc().isValid()) Begin = getExternLoc(); - SourceLocation End = getRBraceLoc(); + SourceLocation End = getBraceRange().getEnd(); if (End.isInvalid()) End = getTypeAsWritten()->getTypeLoc().getEndLoc(); return SourceRange(Begin, End); diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 65d7a9b8fd..9a754ee23c 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -3773,8 +3773,7 @@ void Parser::ParseStructUnionBody(SourceLocation RecordLoc, T.getOpenLocation(), T.getCloseLocation(), attrs.getList()); StructScope.Exit(); - Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, - T.getCloseLocation()); + Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange()); } /// ParseEnumSpecifier @@ -4269,7 +4268,7 @@ void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { ParsedAttributes attrs(AttrFactory); MaybeParseGNUAttributes(attrs); - Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), + Actions.ActOnEnumBody(StartLoc, T.getRange(), EnumDecl, EnumConstantDecls, getCurScope(), attrs.getList()); @@ -4283,8 +4282,7 @@ void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { } EnumScope.Exit(); - Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, - T.getCloseLocation()); + Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, T.getRange()); // The next token must be valid after an enum definition. If not, a ';' // was probably forgotten. diff --git a/lib/Parse/ParseDeclCXX.cpp b/lib/Parse/ParseDeclCXX.cpp index 51f1192fae..6436e3dfc7 100644 --- a/lib/Parse/ParseDeclCXX.cpp +++ b/lib/Parse/ParseDeclCXX.cpp @@ -3143,8 +3143,7 @@ void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, } if (TagDecl) - Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, - T.getCloseLocation()); + Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange()); // Leave the class scope. ParsingDef.Pop(); diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index cedd21476c..ea276a995d 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -13176,10 +13176,10 @@ void Sema::ActOnStartCXXMemberDeclarations(Scope *S, Decl *TagD, } void Sema::ActOnTagFinishDefinition(Scope *S, Decl *TagD, - SourceLocation RBraceLoc) { + SourceRange BraceRange) { AdjustDeclIfTemplate(TagD); TagDecl *Tag = cast(TagD); - Tag->setRBraceLoc(RBraceLoc); + Tag->setBraceRange(BraceRange); // Make sure we "complete" the definition even it is invalid. if (Tag->isBeingDefined()) { @@ -14775,8 +14775,8 @@ bool Sema::IsValueInFlagEnum(const EnumDecl *ED, const llvm::APInt &Val, return !(FlagMask & Val) || (AllowMask && !(FlagMask & ~Val)); } -void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc, - SourceLocation RBraceLoc, Decl *EnumDeclX, +void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange, + Decl *EnumDeclX, ArrayRef Elements, Scope *S, AttributeList *Attr) { EnumDecl *Enum = cast(EnumDeclX); diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index 8175df37ba..7fc5db82d3 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -7509,7 +7509,7 @@ Sema::ActOnExplicitInstantiation(Scope *S, // Set source locations for keywords. Specialization->setExternLoc(ExternLoc); Specialization->setTemplateKeywordLoc(TemplateLoc); - Specialization->setRBraceLoc(SourceLocation()); + Specialization->setBraceRange(SourceRange()); if (Attr) ProcessDeclAttributeList(S, Specialization, Attr); diff --git a/lib/Sema/SemaTemplateInstantiate.cpp b/lib/Sema/SemaTemplateInstantiate.cpp index 4f1879c2de..48c6a506ee 100644 --- a/lib/Sema/SemaTemplateInstantiate.cpp +++ b/lib/Sema/SemaTemplateInstantiate.cpp @@ -2090,7 +2090,7 @@ Sema::InstantiateClass(SourceLocation PointOfInstantiation, if (TSK == TSK_ImplicitInstantiation) { Instantiation->setLocation(Pattern->getLocation()); Instantiation->setLocStart(Pattern->getInnerLocStart()); - Instantiation->setRBraceLoc(Pattern->getRBraceLoc()); + Instantiation->setBraceRange(Pattern->getBraceRange()); } if (!Instantiation->isInvalidDecl()) { diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp index 4d75f80d1f..6a213953ec 100644 --- a/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -1018,9 +1018,7 @@ void TemplateDeclInstantiator::InstantiateEnumDefinition( } } - // FIXME: Fixup LBraceLoc - SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), - Enum->getRBraceLoc(), Enum, + SemaRef.ActOnEnumBody(Enum->getLocation(), Enum->getBraceRange(), Enum, Enumerators, nullptr, nullptr); } diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp index 21db402786..d9ed39a501 100644 --- a/lib/Serialization/ASTReaderDecl.cpp +++ b/lib/Serialization/ASTReaderDecl.cpp @@ -647,7 +647,7 @@ ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) { TD->setEmbeddedInDeclarator(Record[Idx++]); TD->setFreeStanding(Record[Idx++]); TD->setCompleteDefinitionRequired(Record[Idx++]); - TD->setRBraceLoc(ReadSourceLocation(Record, Idx)); + TD->setBraceRange(ReadSourceRange(Record, Idx)); switch (Record[Idx++]) { case 0: @@ -3846,7 +3846,7 @@ void ASTDeclReader::UpdateDecl(Decl *D, ModuleFile &ModuleFile, RD->setTagKind((TagTypeKind)Record[Idx++]); RD->setLocation(Reader.ReadSourceLocation(ModuleFile, Record, Idx)); RD->setLocStart(Reader.ReadSourceLocation(ModuleFile, Record, Idx)); - RD->setRBraceLoc(Reader.ReadSourceLocation(ModuleFile, Record, Idx)); + RD->setBraceRange(Reader.ReadSourceRange(ModuleFile, Record, Idx)); if (Record[Idx++]) { AttrVec Attrs; diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp index ef51225b60..bb6a8ae8f6 100644 --- a/lib/Serialization/ASTWriter.cpp +++ b/lib/Serialization/ASTWriter.cpp @@ -4716,7 +4716,7 @@ void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) { Record.push_back(RD->getTagKind()); Record.AddSourceLocation(RD->getLocation()); Record.AddSourceLocation(RD->getLocStart()); - Record.AddSourceLocation(RD->getRBraceLoc()); + Record.AddSourceRange(RD->getBraceRange()); // Instantiation may change attributes; write them all out afresh. Record.push_back(D->hasAttrs()); diff --git a/lib/Serialization/ASTWriterDecl.cpp b/lib/Serialization/ASTWriterDecl.cpp index 6bc7d72c04..8a54d9e163 100644 --- a/lib/Serialization/ASTWriterDecl.cpp +++ b/lib/Serialization/ASTWriterDecl.cpp @@ -399,7 +399,7 @@ void ASTDeclWriter::VisitTagDecl(TagDecl *D) { Record.push_back(D->isEmbeddedInDeclarator()); Record.push_back(D->isFreeStanding()); Record.push_back(D->isCompleteDefinitionRequired()); - Record.AddSourceLocation(D->getRBraceLoc()); + Record.AddSourceRange(D->getBraceRange()); if (D->hasExtInfo()) { Record.push_back(1); @@ -1769,6 +1769,7 @@ void ASTWriter::WriteDeclAbbrevs() { Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFreeStanding Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsCompleteDefinitionRequired Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation Abv->Add(BitCodeAbbrevOp(0)); // ExtInfoKind // EnumDecl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddTypeRef @@ -1817,6 +1818,7 @@ void ASTWriter::WriteDeclAbbrevs() { Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFreeStanding Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsCompleteDefinitionRequired Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation Abv->Add(BitCodeAbbrevOp(0)); // ExtInfoKind // RecordDecl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // FlexibleArrayMember -- 2.40.0