diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -6447,7 +6447,7 @@ ToFunc->setAccess(D->getAccess()); ToFunc->setLexicalDeclContext(LexicalDC); - LexicalDC->addDeclInternal(ToFunc); + addDeclToContexts(D, ToFunc); ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable(); if (LT && !OldParamDC.empty()) { diff --git a/clang/test/Import/templated-friend/Inputs/T.cpp b/clang/test/Import/templated-friend/Inputs/T.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Import/templated-friend/Inputs/T.cpp @@ -0,0 +1,3 @@ +template struct A { + template friend void f(); +}; diff --git a/clang/test/Import/templated-friend/test.cpp b/clang/test/Import/templated-friend/test.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Import/templated-friend/test.cpp @@ -0,0 +1,5 @@ +// RUN: clang-import-test -import %S/Inputs/T.cpp -expression %s + +void expr() { + A a; +} diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -5634,6 +5634,42 @@ EXPECT_EQ(Imported->getPreviousDecl(), Friend); } +TEST_P(ImportFriendFunctionTemplates, ImportFriendFunctionInsideClassTemplate) { + Decl *From, *To; + std::tie(From, To) = getImportedDecl(R"( + template struct X { + template friend void f(); + }; + )", + Lang_CXX03, "", Lang_CXX03, "X"); + + auto *FromFriend = FirstDeclMatcher().match(From, friendDecl()); + auto *ToFriend = FirstDeclMatcher().match(To, friendDecl()); + + EXPECT_TRUE(FromFriend == + LastDeclMatcher().match(From, friendDecl())); + EXPECT_TRUE(ToFriend == + LastDeclMatcher().match(To, friendDecl())); + + auto *FromDecl = FromFriend->getFriendDecl(); + auto *FromDC = FromFriend->getDeclContext(); + auto *FromLexicalDC = FromFriend->getLexicalDeclContext(); + + EXPECT_TRUE(FromDC->containsDecl(FromFriend)); + EXPECT_FALSE(FromDC->containsDecl(FromDecl)); + EXPECT_TRUE(FromLexicalDC->containsDecl(FromFriend)); + EXPECT_FALSE(FromLexicalDC->containsDecl(FromDecl)); + + auto *ToDecl = ToFriend->getFriendDecl(); + auto *ToDC = ToFriend->getDeclContext(); + auto *ToLexicalDC = ToFriend->getLexicalDeclContext(); + + EXPECT_TRUE(ToDC->containsDecl(ToFriend)); + EXPECT_FALSE(ToDC->containsDecl(ToDecl)); + EXPECT_TRUE(ToLexicalDC->containsDecl(ToFriend)); + EXPECT_FALSE(ToLexicalDC->containsDecl(ToDecl)); +} + struct ASTImporterWithFakeErrors : ASTImporter { using ASTImporter::ASTImporter; bool returnWithErrorInTest() override { return true; }