Skip to content

Commit ec00f15

Browse files
author
git apple-llvm automerger
committed
Merge commit '0ccdd4c28fca' from llvm.org/main into next
2 parents f309a25 + 0ccdd4c commit ec00f15

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,9 @@ Bug Fixes to AST Handling
559559
Miscellaneous Bug Fixes
560560
^^^^^^^^^^^^^^^^^^^^^^^
561561

562+
- Fixed an infinite recursion in ASTImporter, on return type declared inside
563+
body of C++11 lambda without trailing return (#GH68775).
564+
562565
Miscellaneous Clang Crashes Fixed
563566
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
564567

clang/lib/AST/ASTImporter.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ namespace clang {
695695
// Returns true if the given function has a placeholder return type and
696696
// that type is declared inside the body of the function.
697697
// E.g. auto f() { struct X{}; return X(); }
698-
bool hasAutoReturnTypeDeclaredInside(FunctionDecl *D);
698+
bool hasReturnTypeDeclaredInside(FunctionDecl *D);
699699
};
700700

701701
template <typename InContainerTy>
@@ -3666,15 +3666,28 @@ class IsTypeDeclaredInsideVisitor
36663666
};
36673667
} // namespace
36683668

3669-
/// This function checks if the function has 'auto' return type that contains
3669+
/// This function checks if the given function has a return type that contains
36703670
/// a reference (in any way) to a declaration inside the same function.
3671-
bool ASTNodeImporter::hasAutoReturnTypeDeclaredInside(FunctionDecl *D) {
3671+
bool ASTNodeImporter::hasReturnTypeDeclaredInside(FunctionDecl *D) {
36723672
QualType FromTy = D->getType();
36733673
const auto *FromFPT = FromTy->getAs<FunctionProtoType>();
36743674
assert(FromFPT && "Must be called on FunctionProtoType");
36753675

3676+
auto IsCXX11LambdaWithouTrailingReturn = [&]() {
3677+
if (Importer.FromContext.getLangOpts().CPlusPlus14) // C++14 or later
3678+
return false;
3679+
3680+
if (FromFPT->hasTrailingReturn())
3681+
return false;
3682+
3683+
if (const auto *MD = dyn_cast<CXXMethodDecl>(D))
3684+
return cast<CXXRecordDecl>(MD->getDeclContext())->isLambda();
3685+
3686+
return false;
3687+
};
3688+
36763689
QualType RetT = FromFPT->getReturnType();
3677-
if (isa<AutoType>(RetT.getTypePtr())) {
3690+
if (isa<AutoType>(RetT.getTypePtr()) || IsCXX11LambdaWithouTrailingReturn()) {
36783691
FunctionDecl *Def = D->getDefinition();
36793692
IsTypeDeclaredInsideVisitor Visitor(Def ? Def : D);
36803693
return Visitor.CheckType(RetT);
@@ -3830,7 +3843,7 @@ ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
38303843
// E.g.: auto foo() { struct X{}; return X(); }
38313844
// To avoid an infinite recursion when importing, create the FunctionDecl
38323845
// with a simplified return type.
3833-
if (hasAutoReturnTypeDeclaredInside(D)) {
3846+
if (hasReturnTypeDeclaredInside(D)) {
38343847
FromReturnTy = Importer.getFromContext().VoidTy;
38353848
UsedDifferentProtoType = true;
38363849
}

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6721,6 +6721,23 @@ TEST_P(ASTImporterOptionSpecificTestBase, LambdaInFunctionBody) {
67216721
EXPECT_FALSE(FromL->isDependentLambda());
67226722
}
67236723

6724+
TEST_P(ASTImporterOptionSpecificTestBase,
6725+
ReturnTypeDeclaredInsideOfCXX11LambdaWithoutTrailingReturn) {
6726+
Decl *From, *To;
6727+
std::tie(From, To) = getImportedDecl(
6728+
R"(
6729+
void foo() {
6730+
(void) []() {
6731+
struct X {};
6732+
return X();
6733+
};
6734+
}
6735+
)",
6736+
Lang_CXX11, "", Lang_CXX11, "foo"); // c++11 only
6737+
auto *ToLambda = FirstDeclMatcher<LambdaExpr>().match(To, lambdaExpr());
6738+
EXPECT_TRUE(ToLambda);
6739+
}
6740+
67246741
TEST_P(ASTImporterOptionSpecificTestBase, LambdaInFunctionParam) {
67256742
Decl *FromTU = getTuDecl(
67266743
R"(

0 commit comments

Comments
 (0)