From: Gabor Marton Date: Wed, 30 May 2018 09:19:26 +0000 (+0000) Subject: [ASTImporter] Corrected lookup at import of templated record decl X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=86f879c238229ae1d58ce3e605b8eee2e513d660;p=clang [ASTImporter] Corrected lookup at import of templated record decl Summary: When a CXXRecordDecl under ClassTemplateDecl is imported, check the templated record decl for similarity instead of the template. Reviewers: a.sidorin Reviewed By: a.sidorin Subscribers: martong, cfe-commits Differential Revision: https://reviews.llvm.org/D47313 Patch by Balazs Keri! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@333522 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/ASTImporter.cpp b/lib/AST/ASTImporter.cpp index d64b64ce43..f812244ed3 100644 --- a/lib/AST/ASTImporter.cpp +++ b/lib/AST/ASTImporter.cpp @@ -2015,7 +2015,14 @@ Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { if (const auto *Tag = Typedef->getUnderlyingType()->getAs()) Found = Tag->getDecl(); } - + + if (D->getDescribedTemplate()) { + if (auto *Template = dyn_cast(Found)) + Found = Template->getTemplatedDecl(); + else + continue; + } + if (auto *FoundRecord = dyn_cast(Found)) { if (!SearchName) { // If both unnamed structs/unions are in a record context, make sure diff --git a/unittests/AST/ASTImporterTest.cpp b/unittests/AST/ASTImporterTest.cpp index 76ea259433..5e2dcf73fc 100644 --- a/unittests/AST/ASTImporterTest.cpp +++ b/unittests/AST/ASTImporterTest.cpp @@ -1107,6 +1107,50 @@ TEST(ImportExpr, DependentSizedArrayType) { has(fieldDecl(hasType(dependentSizedArrayType()))))))); } +TEST_P(ASTImporterTestBase, ImportOfTemplatedDeclOfClassTemplateDecl) { + Decl *FromTU = getTuDecl("template struct S{};", Lang_CXX); + auto From = + FirstDeclMatcher().match(FromTU, classTemplateDecl()); + ASSERT_TRUE(From); + auto To = cast(Import(From, Lang_CXX)); + ASSERT_TRUE(To); + Decl *ToTemplated = To->getTemplatedDecl(); + Decl *ToTemplated1 = Import(From->getTemplatedDecl(), Lang_CXX); + EXPECT_TRUE(ToTemplated1); + EXPECT_EQ(ToTemplated1, ToTemplated); +} + +TEST_P(ASTImporterTestBase, ImportCorrectTemplatedDecl) { + auto Code = + R"( + namespace x { + template struct S1{}; + template struct S2{}; + template struct S3{}; + } + )"; + Decl *FromTU = getTuDecl(Code, Lang_CXX); + auto FromNs = + FirstDeclMatcher().match(FromTU, namespaceDecl()); + auto ToNs = cast(Import(FromNs, Lang_CXX)); + ASSERT_TRUE(ToNs); + auto From = + FirstDeclMatcher().match(FromTU, + classTemplateDecl( + hasName("S2"))); + auto To = + FirstDeclMatcher().match(ToNs, + classTemplateDecl( + hasName("S2"))); + ASSERT_TRUE(From); + ASSERT_TRUE(To); + auto ToTemplated = To->getTemplatedDecl(); + auto ToTemplated1 = + cast(Import(From->getTemplatedDecl(), Lang_CXX)); + EXPECT_TRUE(ToTemplated1); + ASSERT_EQ(ToTemplated1, ToTemplated); +} + TEST_P(ASTImporterTestBase, DISABLED_ImportFunctionWithBackReferringParameter) { Decl *From, *To; std::tie(From, To) = getImportedDecl(