-
Notifications
You must be signed in to change notification settings - Fork 362
Code checkers
Here is a quick guide how to run the supported analyzers and generate reports such that it fits SonarQube's needs.
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.
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 Valgrind rules see Extending the code analysis.
SonarQube can be fed with results of a Dr. Memory analysis.
Just launch your program with Dr. Memory. The actual call should look something like:
drmemory.exe -logdir c:/logs -- c:/path/to/my/app
Now, you just have to fill the sonar.cxx.drmemory.reportPath:
sonar.cxx.drmemory.reportPath=c:/logs/**/results.txt
Vera++ does static C++ code checking, focusing mostly on style issues. There are two possibilities to create a report.
Newer versions of Vera++ support the --checkstyle-report (-c)
command line option to create a Checkstyle XML report:
vera++ -s -c vera3.xml main.cxx
Another possibility with older versions is to convert the output wit a Perl script. 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
An example output could looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="5.0">
<file name="main.cxx">
<error source="T013" severity="info" line="1" message="no copyright notice found" />
<error source="T011" severity="info" line="2" message="closing curly bracket not in the same line or column" />
</file>
</checkstyle>
To extend the set of known Vera++ rules see Extending the code analysis.
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 see Extending the code analysis.
The PC-lint XML output needs to be formatted 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'
This formatting has been verified with PC-lint 9.0k. For further details on how to configure PC-lint please refer to product page.
To extend the set of known PC-lint rules see Extending the code analysis.
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.