Skip to content

Code checkers

Günter Wirth edited this page May 16, 2014 · 24 revisions

Here is a quick guide how to run the supported analyzers and generate reports such that it fits SonarQube's needs.

Cppcheck

In order to run Cppcheck and generate a fitting report, make sure:

  • to call it from the projects root directory
  • to pass all include directories (using -I <path>) as otherwise the analysis will be incomplete
  • that the parameter matches the sonar.sources list in sonar-project.properties
  • to create a XML-report using the parameter --xml-version=2 for version 2 or --xml for version 1. Both versions are supported, version 2 is the default.
  • to get the report from the standard error channel

Example command line:

cppcheck --xml file1.cpp 2> report.xml
cppcheck --xml-version=2 file1.cpp 2> report.xml

cppcheck -v --enable=all --xml -I[include directory] [sources] 2> report.xml

Example report files:

V1:

<?xml version="1.0"?>
<results>
  <error file=".\file1.cpp" line="123" id="someError"
               severity="error" msg="some error text"/>
</results>

V2:

<?xml version="1.0" encoding="UTF-8"?>
<results version="2">
   <cppcheck version="1.53">
      <errors>
         <error id="someError" severity="error" msg="short error text"
                    verbose="long error text" inconclusive="true">
            <location file=".\file1.cpp" line="1"/>
         </error>
      </errors>
</results>

A Cppcheck run may take a while on a big code base. To cut down analysis times, check the following options:

  • Use -j N option to run N workers in parallel
  • Use only checks you're interested in via the option _--enable=<check>_
  • Restrict checking of preprocessor configurations using the options -D -U
  • Start with project include folders (-I) without system include folders. System include folders and include folders of big libraries like Boost, XERXES, ... make Cppcheck run much slower.
  • Get a faster machine ;)

To extend the set of known Cppcheck rules see Extending the code analysis.

Valgrind

SonarQube can be fed with results of a Valgrind/Memcheck analysis. That's very valuable because:

  • due to its dynamic nature, the false positives rate is very low (Given good maintained suppression files, of course)
  • it is able to find serious issues which are often hard to detect otherwise

Just tell Valgrind to generate XML output. The 'tool' option isn't necessary as 'memcheck' is the default one. Make sure the binaries contain debug info. The actual call should look something like:

valgrind --xml=yes --xml-file=report.xml <program> <arguments>

To extend the set of known Vagrind rules define them in the file $SONARQUBEHOME/extensions/rules/valgrind. See Extending the code analysis for details.

Vera++

Vera++ does static C++ code checking, focusing mostly on style issues. To feed Vera++ analysis results into SonarQube:

  • Find all the files we want to be analysed
  • Pipe this list into Vera++ and
  • Pipe the resulting output into a Perl script which finally generates the required XML.

Altogether:

find <path> -regex ".*\.cc\|.*\.hh" | vera++ - -showrules -nodup |& vera++Report2checkstyleReport.perl > report.xml

To extend the set of known Vera++ rules define them in the file $SONARQUBEHOME/extensions/rules/vera++. See Extending the code analysis for details.

RATS

RATS stands for "Rough Auditing Tool for Security". This tool performs static C++ code checks focusing mainly on security issues. Just tell it to create XML output and redirect the standard channel into a file:

rats -w 3 --xml <sources> > report.xml

To extend the set of known RATS rules define them in the file $SONARQUBEHOME/extensions/rules/rats. See Extending the code analysis for details.

Pc-Lint

The Pc-Lint XML output needs to be formated to fit SonarQube.

// XML options for SONAR.
-v // Turn off verbosity
-width(0,0) // Don't break long lines
+xml(?xml version="1.0" ?) // add version information
+xml(results) // Turn on XML escapes
-"format=<issue file =\q%f\q line = \q%l\q number = \q%n\q desc = \q%m\q/>"
-"format_specific= "
-hFs1 // The height of a message should be 1 i.e. don't output the line in error
-e900 // 'Successful completion message' confuses ALOA

This formatting has been verifed with Pc-Lint 9.0i. For further details on how to configure Pc-Lint please refer to product page.

To extend the set of known Pc-Lint rules define them in the file $SONARQUBEHOME/extensions/rules/pclint. See Extending the code analysis for details.

Note: Rules for this tool are disabled by default, so they need to be enabled in the relevant quality profile before they can be imported into SonarQube.

Clone this wiki locally