[clang] [clang][ASTImporter] Allow import of similar friend template with different depth (PR #115734)
Balázs Kéri via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 11 08:13:18 PST 2024
https://github.com/balazske created https://github.com/llvm/llvm-project/pull/115734
The fix applies to a case that occurs when the AST contains a friend template that is contained within another template and this (outer) template has specialization. (See the added test code in the commit.)
The "friend" class can have a declaration before in the AST. The friend template has the same parent as this first declaration (and is otherwise similar), but has different template depth. At AST import these declarations can be compared and import should not fail with name conflict in this case.
>From eca94b1ff721d8ec857a5fdee8b358ce22d210aa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <balazs.keri at ericsson.com>
Date: Mon, 11 Nov 2024 16:53:59 +0100
Subject: [PATCH] [clang][ASTImporter] Allow import of similar friend template
with different depth
The fix applies to a case that occurs when the AST contains a friend template
that is contained within another template and this (outer) template has specialization.
(See the added test code in the commit.)
The "friend" class can have a declaration before in the AST. The friend template
has the same parent as this first declaration (and is otherwise similar),
but has different template depth. At AST import these declarations can be compared
and import should not fail with name conflict in this case.
---
clang/lib/AST/ASTImporter.cpp | 16 +++-
clang/unittests/AST/ASTImporterTest.cpp | 105 ++++++++++++++++++++++++
2 files changed, 119 insertions(+), 2 deletions(-)
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index 35aba41f0052af..1f4ae74f3c3659 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -6095,8 +6095,7 @@ ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Decl::IDNS_TagFriend))
continue;
- Decl *Found = FoundDecl;
- auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found);
+ auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(FoundDecl);
if (FoundTemplate) {
if (!hasSameVisibilityContextAndLinkage(FoundTemplate, D))
continue;
@@ -6120,6 +6119,19 @@ ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
// see ASTTests test ImportExistingFriendClassTemplateDef.
continue;
}
+ // When importing a friend, it is possible that multiple declarations
+ // with same name can co-exist in specific cases (if a template contains
+ // a friend template and has a specialization). For this case the
+ // declarations should match, except that the "template depth" is
+ // different. No linking of previous declaration is needed in this case.
+ // FIXME: This condition may need refinement.
+ if (D->getFriendObjectKind() != Decl::FOK_None &&
+ FoundTemplate->getFriendObjectKind() != Decl::FOK_None &&
+ D->getFriendObjectKind() != FoundTemplate->getFriendObjectKind() &&
+ IsStructuralMatch(D, FoundTemplate, /*Complain=*/false,
+ /*IgnoreTemplateParmDepth=*/true))
+ continue;
+
ConflictingDecls.push_back(FoundDecl);
}
}
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index bf7313f882e455..49aef7441727c9 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -10181,6 +10181,111 @@ TEST_P(ImportTemplateParmDeclDefaultValue,
FromD, FromDInherited);
}
+TEST_P(ASTImporterOptionSpecificTestBase,
+ ExistingUndeclaredImportDeclaredFriend) {
+ Decl *ToTU = getToTuDecl(
+ R"(
+ template <class A, A>
+ struct foo;
+
+ template <class A>
+ struct X {
+ template <class A1, A1>
+ friend struct foo;
+ };
+ )",
+ Lang_CXX11);
+ Decl *FromTU = getTuDecl(
+ R"(
+ template <class A, A>
+ struct foo;
+
+ template <class A>
+ struct X {
+ template <class A1, A1>
+ friend struct foo1;
+ };
+
+ X<int> x;
+ )",
+ Lang_CXX11);
+
+ auto *ToFr1 = FirstDeclMatcher<FriendDecl>().match(ToTU, friendDecl());
+ auto *ToFrD1 = ToFr1->getFriendDecl();
+
+ auto *FromFr1 = FirstDeclMatcher<FriendDecl>().match(FromTU, friendDecl());
+ auto *FromFr2 = LastDeclMatcher<FriendDecl>().match(FromTU, friendDecl());
+
+ auto *FromFrD1 = FromFr1->getFriendDecl();
+ auto *FromFrD2 = FromFr2->getFriendDecl();
+
+ auto *Ctx1 = cast<Decl>(FromFrD1->getDeclContext());
+ auto *Ctx2 = cast<Decl>(FromFrD2->getDeclContext());
+
+ ASSERT_EQ(Ctx1, Ctx2);
+ ASSERT_EQ(ToFrD1->getTemplateDepth(), 1u);
+ ASSERT_EQ(FromFrD2->getTemplateDepth(), 0u);
+ ASSERT_EQ(ToFrD1->getFriendObjectKind(), Decl::FOK_Undeclared);
+ ASSERT_EQ(FromFrD2->getFriendObjectKind(), Decl::FOK_Declared);
+
+ auto *ToFr2Imp = Import(FromFr2, Lang_CXX11);
+
+ EXPECT_TRUE(ToFr2Imp);
+}
+
+TEST_P(ASTImporterOptionSpecificTestBase,
+ ExistingDeclaredImportUndeclaredFriend) {
+ Decl *ToTU = getToTuDecl(
+ R"(
+ template <class A, A>
+ struct foo;
+
+ template <class A>
+ struct X {
+ template <class A1, A1>
+ friend struct foo;
+ };
+
+ X<int> x;
+ )",
+ Lang_CXX11);
+ Decl *FromTU = getTuDecl(
+ R"(
+ template <class A, A>
+ struct foo;
+
+ template <class A>
+ struct X {
+ template <class A1, A1>
+ friend struct foo;
+ };
+ )",
+ Lang_CXX11);
+
+ auto *ToFr1 = FirstDeclMatcher<FriendDecl>().match(ToTU, friendDecl());
+ auto *ToFr2 = LastDeclMatcher<FriendDecl>().match(ToTU, friendDecl());
+
+ auto *ToFrD1 = ToFr1->getFriendDecl();
+ auto *ToFrD2 = ToFr2->getFriendDecl();
+
+ auto *FromFr1 = FirstDeclMatcher<FriendDecl>().match(FromTU, friendDecl());
+ auto *FromFrD1 = FromFr1->getFriendDecl();
+
+ auto *Ctx1 = cast<Decl>(ToFrD1->getDeclContext());
+ auto *Ctx2 = cast<Decl>(ToFrD2->getDeclContext());
+
+ ASSERT_EQ(Ctx1, Ctx2);
+ ASSERT_EQ(FromFrD1->getTemplateDepth(), 1u);
+ ASSERT_EQ(ToFrD2->getTemplateDepth(), 0u);
+ ASSERT_EQ(FromFrD1->getFriendObjectKind(), Decl::FOK_Undeclared);
+ ASSERT_EQ(ToFrD2->getFriendObjectKind(), Decl::FOK_Declared);
+
+ auto *ToFr1Imp = Import(FromFr1, Lang_CXX11);
+
+ EXPECT_TRUE(ToFr1Imp);
+ EXPECT_EQ(ToFr1Imp, ToFr1);
+}
+
INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,
DefaultTestValuesForRunOptions);
More information about the cfe-commits
mailing list