Re: using c++ ast to determine unused headers (iwyu for gcc).
On 23.11.14 14.01, Glen Stark wrote:
I would like to write a tool to determine which (if any) #include
statements are superfluous to a particular c++ file. I know that there's
a project to do this using clang, but I would like to do the same thing
You should know that this cannot be solved by a algorithm only. Simply
because #include 1 might depend on #include 2 and so each file that
includes #include 1 does no longer need #include 2 directly. But the
dependency of #include 1 on #include 2 might or might not be a
guaranteed property. So including #2 might be required to have portable
Second example. Think of a .h/.cpp pair for a class. The .cpp always
include the corresponding .h, so any include dependency satisfied with
indirect include from .h need not to be repeated in the .cpp file. But
if your .h includes a .h from another class, then the indirect includes
from the other header might be removed in the other header in the future
resulting in a build break at an unexpected location. And if you are in
a library the build break may happen in another project where no build
server will check this for you.
I'm a reasonably competent and knowledgeable c++ programmer, and it would
be my language of choice for implementing the tool.
I would prefer a Eclipse CDT Plugin. Simply because you have a framework
there that knows about references of symbols. But be aware even CDT does
not understand all flavors of C++ dependencies. So you might remove too
much includes in rare cases.
As I understand it the gcc preprocessor provides a complete ast which
would allow me to do everything that's needed, but I'm having a hard time
This are technical dependencies. They are not portable as mentioned above.
Can anyone help me out? I'm particularly interested in links to good
documentation, but if anyone has any tips how they would go about doing
something like this, I'd be pleased hear them.
You need a database with relations of standard symbols to standard
include files. Then you can check the files for this standard symbol
references and if you get a hit you know the particular include should
be referenced from that file.