Skip to content

[clang][ASTImporter] Allow import of similar friend template with different depth #115734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like the logic on 6103 and this here could be merged together? Should IgnoreTemplateParmDepth on 6104 just be changed to also be set if the FriendObjectKind matches? Then I think everything should work out as expected

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not want to modify an already working code part when it is not obvious that the change has no effect on the old behavior. In the new case we already know that there is no structural match and depth was not ignored, then we can check if these match with ignored depth (and this second structural equivalence check looks required to not import non-matching friends). We do not need any of the actions in the if branch at line 6108, only a continue (and it is not "dependent friend").


ConflictingDecls.push_back(FoundDecl);
}
}
Expand Down
105 changes: 105 additions & 0 deletions clang/unittests/AST/ASTImporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 foo;
};

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);

Expand Down
Loading