{"id":1223,"date":"2019-03-07T14:14:53","date_gmt":"2019-03-07T14:14:53","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=1223"},"modified":"2019-03-07T19:39:33","modified_gmt":"2019-03-07T19:39:33","slug":"brief-overview-of-the-cc-sonarqube-plugin-based-on-cppdepend","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/brief-overview-of-the-cc-sonarqube-plugin-based-on-cppdepend\/","title":{"rendered":"Explore a flexible C\/C++ SonarQube  plugin based on CppDepend."},"content":{"rendered":"<p>Both CppDepend and SonarQube are static analyzers that offer a rule-based system to detect problems in C\/C++ code. However, the CppDepend default Rules-Set\u00a0<b>has very few overlaps<\/b>\u00a0with the SonarQube rules<\/p>\n<p>Basically, the SonarQube rules are good at analyzing what is happening inside a method, the code flow while the CppDepend code model, on which the CppDepend rules are based, is optimized for a 360 view of particular higher-scale areas including OOP, dependencies, metrics, breaking changes, mutability, naming&#8230;<br \/>\n<!--more--><\/p>\n<p>Concretely SonarQube rules can warn about problems like a reference could be null, while CppDepend can warn you about too complex classes or components, and offer advice about how to refactor to make the code cleaner and more maintainable.<br \/>\nAnother point that makes the CppDepend ruling system unique is\u00a0<b>how easy it makes to write custom rules<\/b>. With CppDepend a rule is a LINQ query, that queries a code model dedicated to code quality, edited live in Visual Studio, compiled and executed live at edition time.<br \/>\nConcretely, this piece of code below is a fully functional rule, could it be simpler?<\/p>\n<pre><span style=\"color: #008000;\">\/\/\u00a0&lt;Name&gt;<\/span><b>Classes\u00a0must\u00a0start\u00a0with\u00a0an\u00a0I<\/b><span style=\"color: #008000;\">&lt;\/Name&gt;\r\n<\/span><span style=\"color: #0000ff;\">warnif<\/span>\u00a0<b>count<\/b>\u00a0<span style=\"color: #000000;\">&gt;<\/span>\u00a0<b>0<\/b><span style=\"color: #000000;\">\u00a0\r\n<\/span><b>Application<\/b><span style=\"color: #000000;\">.<\/span><span style=\"color: #000000;\">Types<\/span><span style=\"color: #000000;\">.<\/span><span style=\"color: #000000;\">Where<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">t<\/span>\u00a0<span style=\"color: #000000;\">=&gt;<\/span>\u00a0<span style=\"color: #000000;\">t<\/span><span style=\"color: #000000;\">.<\/span><span style=\"color: #000000;\">IsClass<\/span>\u00a0<span style=\"color: #000000;\">&amp;&amp;<\/span>\u00a0<span style=\"color: #000000;\">!<\/span><span style=\"color: #000000;\">t<\/span><span style=\"color: #000000;\">.<\/span><span style=\"color: #000000;\">SimpleName<\/span><span style=\"color: #000000;\">.<\/span><span style=\"color: #000000;\">StartsWith<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #a31515;\">\"C\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">)<\/span>\r\n<\/pre>\n<p>When defining a custom rule with CppDepend, the user doesn&#8217;t need to create a\u00a0 project, create a source file, step into the edit\/compile\/debug cycle, maintain a binary dll that requires effort to be shared, versioned and integrated.<br \/>\nWith CppDepend custom rules are raw texts, embedded as XML CDATA into the CppDepend project or rule files. Also, the documentation and how-to-fix guidelines can be embedded in the rule source code as comments.<\/p>\n<p><img decoding=\"async\" id=\"ctl00_MyContentPH_Image1\" class=\"img-responsive\" title=\"Editing a custom code rule with CppDepend in Visual Studio\" src=\"https:\/\/www.cppdepend.com\/img\/queryedition.png\" \/><\/p>\n<p>Also, each CppDepend rule can present its issues with extra data that will help to understand the problem and fix it.<br \/>\nMoreover, each rule can embed two formulas that attempt to estimate both the\u00a0<i>cost to fix the issue<\/i>\u00a0and the\u00a0<i>annual cost to let the issue unfixed<\/i>, also called the\u00a0<i>technical-debt<\/i>\u00a0and the\u00a0<i>annual interest<\/i>\u00a0of the issue.\u00a0<b>Since these formulas rely on what really matters at fix time, this makes the debt estimations smart<\/b>.<\/p>\n<p><img decoding=\"async\" id=\"ctl00_MyContentPH_Image3\" class=\"img-responsive\" title=\"Architecture Rule with CppDepend\" src=\"https:\/\/www.cppdepend.com\/Doc\/TechnicalDebt\/IssuesFixPriority.PNG\" \/><\/p>\n<p>Finally, with CppDepend each rule is run in a few milliseconds even on a large code base. As a consequence, all rules can be passed in a few seconds (typically 2 or 3 seconds on a real-world code base), both in Visual Studio and in the Continuous-Integration system.<br \/>\nAs a benefit, after each compilation and also at check-in time, the developer instantly knows about the new and fixed issues since the baseline, and the impact in terms of technical debt fixed or created.<br \/>\nNow let&#8217;s explain how to integrate CppDepend rule results into the SonarQube system to cumulate the strength of both products.<br \/>\n<strong><span id=\"Part1\">I &#8211; C\/C++ Plugin Prerequisites<\/span><\/strong><\/p>\n<ul>\n<li>Install\u00a0<a href=\"http:\/\/www.sonarqube.org\/downloads\/\">SonarQube<\/a>.<\/li>\n<li>Install\u00a0<a href=\"https:\/\/docs.sonarqube.org\/display\/SCAN\/Analyzing+with+SonarQube+Scanner\">SonarQube Scanner<\/a>\u00a0or the old\u00a0<a href=\"https:\/\/docs.sonarqube.org\/display\/SONARQUBE51\/Installing+and+Configuring+SonarQube+Runner\">Sonar Runner.<\/a><\/li>\n<li>Add the SonarQube Scanner or the SonarRunner bin directory to the PATH environment variable.<\/li>\n<\/ul>\n<p><strong><span id=\"Part2\">II-Plugin installation and configuration<\/span>\u00a0<\/strong><\/p>\n<ul>\n<ul>\n<li>Copy the sonar-cxx-plugin-cppdepend-1.0 from $CppDependInstallDir$\/SonarPlugin to the $SonarQubeInstallDir$\\extensions\\plugins directory and restart SonarQube.<\/li>\n<\/ul>\n<\/ul>\n<ul>\n<li>The default CppDepend rules are loaded to the SonarQube rules repository. However, if you need to customize these rules you can define your own custom rules using the .cdproj file path located under the SonarQube Administration Tab.<br \/>\n<img decoding=\"async\" class=\"img-responsive\" src=\"https:\/\/www.cppdepend.com\/img\/sonarrules.png\" \/><\/li>\n<li>You have to log in as an admin and activate the CppDepend rules in the profile you want.<img decoding=\"async\" class=\"img-responsive\" src=\"https:\/\/www.cppdepend.com\/img\/sonar9.png\" \/><\/li>\n<li>Execute $CppDependInstallDir$\/SonarRunnerForCppDepend.exe &#8220;the .cdproj file to analyze &#8220;For example:\u00a0<b>SonarRunnerForCppDepend.exe C:\\MyWorkspace\\test.cdproj<\/b>.<br \/>\nSonarRunnerForCppDepend will analyze the cdproj file using CppDepend and launch the SonarQube Scanner executable to load the results into SonarQube.<br \/>\nAny other argument passed to the SonarRunnerForCppDepend after the cdproj file argument will be passed to the SonarScanner command.<br \/>\nFor example you can pass the version with this command\u00a0<b>SonarRunnerForCppDepend.exe C:\\MyWorkspace\\test.cdproj -Dsonar.projectVersion=3.0<\/b><\/li>\n<\/ul>\n<p><strong><span id=\"Part3\">III- Plugin features<\/span><\/strong><\/p>\n<ul>\n<ul>\n<li><b>Multi Module analysis:<\/b>\u00a0a CppDepend project could contain many C\/C++ projects.<br \/>\nAfter the analysis, CppDepend does not put all the code in the same SonarQube module. However, it creates a multi-module sonarqube project to isolate each project into a separate module which makes the code navigation very easy.<img decoding=\"async\" class=\"img-responsive\" src=\"https:\/\/www.cppdepend.com\/img\/sonarmodules.png\" \/><\/li>\n<li><b>Issues:<\/b>\u00a0CppDepend provides by default more than 250 rules, which you can easily customize completely.<br \/>\nCppDepend provides a powerful way to\u00a0<a href=\"http:\/\/www.cppdepend.com\/Doc_TechnicalDebt\">compute the technical debt<\/a>\u00a0of the issues. The CppDepend technical debt and the issue severity are given to SonarQube.<img decoding=\"async\" class=\"img-responsive\" src=\"https:\/\/www.cppdepend.com\/img\/sonarissues.png\" \/><\/li>\n<li><b>Standard metrics:<\/b>\u00a0the plugin calculates all the standard SonarQube metrics.<img decoding=\"async\" class=\"img-responsive\" src=\"https:\/\/www.cppdepend.com\/img\/sonar11.png\" \/><\/li>\n<\/ul>\n<\/ul>\n<ul>\n<ul>\n<li><b>Code duplication:<\/b>\u00a0The duplications are detected by the CPD tool embedded in SonarQube.<img decoding=\"async\" class=\"img-responsive\" src=\"https:\/\/www.cppdepend.com\/img\/sonar13.png\" \/><\/li>\n<\/ul>\n<\/ul>\n<ul>\n<li><b>Coverage:<\/b>\u00a0The plugin loads the coverage result from Cobertura and Microsoft Visual Studio XML result files.<br \/>\nHowever, you have to set the path where the xml coverage files exist.<br \/>\n<img decoding=\"async\" class=\"img-responsive\" src=\"https:\/\/www.cppdepend.com\/img\/sonarcoverage.png\" \/><img decoding=\"async\" class=\"img-responsive\" src=\"https:\/\/www.cppdepend.com\/img\/sonar12.png\" \/><\/li>\n<\/ul>\n<p><strong>Conclusion<\/strong><\/p>\n<p>The C\/C++ SonarQube plugin is easy to install and to use.\u00a0 The rules customization is very simple. You can <a href=\"https:\/\/www.cppdepend.com\/download\">try it<\/a> and give us your feedback.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Both CppDepend and SonarQube are static analyzers that offer a rule-based system to detect problems in C\/C++ code. However, the CppDepend default Rules-Set\u00a0has very few overlaps\u00a0with the SonarQube rules Basically, the SonarQube rules are good at analyzing what is happening inside a method, the code flow while the CppDepend code model, on which the CppDepend &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/brief-overview-of-the-cc-sonarqube-plugin-based-on-cppdepend\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Explore a flexible C\/C++ SonarQube  plugin based on CppDepend.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[7,13,18,65],"class_list":["post-1223","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-cpp","tag-cppdepend","tag-sonarqube"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=1223"}],"version-history":[{"count":11,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1223\/revisions"}],"predecessor-version":[{"id":1234,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1223\/revisions\/1234"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=1223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=1223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=1223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}