This is an archive of the discontinued LLVM Phabricator instance.

[clang][ASTImporter] Fix friend class template import within dependent context
ClosedPublic

Authored by danix800 on Jul 18 2023, 5:06 PM.

Details

Summary

For friend class template within dependent context:

  1. Should not do structure matching checking. This fixes importing failure of template with non-type parm;
  2. Should not be added into redecls chain.

See Sema::CheckClassTemplate().

Fixes https://github.com/llvm/llvm-project/issues/64169

Diff Detail

Event Timeline

danix800 created this revision.Jul 18 2023, 5:06 PM
Herald added a reviewer: shafik. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
danix800 requested review of this revision.Jul 18 2023, 5:06 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 18 2023, 5:06 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
danix800 retitled this revision from [ASTImporter] Fix recursive friend class template with non-type parm to [ASTImporter] Fix friend class template within dependent context.Jul 18 2023, 5:40 PM
danix800 edited the summary of this revision. (Show Details)
danix800 added a reviewer: balazske.
danix800 retitled this revision from [ASTImporter] Fix friend class template within dependent context to [ASTImporter] Fix friend class template import within dependent context.Jul 18 2023, 5:45 PM
danix800 updated this revision to Diff 542778.Jul 21 2023, 12:05 AM

Apply git-clang-format.

danix800 updated this revision to Diff 543387.Jul 23 2023, 11:12 PM
danix800 edited the summary of this revision. (Show Details)Jul 27 2023, 11:42 AM

This mostly makes sense to me, but I want another review.

The changes seem correct to me, but it'd be great if one of the more regular AST importer maintainers could verify just in case I missed something. This should probably have a release note, though.

@balazske Could you please have a look?

danix800 updated this revision to Diff 544993.Jul 27 2023, 6:12 PM
danix800 retitled this revision from [ASTImporter] Fix friend class template import within dependent context to [clang][ASTImporter] Fix friend class template import within dependent context.

Update ReleaseNotes

balazske added inline comments.Jul 31 2023, 3:53 AM
clang/lib/AST/ASTImporter.cpp
2862–2866

The code seems to work but I was confused by the different conditions used (is it possible that IsFriendTemplate and ShouldAddRedecl is true at the same time?). I had the observation that if ShouldAddRedecl is false we do not need the whole search for previous decl. At a friend template we shall not find a definition, the loop would just find the last declaration (this value is not used). So I have the idea of this code (could not find the "suggest edit" command):

bool DependentFriend = IsFriendTemplate && IsDependentContext;

// We may already have a record of the same name; try to find and match it.
RecordDecl *PrevDecl = nullptr;
if (!DependentFriend && !DC->isFunctionOrMethod() && !D->isLambda()) {
2904

This change is not needed if the code above is used.

2976

This change is not needed if the code above is used.

5805–5812

Similar change here:

bool DependentFriend = IsFriendTemplate && IsDependentContext;

ClassTemplateDecl *FoundByLookup = nullptr;

// We may already have a template of the same name; try to find and match it.
if (!DependentFriend && !DC->isFunctionOrMethod()) {

IsFriendTemplate and ShouldAddRedecl is not needed (no changes in the later lines).

danix800 updated this revision to Diff 545912.Jul 31 2023, 9:17 PM

Cleanup as @balazske suggested.

danix800 added inline comments.Jul 31 2023, 9:19 PM
clang/lib/AST/ASTImporter.cpp
2862–2866

This is more clear. Thanks.

danix800 updated this revision to Diff 545913.Jul 31 2023, 9:27 PM

Update ReleaseNotes.rst

balazske accepted this revision.Aug 1 2023, 7:11 AM

The fix looks OK, but the test could be improved and cleaned up (for example FromClass is the same as FromD in the test, and DeclContext is not checked, can be done like in the test UndeclaredFriendClassShouldNotBeVisible but the AST is different). Probably there are other similar cases, and there is a related problem shown in D156693 (the fix in that patch is not correct, the solution here is not good for that case, it is possible that the same code as here needs to be changed again or a better fix is found). I am accepting this code but probably will create a new patch to improve and add tests for similar cases (if not done before by somebody else).

This revision is now accepted and ready to land.Aug 1 2023, 7:11 AM

The fix looks OK, but the test could be improved and cleaned up (for example FromClass is the same as FromD in the test, and DeclContext is not checked, can be done like in the test UndeclaredFriendClassShouldNotBeVisible but the AST is different).

Testcase will be cleaned up in the final commit.

Probably there are other similar cases, and there is a related problem shown in D156693 (the fix in that patch is not correct, the solution here is not good for that case, it is possible that the same code as here needs to be changed again or a better fix is found). I am accepting this code but probably will create a new patch to improve and add tests for similar cases (if not done before by somebody else).

Confirmed, but I'll not touch this issue in this commit.