HOWTO use the scan-build Code Analyzer

From GlueXWiki
Jump to: navigation, search

Introduction

The clang-llvm C++ compiler has built-in code analyzer abilities that can be activated using the scan-build tool. This allows a level of diagnostic beyond the warnings and errors that result from compilation. It can be used fairly easily on any software package provided the clang compiler is available and the build system works in a way compatible with scan-build.

clang-llvm location

The clang-llvm compiler has been built and installed on the Hall-D group disk for the Linux_CentOS6-x86_64-clang3.2 platform. It is located in the group/halld/Software/ExternalPackages/clang-llvm directory tree. One can set up their environment to use the compiler on ifarms01 by sourcing the appropriate setenv.csh script. For the ifarm12s01 system, one would do this:

source /group/halld/Software/ExternalPackages/clang-llvm/svn163307/Linux_CentOS6-x86_64-gcc4.4.6/setenv.csh

This will set the CXX and CC environment variables to use c++ and cc respectively. It also adds the clang bin directory to the front of your PATH which has the clang and clang++ programs in it as well as symbolic links for c++ and cc that point to them. It also has links to scan-build and scan-view so that they are immediately available via PATH as well.

Running the analyzer

To run the analyzer and produce a set of html files containing the report, one uses scan-build to invoke the build command. For example, for a package using make, one could do the following:

scan-build -o html make

The "-o html" option given above will create a local directory named "html" where a subdirectory containing the report will be placed. If this option is not given, then the results are written to a directory in /tmp with an auto-generated name.

An example of the output can be seen here:

Example scan-build output


Make and Scons

For the analyzer to work, the build system has to emit the compile command in a way that scan-build can intercept and replicate with the compiler itself replaced by the appropriate clang analyzer command. The system seems to work OK with make, but I was unable to get a successful analysis using scons. It is unclear why the compilation failed when run through scan-build with scons. I note this here merely to mark my experience in case it benefits anyone trying to build via scones using this tool.