[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 <alejandro.alvarez@sonarsource.com>
This commit is contained in:
Balazs Benics
2024-05-22 17:41:31 +02:00
committed by GitHub
parent c0de13b05f
commit 919df9d75a
4 changed files with 35 additions and 0 deletions

View File

@@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@@ -2188,6 +2188,8 @@ public:
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>();

View File

@@ -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);

View File

@@ -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>");
}