balazske created this revision. Herald added subscribers: steakhal, martong, gamesh411, Szelethus, dkrupp. Herald added a reviewer: a.sidorin. Herald added a reviewer: shafik. Herald added a project: All. balazske requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
ASTImporterLookupTable did not handle correctly friend declarations where the friend type is an UsingType (type of a declaration that comes from an using-declaration). The problem is fixed by handling it in the same way as a friend with TypedefType. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D142607 Files: clang/lib/AST/ASTImporterLookupTable.cpp clang/unittests/AST/ASTImporterTest.cpp Index: clang/unittests/AST/ASTImporterTest.cpp =================================================================== --- clang/unittests/AST/ASTImporterTest.cpp +++ clang/unittests/AST/ASTImporterTest.cpp @@ -5160,6 +5160,39 @@ EXPECT_EQ(Res.count(Alias), 1u); } +TEST_P(ASTImporterLookupTableTest, + LookupFindsFriendClassDeclWithUsingTypeDoesNotAssert) { + TranslationUnitDecl *ToTU = getToTuDecl( + R"( + namespace a { + namespace b { class InnerClass; } + using b::InnerClass; + } + class B { + friend a::InnerClass; + }; + )", + Lang_CXX11); + + // ASTImporterLookupTable constructor handles friend with using-type without + // asserts. + ASTImporterLookupTable LT(*ToTU); + + auto *Using = FirstDeclMatcher<UsingDecl>().match( + ToTU, usingDecl(hasName("InnerClass"))); + DeclarationName Name = Using->getDeclName(); + auto Res = LT.lookup(ToTU, Name); + EXPECT_EQ(Res.size(), 0u); + auto *NsA = FirstDeclMatcher<NamespaceDecl>().match( + ToTU, namespaceDecl(hasName("a"))); + auto *RecordB = FirstDeclMatcher<CXXRecordDecl>().match( + ToTU, cxxRecordDecl(hasName("B"))); + auto Res1 = LT.lookup(NsA, Name); + EXPECT_EQ(Res1.count(Using), 1u); + auto Res2 = LT.lookup(RecordB, Name); + EXPECT_EQ(Res2.size(), 0u); +} + TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassTemplateDecl) { TranslationUnitDecl *ToTU = getToTuDecl( R"( Index: clang/lib/AST/ASTImporterLookupTable.cpp =================================================================== --- clang/lib/AST/ASTImporterLookupTable.cpp +++ clang/lib/AST/ASTImporterLookupTable.cpp @@ -67,6 +67,8 @@ } else if (isa<TypedefType>(Ty)) { // We do not put friend typedefs to the lookup table because // ASTImporter does not organize typedefs into redecl chains. + } else if (isa<UsingType>(Ty)) { + // Similar to TypedefType, not putting into lookup table. } else { llvm_unreachable("Unhandled type of friend class"); }
Index: clang/unittests/AST/ASTImporterTest.cpp =================================================================== --- clang/unittests/AST/ASTImporterTest.cpp +++ clang/unittests/AST/ASTImporterTest.cpp @@ -5160,6 +5160,39 @@ EXPECT_EQ(Res.count(Alias), 1u); } +TEST_P(ASTImporterLookupTableTest, + LookupFindsFriendClassDeclWithUsingTypeDoesNotAssert) { + TranslationUnitDecl *ToTU = getToTuDecl( + R"( + namespace a { + namespace b { class InnerClass; } + using b::InnerClass; + } + class B { + friend a::InnerClass; + }; + )", + Lang_CXX11); + + // ASTImporterLookupTable constructor handles friend with using-type without + // asserts. + ASTImporterLookupTable LT(*ToTU); + + auto *Using = FirstDeclMatcher<UsingDecl>().match( + ToTU, usingDecl(hasName("InnerClass"))); + DeclarationName Name = Using->getDeclName(); + auto Res = LT.lookup(ToTU, Name); + EXPECT_EQ(Res.size(), 0u); + auto *NsA = FirstDeclMatcher<NamespaceDecl>().match( + ToTU, namespaceDecl(hasName("a"))); + auto *RecordB = FirstDeclMatcher<CXXRecordDecl>().match( + ToTU, cxxRecordDecl(hasName("B"))); + auto Res1 = LT.lookup(NsA, Name); + EXPECT_EQ(Res1.count(Using), 1u); + auto Res2 = LT.lookup(RecordB, Name); + EXPECT_EQ(Res2.size(), 0u); +} + TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassTemplateDecl) { TranslationUnitDecl *ToTU = getToTuDecl( R"( Index: clang/lib/AST/ASTImporterLookupTable.cpp =================================================================== --- clang/lib/AST/ASTImporterLookupTable.cpp +++ clang/lib/AST/ASTImporterLookupTable.cpp @@ -67,6 +67,8 @@ } else if (isa<TypedefType>(Ty)) { // We do not put friend typedefs to the lookup table because // ASTImporter does not organize typedefs into redecl chains. + } else if (isa<UsingType>(Ty)) { + // Similar to TypedefType, not putting into lookup table. } else { llvm_unreachable("Unhandled type of friend class"); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits