Use cases related to #include refactoring

I have two use cases for refactoring related to header files / inclusions and I’m wondering if there is any way to accomplish them with clangd:

  1. sometimes the #include of a particular header is actually not needed - maybe due to some previous refactoring that forgot to remove the (now) useless header. Is there some way to have clangd help me identify such cases where a particular #include serves no purpose in the given TU?

  2. sometimes when tracking down dependencies, I want to see what symbols/definitions/macros in the current TU come from a particular header. Is there a way to get a list of symbols in the current TU that came from a given header? Given that information, it would be possible to do determine how much or how little a given TU depends on a particular header, which can be helpful when trying to break dependencies, for example.


#1 is supported in clangd 14, see Include Cleaner.

I’m not aware of anything like #2, but it seems like something like that could be built on top of the same infrastructure.

Yup, we’re working on generalizing the current narrow include-cleaner feature into a library that can

  • diagnose unused includes
  • diagnose use of transitive includes (i.e. missing includes per IWYU style)
  • show symbols used via an #include

So all those will (eventually) come to clangd, and we also have a clang-tidy check etc more suitable for batch use.

CC @kadircet

1 Like

Some more details: [RFC] Lifting "include cleaner" (missing/unused include detection) out of clangd

1 Like