[clang] [clang][ASTImporter] fix clash when import `VarTemplateDecl` in record (PR #67522)

Qizhi Hu via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 27 00:46:04 PDT 2023


https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/67522

>From 23c100253486cf6fb9834d995127e9325a45aec2 Mon Sep 17 00:00:00 2001
From: huqizhi <huqizhi at feysh.com>
Date: Wed, 27 Sep 2023 15:32:10 +0800
Subject: [PATCH] [clang][ASTImporter] fix clash when import `VarTemplateDecl`
 in record

---
 clang/lib/AST/ASTImporter.cpp           |  2 +
 clang/unittests/AST/ASTImporterTest.cpp | 51 +++++++++++++++++++++----
 2 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index c7c2aecc8b179a4..7b102ccc65d7cf2 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -6240,6 +6240,8 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
           // FIXME Check for ODR error if the two definitions have
           // different initializers?
           return Importer.MapImported(D, FoundDef);
+        if (FoundTemplate->getDeclContext()->isRecord())
+          return Importer.MapImported(D, FoundTemplate);
 
         FoundByLookup = FoundTemplate;
         break;
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index c90b5aaeb624306..e6675ccf61ebf10 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -4360,15 +4360,12 @@ TEST_P(ImportFriendClasses, SkipComparingFriendTemplateDepth) {
       R"(
       template <class T, T U>
       class A;
-
       template <class T, T U>
       class A {
       public:
         template <class P, P Q>
         friend class A;
-
         A(T x)  :x(x) {}
-
       private:
         T x;
       };
@@ -4382,19 +4379,15 @@ TEST_P(ImportFriendClasses, SkipComparingFriendTemplateDepth) {
       R"(
       template <class T, T U>
       class A;
-
       template <class T, T U>
       class A {
       public:
         template <class P, P Q>
         friend class A;
-
         A(T x) : x(x) {}
-
       private:
         T x;
       };
-
       A<int,3> a1(0);
       )",
       Lang_CXX11, "input1.cc");
@@ -4988,6 +4981,50 @@ TEST_P(ASTImporterOptionSpecificTestBase,
   }
 }
 
+TEST_P(ImportFriendClasses, RecordVarTemplateDecl) {
+  Decl *ToTU = getToTuDecl(
+      R"(
+      template <class T>
+      class A {
+      public:
+        template <class U>
+        struct B {
+          static U Value;
+        };
+
+        template <class W>
+        static constexpr bool X = !B<W>::Value;
+      };
+      )",
+      Lang_CXX14);
+
+  auto *Fwd = FirstDeclMatcher<VarTemplateDecl>().match(
+      ToTU, varTemplateDecl(hasName("X")));
+  Decl *FromTU = getTuDecl(
+      R"(
+      template <class T>
+      class A {
+      public:
+        template <class U>
+        struct B {
+          static U Value;
+        };
+
+        template <class W>
+        static constexpr bool X = !B<W>::Value;
+      };
+      )",
+      Lang_CXX14, "input1.cc");
+  auto *FromA = FirstDeclMatcher<VarTemplateDecl>().match(
+      FromTU, varTemplateDecl(hasName("X")));
+  auto *ToA = Import(FromA, Lang_CXX11);
+  EXPECT_TRUE(ToA);
+  EXPECT_EQ(Fwd->getTemplatedDecl(),
+            ToA->getTemplatedDecl());
+  EXPECT_EQ(Fwd->getTemplatedDecl()->getDefinition(),
+            ToA->getTemplatedDecl()->getDefinition());
+}
+
 TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) {
   constexpr auto Code =
       R"(



More information about the cfe-commits mailing list