Static Analysis With LLVM/Clang in Linux

Recipe # | posted in Linux, Static analysis | Comments

1 – Problem Description

How to install LLVM/Clang, configure CMAKE and scan your project

2 – Solution

Get LLVM and Clang

cd ~/
git clone http://llvm.org/git/llvm.git
cd llvm/tools
git clone http://llvm.org/git/clang.git

Checkout Compiler-RT

cd ~/
cd llvm/projects
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
cd ../..

Build LLVM release (10x faster) and Clang and install it in your home

mkdir build (for building without polluting the source dir)
cd build
../llvm/configure --prefix=$HOME --enable-optimized
make
make install

This will install LLVM and Clang in your ~/bin folder. Copy from the original Clang folder the helper scripts to the ~/bin folder

cd ~/llvm/tools/clang/tools
cp scan-view/* ~/bin
cp scan-build/* ~/bin

Set the appropriate CMAKE_C_COMPILER and CMAKE_CXX_COMPILER variables to Clang helpers

cmake -G "Your Generator" -DCMAKE_C_COMPILER=/home/your_user_name/bin/ccc-analyzer -DCMAKE_CXX_COMPILER=/home/your_user_name/bin/c++-analyzer path/to/your/source

Then

cd path/to/your/source
scan-build -o static_analysis_results make

At the end of the analysis run

scan-view path/to/source/static_analysis_results/date_of_scan

to examine results.

GO FIX THEM! :P

3 – References

[1] LLVM/Clang

[2] Cmake

Comments