Skip to content

sonar.cxx.gcc.reportPaths

Günter Wirth edited this page Sep 5, 2021 · 8 revisions

Overview

Sensor to read reports from the GNU C++ Compiler tool. The GNU Compiler Collection (GCC) is an optimizing compiler produced by the GNU Project supporting various programming languages, hardware architectures and operating systems. GCC is a key component of the GNU toolchain and the standard compiler for most projects related to GNU and the Linux kernel.

Note: The cxx plugin itself does not run the tool, you have to do that yourself beforehand. The sensor only reads the report generated by the tool!

Supported versions

  • Link to the tool page: https://gcc.gnu.org/
  • The sensor supports rules from GNU C++ Compiler with version GCC 6.3.

Create report

In order to run GNU C++ Compiler and generate a fitting report, make sure:

  • to call it from the projects root directory, so that the paths in the report fit
  • that the parameter matches the sonar.sources list in sonar-project.properties
  • add the -fdiagnostics-show-option option in your GCC build options (CFLAGS, CPPFLAGS or similar)
  • capture the warnings from your build into a file (e.g build.log) using shell redirections or similar)

Sample command line:

g++ -fdiagnostics-show-option foo.c -Wall > build.log

Example of a report file

If the tool was executed successfully, a report like the example below should be generated:

Compiling...
ZipManager.cpp
src/zipmanager.cpp: In function 'int unzip(char *, int)'
src/zipmanager.cpp:141:10: warning: conversion to 'unsigned char' from 'unsigned int' may alter its value [-Wconversion]
src/zipmanager.cpp:222:9: warning: enumeration value 'Type_Deflate' not handled in switch [-Wswitch-enum]
src/zipmanager.cpp: In member function 'bool getRatio(int*) const':
src/zipmanager.cpp:271:75: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]
Compiling manifest to resources...

Configure cxx plugin

  1. First check if the file extensions read in by the cxx plugin are set (sonar.cxx.file.suffixes).
  2. The rules for which you want to generate issue must be activated in the Quality Profile of your project. You can find instructions on how to do this under Manage Quality Profiles.
  3. Set the analysis parameter sonar.cxx.gcc.reportPaths in the configuration file sonar-project.properties of your project. The Report Paths link describes the configuration options.
  4. Normally the File Encoding is UTF-8. Use the parameter sonar.cxx.gcc.encoding to use another one.
  5. Create a matching sonar.cxx.gcc.regex, some samples are below.
  6. Execute the SonarScanner to transfer the project with the report to the SonarQube Server.
option sonar.cxx.gcc.regex
with column number sonar.cxx.gcc.regex=(?<file>.*):(?<line>[0-9]+):[0-9]+:\x20warning:\x20(?<message>.*)\x20\[(?<id>.*)\]
without column number sonar.cxx.gcc.regex=(?<file>.*):(?<line>[0-9]+):\x20warning:\x20(?<message>.*)\x20\[(?<id>.*)\]

Hint: You have to escape the backslash (\-> \\) for the regex parameter in the configuration file!

Sample for sonar-project.properties:

sonar.cxx.gcc.reportPaths=*.log
sonar.cxx.gcc.encoding=UTF-8
sonar.cxx.gcc.regex=(?<file>.*):(?<line>[0-9]+):[0-9]+:\\x20warning:\\x20(?<message>.*)\\x20\\[(?<id>.*)\\]

Troubleshooting

Clone this wiki locally