This is an archive of the discontinued LLVM Phabricator instance.

[include-cleaner] Include-cleaner library structure, and simplistic AST walking.
ClosedPublic

Authored by sammccall on Apr 21 2022, 5:46 AM.

Details

Summary

Include-cleaner is a library that uses the clang AST and preprocessor to
determine which headers are used. It will be used in clang-tidy, in
clangd, in a standalone tool at least for testing, and in out-of-tree tools.

Roughly, it walks the AST, finds referenced decls, maps these to
used sourcelocations, then to FileEntrys, then matching these against #includes.
However there are many wrinkles: dealing with macros, standard library
symbols, umbrella headers, IWYU directives etc.

It is not built on the C++20 modules concept of usage, to allow:

  • use with existing non-modules codebases
  • a flexible API embeddable in clang-tidy, clangd, and other tools
  • avoiding a chicken-and-egg problem where include cleanups are needed before modules can be adopted

This library is based on existing functionality in clangd that provides
an unused-include warning. However it has design changes:

  • it accommodates diagnosing missing includes too (this means tracking where references come from, not just the set of targets)
  • it more clearly separates the different mappings (symbol => location => header => include) for better testing
  • it handles special cases like standard library symbols and IWYU directives more elegantly by adding unified Location and Header types instead of side-tables
  • it will support some customization of policy where necessary (e.g. for style questions of what constitutes a use, or to allow both missing-include and unused-include modes to be conservative)

This patch adds the basic directory structure under clang-tools-extra
and a skeleton version of the AST traversal, which will be the central
piece.
A more end-to-end prototype is in https://reviews.llvm.org/D122677

RFC: https://discourse.llvm.org/t/rfc-lifting-include-cleaner-missing-unused-include-detection-out-of-clangd/61228

Diff Detail

Event Timeline

sammccall created this revision.Apr 21 2022, 5:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 21 2022, 5:46 AM
sammccall requested review of this revision.Apr 21 2022, 5:46 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 21 2022, 5:46 AM
kadircet accepted this revision.Apr 27 2022, 5:29 AM

thanks, as discussed offline. this mostly LG. there are concerns about more code re-use, especially around handling pragmas but we should probably address them as we go rather than now.

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

nit: early exit

This revision is now accepted and ready to land.Apr 27 2022, 5:29 AM
CJ-Johnson accepted this revision.Apr 28 2022, 1:53 PM
CJ-Johnson added inline comments.
clang-tools-extra/include-cleaner/lib/WalkAST.cpp
18

Apologies for my ignorance of LLVM style. Should this be named with a trailing underscore? And should it be a private field?

sammccall marked 2 inline comments as done.Apr 29 2022, 2:01 AM
sammccall added inline comments.
clang-tools-extra/include-cleaner/lib/WalkAST.cpp
18

no, LLVM members are named the same way as locals and classes. (Yes, it's awful).

It is already a private field: members of classes are private by default (the difference between classes and structs). Google style requires private members to be after public ones (and thus private: is always present), but LLVM doesn't require this.

This revision was landed with ongoing or failed builds.Apr 29 2022, 2:04 AM
This revision was automatically updated to reflect the committed changes.
sammccall marked an inline comment as done.

@sammccall It looks like this is causing a link error on some PPC builds: https://lab.llvm.org/buildbot/#/builders/57/builds/17387

@sammccall It looks like this is causing a link error on some PPC builds: https://lab.llvm.org/buildbot/#/builders/57/builds/17387

Thanks, added missing deps in 97b6c92dcd56937bc27de7c4c08381fc71c402e7, will watch the builder