Skip to content

Commit 4b0314d

Browse files
authored
[clang][ASTImporter] Improve import of friend class templates. (#74627)
A friend template that is in a dependent context is not linked into declaration chains (for example with the definition of the befriended template). This condition was not correctly handled by `ASTImporter`.
1 parent 1de3f46 commit 4b0314d

File tree

2 files changed

+173
-7
lines changed

2 files changed

+173
-7
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5929,15 +5929,22 @@ ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
59295929
if (ToD)
59305930
return ToD;
59315931

5932-
bool IsFriendTemplate = D->getFriendObjectKind() != Decl::FOK_None;
5933-
bool IsDependentContext = DC != LexicalDC ? LexicalDC->isDependentContext()
5934-
: DC->isDependentContext();
5935-
bool DependentFriend = IsFriendTemplate && IsDependentContext;
5932+
// Should check if a declaration is friend in a dependent context.
5933+
// Such templates are not linked together in a declaration chain.
5934+
// The ASTImporter strategy is to map existing forward declarations to
5935+
// imported ones only if strictly necessary, otherwise import these as new
5936+
// forward declarations. In case of the "dependent friend" declarations, new
5937+
// declarations are created, but not linked in a declaration chain.
5938+
auto IsDependentFriend = [](ClassTemplateDecl *TD) {
5939+
return TD->getFriendObjectKind() != Decl::FOK_None &&
5940+
TD->getLexicalDeclContext()->isDependentContext();
5941+
};
5942+
bool DependentFriend = IsDependentFriend(D);
59365943

59375944
ClassTemplateDecl *FoundByLookup = nullptr;
59385945

59395946
// We may already have a template of the same name; try to find and match it.
5940-
if (!DependentFriend && !DC->isFunctionOrMethod()) {
5947+
if (!DC->isFunctionOrMethod()) {
59415948
SmallVector<NamedDecl *, 4> ConflictingDecls;
59425949
auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
59435950
for (auto *FoundDecl : FoundDecls) {
@@ -5953,10 +5960,13 @@ ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
59535960

59545961
// FIXME: sufficient conditon for 'IgnoreTemplateParmDepth'?
59555962
bool IgnoreTemplateParmDepth =
5956-
FoundTemplate->getFriendObjectKind() != Decl::FOK_None &&
5957-
!D->specializations().empty();
5963+
(FoundTemplate->getFriendObjectKind() != Decl::FOK_None) !=
5964+
(D->getFriendObjectKind() != Decl::FOK_None);
59585965
if (IsStructuralMatch(D, FoundTemplate, /*Complain=*/true,
59595966
IgnoreTemplateParmDepth)) {
5967+
if (DependentFriend || IsDependentFriend(FoundTemplate))
5968+
continue;
5969+
59605970
ClassTemplateDecl *TemplateWithDef =
59615971
getTemplateDefinition(FoundTemplate);
59625972
if (D->isThisDeclarationADefinition() && TemplateWithDef)

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4540,6 +4540,162 @@ TEST_P(ImportFriendClasses, ImportOfClassDefinitionAndFwdFriendShouldBeLinked) {
45404540
EXPECT_EQ(ImportedFwd, ImportedDef->getPreviousDecl());
45414541
}
45424542

4543+
TEST_P(ImportFriendClasses,
4544+
ImportFriendTemplatesInDependentContext_DefToFriend) {
4545+
Decl *ToTU = getToTuDecl(
4546+
R"(
4547+
template<class T1>
4548+
struct X {
4549+
template<class T2>
4550+
friend struct Y;
4551+
};
4552+
)",
4553+
Lang_CXX03);
4554+
auto *ToYFriend = FirstDeclMatcher<ClassTemplateDecl>().match(
4555+
ToTU, classTemplateDecl(hasName("Y")));
4556+
Decl *FromTU = getTuDecl(
4557+
R"(
4558+
template<class T1>
4559+
struct Y {};
4560+
)",
4561+
Lang_CXX03, "input0.cc");
4562+
auto *FromYDef = FirstDeclMatcher<ClassTemplateDecl>().match(
4563+
FromTU, classTemplateDecl(hasName("Y")));
4564+
auto *ImportedYDef = Import(FromYDef, Lang_CXX03);
4565+
EXPECT_TRUE(ImportedYDef);
4566+
EXPECT_FALSE(ImportedYDef->getPreviousDecl());
4567+
EXPECT_NE(ImportedYDef, ToYFriend);
4568+
}
4569+
4570+
TEST_P(ImportFriendClasses,
4571+
ImportFriendTemplatesInDependentContext_DefToFriend_NE) {
4572+
getToTuDecl(
4573+
R"(
4574+
template<class T1>
4575+
struct X {
4576+
template<class T2>
4577+
friend struct Y;
4578+
};
4579+
)",
4580+
Lang_CXX03);
4581+
Decl *FromTU = getTuDecl(
4582+
R"(
4583+
template<class T1, class T2>
4584+
struct Y {};
4585+
)",
4586+
Lang_CXX03, "input0.cc");
4587+
auto *FromYDef = FirstDeclMatcher<ClassTemplateDecl>().match(
4588+
FromTU, classTemplateDecl(hasName("Y")));
4589+
auto *ImportedYDef = Import(FromYDef, Lang_CXX03);
4590+
EXPECT_FALSE(ImportedYDef);
4591+
}
4592+
4593+
TEST_P(ImportFriendClasses,
4594+
ImportFriendTemplatesInDependentContext_FriendToFriend) {
4595+
Decl *ToTU = getToTuDecl(
4596+
R"(
4597+
template<class T1>
4598+
struct X {
4599+
template<class T2>
4600+
friend struct Y;
4601+
};
4602+
)",
4603+
Lang_CXX03);
4604+
auto *ToYFriend = FirstDeclMatcher<ClassTemplateDecl>().match(
4605+
ToTU, classTemplateDecl(hasName("Y")));
4606+
Decl *FromTU = getTuDecl(
4607+
R"(
4608+
template<class T1>
4609+
struct X {
4610+
template<class T2>
4611+
friend struct Y;
4612+
};
4613+
)",
4614+
Lang_CXX03, "input0.cc");
4615+
auto *FromYFriend = FirstDeclMatcher<ClassTemplateDecl>().match(
4616+
FromTU, classTemplateDecl(hasName("Y")));
4617+
auto *ImportedYFriend = Import(FromYFriend, Lang_CXX03);
4618+
EXPECT_TRUE(ImportedYFriend);
4619+
EXPECT_FALSE(ImportedYFriend->getPreviousDecl());
4620+
EXPECT_NE(ImportedYFriend, ToYFriend);
4621+
}
4622+
4623+
TEST_P(ImportFriendClasses,
4624+
ImportFriendTemplatesInDependentContext_FriendToFriend_NE) {
4625+
getToTuDecl(
4626+
R"(
4627+
template<class T1>
4628+
struct X {
4629+
template<class T2>
4630+
friend struct Y;
4631+
};
4632+
)",
4633+
Lang_CXX03);
4634+
Decl *FromTU = getTuDecl(
4635+
R"(
4636+
template<class T1>
4637+
struct X {
4638+
template<class T2, class T3>
4639+
friend struct Y;
4640+
};
4641+
)",
4642+
Lang_CXX03, "input0.cc");
4643+
auto *FromYFriend = FirstDeclMatcher<ClassTemplateDecl>().match(
4644+
FromTU, classTemplateDecl(hasName("Y")));
4645+
auto *ImportedYFriend = Import(FromYFriend, Lang_CXX03);
4646+
EXPECT_FALSE(ImportedYFriend);
4647+
}
4648+
4649+
TEST_P(ImportFriendClasses,
4650+
ImportFriendTemplatesInDependentContext_FriendToDef) {
4651+
Decl *ToTU = getToTuDecl(
4652+
R"(
4653+
template<class T1>
4654+
struct Y {};
4655+
)",
4656+
Lang_CXX03);
4657+
auto *ToYDef = FirstDeclMatcher<ClassTemplateDecl>().match(
4658+
ToTU, classTemplateDecl(hasName("Y")));
4659+
Decl *FromTU = getTuDecl(
4660+
R"(
4661+
template<class T1>
4662+
struct X {
4663+
template<class T2>
4664+
friend struct Y;
4665+
};
4666+
)",
4667+
Lang_CXX03, "input0.cc");
4668+
auto *FromYFriend = FirstDeclMatcher<ClassTemplateDecl>().match(
4669+
FromTU, classTemplateDecl(hasName("Y")));
4670+
auto *ImportedYFriend = Import(FromYFriend, Lang_CXX03);
4671+
EXPECT_TRUE(ImportedYFriend);
4672+
EXPECT_FALSE(ImportedYFriend->getPreviousDecl());
4673+
EXPECT_NE(ImportedYFriend, ToYDef);
4674+
}
4675+
4676+
TEST_P(ImportFriendClasses,
4677+
ImportFriendTemplatesInDependentContext_FriendToDef_NE) {
4678+
getToTuDecl(
4679+
R"(
4680+
template<class T1>
4681+
struct Y {};
4682+
)",
4683+
Lang_CXX03);
4684+
Decl *FromTU = getTuDecl(
4685+
R"(
4686+
template<class T1>
4687+
struct X {
4688+
template<class T2, class T3>
4689+
friend struct Y;
4690+
};
4691+
)",
4692+
Lang_CXX03, "input0.cc");
4693+
auto *FromYFriend = FirstDeclMatcher<ClassTemplateDecl>().match(
4694+
FromTU, classTemplateDecl(hasName("Y")));
4695+
auto *ImportedYFriend = Import(FromYFriend, Lang_CXX03);
4696+
EXPECT_FALSE(ImportedYFriend);
4697+
}
4698+
45434699
TEST_P(ImportFriendClasses, ImportOfRepeatedFriendType) {
45444700
const char *Code =
45454701
R"(

0 commit comments

Comments
 (0)