3

I'm looking for a tool which can do at least one of two things.

  • Guess what headers might be unused and can be removed.
  • Guess which headers should be included in a file, but are included indirectly through inclusion of other files. Thus allows proper compilation of the file.

Is there such a tool?

ronag
  • 49,529
  • 25
  • 126
  • 221
  • Similar question: http://stackoverflow.com/questions/3644293/are-there-tools-that-help-organizing-includes – Frank Nov 08 '10 at 17:01

2 Answers2

4

You could use GCC warnings "-Wmissing-declarations" and "-Wredundant-decls". It's not precisely what you want, but might help a lot.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • This might work. However, it will not work for me as I'm a VS user. I'm leaving it open so that non VS users might also find something useful from potential answers. – ronag Nov 08 '10 at 16:07
1

A colleague of mine wrote a very simple script to achieve part of this (and slow too...).

Basically the idea is to try to comment each include in turn and then try to compile the object, it doesn't deal with include within headers but already remove a substantial number of unused files :)

EDIT:

Pseudo code of the algorithm

for s in sourceFiles:
  while t := commentNextInclude(s):
    if compilationOk(): s := t

As I said, comment each #include in turn, and each time check if the program still compiles, if it does, validate the commenting, and move on to the next one.

I don't have the rights to disclose the script source though.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • Would you mind to share a bit more details regarding this "simple" script? – ronag Nov 09 '10 at 12:28
  • this might lead you to remove "valid" includes, though, because some previous header already indirectly included the code you need. – jalf Nov 09 '10 at 16:00
  • @jalf: yes, it's very rough, but I don't really mind this case of re-inclusion. More annoying would be the removal of template specialization or function overloads without which the file still compiles, but produces a different result. This one is annoying, but rare, and shall be caught by testing. It normally doesn't happen unless the methods have been thrown haphazardly in the headers... – Matthieu M. Nov 10 '10 at 07:31