From 1693e154bef16ca060b5e3786d8528ddc11f5637 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Tue, 6 Jul 2010 18:42:40 +0000 Subject: [PATCH] Improve the accuracy of getSourceRange() for DeclaratorDecl and TagDecl subclasses when out-of-line template declaration information is available, from Peter Collingbourne! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@107686 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Decl.h | 21 ++++++++++++++++++++- include/clang/AST/DeclTemplate.h | 2 ++ lib/AST/Decl.cpp | 29 ++++++++++++++++++++++++----- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index bdfb49e0d0..940f1b9187 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -436,6 +436,17 @@ public: DeclInfo = TI; } + /// getInnerLocStart - Return SourceLocation representing start of source + /// range ignoring outer template declarations. + virtual SourceLocation getInnerLocStart() const { return getLocation(); } + + /// getOuterLocStart - Return SourceLocation representing start of source + /// range taking into account any outer template declarations. + SourceLocation getOuterLocStart() const; + SourceRange getSourceRange() const { + return SourceRange(getOuterLocStart(), getLocation()); + } + NestedNameSpecifier *getQualifier() const { return hasExtInfo() ? getExtInfo()->NNS : 0; } @@ -601,6 +612,7 @@ public: virtual void Destroy(ASTContext& C); virtual ~VarDecl(); + virtual SourceLocation getInnerLocStart() const; virtual SourceRange getSourceRange() const; StorageClass getStorageClass() const { return (StorageClass)SClass; } @@ -1209,7 +1221,7 @@ public: bool Qualified) const; virtual SourceRange getSourceRange() const { - return SourceRange(getLocation(), EndRangeLoc); + return SourceRange(getOuterLocStart(), EndRangeLoc); } void setLocEnd(SourceLocation E) { EndRangeLoc = E; @@ -1865,6 +1877,13 @@ public: SourceLocation getTagKeywordLoc() const { return TagKeywordLoc; } void setTagKeywordLoc(SourceLocation TKL) { TagKeywordLoc = TKL; } + /// getInnerLocStart - Return SourceLocation representing start of source + /// range ignoring outer template declarations. + virtual SourceLocation getInnerLocStart() const { return TagKeywordLoc; } + + /// getOuterLocStart - Return SourceLocation representing start of source + /// range taking into account any outer template declarations. + SourceLocation getOuterLocStart() const; virtual SourceRange getSourceRange() const; virtual TagDecl* getCanonicalDecl(); diff --git a/include/clang/AST/DeclTemplate.h b/include/clang/AST/DeclTemplate.h index 14ef01dcb8..10230d2223 100644 --- a/include/clang/AST/DeclTemplate.h +++ b/include/clang/AST/DeclTemplate.h @@ -1149,6 +1149,8 @@ public: return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation(); } + SourceLocation getInnerLocStart() const { return getTemplateKeywordLoc(); } + void Profile(llvm::FoldingSetNodeID &ID) const { Profile(ID, TemplateArgs.getFlatArgumentList(), TemplateArgs.flat_size(), getASTContext()); diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 6fa6745870..6b52a17a21 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -523,6 +523,14 @@ bool NamedDecl::isCXXInstanceMember() const { // DeclaratorDecl Implementation //===----------------------------------------------------------------------===// +template +static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { + if (decl->getNumTemplateParameterLists() > 0) + return decl->getTemplateParameterList(0)->getTemplateLoc(); + else + return decl->getInnerLocStart(); +} + DeclaratorDecl::~DeclaratorDecl() {} void DeclaratorDecl::Destroy(ASTContext &C) { if (hasExtInfo()) @@ -566,6 +574,10 @@ void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier, } } +SourceLocation DeclaratorDecl::getOuterLocStart() const { + return getTemplateOrInnerLocStart(this); +} + void QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context, unsigned NumTPLists, @@ -636,14 +648,17 @@ void VarDecl::Destroy(ASTContext& C) { VarDecl::~VarDecl() { } -SourceRange VarDecl::getSourceRange() const { +SourceLocation VarDecl::getInnerLocStart() const { SourceLocation Start = getTypeSpecStartLoc(); if (Start.isInvalid()) Start = getLocation(); - + return Start; +} + +SourceRange VarDecl::getSourceRange() const { if (getInit()) - return SourceRange(Start, getInit()->getLocEnd()); - return SourceRange(Start, getLocation()); + return SourceRange(getOuterLocStart(), getInit()->getLocEnd()); + return SourceRange(getOuterLocStart(), getLocation()); } bool VarDecl::isExternC() const { @@ -1544,9 +1559,13 @@ void TagDecl::Destroy(ASTContext &C) { TypeDecl::Destroy(C); } +SourceLocation TagDecl::getOuterLocStart() const { + return getTemplateOrInnerLocStart(this); +} + SourceRange TagDecl::getSourceRange() const { SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); - return SourceRange(TagKeywordLoc, E); + return SourceRange(getOuterLocStart(), E); } TagDecl* TagDecl::getCanonicalDecl() { -- 2.40.0