-
Notifications
You must be signed in to change notification settings - Fork 362
sonar.cxx.clangtidy.reportPaths
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!
- Link to the tool page: https://clang.llvm.org/extra/clang-tidy/
- The sensor supports rules from Clang-Tidy with version LLVM 19.0.0.
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 insonar-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
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);
^
- First check if the file extensions read in by the cxx plugin are set (sonar.cxx.file.suffixes).
- 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.
- Set the analysis parameter
sonar.cxx.clangtidy.reportPaths
in the configuration filesonar-project.properties
of your project. The Report Paths link describes the configuration options. - Normally the File Encoding of Clang-Tidy reports is UTF-8. Use the parameter sonar.cxx.clangtidy.encoding to use another one.
- Execute the SonarScanner to transfer the project with the report to the SonarQube Server.
- With CXX Custom Template Rules it's possible to extend the rule repository.
- It is also possible to display unknown rules on the SonarQube Server.
Sample for sonar-project.properties:
sonar.cxx.clangtidy.encoding=UTF-8
sonar.cxx.clangtidy.reportPaths=clang-tidy.txt
- If no results are displayed after a successful scan, check Manage Quality Profiles first.
- If you cannot find an issue under 'New code', also look under 'Overall code' to see if it is listed there.
- If scanning is failing, check items listed under Troubleshooting Configuration.
- If no issues are displayed for your source code in SonarQube, check the items listed under Troubleshooting Reports.
- In the case of incomplete scans, Detect and fix parsing errors gives advice on how to fix this.