include cleanup

F

Florian Kaufmann

Hello

Is there a tool that helps to keep the list of include directives
clean and tidy? I would like that each header/source file has only the
includes that it really needs, and that forward declarations are used
instead of includes where possible.
I can imagine that this task is impossible to solve in general, but I
hope there are tools that get the job at least partially done.

Flo
 
V

Victor Bazarov

Florian said:
Is there a tool that helps to keep the list of include directives
clean and tidy? I would like that each header/source file has only the
includes that it really needs, and that forward declarations are used
instead of includes where possible.
I can imagine that this task is impossible to solve in general, but I
hope there are tools that get the job at least partially done.

I call it "the combination of your text editor and the compiler".
Start by removing all includes and then include only those that are
needed.

V
 
F

Florian Kaufmann

I call it "the combination of your text editor and the compiler".
Start by removing all includes and then include only those that are
needed.

Yes, that is surely possible. However, the automated approach has the
advantage that the work is done by someone else (the tool) and I can
concentrate on the important stuff. I imagine it quite time consuming
to do the manual approach every half year or so for all the hundredths
source/header files.
 
W

Walter Roberson

Hello

Is there a tool that helps to keep the list of include directives
clean and tidy? I would like that each header/source file has only the
includes that it really needs, and that forward declarations are used
instead of includes where possible.

[OT]
The latter part of that is not recommended. If you have the same
function declaration in multiple files, then when you change
the declaration, you will have to go around to each file that
textually declares it and modify the code. That process is error
prone; it is pretty much inevitable that the declarations *will*
get out of sync. If, however, you #include the declaration of
the header, then you only need to modify one place, the #include
file, after which the compiler will be able to catch any
now-incompatible calls to the function.
 
A

anon

Florian said:
Hello

Is there a tool that helps to keep the list of include directives
clean and tidy? I would like that each header/source file has only the
includes that it really needs, and that forward declarations are used
instead of includes where possible.
I can imagine that this task is impossible to solve in general, but I
hope there are tools that get the job at least partially done.

Flo

try this:
http://www.stolk.org/idep-0.5.tar.gz
 
F

Florian Kaufmann

The latter part of that is not recommended. If you have the same
function declaration in multiple files, then when you change
the declaration, you will have to go around to each file that
textually declares it and modify the code. That process is error

You are right - I wasn't specific enough. I am aware of the general
rule that any kind redundancy is to be avoided. What I meant are the
kind of forward declarations that are often done in C++ like

class A;
class B { A& a;};

Flo
 
J

James Kanze

I call it "the combination of your text editor and the
compiler". Start by removing all includes and then include
only those that are needed.

That will usually result in too many includes being used---if
the includes A.hh "incidentally" includes B.hh, for example,
this technique will result in your removing the include of B.hh,
even if you need it. From experience, I know that I often have
to add includes that I've forgotten when moving between
different implementations of the standard library.

Also, it doesn't consider the possibility of using a forward
declaration, rather than an include.

What you really need is a tool which looks at every symbol in
the file, and determines where it was defined, and whether a
complete type was needed or not. For symbols for which a
complete type is required, it outputs the name of the file in
which the type was defined; for the others, if the type is a
class type, it outputs a simple "class T;", with the name of the
file where the symbol was defined in comments. (The user must
then intervene to determine whether the symbol is guaranteed to
be a class, or whether future evolution might replace the class
with e.g. a typedef to a template instantiation. In the latter
case, you need the include, even if the forward declaration
works at present.)
 
B

Billy Bong

Hello

Is there a tool that helps to keep the list of include directives clean
and tidy? I would like that each header/source file has only the
includes that it really needs, and that forward declarations are used
instead of includes where possible.
I can imagine that this task is impossible to solve in general, but I
hope there are tools that get the job at least partially done.

Flo

PC-lint will do this.

http://www.gimpel.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top