Skip to content

sonar.cxx.clangtidy.reportPaths

Günter Wirth edited this page Jan 12, 2023 · 10 revisions

Overview

Sensor to read reports from the Clang-Tidy tool. Clang-Tidy is a clang-based C++ “linter” tool. Its purpose is to provide an extensible framework for diagnosing and fixing typical programming errors, like style violations, interface misuse, or bugs that can be deduced via static analysis. Clang-Tidy is modular and provides a convenient interface for writing new checks.

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

Create report

The most convenient way to run Clang-Tidy is with a compile command database; CMake can automatically generate one, for a description of how to enable it see How To Setup Clang Tooling For LLVM. Once compile_commands.json is in place and a working version of Clang-Tidy is in PATH the entire code base can be analyzed with clang-tidy/tool/run-clang-tidy.py (script run-clang-tidy.py). The script executes Clang-Tidy with the default set of checks on every translation unit in the compile command database and displays the resulting warnings and errors. The script provides multiple configuration flags.

In order to run Clang-Tidy 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

Sample command line:

# Run clang-tidy on all files in the current working directory with a default
# set of checks and show warnings in the cpp files and all project headers.
run-clang-tidy.py %cd% > clang-tidy.txt

Example of a report file

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

C:\MyProject\Sample.cpp:2:10: warning: function 'A::foo' has a definition with different parameter names [readability-inconsistent-declaration-parameter-name]
    void foo(int bar);
         ^
C:\MyProject\Sample.cpp:2:5: error: use of undeclared identifier 'gets' [clang-diagnostic-error]
    gets(buffer);
    ^

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.clangtidy.reportPaths in the configuration file sonar-project.properties of your project. The Report Paths link describes the configuration options.
  4. Normally the File Encoding of Clang-Tidy reports is UTF-8. Use the parameter sonar.cxx.clangtidy.encoding to use another one.
  5. Execute the SonarScanner to transfer the project with the report to the SonarQube Server.

Sample for sonar-project.properties:

sonar.cxx.clangtidy.encoding=UTF-8
sonar.cxx.clangtidy.reportPaths=clang-tidy.txt

Troubleshooting

Clone this wiki locally