This is an archive of the discontinued LLVM Phabricator instance.

[include-cleaner] Add include-cleaner tool, with initial HTML report
ClosedPublic

Authored by sammccall on Oct 14 2022, 3:56 AM.

Details

Summary

The immediate goal is to start producing an HTML report to debug and explain
include-cleaner recommendations.
For now, this includes only the lowest-level piece: a list of the references
found in the source code.

How this fits into future ideas:

  • under refs we can also show the headers providing the symbol, which includes match those headers etc
  • we can also annotate the #include lines with which symbols they cover, and add whichever includes we're suggesting too
  • the include-cleaner tool will likely have modes where it emits diagnostics and/or applies edits, so the HTML report is behind a flag

Diff Detail

Event Timeline

sammccall created this revision.Oct 14 2022, 3:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 14 2022, 3:56 AM
Herald added a subscriber: mgrang. · View Herald Transcript
sammccall requested review of this revision.Oct 14 2022, 3:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 14 2022, 3:56 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Demo: https://htmlpreview.github.io/?https://gist.githubusercontent.com/sam-mccall/0cc7dd1f6c5605837966cb113babe591/raw/7c9a29142eca1fc1f486540f255586a0f72a9c9e/AST.html

This is missing tests, my plan is:

  • add some basic smoke tests of include-cleaner -html via lit
  • don't do any additional direct testing of writeHTMLReport
  • add unit tests for RecordedAST (this is part of the public API that's used when you don't have something like clangd tracking decls already)

Will add those after initial feedback of whether this is the right thing at all.

+1, it looks a great start to me (left some initial comments). The plan also looks good to me.

clang-tools-extra/include-cleaner/include/clang-include-cleaner/Hooks.h
1 ↗(On Diff #467730)

nit: the name Hooks.h seems a little vague, Record.h seems slightly better to me.

9 ↗(On Diff #467730)

I assume we are all on the same page of the design -- I'd create the Analysis.h file, and move the writeHTMLReport function there in this patch.

39 ↗(On Diff #467730)

nit: I'd probably mention the "main file" bit in the name, MainTopLevelDecls?

sammccall updated this revision to Diff 468343.Oct 17 2022, 3:08 PM
sammccall marked 2 inline comments as done.

address comments, add tests

Herald added a project: Restricted Project. · View Herald TranscriptOct 17 2022, 3:08 PM
sammccall added inline comments.Oct 17 2022, 3:09 PM
clang-tools-extra/include-cleaner/include/clang-include-cleaner/Hooks.h
9 ↗(On Diff #467730)

This patch already does a fair amount of yak-shaving, and writeHTMLReport is very tangential to that header - I don't think it's possible to review e.g. a good file comment at this point.

39 ↗(On Diff #467730)

Agree it's missing detail, but I think MainTopLevelDecls is too wordy.
How about Roots instead? It needs a bit more context to understand, but it's in the comment.

hokein accepted this revision.Oct 18 2022, 3:29 AM

It looks good to me.

As discussed offline, would be nice to show line number for each line of code in the html dump.

clang-tools-extra/include-cleaner/lib/HTMLReport.cpp
91

This is for main-file right? worth a comment.

98

using auto [RefFileID, Offset] = SM.getDecomposedLoc(SM.getFileLoc(Loc)); is more readable, comparing the following .first, .second usage.

102

If I read this code correctly, Refs should only collect main-file refs, if so then we should bailout if Coords.first != File.

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

nit: missing a file comment.

This revision is now accepted and ready to land.Oct 18 2022, 3:29 AM
This revision was landed with ongoing or failed builds.Oct 18 2022, 9:21 AM
This revision was automatically updated to reflect the committed changes.
sammccall marked 4 inline comments as done.