Skip to content

Commit

Permalink
Add SonarQube plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Lussier authored and Kevin Lussier committed May 21, 2019
1 parent 54015bc commit 456e1ba
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "OpenCppCoverage"]
path = OpenCppCoverage
url = https://github.com/OpenCppCoverage/OpenCppCoverage.git
41 changes: 41 additions & 0 deletions OCCSonarQube/OCCSonarQube.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.329
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SonarQube", "SonarQube\SonarQube.vcxproj", "{19007FBB-9040-43AA-BCF2-F3D84F07B302}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Plugin", "..\..\OpenCppCoverage\Plugin\Plugin.vcxproj", "{2F439508-07E0-4084-9614-1A42BDE8ED9A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{19007FBB-9040-43AA-BCF2-F3D84F07B302}.Debug|x64.ActiveCfg = Debug|x64
{19007FBB-9040-43AA-BCF2-F3D84F07B302}.Debug|x64.Build.0 = Debug|x64
{19007FBB-9040-43AA-BCF2-F3D84F07B302}.Debug|x86.ActiveCfg = Debug|Win32
{19007FBB-9040-43AA-BCF2-F3D84F07B302}.Debug|x86.Build.0 = Debug|Win32
{19007FBB-9040-43AA-BCF2-F3D84F07B302}.Release|x64.ActiveCfg = Release|x64
{19007FBB-9040-43AA-BCF2-F3D84F07B302}.Release|x64.Build.0 = Release|x64
{19007FBB-9040-43AA-BCF2-F3D84F07B302}.Release|x86.ActiveCfg = Release|Win32
{19007FBB-9040-43AA-BCF2-F3D84F07B302}.Release|x86.Build.0 = Release|Win32
{2F439508-07E0-4084-9614-1A42BDE8ED9A}.Debug|x64.ActiveCfg = Debug|x64
{2F439508-07E0-4084-9614-1A42BDE8ED9A}.Debug|x64.Build.0 = Debug|x64
{2F439508-07E0-4084-9614-1A42BDE8ED9A}.Debug|x86.ActiveCfg = Debug|Win32
{2F439508-07E0-4084-9614-1A42BDE8ED9A}.Debug|x86.Build.0 = Debug|Win32
{2F439508-07E0-4084-9614-1A42BDE8ED9A}.Release|x64.ActiveCfg = Release|x64
{2F439508-07E0-4084-9614-1A42BDE8ED9A}.Release|x64.Build.0 = Release|x64
{2F439508-07E0-4084-9614-1A42BDE8ED9A}.Release|x86.ActiveCfg = Release|Win32
{2F439508-07E0-4084-9614-1A42BDE8ED9A}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {76ACB947-B189-462E-BA68-2D11D0787AA4}
EndGlobalSection
EndGlobal
107 changes: 107 additions & 0 deletions OCCSonarQube/SonarQube/SonarQube.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "stdafx.h"

#include "Plugin/Exporter/IExportPlugin.hpp"
#include "Plugin/Exporter/CoverageData.hpp"
#include "Plugin/Exporter/ModuleCoverage.hpp"
#include "Plugin/Exporter/FileCoverage.hpp"
#include "Plugin/Exporter/LineCoverage.hpp"
#include "Plugin/OptionsParserException.hpp"

#include <filesystem>
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <map>
#include <string>


class SonarQubeExport : public Plugin::IExportPlugin
{
public:
//-------------------------------------------------------------------------
std::optional<std::filesystem::path> Export(
const Plugin::CoverageData& coverageData,
const std::optional<std::wstring>& argument ) override
{
std::wstring currentfile;
std::filesystem::path output = argument ? *argument : L"SonarQube.xml";
std::wofstream ofs{ output };

if ( !ofs )
throw std::runtime_error( "Cannot create the output file for SonarQube exporting" );

// Convert to our internal maps. We do this because we want to collate the same file if it is seen multiple times
for ( const auto& mod : coverageData.GetModules() )
{
// Skip the module if it has no files
if ( mod->GetFiles().empty() )
continue;

for ( const auto& file : mod->GetFiles() )
{
// Skip the file if it has no lines
if ( file->GetLines().empty() )
continue;

currentfile = file->GetPath().wstring();
auto &file_entry = coverage[currentfile];

for ( const auto &line : file->GetLines() )
{
// In case we have seen thils file/line before, we want to 'or' in the iteration's HasBeenExecuted
auto &line_entry = file_entry[line.GetLineNumber()];
line_entry |= line.HasBeenExecuted();
}
}
}

ofs << L"<coverage version=\"1\">" << std::endl;

std::wstring covered;
for ( const auto &file_iter : coverage )
{
ofs << L" <file path=\"" << file_iter.first << L"\">" << std::endl;
for ( const auto &line_iter : file_iter.second )
{
covered = line_iter.second ? L"true" : L"false";
ofs << L" <lineToCover lineNumber=\"" << line_iter.first << L"\" covered=\"" << covered << L"\"/>" << std::endl;
}
ofs << L" </file>" << std::endl;
}
ofs << L"</coverage>" << std::endl;

return output;
}

//-------------------------------------------------------------------------
void CheckArgument( const std::optional<std::wstring>& argument ) override
{
// Try to check if the argument is a file.
if ( argument && !std::filesystem::path{ *argument }.has_filename() )
throw Plugin::OptionsParserException( "Invalid argument for SonarQube export." );
}

//-------------------------------------------------------------------------
std::wstring GetArgumentHelpDescription() override
{
return L"output file (optional)";
}

//-------------------------------------------------------------------------
int GetExportPluginVersion() const override
{
return Plugin::CurrentExportPluginVersion;
}

protected:
std::unordered_map< std::wstring, std::map<size_t, bool> > coverage;
};

extern "C"
{
//-------------------------------------------------------------------------
__declspec(dllexport) Plugin::IExportPlugin* CreatePlugin()
{
return new SonarQubeExport();
}
}
183 changes: 183 additions & 0 deletions OCCSonarQube/SonarQube/SonarQube.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{19007FBB-9040-43AA-BCF2-F3D84F07B302}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>SonarQube</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;SONARQUBE_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalIncludeDirectories>..\..\OpenCppCoverage</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;SONARQUBE_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalIncludeDirectories>..\..\OpenCppCoverage</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;SONARQUBE_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalIncludeDirectories>..\..\OpenCppCoverage</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;SONARQUBE_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalIncludeDirectories>..\..\OpenCppCoverage</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="SonarQube.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\OpenCppCoverage\Plugin\Plugin.vcxproj">
<Project>{2f439508-07e0-4084-9614-1a42bde8ed9a}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
36 changes: 36 additions & 0 deletions OCCSonarQube/SonarQube/SonarQube.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="stdafx.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="targetver.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SonarQube.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="dllmain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
19 changes: 19 additions & 0 deletions OCCSonarQube/SonarQube/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

1 change: 1 addition & 0 deletions OCCSonarQube/SonarQube/stdafx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "stdafx.h"
Loading

0 comments on commit 456e1ba

Please sign in to comment.