https://github.com/danix800 updated 
https://github.com/llvm/llvm-project/pull/101031

>From 336bebf7d20feb7a6fab5ad038239b058f62bdfb Mon Sep 17 00:00:00 2001
From: dingfei <fd...@feysh.com>
Date: Mon, 29 Jul 2024 23:18:20 +0800
Subject: [PATCH 1/2] [clang][ASTImporter] Fix typos in trailing return testing
 on lambda proto

The test should be true on function with trailing return.
This fixes crashes (infinite recursion) on lambda expr without parameters
(without parentheses).
---
 clang/lib/AST/ASTImporter.cpp           |  8 +++-----
 clang/unittests/AST/ASTImporterTest.cpp | 17 +++++++++++++++++
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index da1981d8dd05f..83aa017a00a7f 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -3665,13 +3665,10 @@ bool 
ASTNodeImporter::hasReturnTypeDeclaredInside(FunctionDecl *D) {
   const auto *FromFPT = FromTy->getAs<FunctionProtoType>();
   assert(FromFPT && "Must be called on FunctionProtoType");
 
-  auto IsCXX11LambdaWithouTrailingReturn = [&]() {
+  auto IsCXX11LambdaWithoutTrailingReturn = [&]() {
     if (Importer.FromContext.getLangOpts().CPlusPlus14) // C++14 or later
       return false;
 
-    if (FromFPT->hasTrailingReturn())
-      return false;
-
     if (const auto *MD = dyn_cast<CXXMethodDecl>(D))
       return cast<CXXRecordDecl>(MD->getDeclContext())->isLambda();
 
@@ -3679,7 +3676,8 @@ bool 
ASTNodeImporter::hasReturnTypeDeclaredInside(FunctionDecl *D) {
   };
 
   QualType RetT = FromFPT->getReturnType();
-  if (isa<AutoType>(RetT.getTypePtr()) || IsCXX11LambdaWithouTrailingReturn()) 
{
+  if (FromFPT->hasTrailingReturn() || isa<AutoType>(RetT.getTypePtr()) ||
+      IsCXX11LambdaWithoutTrailingReturn()) {
     FunctionDecl *Def = D->getDefinition();
     IsTypeDeclaredInsideVisitor Visitor(Def ? Def : D);
     return Visitor.CheckType(RetT);
diff --git a/clang/unittests/AST/ASTImporterTest.cpp 
b/clang/unittests/AST/ASTImporterTest.cpp
index 57c5f79651824..d275a08dca895 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -6738,6 +6738,23 @@ TEST_P(ASTImporterOptionSpecificTestBase,
   EXPECT_TRUE(ToLambda);
 }
 
+TEST_P(ASTImporterOptionSpecificTestBase,
+       ReturnTypeDeclaredInsideOfCXX11LambdaWithTrailingReturn) {
+  Decl *From, *To;
+  std::tie(From, To) = getImportedDecl(
+      R"(
+      void foo() {
+        (void) [] {
+          struct X {};
+          return X();
+        };
+      }
+      )",
+      Lang_CXX11, "", Lang_CXX11, "foo"); // c++11 only
+  auto *ToLambda = FirstDeclMatcher<LambdaExpr>().match(To, lambdaExpr());
+  EXPECT_TRUE(ToLambda);
+}
+
 TEST_P(ASTImporterOptionSpecificTestBase, LambdaInFunctionParam) {
   Decl *FromTU = getTuDecl(
       R"(

>From 25101a790ac72df27e3ea766ee7db47abe0e3135 Mon Sep 17 00:00:00 2001
From: dingfei <fd...@feysh.com>
Date: Wed, 31 Jul 2024 10:29:12 +0800
Subject: [PATCH 2/2] remove trailing return test

Lambdas without trailing return could also have return type defined
inside its body.
---
 clang/lib/AST/ASTImporter.cpp | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index 83aa017a00a7f..b1c7c400861bc 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -3665,7 +3665,7 @@ bool 
ASTNodeImporter::hasReturnTypeDeclaredInside(FunctionDecl *D) {
   const auto *FromFPT = FromTy->getAs<FunctionProtoType>();
   assert(FromFPT && "Must be called on FunctionProtoType");
 
-  auto IsCXX11LambdaWithoutTrailingReturn = [&]() {
+  auto IsCXX11Lambda = [&]() {
     if (Importer.FromContext.getLangOpts().CPlusPlus14) // C++14 or later
       return false;
 
@@ -3676,8 +3676,7 @@ bool 
ASTNodeImporter::hasReturnTypeDeclaredInside(FunctionDecl *D) {
   };
 
   QualType RetT = FromFPT->getReturnType();
-  if (FromFPT->hasTrailingReturn() || isa<AutoType>(RetT.getTypePtr()) ||
-      IsCXX11LambdaWithoutTrailingReturn()) {
+  if (isa<AutoType>(RetT.getTypePtr()) || IsCXX11Lambda()) {
     FunctionDecl *Def = D->getDefinition();
     IsTypeDeclaredInsideVisitor Visitor(Def ? Def : D);
     return Visitor.CheckType(RetT);

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to