[OT] Identifying unused include directives?

Randy.Dunlap rddunlap@osdl.org
Sun Aug 31 14:00:00 GMT 2003


> Robert Dewar <dewar@gnat.com> writes:
>
>> Yes, that sounds like a reasonable definition, although the only practical
>> way of having the compiler test this would be to compile with and without
>> each #include.
>
> Given that this is the sort of thing that one could reasonably run
> periodically on one's entire source base, I'm not sure that's necessarily a
> bad way of approaching it.  It seems like one could write a small program to
> do this without changing gcc at all; it probably wouldn't handle the edge
> cases for include via preprocessor abuse, but for the common case it
> wouldn't be particularly difficult.
>
> It would be slow, but it's the sort of thing one could run overnight and
> then look at the results later.  It's not like there's generally much
> urgency to removing unnecessary includes.
>
> One difficulty in applying this to a practical problem is that the set of
> necessary includes frequently varies by platform.

If someone is particularly inclined, they could generalize a Perl
script that I did last week to find unneeded includes of linux/version.h
in Linux source files, or places where it is needed but not included.
I hard-coded the contents on version.h in Linux 2.6.0, but the #defines
there could be read and searched for in the target file(s).

~Randy
script: http://developer.osdl.org/rddunlap/scripts/checkversion.pl





More information about the Gcc mailing list