Skip to content

Commit 919df9d

Browse files
[clang][AST] Fix end location of DeclarationNameInfo on instantiated methods (#92654)
Fixes #71161 [D64087](https://reviews.llvm.org/D64087) updated some locations of the instantiated method but forgot `DNLoc`. `FunctionDecl::getNameInfo()` constructs a `DeclarationNameInfo` using `Decl::Loc` as the beginning of the declaration name, and `FunctionDecl::DNLoc` to compute the end of the declaration name. The former was updated, but the latter was not, so `DeclarationName::getSourceRange()` would return a range where the end of the declaration name could come before its beginning. Patch by Alejandro Alvarez Ayllon Co-authored-by: steakhal CPP-5166 Co-authored-by: Alejandro Alvarez Ayllon <[email protected]>
1 parent c0de13b commit 919df9d

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ Miscellaneous Bug Fixes
766766

767767
- Fixed an infinite recursion in ASTImporter, on return type declared inside
768768
body of C++11 lambda without trailing return (#GH68775).
769+
- Fixed declaration name source location of instantiated function definitions (GH71161).
769770

770771
Miscellaneous Clang Crashes Fixed
771772
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/AST/Decl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,6 +2188,8 @@ class FunctionDecl : public DeclaratorDecl,
21882188

21892189
void setRangeEnd(SourceLocation E) { EndRangeLoc = E; }
21902190

2191+
void setDeclarationNameLoc(DeclarationNameLoc L) { DNLoc = L; }
2192+
21912193
/// Returns the location of the ellipsis of a variadic function.
21922194
SourceLocation getEllipsisLoc() const {
21932195
const auto *FPT = getType()->getAs<FunctionProtoType>();

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5055,6 +5055,7 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
50555055
Function->setLocation(PatternDecl->getLocation());
50565056
Function->setInnerLocStart(PatternDecl->getInnerLocStart());
50575057
Function->setRangeEnd(PatternDecl->getEndLoc());
5058+
Function->setDeclarationNameLoc(PatternDecl->getNameInfo().getInfo());
50585059

50595060
EnterExpressionEvaluationContext EvalContext(
50605061
*this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);

clang/unittests/AST/DeclTest.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,3 +545,34 @@ TEST(Decl, TemplateArgumentDefaulted) {
545545
EXPECT_TRUE(ArgList.get(2).getIsDefaulted());
546546
EXPECT_TRUE(ArgList.get(3).getIsDefaulted());
547547
}
548+
549+
TEST(Decl, CXXDestructorDeclsShouldHaveWellFormedNameInfoRanges) {
550+
// GH71161
551+
llvm::Annotations Code(R"cpp(
552+
template <typename T> struct Resource {
553+
~Resource(); // 1
554+
};
555+
template <typename T>
556+
Resource<T>::~Resource() {} // 2,3
557+
558+
void instantiate_template() {
559+
Resource<int> x;
560+
}
561+
)cpp");
562+
563+
auto AST = tooling::buildASTFromCode(Code.code());
564+
ASTContext &Ctx = AST->getASTContext();
565+
566+
const auto &SM = Ctx.getSourceManager();
567+
auto GetNameInfoRange = [&SM](const BoundNodes &Match) {
568+
const auto *D = Match.getNodeAs<CXXDestructorDecl>("dtor");
569+
return D->getNameInfo().getSourceRange().printToString(SM);
570+
};
571+
572+
auto Matches = match(findAll(cxxDestructorDecl().bind("dtor")),
573+
*Ctx.getTranslationUnitDecl(), Ctx);
574+
ASSERT_EQ(Matches.size(), 3U);
575+
EXPECT_EQ(GetNameInfoRange(Matches[0]), "<input.cc:3:3, col:4>");
576+
EXPECT_EQ(GetNameInfoRange(Matches[1]), "<input.cc:6:14, col:15>");
577+
EXPECT_EQ(GetNameInfoRange(Matches[2]), "<input.cc:6:14, col:15>");
578+
}

0 commit comments

Comments
 (0)