Skip to content

[clang][AST] Fix end location of DeclarationNameInfo on instantiated methods #92654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ Miscellaneous Bug Fixes

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

Miscellaneous Clang Crashes Fixed
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2188,6 +2188,8 @@ class FunctionDecl : public DeclaratorDecl,

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

void setDeclarationNameLoc(DeclarationNameLoc L) { DNLoc = L; }

/// Returns the location of the ellipsis of a variadic function.
SourceLocation getEllipsisLoc() const {
const auto *FPT = getType()->getAs<FunctionProtoType>();
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5055,6 +5055,7 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Function->setLocation(PatternDecl->getLocation());
Function->setInnerLocStart(PatternDecl->getInnerLocStart());
Function->setRangeEnd(PatternDecl->getEndLoc());
Function->setDeclarationNameLoc(PatternDecl->getNameInfo().getInfo());

EnterExpressionEvaluationContext EvalContext(
*this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
Expand Down
31 changes: 31 additions & 0 deletions clang/unittests/AST/DeclTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,34 @@ TEST(Decl, TemplateArgumentDefaulted) {
EXPECT_TRUE(ArgList.get(2).getIsDefaulted());
EXPECT_TRUE(ArgList.get(3).getIsDefaulted());
}

TEST(Decl, CXXDestructorDeclsShouldHaveWellFormedNameInfoRanges) {
// GH71161
llvm::Annotations Code(R"cpp(
template <typename T> struct Resource {
~Resource(); // 1
};
template <typename T>
Resource<T>::~Resource() {} // 2,3

void instantiate_template() {
Resource<int> x;
}
)cpp");

auto AST = tooling::buildASTFromCode(Code.code());
ASTContext &Ctx = AST->getASTContext();

const auto &SM = Ctx.getSourceManager();
auto GetNameInfoRange = [&SM](const BoundNodes &Match) {
const auto *D = Match.getNodeAs<CXXDestructorDecl>("dtor");
return D->getNameInfo().getSourceRange().printToString(SM);
};

auto Matches = match(findAll(cxxDestructorDecl().bind("dtor")),
*Ctx.getTranslationUnitDecl(), Ctx);
ASSERT_EQ(Matches.size(), 3U);
EXPECT_EQ(GetNameInfoRange(Matches[0]), "<input.cc:3:3, col:4>");
EXPECT_EQ(GetNameInfoRange(Matches[1]), "<input.cc:6:14, col:15>");
EXPECT_EQ(GetNameInfoRange(Matches[2]), "<input.cc:6:14, col:15>");
}
Loading