diff options
author | Gabor Marton <gabor.marton@ericsson.com> | 2019-06-11 13:35:25 +0000 |
---|---|---|
committer | Gabor Marton <gabor.marton@ericsson.com> | 2019-06-11 13:35:25 +0000 |
commit | dd2b76e13eb8da90bce124d65092bfaff65fe6c0 (patch) | |
tree | e077cbde4b637fb520f48a007fc4a9dfd215e4b8 | |
parent | 963d73ff44d68cf3b350b771f26b7fcd2f943a46 (diff) | |
download | bcm5719-llvm-dd2b76e13eb8da90bce124d65092bfaff65fe6c0.tar.gz bcm5719-llvm-dd2b76e13eb8da90bce124d65092bfaff65fe6c0.zip |
[ASTImporter] Fix unhandled cases in ASTImporterLookupTable
Summary:
In most cases the FriendDecl contains the declaration of the befriended
class as a child node, so it is discovered during the recursive
visitation. However, there are cases when the befriended class is not a
child, thus it must be fetched explicitly from the FriendDecl, and only
then can we add it to the lookup table.
(Note, this does affect only CTU and does not affect LLDB, because we
cannot and do not use the ASTImporterLookupTable in LLDB.)
Reviewers: a_sidorin, a.sidorin, shafik
Subscribers: rnkovacs, dkrupp, Szelethus, gamesh411, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D62064
llvm-svn: 363062
-rw-r--r-- | clang/lib/AST/ASTImporter.cpp | 3 | ||||
-rw-r--r-- | clang/lib/AST/ASTImporterLookupTable.cpp | 25 | ||||
-rw-r--r-- | clang/unittests/AST/ASTImporterTest.cpp | 56 |
3 files changed, 72 insertions, 12 deletions
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index 1f1ec1d687c..e0a9da5d2ad 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -2256,7 +2256,8 @@ ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType()) return Importer.MapImported(D, FoundTypedef); } - // FIXME Handle redecl chain. + // FIXME Handle redecl chain. When you do that make consistent changes + // in ASTImporterLookupTable too. break; } diff --git a/clang/lib/AST/ASTImporterLookupTable.cpp b/clang/lib/AST/ASTImporterLookupTable.cpp index 2c0c284c06e..7390329d4ed 100644 --- a/clang/lib/AST/ASTImporterLookupTable.cpp +++ b/clang/lib/AST/ASTImporterLookupTable.cpp @@ -26,17 +26,30 @@ struct Builder : RecursiveASTVisitor<Builder> { LT.add(D); return true; } + // In most cases the FriendDecl contains the declaration of the befriended + // class as a child node, so it is discovered during the recursive + // visitation. However, there are cases when the befriended class is not a + // child, thus it must be fetched explicitly from the FriendDecl, and only + // then can we add it to the lookup table. bool VisitFriendDecl(FriendDecl *D) { if (D->getFriendType()) { QualType Ty = D->getFriendType()->getType(); - // FIXME Can this be other than elaborated? - QualType NamedTy = cast<ElaboratedType>(Ty)->getNamedType(); - if (!NamedTy->isDependentType()) { - if (const auto *RTy = dyn_cast<RecordType>(NamedTy)) + if (isa<ElaboratedType>(Ty)) + Ty = cast<ElaboratedType>(Ty)->getNamedType(); + // A FriendDecl with a dependent type (e.g. ClassTemplateSpecialization) + // always has that decl as child node. + // However, there are non-dependent cases which does not have the + // type as a child node. We have to dig up that type now. + if (!Ty->isDependentType()) { + if (const auto *RTy = dyn_cast<RecordType>(Ty)) LT.add(RTy->getAsCXXRecordDecl()); - else if (const auto *SpecTy = - dyn_cast<TemplateSpecializationType>(NamedTy)) { + else if (const auto *SpecTy = dyn_cast<TemplateSpecializationType>(Ty)) LT.add(SpecTy->getAsCXXRecordDecl()); + 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 { + llvm_unreachable("Unhandled type of friend class"); } } } diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index 02cfaad91e6..63bcc609f06 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -4237,13 +4237,13 @@ TEST_P(ASTImporterLookupTableTest, LookupDeclNamesFromDifferentTUs) { ASSERT_EQ(Res.size(), 0u); } -static const RecordDecl * getRecordDeclOfFriend(FriendDecl *FD) { - QualType Ty = FD->getFriendType()->getType(); - QualType NamedTy = cast<ElaboratedType>(Ty)->getNamedType(); - return cast<RecordType>(NamedTy)->getDecl(); +static const RecordDecl *getRecordDeclOfFriend(FriendDecl *FD) { + QualType Ty = FD->getFriendType()->getType().getCanonicalType(); + return cast<RecordType>(Ty)->getDecl(); } -TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassDecl) { +TEST_P(ASTImporterLookupTableTest, + LookupFindsFwdFriendClassDeclWithElaboratedType) { TranslationUnitDecl *ToTU = getToTuDecl( R"( class Y { friend class F; }; @@ -4267,6 +4267,52 @@ TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassDecl) { EXPECT_EQ(Res.size(), 0u); } +TEST_P(ASTImporterLookupTableTest, + LookupFindsFwdFriendClassDeclWithUnelaboratedType) { + TranslationUnitDecl *ToTU = getToTuDecl( + R"( + class F; + class Y { friend F; }; + )", + Lang_CXX11); + + // In this case, the CXXRecordDecl is hidden, the FriendDecl is not a parent. + // So we must dig up the underlying CXXRecordDecl. + ASTImporterLookupTable LT(*ToTU); + auto *FriendD = FirstDeclMatcher<FriendDecl>().match(ToTU, friendDecl()); + const RecordDecl *RD = getRecordDeclOfFriend(FriendD); + auto *Y = FirstDeclMatcher<CXXRecordDecl>().match(ToTU, cxxRecordDecl(hasName("Y"))); + + DeclarationName Name = RD->getDeclName(); + auto Res = LT.lookup(ToTU, Name); + EXPECT_EQ(Res.size(), 1u); + EXPECT_EQ(*Res.begin(), RD); + + Res = LT.lookup(Y, Name); + EXPECT_EQ(Res.size(), 0u); +} + +TEST_P(ASTImporterLookupTableTest, + LookupFindsFriendClassDeclWithTypeAliasDoesNotAssert) { + TranslationUnitDecl *ToTU = getToTuDecl( + R"( + class F; + using alias_of_f = F; + class Y { friend alias_of_f; }; + )", + Lang_CXX11); + + // ASTImporterLookupTable constructor handles using declarations correctly, + // no assert is expected. + ASTImporterLookupTable LT(*ToTU); + + auto *Alias = FirstDeclMatcher<TypeAliasDecl>().match( + ToTU, typeAliasDecl(hasName("alias_of_f"))); + DeclarationName Name = Alias->getDeclName(); + auto Res = LT.lookup(ToTU, Name); + EXPECT_EQ(Res.count(Alias), 1u); +} + TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassTemplateDecl) { TranslationUnitDecl *ToTU = getToTuDecl( R"( |