PDA

View Full Version : A tool to remove unused header files?



agarny
13th July 2012, 09:34
As the title says... is anyone aware of a tool (ideally, as part of Qt Creator) to remove unused header files in a CPP file?

Cheers, Alan.

wysota
13th July 2012, 09:49
There is no such tool. I don't see how you'd like to detect if a header file is "used" or not.

You can have relationships between header files such as those:


/* a.h */
#define A 7


/* b.h */
#ifndef A
#define A 8
#endif

and then you have:

#include "a.h"
#include "b.h"
So which ones are "used" and which are not?

agarny
13th July 2012, 09:54
I would call what you gave as an example as very bad programming, but maybe that's just me?...

wysota
13th July 2012, 10:23
I assure you that you daily use a lot of headers such as these.

agarny
13th July 2012, 10:29
Sadly, I wouldn't be at all surprised if you were right and, in that context, I appreciate your original comment.

Otherwise, someone pointed me to https://code.google.com/p/include-what-you-use/. I wonder how that (beta) tool would handle your example...

wysota
13th July 2012, 13:26
No idea. However no tool will be able to detect that both files are needed as when a.h is present then b.h is not needed but once you remove a.h (or better yet have an ifndef there as well) b.h (or equivalent) is required despite the fact it is not used originally. It becomes even more complex with:


#ifdef Q_OS_UNIX
#include "a.h"
#endif
#include "b.h"

I'm sure I can find myrriads of other examples when such automatic cleanup would fail. Sorry but no tool can replace the process of thinking about what one's doing.