[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 01:05:56 PDT 2023


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

>From bbde18aade35c431e9c113a9d200102330aa3e3a 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 | 43 +++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

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..1ed0a84a580f686 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -4988,6 +4988,49 @@ 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