[clang] [clang][ASTImporter] delay import funtion return type (PR #74991)
Qizhi Hu via cfe-commits
cfe-commits at lists.llvm.org
Sun Dec 10 05:08:42 PST 2023
https://github.com/jcsxky created https://github.com/llvm/llvm-project/pull/74991
Import return type of a function would lead infinite recursion when `getAssociatedDecl()` returns itself in `ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr`. Delay import the return type whether it is auto would make sense. This patch try to fix [this issue](https://github.com/llvm/llvm-project/issues/74839)
>From 1527cc2046d4c9881ca536a2dbf59330d9291ba2 Mon Sep 17 00:00:00 2001
From: huqizhi <huqizhi at feysh.com>
Date: Sun, 10 Dec 2023 21:01:49 +0800
Subject: [PATCH] [clang][ASTImporter] delay import funtion return type
---
clang/lib/AST/ASTImporter.cpp | 8 ++------
clang/unittests/AST/ASTImporterTest.cpp | 20 ++++++++++++++++++++
2 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index f1f335118f37a4..335d10016f2060 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -3739,16 +3739,13 @@ ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
// do the same with TypeSourceInfo.
bool UsedDifferentProtoType = false;
if (const auto *FromFPT = FromTy->getAs<FunctionProtoType>()) {
- QualType FromReturnTy = FromFPT->getReturnType();
// Functions with auto return type may define a struct inside their body
// and the return type could refer to that struct.
// E.g.: auto foo() { struct X{}; return X(); }
// To avoid an infinite recursion when importing, create the FunctionDecl
// with a simplified return type.
- if (hasAutoReturnTypeDeclaredInside(D)) {
- FromReturnTy = Importer.getFromContext().VoidTy;
- UsedDifferentProtoType = true;
- }
+ QualType FromReturnTy = Importer.getFromContext().VoidTy;
+ UsedDifferentProtoType = true;
FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
// FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
// FunctionDecl that we are importing the FunctionProtoType for.
@@ -3759,7 +3756,6 @@ ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
FromEPI.ExceptionSpec.NoexceptExpr) {
FunctionProtoType::ExtProtoInfo DefaultEPI;
FromEPI = DefaultEPI;
- UsedDifferentProtoType = true;
}
FromTy = Importer.getFromContext().getFunctionType(
FromReturnTy, FromFPT->getParamTypes(), FromEPI);
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 4dd7510bf8ddf8..7ab9db4251e80b 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -9284,6 +9284,26 @@ TEST_P(ASTImporterOptionSpecificTestBase,
// EXPECT_EQ(ToF1Imported->getPreviousDecl(), ToF1);
}
+TEST_P(ASTImporterOptionSpecificTestBase, ImportFunctionAutoType) {
+ 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);
+}
+
INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,
DefaultTestValuesForRunOptions);
More information about the cfe-commits
mailing list