Brain Phrye

code cooking diy fiction personal photos politics reviews tools 


Static analysis with autoconf

Static analysis is one way to root out more complex bugs in C and C++ programs. And clang offers a static code analyzer. To make use of it as an analyze target in an autoconfed project, just add this snippet to your Makefile.am.

Put the filenames you want analyzed on the analyze_srcs line. If you have any local include dirs, just add them as -I flags to the clang lines.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Static analysis.
analyze_srcs = protobuf-c-text/generate.c protobuf-c-text/parse.c
.PHONY: analyze
analyze: all
        @for f in $(analyze_srcs); do \
          if test "@AM_V@" = 1; then \
            echo clang --analyze -x c -I build-aux $$f -o /dev/null; \
          else \
            echo "  CCSA  " $$f; \
          fi; \
          clang --analyze -x c -I build-aux $$f -o /dev/null; \
        done