This is an archive of the discontinued LLVM Phabricator instance.

[include-cleaner] Improve handling for templates
ClosedPublic

Authored by kadircet on Apr 12 2023, 4:13 AM.

Details

Summary

Principal here is:

  • Making sure each template instantiation implies use of the most specialized template. As explicit instantiations/specializations are not redeclarations of the primary template.
  • Introducing a use from explicit instantions/specializaitons to the primary template, as they're required but not traversed as part of the RAV.

Diff Detail

Event Timeline

kadircet created this revision.Apr 12 2023, 4:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2023, 4:13 AM
Herald added a subscriber: mgrang. · View Herald Transcript
kadircet requested review of this revision.Apr 12 2023, 4:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2023, 4:13 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
kadircet updated this revision to Diff 512775.Apr 12 2023, 4:41 AM
kadircet edited the summary of this revision. (Show Details)
  • Use template instantion pattern helper instead of dealing with partial specializations
sammccall accepted this revision.Apr 12 2023, 5:23 AM
sammccall added inline comments.
clang-tools-extra/include-cleaner/lib/WalkAST.cpp
91

We could consider the other order:

if (auto *Pattern = TD->getTemplateInstantiationPattern())
  return Pattern;
return TD;

To me this feels a bit more like there's an obvious principle and less like case bashing.

Concretely I think the difference is that we now report the pattern rather than the specialization for explicit template instantiations. As discussed offline, I'm not very familiar with the patterns where explicit instantiations are used. But it seems at least ambiguous whether a visible explicit instantiation is semantically significant, whereas it's clear that the pattern is important. So I think preferring the pattern here would be slightly better too, up to you.

196

maybe add to the comment that implicit ones are never visited?
(It should have been obvious to me, but it's easy to fall into the trap of "what are all the things a CTSD can be again?")

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

this seems a little ad-hoc (is class name always enough info, why are we sorting/uniquing here)

Consider returning vector<NamedDecl*> and using a matcher to verify the things you care about (kind(Decl::ClassTemplate) or so?)

This revision is now accepted and ready to land.Apr 12 2023, 5:23 AM
kadircet marked 3 inline comments as done.Apr 12 2023, 8:35 AM
kadircet added inline comments.
clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
111

sorry was planning to drop this actually, analysistest covers the "we're picking the right declaration at a certain location" logic. but since i forgot, i might as well change it to keep decls around :P

kadircet updated this revision to Diff 512851.Apr 12 2023, 8:35 AM
kadircet marked an inline comment as done.
  • Ignore explicit instantiations
  • Update tests to use decls
This revision was landed with ongoing or failed builds.Apr 12 2023, 8:41 AM
This revision was automatically updated to reflect the committed changes.
dyung added a subscriber: dyung.Apr 12 2023, 10:46 AM

@kadircet, your change is causing 3 test failures on Windows bots, can you take a look and revert if you need time to investigate?

https://lab.llvm.org/buildbot/#/builders/216/builds/19740
https://lab.llvm.org/buildbot/#/builders/123/builds/18332

figured out the issue, preparing a fix