Development/Clang Code Analysis
CLANG Code Analysis
This short tutorial aims to describe how to run the clang static code analyzer to generate reports.
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.
Install LLVM/CLANG from source
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.
Install C++ Analyzer
In past versions of the build system, the C++ analyzer did not get installed when running 'make install'. This was because the LLVM/CLANG developers considered 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/
In current versions of the build system, the analyzer does get installed when running 'make install' so this step is no longer necessary when installing from source.
Install LLVM/CLANG packages
You can also choose to install the pre-build binary packages that come with your Linux distribution. On Fedora, you can install the packages by running this command:
sudo yum install llvm clang clang-analyzer
On Debian, you can do so like this :
sudo apt-get install llvm clang
Configure and build LibreOffice
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: http://clang-analyzer.llvm.org/scan-build.html
scan-build --use-cc=clang --use-c++=clang++ ./autogen.sh
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.
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
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.