This is an archive of the discontinued LLVM Phabricator instance.

[include-cleaner] Filter template instantiations from AST roots.
ClosedPublic

Authored by VitaNuo on Jan 9 2023, 2:49 AM.

Diff Detail

Event Timeline

VitaNuo created this revision.Jan 9 2023, 2:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 9 2023, 2:49 AM
Herald added a subscriber: kadircet. · View Herald Transcript
VitaNuo requested review of this revision.Jan 9 2023, 2:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 9 2023, 2:49 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
hokein added inline comments.Jan 9 2023, 4:11 AM
clang-tools-extra/include-cleaner/lib/Record.cpp
377

as we already do a dyn_cast inside the isImplicitTemplateInstantiation, we can get rid of this NamedDecl cast by changing the isImplicitTemplateInstantiation to accept a Decl*.

387

we can move it to the above anonymous namespace as well (it is fine to have two functions with same name because of C++ overloads). In this case, I'd probably inline it in HandleTopLevelDecl.

clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
105

maybe DropImplicitTemplateTopDecl?

120

this example is a nice example to show the original bug (where we inserted unexpected additional headers) in include-cleaner.

The scope of this patch is to filter out the implicit template instantiation, a simple testcase like below could work. (the current testcase is fine as well, up to you)

template<typename T>
int func(T) { return 0; } 
auto s = func<int>(1);
123

nit: instead doing a negative verification, I'd check the captured root decl, which is (a MyGetter CXXRecordDecl, and a v var decl), and with a comment saying the implicitly-instantiated method decl is filtered out.

VitaNuo updated this revision to Diff 487385.Jan 9 2023, 4:42 AM

Address review comments.

clang-tools-extra/include-cleaner/lib/Record.cpp
377

Oh right, I didn't notice. Thanks.

387

I am not sure I fully understand this comment, since I cannot both move this method to the anonymous namespace above and inline it at the same time. I have inlined it now, hopefully it looks fine to you.

clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
105

Not sure, since the rest of tests above only use nouns, e.g., "Macros", "Inclusion", "Namespace", etc. I've called it "ImplicitTemplates" now, hopefully it's fine.

120

You're right, but IMHO I would rather keep the current example. The current example, although simplified, still highly resembles the pattern we've found it real code. The snippet above does not anymore. Since it's a bug fix for a real pattern, I'd rather keep it as is. Let me know if you disagree.

123

Sure, I have no preference.

hokein accepted this revision.Jan 9 2023, 4:52 AM

thanks looks good!

Please remember to add a FIX: <link to github issue> to the commit message.

clang-tools-extra/include-cleaner/lib/Record.cpp
387

the current code looks good. (I meant either, not both, sorry for the confusion)

This revision is now accepted and ready to land.Jan 9 2023, 4:52 AM
This revision was landed with ongoing or failed builds.Jan 9 2023, 4:58 AM
This revision was automatically updated to reflect the committed changes.