This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

new switch that warns about missing includes


Hi folks,

I am thinking of a switch for the gcc that warns you about missing
includes of standard header files to get higher reliablity of your
code.

I think of the following situation:

a.hh:
----
#include <string>

extern std::string foo;
----

b.hh:
----
#include "a.hh"

extern std::string bar;
----

I think it would be better to write b.hh as follows:
b.hh:
----
#include <string>
#include "a.hh"

extern std::string bar;
----

This leads to more reliable code in repect to changes during the
software development process,
because you can change the includes in a.hh without affecting
compilation of files that include b.hh (b.hh would become uncompilable
because string could become missing).
I think, every .hh file should include the files that define the
things that it uses directly.
I think a switch that can warn about such dependencies would be
very nice and should suggest to include <string> in b.hh because
std::string occurs directly in b.hh. 
The situation can be more complicated when include
hierarchies are more complicated. Often the standard headers are
only small wrappers that include other files... and the user don't
want to get warnings that he should included the 
library internal files instead the standard files.
So the implementation has to be done 'intelligently'x.

I don't know exectly how to implement this. Maybe you are
interested to think about it.

Some thoughts about this:
The compiler has to know which headers are standard headers.
The compiler has to analyze which identifiers are used in a
header file and are defined in a standard header that is not included
directly in this file. 

Maybe this is not only useful for standard headers.
It could be steered by additional information while including things:

c.hh:
----
typedef int newint;
----

a.hh:
----
#pragma BEGIN_INCLUDE_FOR_LOCAL_USE
#include <string>
#pragma END_INCLUDE_FOR_LOCAL_USE

#pragma BEGIN_INCLUDE_FOR_PROPAGATION
#include "c.hh"
#pragma BEGIN_INCLUDE_FOR_PROPAGATION

extern std::string foo;
----

b.hh:
----
#include <string>
#include "a.hh"
#include "c.hh"

extern std::string bar;
extern newint foobar;
----

Not including <string> in b.hh would warn the user if compiling with
the new switch.
Not including "c.hh" in b.hh would not warn the user if compiling with
the new switch.

(Please assume correct include guards for all .h files, I haven't 
written them here to focus on the important stuff)

Are there already solutions to analyze such things?
Could this be interesting for GCC? (Maybe with a completely other
way to achieve the same goal?)
Does other compilers have such nice features?

Thanks for your comments and additional ideas in advance.

Florian Schintke
-- 
Florian Schintke <schintke@zib.de>, http://www.zib.de/schintke/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]