This page documents how to generate cppcheck reports in the form of a browseable tree of sources files.
Cppcheck is an open source static code analyzer tool for C/C++ programming languages. So its goal is to detect some bugs (other than syntax errors).
Example generated results can be found here: https://dev-builds.libreoffice.org/cppcheck_reports/
Generated by this script: https://gerrit.libreoffice.org/gitweb?p=dev-tools.git;a=blob;f=cppcheck/cppcheck-report.sh
Retrieve LibreOffice sources
Retrieve LibreOffice sources in a <LO sources> directory with this command:
$ git clone git://anongit.freedesktop.org/libreoffice/core libo
(see Development/Native Build for more details)
Retrieve and build cppcheck sources
You can install cppcheck from distribution packages but it's better to retrieve last sources with git since there are improvements everyday. So choose and go into <cppcheck directory> then run these commands to retrieve sources and build cppcheck:
$ git clone git://github.com/danmar/cppcheck.git $ cd cppcheck $ make
Building takes less than a minute, but it can be sped up with
-j4 make option.
python-pygments required by cppcheck-htmlreport report builder.
To summarize, you've got to generate an xml file containing all the warnings/errors detected by cppcheck. Then you must launch a script to generate a browseable tree from the xml file + LibreOffice sources. Cppcheck allows a lot of options but here are some simple steps: - go to <LO sources> directory - create a file named "cppcheck_supp.txt" which contains only unusedFunction - run this command to launch cppcheck detection:
$ <cppcheck directory>/cppcheck -i external/ -i workdir/ --xml --suppressions-list=cppcheck_supp.txt --enable=all --max-configs=25 ./ 2> ./err.xml
(-i option allows to remove some directories to check, --suppressions-list allows to disable some types of detection, --enable=all allows to detect every types of detections, removing it allows to have only errors) Notice that on i5 with 6Gb, it takes several hours to have the result - run this command to generate the browseable tree:
$ <cppcheck directory>/htmlreport/cppcheck-htmlreport --file=err.xml --title=LibreOffice --report-dir=cppcheck_reports --source-dir=.
You'll get a new directory called cppcheck_reports in your LO sources. The main page to browse in it is index.html With the configuration mentioned above, this part only takes a few minutes.
Checking cppcheck's configuration
The above starter example reports ~7000 errors, nearly all of which are false positives. To see why you can add the
--check-config parameter to analyse includes. The resulting report shows that cppcheck cannot find any of our header files.
Headers can be included 2 ways
--I include/ Adding this parameter alone, eliminates ~ 1 thousand false positives, but it leaves out all of the headers in inc/
---includes-file=inc.txt with this inc.txt file: https://pastebin.com/qGfPRBJ6
Teaching cppcheck about all of our header locations drops the number of errors found from 7K to a few hundred. However it also creates 2 new problems: speed and ~10K "toomanyconfigs" warnings.
An example of 2 configurations defined by A is this:
#ifdef A x = y; #else x = z; #endif
By default cppcheck will check all preprocessor configurations (except those that have #error in them). So the above code will be analyzed both when A is defined and when it is not
We can speed things up by not checking our DEBUG configurations. We can also eliminate even more false positives by not checking configurations that are missing the system header files. For example on Linux, we should add -U_WIN32 to not check Windows configs.
Here is the result of my tweaking:
$ cppcheck -U_WIN32 -i external/ -i workdir/ --includes-file=inc.txt --xml --suppressions-list=cppcheck_supp.txt --enable=all --max-configs=100 ./ 2> ./err.xml
This command is not perfect, but it greatly improved the signal-to-noise ratio, only showing a few hundred errors. The run time went from about from approx. 2 hours to 8, but I'm sure that can be by further by reducing the number of configurations checked (-D and -U).
All of these commands are explained in detail on the cppcheck manual: http://cppcheck.sourceforge.net/manual.pdf