Skip to content

Commit

Permalink
[clang][ASTImporter] add processing of SubstNonTypeTemplateParmExpr i…
Browse files Browse the repository at this point in the history
…n isAncestorDeclContextOf (#74991)

Lack of processing of `SubstNonTypeTemplateParmExpr` in
`isAncestorDeclContextOf` would make `hasAutoReturnTypeDeclaredInside`
returns false and lead to infinite recursion. This patch adds the
processor and try to fix [this
issue](#74839)

Co-authored-by: huqizhi <836744285@qq.com>
  • Loading branch information
jcsxky committed Dec 21, 2023
1 parent 9b561ca commit 72e8ab7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3418,10 +3418,16 @@ static bool isAncestorDeclContextOf(const DeclContext *DC, const Stmt *S) {
while (!ToProcess.empty()) {
const Stmt *CurrentS = ToProcess.pop_back_val();
ToProcess.append(CurrentS->child_begin(), CurrentS->child_end());
if (const auto *DeclRef = dyn_cast<DeclRefExpr>(CurrentS))
if (const auto *DeclRef = dyn_cast<DeclRefExpr>(CurrentS)) {
if (const Decl *D = DeclRef->getDecl())
if (isAncestorDeclContextOf(DC, D))
return true;
} else if (const auto *E =
dyn_cast_or_null<SubstNonTypeTemplateParmExpr>(CurrentS)) {
if (const Decl *D = E->getAssociatedDecl())
if (isAncestorDeclContextOf(DC, D))
return true;
}
}
return false;
}
Expand Down
20 changes: 20 additions & 0 deletions clang/unittests/AST/ASTImporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7250,6 +7250,26 @@ TEST_P(ImportAutoFunctions, ReturnWithAutoTemplateType) {
Lang_CXX14, /*FindLast=*/true);
}

TEST_P(ImportAutoFunctions, ReturnWithSubstNonTypeTemplateParmExpr) {
const char *Code =
R"(
template<int>
struct array {};
template <int N>
auto foo() { return array<N>(); }
void bar() { foo<0>(); }
)";
Decl *FromTU = getTuDecl(Code, Lang_CXX17);

auto *FromBar = FirstDeclMatcher<FunctionDecl>().match(
FromTU, functionDecl(hasName("bar")));

auto *ToBar = Import(FromBar, Lang_CXX17);
EXPECT_TRUE(ToBar);
}

struct ImportSourceLocations : ASTImporterOptionSpecificTestBase {};

TEST_P(ImportSourceLocations, PreserveFileIDTreeStructure) {
Expand Down

0 comments on commit 72e8ab7

Please sign in to comment.