Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using c++ ast to determine unused headers (iwyu for gcc).

33 views
Skip to first unread message

Glen Stark

unread,
Nov 23, 2014, 8:02:09 AM11/23/14
to
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
for g++.

I'm a reasonably competent and knowledgeable c++ programmer, and it would
be my language of choice for implementing the tool.

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
getting started.

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.

Thanks for your time,

Glen

Marcel Mueller

unread,
Nov 23, 2014, 9:51:11 AM11/23/14
to
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
> for g++.

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
code.

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
> getting started.

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.


Marcel

Öö Tiib

unread,
Nov 23, 2014, 4:24:06 PM11/23/14
to
On Sunday, 23 November 2014 15:02:09 UTC+2, 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
> for g++.

One of driving forces behind of why clang is here is that g++ is sort
of useless for making development tools from it. The C++ tools made with
using clang are not only for clang.

> I'm a reasonably competent and knowledgeable c++ programmer, and it would
> be my language of choice for implementing the tool.
>
> 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
> getting started.

The AST of g++ is partially optimized sometimes so on occasions even the
diagnostics of its own say weird things.

> 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.

Major difficulty are likely related with preprocessor.
For example if some header is included only because of a macro and
preprocessor has already expanded the macro then your tool will likely
give false positive that the header is not needed.
Or for other example if some header is included only for code that is
compiled conditionally under certain defines and you analyse it
with other defines then it will again give false positive.
0 new messages