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
cd llvm/tools
git clone

Checkout Compiler-RT

cd ~/
cd llvm/projects
svn co 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 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


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.


3 – References

[1] LLVM/Clang

[2] Cmake