This is an archive of the discontinued LLVM Phabricator instance.

[include-cleaner] Initial version for the "Location=>Header" step
ClosedPublic

Authored by hokein on Nov 3 2022, 2:20 AM.

Details

Summary

This patch implements the initial version of "Location => Header" step:

  • define the interface;
  • integrate into the existing workflow, and use the PragmaIncludes;

Diff Detail

Event Timeline

hokein created this revision.Nov 3 2022, 2:20 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 3 2022, 2:20 AM
hokein requested review of this revision.Nov 3 2022, 2:20 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 3 2022, 2:20 AM
kadircet added inline comments.Nov 3 2022, 3:10 AM
clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
115

can you add comments here describing what this case is used for

145

let's move this to AnalysisInternal.h, as it isn't used in any of the public apis.

clang-tools-extra/include-cleaner/lib/Analysis.cpp
84

nit: if (auto *Loc = std::get_if<SourceLocation>(&SLoc))

94

what about exporters here?

97

not sure i follow the fixme here, are you suggesting we should also compute transitive exporters?

102

nit: if (auto *Sym = std::get<tooling::stdlib::Symbol>(&SLoc))

clang-tools-extra/include-cleaner/lib/AnalysisInternal.h
52

this is fine for the initial version, but we'll likely lose a lot of performance here going forward. we can address it later on though, as this is an internal api and those performance concerns won't be a problem until this is used more widely.

we're likely going to call this function multiple times for each symbol, and also despite having different sourcelocations, there'll probably be lots of declarations that're originating from the same FileID.
Hence we'll want to accumulate outputs on a single container, rather than returning a new container at each call, and also have some sort of file-id based cache.

clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
113

can we use findIncludeHeaders directly?

that way we don't need to set up a referencing file, e.g. you can directly create a SourceLocation inside private.h (in this case even the decl is not important).

(same for the export test)

132

calling this private is somewhat confusing, maybe call it Detail?

hokein updated this revision to Diff 472911.Nov 3 2022, 5:51 AM
hokein marked 6 inline comments as done.

address comments

clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
145

I'm not quite sure about this --the SymbolLocation is an important concept and API, even though it is not used in public APIs. Moved anyway.

clang-tools-extra/include-cleaner/lib/Analysis.cpp
94

based on our previous discussion, we decided to initially treat the spelling header in the IWYU private pragma as the final public header, so no need to do the exporters here.

97

Yeah, rephrased the FIXME.

clang-tools-extra/include-cleaner/lib/AnalysisInternal.h
52

Acked, agree that this is likely a hot function, we should not be concern about it at the moment.

clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
113

the intention was to do an end-to-end test of the walkUsed API.

but you're right, we should have a unittest for findIncludeHeaders, added one, and simplified the end-to-end walkUsed test.

kadircet added inline comments.Nov 3 2022, 7:14 AM
clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
116

nit: . at the end of comment

clang-tools-extra/include-cleaner/lib/Analysis.cpp
94

yeah SG, can you put a comment about that though? saying // Note that we might need to look for exporters in this case as well.

97

i see, i am not sure if that's desired though.

e.g. if we have foo.h exporting bar.h exporting baz.h, I don't think baz.h counts (or should count) as an alternative to foo.h. do we have information proving otherwise?

106

can you actually put an llvm_unreachable here?

clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
113

the intention was to do an end-to-end test of the walkUsed API.

We can update the Basic test above for that purpose (and turn it into a smoke test instead).

but you're right, we should have a unittest for findIncludeHeaders, added one, and simplified the end-to-end walkUsed test.

Since these are all trying to test specific IWYU pragma behaviour, I still think it's better to test them in isolation, both for test brevity but also for debuggability in the future when something breaks.
Also making sure majority of the tests are smaller is good for incremental progress as well, when we update ast walking logic, we shouldn't be updating these tests for whatever reasons (+ arguments around test times).

131

you can still use the same fixture here, if you want a different name you can always do: using FindIncludeHeaders = WalkUsedTest; or class FindIncludeHeaders : public WalkUsedTest {};

hokein updated this revision to Diff 473185.Nov 4 2022, 3:28 AM
hokein marked 3 inline comments as done.

address comments

clang-tools-extra/include-cleaner/lib/Analysis.cpp
97

The classical IWYU tool has this semantic, see the nested_export test in https://github.com/include-what-you-use/include-what-you-use/commit/9945e543d894e90fab5ebd0b685cabd5aa8dd21f.

clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
113

ok, moved to WalkUsedTest.Basic. now the test is just a smoke test for IWYU pragma.

131

There is not much code being shared between these two tests (only the MakeAction, walk() is not used) -- the FindIncludeHeaders test is simpler than the WalkUsedTest, we don't need to build an AST or traverse the AST nodes.

kadircet accepted this revision.Nov 10 2022, 5:35 AM

thanks, lgtm!

clang-tools-extra/include-cleaner/lib/Analysis.cpp
97

wow, that's surprising. but i guess is a valid point.

This revision is now accepted and ready to land.Nov 10 2022, 5:35 AM
This revision was landed with ongoing or failed builds.Nov 11 2022, 1:35 AM
This revision was automatically updated to reflect the committed changes.
sammccall added inline comments.Nov 11 2022, 2:24 AM
clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
145

FWIW +1 to the idea that this belongs in Types.h even if it's not used in public APIs at present, as discussed offline.

clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
33

@kadircet gentle reminder - you wanted to defer this, this patch adds a copy/paste of the test setup - want me to try to pull something out before this gets out of hand?

sammccall added inline comments.Nov 11 2022, 3:35 AM
clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
33

disregard, I misread this, the new test isn't calling walkUsed at all so belongs somewhere else