[clang] 5f2157f - [clang][ASTImporter] Fix friend class template import within dependent context
via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 1 20:21:57 PDT 2023
Author: dingfei
Date: 2023-08-02T11:21:09+08:00
New Revision: 5f2157f8fcaede27ccf4ab215fcb1a4725a36b2a
URL: https://github.com/llvm/llvm-project/commit/5f2157f8fcaede27ccf4ab215fcb1a4725a36b2a
DIFF: https://github.com/llvm/llvm-project/commit/5f2157f8fcaede27ccf4ab215fcb1a4725a36b2a.diff
LOG: [clang][ASTImporter] Fix friend class template import within dependent context
For friend class template within dependent context:
1. Should not do structure matching checking. This fixes importing failure of
template with non-type parm;
2. Should not be added into redecls chain.
See Sema::CheckClassTemplate().
Fixes https://github.com/llvm/llvm-project/issues/64169.
Reviewed By: aaron.ballman, balazske, shafik
Differential Revision: https://reviews.llvm.org/D155661
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/AST/ASTImporter.cpp
clang/unittests/AST/ASTImporterTest.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a6b70042cc50f9..a536a9fe2363cf 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -131,6 +131,8 @@ Bug Fixes to C++ Support
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
+- Fixed an import failure of recursive friend class template.
+ `Issue 64169 <https://github.com/llvm/llvm-project/issues/64169>`_
Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index eb8e28ccd3e622..1ad7067f8ca9c1 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -2857,9 +2857,13 @@ ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
} else if (Importer.getToContext().getLangOpts().CPlusPlus)
IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend;
+ bool IsDependentContext = DC != LexicalDC ? LexicalDC->isDependentContext()
+ : DC->isDependentContext();
+ bool DependentFriend = IsFriendTemplate && IsDependentContext;
+
// We may already have a record of the same name; try to find and match it.
RecordDecl *PrevDecl = nullptr;
- if (!DC->isFunctionOrMethod() && !D->isLambda()) {
+ if (!DependentFriend && !DC->isFunctionOrMethod() && !D->isLambda()) {
SmallVector<NamedDecl *, 4> ConflictingDecls;
auto FoundDecls =
Importer.findDeclsInToCtx(DC, SearchName);
@@ -5796,10 +5800,15 @@ ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
if (ToD)
return ToD;
+ bool IsFriendTemplate = D->getFriendObjectKind() != Decl::FOK_None;
+ bool IsDependentContext = DC != LexicalDC ? LexicalDC->isDependentContext()
+ : DC->isDependentContext();
+ bool DependentFriend = IsFriendTemplate && IsDependentContext;
+
ClassTemplateDecl *FoundByLookup = nullptr;
// We may already have a template of the same name; try to find and match it.
- if (!DC->isFunctionOrMethod()) {
+ if (!DependentFriend && !DC->isFunctionOrMethod()) {
SmallVector<NamedDecl *, 4> ConflictingDecls;
auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
for (auto *FoundDecl : FoundDecls) {
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index c6ae2693930409..544550f05fd747 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -3968,8 +3968,58 @@ TEST_P(ImportClasses, ImportNestedPrototypeThenDefinition) {
EXPECT_EQ(ToDef->getPreviousDecl(), ToProto);
}
-
-struct ImportFriendClasses : ASTImporterOptionSpecificTestBase {};
+struct ImportFriendClasses : ASTImporterOptionSpecificTestBase {
+ void testRecursiveFriendClassTemplate(Decl *FromTu) {
+ auto *FromD = FirstDeclMatcher<ClassTemplateDecl>().match(
+ FromTu, classTemplateDecl());
+
+ auto Pattern = classTemplateDecl(
+ has(cxxRecordDecl(has(friendDecl(has(classTemplateDecl()))))));
+ ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromD, Pattern));
+
+ auto *FromFriend =
+ FirstDeclMatcher<FriendDecl>().match(FromD, friendDecl());
+ auto *FromRecordOfFriend =
+ cast<ClassTemplateDecl>(FromFriend->getFriendDecl())
+ ->getTemplatedDecl();
+ EXPECT_NE(FromRecordOfFriend, FromD->getTemplatedDecl());
+ EXPECT_TRUE(FromRecordOfFriend->getPreviousDecl() == nullptr);
+
+ auto *FromDC = FromRecordOfFriend->getDeclContext();
+ auto *FromLexicalDC = FromRecordOfFriend->getLexicalDeclContext();
+ ASSERT_EQ(FromDC, cast<DeclContext>(FromTu));
+ ASSERT_EQ(FromLexicalDC, cast<DeclContext>(FromD->getTemplatedDecl()));
+
+ ASSERT_FALSE(FromDC->containsDecl(FromRecordOfFriend));
+ ASSERT_FALSE(FromLexicalDC->containsDecl(FromRecordOfFriend));
+ ASSERT_FALSE(cast<RecordDecl>(FromRecordOfFriend)
+ ->getLookupParent()
+ ->lookup(FromRecordOfFriend->getDeclName())
+ .empty());
+
+ auto *ToD = Import(FromD, Lang_CXX03);
+ EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToD, Pattern));
+
+ auto *ToFriend = FirstDeclMatcher<FriendDecl>().match(ToD, friendDecl());
+ auto *ToRecordOfFriend =
+ cast<ClassTemplateDecl>(ToFriend->getFriendDecl())->getTemplatedDecl();
+
+ EXPECT_NE(ToRecordOfFriend, ToD->getTemplatedDecl());
+ EXPECT_TRUE(ToRecordOfFriend->getPreviousDecl() == nullptr);
+
+ auto *ToDC = ToRecordOfFriend->getDeclContext();
+ auto *ToLexicalDC = ToRecordOfFriend->getLexicalDeclContext();
+ ASSERT_EQ(ToDC, cast<DeclContext>(ToD->getTranslationUnitDecl()));
+ ASSERT_EQ(ToLexicalDC, cast<DeclContext>(ToD->getTemplatedDecl()));
+
+ ASSERT_FALSE(ToDC->containsDecl(ToRecordOfFriend));
+ ASSERT_FALSE(ToLexicalDC->containsDecl(ToRecordOfFriend));
+ ASSERT_FALSE(cast<RecordDecl>(ToRecordOfFriend)
+ ->getLookupParent()
+ ->lookup(ToRecordOfFriend->getDeclName())
+ .empty());
+ }
+};
TEST_P(ImportFriendClasses, ImportOfFriendRecordDoesNotMergeDefinition) {
Decl *FromTU = getTuDecl(
@@ -4074,20 +4124,19 @@ TEST_P(ImportFriendClasses, ImportOfRecursiveFriendClassTemplate) {
)",
Lang_CXX03, "input.cc");
- auto *FromD =
- FirstDeclMatcher<ClassTemplateDecl>().match(FromTu, classTemplateDecl());
- auto *ToD = Import(FromD, Lang_CXX03);
-
- auto Pattern = classTemplateDecl(
- has(cxxRecordDecl(has(friendDecl(has(classTemplateDecl()))))));
- ASSERT_TRUE(MatchVerifier<Decl>{}.match(FromD, Pattern));
- EXPECT_TRUE(MatchVerifier<Decl>{}.match(ToD, Pattern));
+ testRecursiveFriendClassTemplate(FromTu);
+}
- auto *Class =
- FirstDeclMatcher<ClassTemplateDecl>().match(ToD, classTemplateDecl());
- auto *Friend = FirstDeclMatcher<FriendDecl>().match(ToD, friendDecl());
- EXPECT_NE(Friend->getFriendDecl(), Class);
- EXPECT_EQ(Friend->getFriendDecl()->getPreviousDecl(), Class);
+TEST_P(ImportFriendClasses,
+ ImportOfRecursiveFriendClassTemplateWithNonTypeParm) {
+ Decl *FromTu = getTuDecl(
+ R"(
+ template<class A1, A1 A> class declToImport {
+ template<class B1, B1> friend class declToImport;
+ };
+ )",
+ Lang_CXX03, "input.cc");
+ testRecursiveFriendClassTemplate(FromTu);
}
TEST_P(ImportFriendClasses, ProperPrevDeclForClassTemplateDecls) {
More information about the cfe-commits
mailing list