0

How to detect not used(not needed) header files included in other .hpp and .cpp files?

Maybe it can be done using some static analysis tool?

mrgloom
  • 15,245
  • 23
  • 126
  • 226
  • Delete them. If it still compiles they weren't necessary. – tadman Dec 02 '16 at 16:39
  • 2
    Hardly. Deleting them could arbitrarily change behavior while still compiling. Imagine a header that redefines a macro, defines a template specialization, or a function overload or a list of a million other things. – BeeOnRope Dec 02 '16 at 16:41
  • @BeeOnRope I've never heard of code that arbitrarily changes when doing a straight build because of a missing header file. Either the file is there or it isn't. If a header file is necessary it will error out as missing. – tadman Dec 02 '16 at 16:42
  • 2
    BeeOnRope gives three examples in his unedited comment, so I'm not really sure how it is possible that you've never heard of it.... – Cody Gray Dec 02 '16 at 16:56
  • 2
    Often, a compiler will have such a static analysis tool built into it. MSVC has one, for example. This will show all of the headers that your file(s) depend on. Anything *not* listed there is obviously unused. But still, be careful. The best tool is the one between your ears. – Cody Gray Dec 02 '16 at 16:57
  • @tadman Then you have never worked on a big, messy project I suppose :) It's not _common_ weighted by header file, but in a big enough project it will happen in many places. A common real-world example is a head file which defines functionality and makes it available via some macro XYZ_AVAILABLE, and then later files test it like `#ifdef XYZ_AVAILABLE` and generates different code. Another real-world example I've seen is a bunch of operator overloads to pretty print classes defined elsewhere. Is this good practice? Perhaps not. Does it happen? Yeah. – BeeOnRope Dec 02 '16 at 17:12
  • @tadman But to delete them I need to find them and I what to do it automatically by some tool. – mrgloom Dec 02 '16 at 17:37
  • @BeeOnRope I get that a file not being loaded can change behaviour, but files don't get loaded in by wildcard or some other method that's indifferent to missing files, they need to be specified explicitly. Is there a case where `#include "x.h"` will *succeed* if `x.h` is not present? The only way I can see this happening is if you have multiple files with exactly the same name and multiple `-I` include paths specified, but that's hazardously bad practice. – tadman Dec 02 '16 at 17:37
  • @tadman of course not, if the file doesn't exist it will fail. I'm not following you though - how does that related to your claim that you can determine if `x.h` is _used_? You can interpret _used_ in a few ways, mostly something like "doesn't change the resultant binary" or "doesn't change the semantics or behavior of the problem". – BeeOnRope Dec 02 '16 at 17:40
  • 1
    Now I understand your point. When you said "delete them", I thought you meant "delete the `#include` lines", but I understand now you mean "delete the files on disk". The latter approach will definitely tell you if a file is ever transitively _included_, but the OP is asking about files which are definitely _included_, but are not actually _needed_. – BeeOnRope Dec 02 '16 at 17:42
  • ... for the problem of determine which files are actually transitively included, the solution is simple: `makedepends` or any of the various compile options that do the same thing. – BeeOnRope Dec 02 '16 at 17:42
  • I mean explicitly referenced, as how else can a header file impact your code? I know `Configure` scripts work lots of dark magic, but this is in the case of an existing project with a multitude of `.h` files. – tadman Dec 02 '16 at 17:42
  • @tadman - see my comments above. I think I understand better your comment now, but it's different than what the OP is asking. – BeeOnRope Dec 02 '16 at 17:43
  • @BeeOnRope Yeah, I mean delete the files, not the references. You can easily restore those one by one using any version control system. – tadman Dec 02 '16 at 17:43
  • If they're included it's almost impossible to tell if they're not "needed" as that's a very subjective call, it requires reading the actual header file and understanding the implications. Often that's like trying to determine of a CSS rule is used. The problem often ends up being wickedly complicated. – tadman Dec 02 '16 at 17:44
  • 1
    @tadman yup, it's, it's complicated. There are lots of good suggestions on the linked duplicate. – BeeOnRope Dec 02 '16 at 17:58

0 Answers0