This is an archive of the discontinued LLVM Phabricator instance.

[include-cleaner] Handle dependent type members in AST
ClosedPublic

Authored by VitaNuo on Dec 6 2022, 2:59 AM.

Details

Summary

Handles dependent type members in AST.

Diff Detail

Event Timeline

VitaNuo created this revision.Dec 6 2022, 2:59 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 6 2022, 2:59 AM
Herald added a subscriber: kadircet. · View Herald Transcript
VitaNuo requested review of this revision.Dec 6 2022, 2:59 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 6 2022, 2:59 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
VitaNuo updated this revision to Diff 480466.Dec 6 2022, 6:30 AM

Implement dependent type handling for template specialization types.

VitaNuo updated this revision to Diff 480467.Dec 6 2022, 6:31 AM

Remove extra import.

VitaNuo updated this revision to Diff 480469.Dec 6 2022, 6:42 AM

Adjust implementation to handle pointer types.

VitaNuo updated this revision to Diff 480528.Dec 6 2022, 9:32 AM

Add handling of nested types.

VitaNuo updated this revision to Diff 480530.Dec 6 2022, 9:41 AM

Add more test cases.

VitaNuo removed a reviewer: hokein.Dec 6 2022, 9:49 AM
VitaNuo updated this revision to Diff 480539.Dec 6 2022, 10:32 AM

Add more test cases.

hokein added inline comments.Dec 9 2022, 2:18 AM
clang-tools-extra/include-cleaner/lib/WalkAST.cpp
75

Rebase the current patch. Move the implementation in the resolveType method, then this method will just pass the BaseType to resolveType.

87

if we just aim to support the vector<T>().size() case, we only need this part of code right?

89

You can simplify it in a single if statement:

if (const TemplateSpecializationType *TS = Type->getAs<TemplateSpecializationType>())

clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
220

Thanks for coming up these cases. IMO a more reasonable behavior would be that the target symbol of the reported reference is Nested rather than the outer class Base, but implementing it would require some dependent name lookup in the Base (in clangd we have a similar thing HeuristicResolver, but it is sophisticated).

I think it is find to ignore these cases (these cases are more tricky, and less interesting to us) right now, this would simplify the implementation.

Note that the existing behavior of accessing a member expr from a dependent type is that we don't report any reference on d.^method(), so the original "include base header via a base-member-access from a derived-class object" issue doesn't exist. We aim to improve this behavior for some critical cases.

The critical case we want to support is the vector<T>().size(), if other cases come up in the furture, we could revisit it.

VitaNuo updated this revision to Diff 481659.Dec 9 2022, 8:01 AM
VitaNuo marked 3 inline comments as done.

Address review comments.

VitaNuo marked an inline comment as done.Dec 9 2022, 8:01 AM
VitaNuo added inline comments.
clang-tools-extra/include-cleaner/lib/WalkAST.cpp
87

Sure.

clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
220

Thank you for the extended explanation!

thanks, looks mostly good, I think we can simplify the unittest further.

clang-tools-extra/include-cleaner/lib/WalkAST.cpp
51

nit: we can use const auto* here as the type is explicitly spelled in the getAs<XXX>

52

nit: remove the {} around the if statement

74

nit: I will remove this blank line, to make VisitMemberExpr and VisitCXXDependentScopeMemberExpr group together as they both achieve the same goal.

clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
207

I think keeping the above 3 test cases is enough, I would probably add them to the exiting MemberExprs test (with a comment saying this is dependent-type case).

209

I think we can remove this test, this test duplicates the first test

211

And this case as well

hokein added inline comments.Dec 19 2022, 12:28 AM
clang-tools-extra/include-cleaner/lib/WalkAST.cpp
48

please rebase the patch, this API is changed in https://github.com/llvm/llvm-project/commit/0ab57bdc9745bfc8147831c09ed05073f87e7040, and the TemplateSpecializationType is handled already (to fix the sugar type issue).

VitaNuo updated this revision to Diff 483881.Dec 19 2022, 1:36 AM
VitaNuo marked 6 inline comments as done.

Address review comments.

Thanks for the comments!

clang-tools-extra/include-cleaner/lib/WalkAST.cpp
51

Seems to be obsolete with the above patch.

52

Seems to be obsolete with the above patch.

VitaNuo marked an inline comment as done.Dec 19 2022, 1:36 AM
VitaNuo updated this revision to Diff 483882.Dec 19 2022, 1:39 AM

Remove unusued include.

hokein accepted this revision.Dec 19 2022, 1:47 AM

Thanks!

This revision is now accepted and ready to land.Dec 19 2022, 1:47 AM
This revision was automatically updated to reflect the committed changes.