Development/Clang Code Analysis

From The Document Foundation Wiki
Jump to: navigation, search

CLANG Code Analysis

This short tutorial aims to describe how to run the clang static code analyzer to generate reports.

Example results

Example generated results can be found here:


Set up build environment

Follow the guidelines at : How to Build to set up your LibreOffice build environment.


First you need to install LLVM and CLANG. Installing from the latest sources is recommended, as at the time of writing this tutorial c++ analysis is still very much a work in progress, and it makes sense to use the latest code with the best support for c++ analysis. You can install from scratch by following this guide:

When running ./configure llvm/clang, please specify '--enable-optimized'. The default build generates a (bigger, slower) debug build. This switch generates a release build of llvm/clang.

../llvm/configure --enable-optimized

Install C++ Analyzer

When you run 'make install', the C++ analyzer does not get installed. This is because the LLVM/CLANG developers consider the analyzer to be a separate project even though it lives inside the clang tree. You need to copy it to a directory in your path, for example /usr/local/bin

cp ../llvm/tools/clang/tools/scan-build/* /usr/local/bin/
cp ../llvm/tools/clang/tools/scan-view/* /usr/local/bin/

Configure and build LibreOffice

Run ./

First you need to configure your build environment for analysis. This is done by running the 'scan-build' command. A short tutorial about it can be found here:

scan-build --use-cc=clang --use-c++=clang++ ./

Note: 1. If you have clang installed in a non standard path, you need --use-analyzer=[directory of your clang binary file] even if clang is already in $PATH. 2. Error message "Failed to run autoconf" could be caused by old versions of autoconf. Autoconf2.13 is default installed with Ubuntu14.04 which is not enough. Autoconf2.64 provides successful experience.

Run ./

Now we can build and analyze the libreoffice code. By default the analyzer works by inserting the clang analyzer and performing the analysis before the code gets build. It does this by using clang for analysis, and the default compiler (GCC in this case) alone. This is the least intrusive way of functioning for clang. However, although clang is meant as a drop-in replacement for GCC, it is not 100% compatible in some corner-cases. Unfortunately, some of these are hit in LibreOffice. Therefore we need to override the default behavior and also compile with clang using '--use-cc=clang --use-c++=clang++'.I ran into weird issues when using ccache, where the analyzer tried to analyze all the files located in the cache. So I disabled it using '--disable-ccache'.

I enabled a 'debug build' of LibreOffice, as there are some instances where debug code can assist the analyzer.

As much of the system libraries instead of the internal libreoffice supplied ones are used by specifying '--with-system-libs'.

scan-build --use-cc=clang --use-c++=clang++ ./configure --disable-ccache --enable-debug \ 
--with-system-libcmis=no --with-system-hsqldb=no --with-system-saxon=no --with-system-libs

Run make

You can now analyze+build LibreOffice.

mkdir /tmp/libreoffice
scan-build --use-cc=clang --use-c++=clang++ -o /tmp/libreoffice make


You should now have the generated HTML report files.