-
Notifications
You must be signed in to change notification settings - Fork 365
sonar.cxx.vc.reportPaths
Sensor to read reports from the Visual Studio C/C++ Compiler. The compiler sensor consumes the warning messages available in the LOG file. Depending on the compiler options there are more or less warnings available.
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://visualstudio.microsoft.com/
- The sensor supports rules from Visual Studio with version 2010 to 2019.
In order to generate a fitting report:
- To create the 'C/C++ Build Warnings C4001-C5999' you have to activate the following configuration properties. Open the project property page with
Project > Properties. SelectConfiguration Properties > C/C++ > Generaland setWarning LeveltoLevel3orLevel4. - Enable 'C++ Core Guidelines checker warnings': Select
Project > Properties > Configuration Properties > Code Analysis > General, activateEnable Code Analysis on BuildandEnable Microsoft Code Analysis. As default you can useMicrosoft > Active rules > Microsoft Native Recommended Rules. - To create absolute paths in the LOG file open
Tools > Optionsand set option forProject and Solutions > Build and RuntoMSBuild project build log file verbosity = Detailed. - That the paths in the LOG file matches the
sonar.sourceslist insonar-project.properties.
Sample command lines:
Visual Studio provides two tools to build and analyze a project from the command-line. With both tools it is the easiest to redirect stdout to a LOG file:
devenv.exe ... > output.log
msbuild.exe ... > output.logDevenv example doing a solution rebuild and write the warnings to example.log:
rem VS2019 Enterprise
call "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
devenv example.sln /rebuild Release /out example.logMSBuild example doing a project rebuild and write the warnings to example.log:
rem VS2019 Enterprise
call "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
MSBuild.exe example.proj /t:rebuild /p:Configuration=Release;WarningLevel=3 /fileLogger /fileLoggerParameters:WarningsOnly;LogFile=example.log;Verbosity=detailed;Encoding=UTF-8If the tool was executed successfully, a report like the example below should be generated:
Build started 26.01.2021 14:16:53.
Logging verbosity is set to: Detailed.
1>Project "D:\Sample\Sample.vcxproj" on node 3 (Rebuild target(s)).
...
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.28.29333\include\string.h(107,17): warning C4995: 'memcpy': name was marked as #pragma deprecated [D:\Sample\Sample.vcxproj]
1>D:\Sample\File1.cpp(44,89): warning C4838: conversion from 'uint' to 'uint8' requires a narrowing conversion [D:\Sample\Sample.vcxproj]
1>D:\Sample\File2.cpp(7,1): warning C4100: 'result_listener': unreferenced formal parameter [D:\Sample\Sample.vcxproj]
1>D:\Sample\File1.cpp(414,41): warning C4239: nonstandard
...
- 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.vc.reportPathsin the configuration filesonar-project.propertiesof your project. The Report Paths link describes the configuration options. - Normally the File Encoding is UTF-8. Use the parameter sonar.cxx.vc.encoding to use another one.
- Create a matching sonar.cxx.vc.regex, some samples are below.
- Execute the SonarScanner to transfer the project with the report to the SonarQube Server.
Depending on the Visual Studio version and the compiler options, the format of the warnings differs slightly:
| Visual Studio Version | sonar.cxx.vc.regex |
|---|---|
| Visual Studio 2010-2015 | (.*>)?(?<file>.*)\((?<line>\d+)\)\x20:\x20warning\x20(?<id>C\d+):(?<message>.*) |
| Visual Studio 2017 | (.*>)?(?<file>.*)\((?<line>\d+)\):\x20warning\x20(?<id>C\d+):\x20(?<message>.*) |
| Visual Studio 2019 | (?>[^>]*+>)?(?<file>(?>[^\\]{1,260}\\)*[^\\]{1,260})\((?<line>\d{1,5})\)\x20?:\x20warning\x20(?<id>C\d{4,5}):\x20?(?<message>.*) |
Hint: You have to escape the backslash (
\->\\) for the regex parameter in the configuration file!
The format of the build log looks different for parallel project builds (e.g. MSBuild options /maxcpucount:xx) and the regular expression has to consider this.
| log type | example |
|---|---|
| single process | D:\Sample\File1.cpp(414,41): warning ... |
| multiple processes | 1>D:\Sample\File1.cpp(414,41): warning ... |
Visual Studio 2019 sample for sonar-project.properties:
sonar.cxx.vc.reportPaths=*.log
sonar.cxx.vc.encoding=UTF-8
sonar.cxx.vc.regex=(?>[^>]*+>)?(?<file>(?>[^\\\\]{1,260}\\\\)*[^\\\\]{1,260})\\((?<line>\\d{1,5})\\)\\x20?:\\x20warning\\x20(?<id>C\\d{4,5}):\\x20?(?<message>.*)- If no results are displayed after a successful scan, check Manage Quality Profiles first.
- If the sensor cannot read the report, check the coding of the file, the parameter sonar.cxx.vc.encoding must be set correctly.
- 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.