clangd has a feature to warn when you #include headers but don’t use them.
It works well (it’s still off by default, but we’re planning to turn it on soon).
We’re also planning to add a warning when you use a symbol are missing #include.
Together these provide analysis similar to the include-what-you-use tool, with some differences[1].
We’ve had several requests to make this available for reuse:
- @serge-sans-paille has been cleaning up LLVM and wants better tools. D121593
- On the tracking bug for this feature, we get requests for a clang-tidy version of it
- we’ve got at least 2 internal-only clang-tidy checks implementing special cases of IWYU. A generic upstream check would benefit everyone.
- inside Google, a team wants to do ~completely automated continuous cleanups (and to ensure the policy enforced matches clang-tidy + clangd)
What do people think of lifting this up and generalizing a bit (to support suggesting new includes) as a library in clang-tools-extra? That way it could be shared between clangd, a new clang-tidy check, and our downstream deployment.
At alternative: there was an RFC in the past to merge IWYU itself into clang-tools-extra. It seems to have gotten stuck over the spectre of modules, and whether it should be incrementally rewritten.
Main differences from IWYU:
- lightweight implementation suitable for fast analysis with preambles
- conservative about suggesting changes to ensure a low false-warning rate (at the cost of missing some suggestions)
- no attempts to work out when forward declarations are possible (but respects them)
- enforces simplified (selectable) style rules rather than than trying to track compilation requirements exactly
- because of the above, much simpler implementation
FWIW, the IWYU implementation is not suitable for clangd (lack of preamble support and generally does too much). It was also judged unsuitable for the continuous cleanups, where the policies need to be simple and reliable for transparency, and we don’t have confidence we can make IWYU reliable.