[clang] [clang][ASTImporter] set nonnull type for var specialized from lambda template (PR #105492)

Ding Fei via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 21 06:56:38 PDT 2024


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

>From 2ac3aa1b85d727fd15d49fa1649147f2229576cb Mon Sep 17 00:00:00 2001
From: dingfei <fding at feysh.com>
Date: Wed, 21 Aug 2024 17:52:04 +0800
Subject: [PATCH 1/3] [clang][ASTImporter] set nonnull type for var specialized
 from lambda template

There's circular deps here when importing btw VarTemplateSpecializationDecl (m)
and its type.

VarDecl is created with a null type QualType(), and the importing of its type
will leads to linkage computation (for the templated lambda) which depends on
the var's type (not yet imported).

This fix uses a non-null type instead of a null one. But this seems
still cannot ensure the computation is correct.
---
 clang/lib/AST/ASTImporter.cpp           |  6 ++++--
 clang/unittests/AST/ASTImporterTest.cpp | 13 +++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index 103235547f482e..cc1bd01f15bef6 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -6582,6 +6582,8 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
       return std::move(Err);
   }
 
+  QualType ToTmpTy = Importer.getToContext().IntTy;
+  ToTmpTy = ToTmpTy.withCVRQualifiers(D->getType().getCVRQualifiers());
   using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
   // Create a new specialization.
   if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
@@ -6592,7 +6594,7 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
     PartVarSpecDecl *ToPartial;
     if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
                                 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
-                                VarTemplate, QualType(), nullptr,
+                                VarTemplate, ToTmpTy, nullptr,
                                 D->getStorageClass(), TemplateArgs))
       return ToPartial;
 
@@ -6613,7 +6615,7 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
   } else { // Full specialization
     if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
                                 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
-                                QualType(), nullptr, D->getStorageClass(),
+                                ToTmpTy, nullptr, D->getStorageClass(),
                                 TemplateArgs))
       return D2;
   }
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 57242ff49fe3b8..06996268d1fb94 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -9919,6 +9919,19 @@ TEST_P(ImportTemplateParmDeclDefaultValue, ImportExistingVarTemplate) {
   testImport(FromLastD);
 }
 
+TEST_P(ASTImporterOptionSpecificTestBase, VarTemplatedLambdaInGlobalScope) {
+  Decl *FromTU = getTuDecl(
+      R"(
+        namespace { template <typename> auto m = [] {}; }
+        void bar() { auto n = m<int>; }
+      )",
+      Lang_CXX14, "input0.cc");
+  FunctionDecl *FromF = FirstDeclMatcher<FunctionDecl>().match(
+      FromTU, functionDecl(hasName("bar")));
+  FunctionDecl *ToF = Import(FromF, Lang_CXX14);
+  EXPECT_TRUE(ToF);
+}
+
 INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,
                          DefaultTestValuesForRunOptions);
 

>From 81efe9531fb901b18191c850e838b1825562d650 Mon Sep 17 00:00:00 2001
From: dingfei <fding at feysh.com>
Date: Wed, 21 Aug 2024 21:52:04 +0800
Subject: [PATCH 2/3] fix clang-format check failure

---
 clang/lib/AST/ASTImporter.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index cc1bd01f15bef6..ad7cd7eee2a4e7 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -6613,10 +6613,9 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
     // to adopt template parameters.
     // updateLookupTableForTemplateParameters(**ToTPListOrErr);
   } else { // Full specialization
-    if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
-                                *BeginLocOrErr, *IdLocOrErr, VarTemplate,
-                                ToTmpTy, nullptr, D->getStorageClass(),
-                                TemplateArgs))
+    if (GetImportedOrCreateDecl(
+            D2, D, Importer.getToContext(), DC, *BeginLocOrErr, *IdLocOrErr,
+            VarTemplate, ToTmpTy, nullptr, D->getStorageClass(), TemplateArgs))
       return D2;
   }
 

>From 8f1fc146b7d2a6d6accfdfeae53f13c6cdb5413b Mon Sep 17 00:00:00 2001
From: dingfei <fding at feysh.com>
Date: Wed, 21 Aug 2024 21:56:12 +0800
Subject: [PATCH 3/3] rename testcase

---
 clang/unittests/AST/ASTImporterTest.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 06996268d1fb94..827e73e90d2e70 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -9919,7 +9919,7 @@ TEST_P(ImportTemplateParmDeclDefaultValue, ImportExistingVarTemplate) {
   testImport(FromLastD);
 }
 
-TEST_P(ASTImporterOptionSpecificTestBase, VarTemplatedLambdaInGlobalScope) {
+TEST_P(ASTImporterOptionSpecificTestBase, VarTemplatedLambdaWithCircularDeps) {
   Decl *FromTU = getTuDecl(
       R"(
         namespace { template <typename> auto m = [] {}; }



More information about the cfe-commits mailing list