5

I've recently discovered clang++'s static analyzer feature, and it's fantastic for going over my code with a fine-toothed comb to find latent bugs. I just uncomment this line in my Makefile:

CXXFLAGS += --analyze -Xanalyzer -analyzer-output=text

et voila, I'm in deep-bug-checking mode.

One minor problem with this, however, is that whenever the analyzer does not find any problems in a particular .cpp file, doesn't produce any .o file.

Normally that wouldn't be a big deal (I can always re-comment the above line to build an actual executable), but usually when I see an analyzer-warning, the first thing I want to do is try to fix the underlying problem and then re-run make.

... which works, but since no .o files are being generated, make will start re-analyzing all the .cpp files again from the beginning, rather than just the .cpp files I actually modified since the previous run. This means I end up spending rather a lot of time re-checking .cpp files that haven't changed.

My question is, is there any way to get the static analyzer to output a .o file (it doesn't have to be a valid object file, just any file with an updated timestamp) so that Make will know that a "clean" .cpp file does not need to be re-processed? (i.e. make Make work the same way it does when doing a normal compile)

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • I don't think you really want to create a non-valid object file. If you do then make won't rebuild that file as a real object file (because its timestamp is newer than the source file) and the link will fail. – MadScientist Sep 11 '13 at 01:58
  • That would be okay with me, I'd just do a 'make clean; make' to recover. At least, it would be better than having to re-analyze (N-1) files in order to re-analyze the Nth one. – Jeremy Friesner Sep 11 '13 at 04:51
  • True but it seems needless. Why not just add rules to build the analysis to some other file, not an object file? As long as the analysis touches SOME file it will be enough to avoid redoing the build, and if you don't mess around with the object files then you get the best of both worlds. Anyway, I see you've found a solution that works for you. – MadScientist Sep 11 '13 at 13:08

1 Answers1

5

Check out the clang static analyzer page, and get the package there for download. You can use the included scan-build tool to do what you're trying.

The normal way to use is to get rid of the flags you have above and just run:

$ scan-build make whatever

And it should 'just work'. You might need to pass some more flags or set some environment variables if you don't use standard make variable names.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469