This is an archive of the discontinued LLVM Phabricator instance.

[include-cleaner] Use expansion locations for macros.
ClosedPublic

Authored by VitaNuo on Dec 9 2022, 6:48 AM.

Details

Summary

Use expansion locations for target symbol decls when finding headers for macros.

Diff Detail

Event Timeline

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

Remove FIXME.

The implementation looks good. Some comments around the test.

clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
162

these 3 newly-added tests are similar, instead of duplicating them, we can use a table-gen style test, see an example

168

nit: use guard(R"cpp(...)cpp) for the normal header code.

187

The intention of the test is to test the function findHeaders, we're testing it in an implicit way (findHeaders is hidden inside the walkUsed), there is no code here explicitly calling this function.

We can make it clearer (no need to use walkUsed):

  1. we get a Foo Decl AST node from the testcase.
  2. with the AST node from step1, we cam call the findHeaders directly in the test code.

for 1, we can just write a simple RecursiveASTVisitor, and capture a NamedDecl which called Foo, something like:

struct Visitor : RecursiveASTVisitor<Visitor> {
     const NamedDecl *Out = nullptr;
     bool VisitNamedDecl(const NamedDecl *ND) {
       if (ND->getName() == "Foo") {
         EXPECT_TRUE(Out == nullptr);
         Out = ND;
       }
       return true;
     }
   };
VitaNuo updated this revision to Diff 483910.Dec 19 2022, 4:31 AM

Address review comments.

VitaNuo updated this revision to Diff 483911.Dec 19 2022, 4:32 AM

Add guard calls to headers.

VitaNuo marked an inline comment as done.Dec 19 2022, 4:32 AM
VitaNuo added inline comments.
clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
162

Great idea, thanks!

187

Ok, I've attempted to follow your advice now, please have a look.

thanks, the test looks better now, a few more suggestions.

clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
162

Move the definition to the TEST_F body.

174

I think if (ND->getName() == "FLAG_foo" || ND->getName() == "Foo") should just work (without the VisitCXXRecordDecl method)?

190

I think the Foo foo; is not really needed for the test (#include "declare.h" is the key part of the main file) as we only want the location of the CXXRecordDecl. The same for the following testcases.

So I think we can simplify it further: TestCases has two members (DeclareHeader, MacroHeader). The main file content is always #include "declare.h", we can construct the main code in the for-loop body.

227

nit: Annotations is not needed as we don't use any annotation here.

233

Visitor.TraverseDecl(AST->context().getTranslationUnitDecl());

241

this is not needed -- the following EXPECT_THAT already covers it.

VitaNuo updated this revision to Diff 483926.Dec 19 2022, 5:31 AM
VitaNuo marked 7 inline comments as done.

Address most recent review comments.

Thanks for the review!

VitaNuo updated this revision to Diff 483927.Dec 19 2022, 5:32 AM

Remove extra variable.

hokein accepted this revision.Dec 19 2022, 6:08 AM

Thanks!

clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
176

nit: it feels more natural to declare MacroHeader first then DeclareHeader, as the DeclareHeader already includes the macro.h

177

The clang-format's indentation seems odd to me, I think if you put a trailing , on the last element }, };, its format is better.

178

nit: I'd add /*DeclareHeader=*/, /*MacroHeader=*/, which improves the code readability.

212

nit: /*PragmaIncludes=*/nullptr

This revision is now accepted and ready to land.Dec 19 2022, 6:08 AM
VitaNuo updated this revision to Diff 483941.Dec 19 2022, 6:16 AM
VitaNuo marked 4 inline comments as done.

Address review comments.

This revision was landed with ongoing or failed builds.Dec 19 2022, 6:19 AM
This revision was automatically updated to reflect the committed changes.