diff --git a/lib/AST/ASTImporter.cpp b/lib/AST/ASTImporter.cpp index e36a3b8e14aa..877e5ce8d33b 100644 --- a/lib/AST/ASTImporter.cpp +++ b/lib/AST/ASTImporter.cpp @@ -4174,7 +4174,7 @@ Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { // is created. // FIXME: Import default argument. - return TemplateTypeParmDecl::Create(Importer.getToContext(), + auto ToD = TemplateTypeParmDecl::Create(Importer.getToContext(), Importer.getToContext().getTranslationUnitDecl(), Importer.Import(D->getLocStart()), Importer.Import(D->getLocation()), @@ -4183,6 +4183,13 @@ Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { Importer.Import(D->getIdentifier()), D->wasDeclaredWithTypename(), D->isParameterPack()); + Importer.Imported(D, ToD); + if (D->hasDefaultArgument()) { + TypeSourceInfo *ToDefaultArgInfo = + Importer.Import(D->getDefaultArgumentInfo()); + ToD->setDefaultArgument(ToDefaultArgInfo); + } + return ToD; } Decl * diff --git a/unittests/AST/ASTImporterTest.cpp b/unittests/AST/ASTImporterTest.cpp index f432baf9d865..77214a239cbe 100644 --- a/unittests/AST/ASTImporterTest.cpp +++ b/unittests/AST/ASTImporterTest.cpp @@ -2392,5 +2392,55 @@ long b = strtol(&a); EXPECT_EQ(DeclCounter().match(ToTU, usingShadowDecl()), 1u); } +TEST_F(Fixture, ShouldImportDefaultTemplateArgs) { + Decl *FromTU = + getTuDecl("template struct X{};", Lang_CXX, "input0.cc"); + auto FromD = + FirstDeclMatcher().match(FromTU, classTemplateDecl()); + + { + auto TPL = FromD->getTemplateParameters(); + ASSERT_EQ((TPL->end() - TPL->begin()), 1); + TemplateTypeParmDecl *ParmD = cast(*(TPL->begin())); + ASSERT_TRUE(ParmD->hasDefaultArgument()); + } + + { + auto ToD = cast(Import(FromD, Lang_CXX)); + auto TPL = ToD->getTemplateParameters(); + ASSERT_EQ((TPL->end() - TPL->begin()), 1); + TemplateTypeParmDecl *ParmD = cast(*(TPL->begin())); + EXPECT_TRUE(ParmD->hasDefaultArgument()); + } +} + +TEST_F(Fixture, ShouldImportDefaultTemplateArgs2) { + Decl *FromTU = getTuDecl("template struct X{};", + Lang_CXX, "input0.cc"); + auto FromD = + FirstDeclMatcher().match(FromTU, classTemplateDecl()); + + { + auto TPL = FromD->getTemplateParameters(); + ASSERT_EQ((TPL->end() - TPL->begin()), 2); + auto it = TPL->begin(); + TemplateTypeParmDecl *ParmD0 = cast(*it++); + ASSERT_TRUE(ParmD0->hasDefaultArgument()); + TemplateTypeParmDecl *ParmD1 = cast(*it); + ASSERT_TRUE(ParmD1->hasDefaultArgument()); + } + + { + auto ToD = cast(Import(FromD, Lang_CXX)); + auto TPL = ToD->getTemplateParameters(); + ASSERT_EQ((TPL->end() - TPL->begin()), 2); + auto it = TPL->begin(); + TemplateTypeParmDecl *ParmD0 = cast(*it++); + EXPECT_TRUE(ParmD0->hasDefaultArgument()); + TemplateTypeParmDecl *ParmD1 = cast(*it); + EXPECT_TRUE(ParmD1->hasDefaultArgument()); + } +} + } // end namespace ast_matchers } // end namespace clang