-
Notifications
You must be signed in to change notification settings - Fork 1
Compilers
The Community C++ Plugin supports feeding compiler warnings as violations into SonarQube. Here is a quick guide how to run the supported compilers and catch needed relevant output.
To feed GCC warnings into SonarQube, make sure to:
- 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
- Set configuration properties, example:
sonar.cxx.compiler.parser=GCC
sonar.cxx.compiler.reportPath=*.log
sonar.cxx.compiler.charset=UTF-8
sonar.cxx.compiler.regex=^(.*):([0-9]+):[0-9]+: warning: (.*)\\[(.*)\\]$
- Activate the rules from the rule repository "compiler-gcc"
- Run the analysis
The compiler sensor consumes the warning messages available in the build log. Depending on the configuration properties there are more or less warnings available. Briefly these are the regular expressions that should be used, see details in next sections.
Visual Studio Version | Regular Expression |
Visual Studio 2010 to 2013 | ^.*>(?.*)\\((?[0-9]+)\\):\\x20warning\\x20(?C\\d\\d\\d\\d):(?.*)$ |
Visual Studio 2015 | ^.*[\\\\,/](.*)\\((\\d+)\\)\\x20*:\\x20warning\\x20(C\\d+):(.*)$ |
Visual Studio settings
-
To create the 'C/C++ Build Warnings C4001-C4999' you have to activate the following configuration properties. Open the project property page with
Project\Properties
. SelectConfiguration Properties\C/C++\General
and setWarning Level
toLevel3
orLevel4
. -
Enable 'Code Analysis for C/C++ Warnings C6001-C28351'. This feature is available in Premium or Ultimate editions only. Select
Configuration Properties\Code Analysis\General
, activateEnable Code Analysis on Build
. As default you can useMicrosoft Native Recommended Rules
. -
Open
Tools\Options
and set option forProject and Solutions\Build and Run
toMSBuild Project build log verbosity = Detailed
.
Create the LOG file from the command line
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|msbuild.exe] ... > output.log
Additional both tools provide command line options to redirect the output to a file:
- Devenv example doing a solution rebuild and write the warnings to example.log.
rem VS2010
call "%ProgramFiles(x86)%\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"
devenv example.sln /rebuild Release /out example.log
- MSBuild example doing a project rebuild and write the warnings to example.log.
rem VS2010
call "%ProgramFiles(x86)%\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"
MSBuild.exe example.proj /t:rebuild /p:Configuration=Release;WarningLevel=3 /fileLogger /fileLoggerParameters:WarningsOnly;LogFile=example.log;Verbosity=detailed;Encoding=UTF-8
Retrieving compiler information from buildlog
Version 0.9.3 and above extracts includes, defines and compiler options from the build log automatically.
This feature is enabled only if the produced log during compilation contains enough information (msbuild verbosity set to detailed or diagnostic). For example
msbuild.exe /v:Detailed Solution.SLN > buildlog.log
and then
sonar.cxx.compiler.reportPath=buildlog.log
will automate much of the plugin configuration, otherwise set using sonar.cxx.includeDirectories and sonar.cxx.defines.
This feature can be used in any version of visual studio and it is independent of the Code Analysis described in earlier sections. Altough they come together using the same sonar.cxx.compiler.reportPath option.
SonarQube Configuration settings
To read the messages from the LOG file the following configuration settings have to be defined. The regular expression can be configured and must match to the format in the LOG file.
sonar.cxx.compiler.parser=Visual C++
sonar.cxx.compiler.reportPath=*.log
sonar.cxx.compiler.charset=UTF-8
sonar.cxx.compiler.regex=^.*>(?<filename>.*)\\((?<line>[0-9]+)\\):\\x20warning\\x20(?<id>C\\d\\d\\d\\d):(?<message>.*)$
A typical log file contains compiler warnings as shown in the following excerpt:
Build started 26.02.2014 17:59:20.
1>Project "D:\training-kit\plaza_common\Source\sample.dll.vcxproj" on node 3 (Rebuild target(s)).
1>_PrepareForClean:
Deleting file "..\..\..\bin\x86\Debug\sample\sample.tlog\pcvcom.lastbuildstate".
InitializeBuildStatus:
Creating "..\..\..\bin\x86\Debug\sample\sample.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /I..\..\..\boost\include ... (truncated) ... stdafx.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h(55): warning C4995: 'memcpy': name was marked as #pragma deprecated
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h(74): warning C4995: 'memcpy': name was marked as #pragma deprecated
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h(112): warning C4995: 'strcpy': name was marked as #pragma deprecated
Visual Studio 2015 regular expression
The following regular expression handles Visual Studio 2015 and VC-14 compiler
sonar.cxx.compiler.regex=^.*[\\\\,/](.*)\\((\\d+)\\)\\x20*:\\x20warning\\x20(C\\d+):(.*)$