Skip to content

Commit

Permalink
[clang][ASTImporter] Remove extra FunctionTemplateDecl introduced by …
Browse files Browse the repository at this point in the history
…templated friend

An extranous FunctionTemplateDecl is introduced in the following testcase:

  template <typename T> struct A {
    template <typename U> friend void f();
  };

"To" Context:

  ClassTemplateDecl 0x55dae7116618 <input.cc:1:1, col:73> col:30 A
  |-TemplateTypeParmDecl 0x55dae7116490 <col:11, col:20> col:20 typename depth 0 index 0 T
  `-CXXRecordDecl 0x55dae7116550 <col:23, col:73> col:30 struct A definition
    |-DefinitionData empty aggregate standard_layout trivially_copyable pod trivial literal has_constexpr_non_copy_move_ctor can_const_default_init
    | |-DefaultConstructor exists trivial constexpr needs_implicit defaulted_is_constexpr
    | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param
    | |-MoveConstructor exists simple trivial needs_implicit
    | |-CopyAssignment simple trivial has_const_param needs_implicit implicit_has_const_param
    | |-MoveAssignment exists simple trivial needs_implicit
    | `-Destructor simple irrelevant trivial needs_implicit
    |-FunctionTemplateDecl 0x55dae7116a38 parent 0x55dae6fa2b68 <col:35, col:71> col:69 f                            // extranous node
    | |-TemplateTypeParmDecl 0x55dae7116860 <col:45, col:54> col:54 typename depth 1 index 0 U
    | `-FunctionDecl 0x55dae7116968 parent 0x55dae6fa2b68 <col:57, col:71> col:69 f 'void ()'
    |-FriendDecl 0x55dae7116aa0 <col:35, col:71> col:69
    | `-FunctionTemplateDecl 0x55dae7116a38 parent 0x55dae6fa2b68 <col:35, col:71> col:69 f
    |   |-TemplateTypeParmDecl 0x55dae7116860 <col:45, col:54> col:54 typename depth 1 index 0 U
    |   `-FunctionDecl 0x55dae7116968 parent 0x55dae6fa2b68 <col:57, col:71> col:69 f 'void ()'
    `-CXXRecordDecl 0x55dae7116ae0 <col:23, col:30> col:30 implicit struct A

Reviewed By: balazske

Differential Revision: https://reviews.llvm.org/D157691
  • Loading branch information
danix800 committed Aug 16, 2023
1 parent 3b0eeb6 commit eabc7ad
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6459,7 +6459,7 @@ ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {

ToFunc->setAccess(D->getAccess());
ToFunc->setLexicalDeclContext(LexicalDC);
LexicalDC->addDeclInternal(ToFunc);
addDeclToContexts(D, ToFunc);

ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable();
if (LT && !OldParamDC.empty()) {
Expand Down
37 changes: 37 additions & 0 deletions clang/unittests/AST/ASTImporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5634,6 +5634,43 @@ TEST_P(ImportFriendFunctionTemplates, LookupShouldFindPreviousFriend) {
EXPECT_EQ(Imported->getPreviousDecl(), Friend);
}

TEST_P(ImportFriendFunctionTemplates, ImportFriendFunctionInsideClassTemplate) {
Decl *From, *To;
std::tie(From, To) = getImportedDecl(
R"(
template <typename T> struct X {
template <typename U> friend void f();
};
)",
Lang_CXX03, "", Lang_CXX03, "X");

auto *FromFriend = FirstDeclMatcher<FriendDecl>().match(From, friendDecl());
auto *ToFriend = FirstDeclMatcher<FriendDecl>().match(To, friendDecl());

EXPECT_TRUE(FromFriend ==
LastDeclMatcher<FriendDecl>().match(From, friendDecl()));
EXPECT_TRUE(ToFriend ==
LastDeclMatcher<FriendDecl>().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; }
Expand Down

0 comments on commit eabc7ad

Please sign in to comment.