{"id":1755,"date":"2021-01-08T11:49:56","date_gmt":"2021-01-08T15:49:56","guid":{"rendered":"http:\/\/blog.wholetomato.com\/?p=1755"},"modified":"2022-06-22T18:55:36","modified_gmt":"2022-06-22T22:55:36","slug":"a-brief-introduction-to-clang-tidy-and-its-role-in-visual-assist","status":"publish","type":"post","link":"https:\/\/stage.wholetomato.com\/blog\/a-brief-introduction-to-clang-tidy-and-its-role-in-visual-assist\/","title":{"rendered":"A Brief Introduction To Clang-Tidy And Its Role in Visual Assist"},"content":{"rendered":"\r\n<p>As a developer, you probably know that parsing C++ source code is very complicated. This is also the reason why there might be fewer good tools for coding assistance than we have in other &#8220;lighter&#8221; languages. Fortunately, \u00c2\u00a0parsing C++ source code has become much more straightforward over the past decade, mainly because of the Clang\/LLVM infrastructure. Clang not only introduced a new excellent C++ compiler but also many extra tools that allow us to analyse C++ code efficiently.<\/p>\r\n\r\n\r\n\r\n<p>Today we&#8217;ll meet one tool that can make our life much easier! It&#8217;s called <strong>clang-tidy<\/strong>.<\/p>\r\n\r\n\r\n\r\n<p>We&#8217;ll cover this valuable utility as it&#8217;s also an important part for the Visual Assist internal code analysis system.<\/p>\r\n\r\n\r\n\r\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_76 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\r\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\r\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/stage.wholetomato.com\/blog\/a-brief-introduction-to-clang-tidy-and-its-role-in-visual-assist\/#Lets_Meet_clang-tidy\" >Let&#8217;s Meet clang-tidy<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/stage.wholetomato.com\/blog\/a-brief-introduction-to-clang-tidy-and-its-role-in-visual-assist\/#Installation_on_Windows\" >Installation on Windows<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/stage.wholetomato.com\/blog\/a-brief-introduction-to-clang-tidy-and-its-role-in-visual-assist\/#Running_a_Simple_Test\" >Running a Simple Test<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/stage.wholetomato.com\/blog\/a-brief-introduction-to-clang-tidy-and-its-role-in-visual-assist\/#Experimenting_with_Fixes\" >Experimenting with Fixes<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/stage.wholetomato.com\/blog\/a-brief-introduction-to-clang-tidy-and-its-role-in-visual-assist\/#Clang_Tools_in_Visual_Studio\" >Clang Tools in Visual Studio<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/stage.wholetomato.com\/blog\/a-brief-introduction-to-clang-tidy-and-its-role-in-visual-assist\/#How_Visual_Assist_Makes_Things_Much_Safer_And_Easier\" >How Visual Assist Makes Things Much Safer And Easier<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/stage.wholetomato.com\/blog\/a-brief-introduction-to-clang-tidy-and-its-role-in-visual-assist\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\r\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Lets_Meet_clang-tidy\"><\/span>Let&#8217;s Meet clang-tidy<span class=\"ez-toc-section-end\"><\/span><\/h2>\r\n\r\n\r\n\r\n<p><a href=\"https:\/\/clang.llvm.org\/extra\/clang-tidy\/\">Here&#8217;s a concise<\/a> and a brief description of this handy tool:<\/p>\r\n\r\n\r\n\r\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\r\n<p><strong>clang-tidy<\/strong> is a clang-based C++ \u00e2\u20ac\u0153linter\u00e2\u20ac\u009d 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. <strong>clang-tidy<\/strong> is modular and provides a convenient interface for writing new checks.<\/p>\r\n<\/blockquote>\r\n\r\n\r\n\r\n<p>As you can read above, this tool gives us a way to check our source code and fix common issues.<\/p>\r\n\r\n\r\n\r\n<p>Let&#8217;s now see how you can install and work with this tool on Windows.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Installation_on_Windows\"><\/span>Installation on Windows<span class=\"ez-toc-section-end\"><\/span><\/h3>\r\n\r\n\r\n\r\n<p><strong>clang-tidy<\/strong> is attached with the main Clang package for Windows. You can download it from this site:<\/p>\r\n\r\n\r\n\r\n<p><a href=\"https:\/\/releases.llvm.org\/download.html#11.0.0\">LLVM Download Page &#8211; version 11.0 (Nov 2020)<\/a><\/p>\r\n\r\n\r\n\r\n<p>(Or also see other Releases: <a href=\"https:\/\/releases.llvm.org\/\">Download LLVM releases<\/a>)<\/p>\r\n\r\n\r\n\r\n<p>When Clang is installed, you can open your command line (for example, for this article I&#8217;m using PowerShell) and type:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">clang-tidy --version<\/pre>\r\n\r\n\r\n\r\n<p>I get the following output:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">PS D:\\code&gt; clang-tidy --version<br \/>LLVM (http:\/\/llvm.org\/):<br \/>  LLVM version 11.0.0<br \/>  Optimised build.<br \/>  Default target: x86_64-pc-windows-msvc<br \/>  Host CPU: haswell<\/pre>\r\n\r\n\r\n\r\n<p>In general, <strong>clang-tidy<\/strong> operates on a single input file. You pass a filename, plus standard compilations flags and then extra flags about selected checks that will be performed.<\/p>\r\n\r\n\r\n\r\n<p>Let&#8217;s run a simple test to see how it works.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Running_a_Simple_Test\"><\/span>Running a Simple Test<span class=\"ez-toc-section-end\"><\/span><\/h3>\r\n\r\n\r\n\r\n<p>See this artificial code:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code\">#include &lt;vector&gt;\r\n#include &lt;memory&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n\r\nint main() {\r\n    int a;\r\n\r\n    std::string hello {\"hello\"};\r\n    std::string world {\"world\"};\r\n    if (hello + world == \"\")\r\n        std::cout &lt;&lt; \"empty!\\n\";\r\n\r\n    std::unique_ptr&lt;std::string&gt; ptr { new std::string { \"abc\" } };\r\n    ptr.reset(new std::string{ \"xyz\" });\r\n    ptr.reset(NULL);\r\n\r\n    std::vector&lt;int&gt; vec{ 1, 2, 3, 4 };\r\n    for (std::vector&lt;int&gt;::iterator it = vec.begin(); it != vec.end(); ++it)\r\n        std::cout &lt;&lt; *it &lt;&lt; '\\n';\r\n}<\/pre>\r\n\r\n\r\n\r\n<p>Can you now play a game and list all of the &#8220;spurious&#8221; smells in the code? What would you improve here?<\/p>\r\n\r\n\r\n\r\n<p>Let&#8217;s now compare your results with suggestions from <strong>clang-tidy<\/strong>.<\/p>\r\n\r\n\r\n\r\n<p>I ran the tool with the following options:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">clang-tidy --checks='modernize*, readability*' hello_tidy.cpp -- -std=c++14<\/pre>\r\n\r\n\r\n\r\n<p>On my machine, I get the those results:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-2.png?ssl=1\"><img decoding=\"async\" width=\"1140\" height=\"673\" data-attachment-id=\"1772\" data-permalink=\"https:\/\/stage.wholetomato.com\/blog\/clang-ps-output-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-2.png?fit=1322%2C780&amp;ssl=1\" data-orig-size=\"1322,780\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"clang-ps-output-2\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-2.png?fit=300%2C177&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-2.png?fit=1024%2C604&amp;ssl=1\" class=\"wp-image-1772\" src=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-2.png?resize=1140%2C673&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-2.png?w=1322&amp;ssl=1 1322w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-2.png?resize=300%2C177&amp;ssl=1 300w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-2.png?resize=1024%2C604&amp;ssl=1 1024w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-2.png?resize=768%2C453&amp;ssl=1 768w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-2.png?resize=360%2C212&amp;ssl=1 360w\" sizes=\"(max-width: 1140px) 100vw, 1140px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\r\n\r\n\r\n\r\n<p>As you can see above, I run it against all checks in the &#8220;modernise&#8221; and &#8220;readability&#8221; categories. You can find all available checks here: <a href=\"https:\/\/clang.llvm.org\/extra\/clang-tidy\/checks\/list.html\">clang-tidy &#8211; Clang-Tidy Checks \u00e2\u20ac\u201d Extra Clang Tools 12 documentation<\/a><\/p>\r\n\r\n\r\n\r\n<p>You can also list all available checks for your version with the following command:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">clang-tidy --list-checks<\/pre>\r\n\r\n\r\n\r\n<p>In summary the tool found the following issues:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>suggestion to use trailing return type for main()<\/li>\r\n<li>suggestion about using .empty() rather than comparing string with an empty string<\/li>\r\n<li>make_unique<\/li>\r\n<li>nullptr rather than NULL<\/li>\r\n<li>modernising range based for loop<\/li>\r\n<li>braces around single-line if and loop statements<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Experimenting_with_Fixes\"><\/span>Experimenting with Fixes<span class=\"ez-toc-section-end\"><\/span><\/h3>\r\n\r\n\r\n\r\n<p>But there&#8217;s more!<\/p>\r\n\r\n\r\n\r\n<p><strong>clang-tidy<\/strong> can not only find issues but also fix them! A lot of checks have &#8220;auto-fixes&#8221;! By providing the <code>-fix<\/code> command, you can ask the tool to modify the code. For example:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">clang-tidy --checks='modernize-* readability-container*' -fix hello_tidy.cpp -- -std=c++14<\/pre>\r\n\r\n\r\n\r\n<p>As you can see, this time I used &#8220;readability-container&#8221; only as I didn&#8217;t want to modify braces around simple if statements.<\/p>\r\n\r\n\r\n\r\n<p>I got the following output:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-fix.png?ssl=1\"><img decoding=\"async\" width=\"1140\" height=\"963\" data-attachment-id=\"1774\" data-permalink=\"https:\/\/stage.wholetomato.com\/blog\/clang-ps-output-fix\/\" data-orig-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-fix.png?fit=1201%2C1015&amp;ssl=1\" data-orig-size=\"1201,1015\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"clang-ps-output-fix\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-fix.png?fit=300%2C254&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-fix.png?fit=1024%2C865&amp;ssl=1\" class=\"wp-image-1774\" src=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-fix.png?resize=1140%2C963&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-fix.png?w=1201&amp;ssl=1 1201w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-fix.png?resize=300%2C254&amp;ssl=1 300w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-fix.png?resize=1024%2C865&amp;ssl=1 1024w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-fix.png?resize=768%2C649&amp;ssl=1 768w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-ps-output-fix.png?resize=360%2C304&amp;ssl=1 360w\" sizes=\"(max-width: 1140px) 100vw, 1140px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\r\n\r\n\r\n\r\n<p><strong>clang-tidy<\/strong> nicely lists all the fixes that it managed to apply.<\/p>\r\n\r\n\r\n\r\n<p>The final source code looks as follows:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code\">#include &lt;memory&gt;   \/\/ oops!\r\n#include &lt;vector&gt;\r\n#include &lt;memory&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n\r\nauto main() -&gt; int {\r\n\tint a;\r\n\r\n    std::string hello {\"hello\"};\r\n    std::string world {\"world\"};\r\n    if (hello + world.empty())   \/\/ oops!\r\n        std::cout &lt;&lt; \"empty!\\n\";\r\n\r\n\tstd::unique_ptr&lt;std::string&gt; ptr { new std::string { \"abc\" } };\r\n\tptr = std::make_unique&lt;std::string&gt;( \"xyz\" );\r\n\tptr.reset(nullptr);\r\n\r\n\tstd::vector&lt;int&gt; vec{ 1, 2, 3, 4 };\r\n\tfor (int &amp; it : vec)\r\n\t\tstd::cout &lt;&lt; it &lt;&lt; '\\n';\r\n}<\/pre>\r\n\r\n\r\n\r\n<p>In the above code sample the tool managed to fix several issues, and we can decide if we want to apply all or maybe just select a few of them. For example, I&#8217;m not sure about using a trailing return type for all functions. Additionally, the tool couldn&#8217;t improve and apply <code>make_unique<\/code> in the place where we declare and initialise <code>ptr<\/code>. So hopefully, with each new revision, we&#8217;ll get even better results.<\/p>\r\n\r\n\r\n\r\n<p>But also it&#8217;s important to remember that you have to <strong>be careful<\/strong> with the fixes.<\/p>\r\n\r\n\r\n\r\n<p>See lines 1 and 12.<\/p>\r\n\r\n\r\n\r\n<p>Clang-tidy added extra and duplicated header file &#8220;memory&#8221; (this was probably needed for make_unique()). This is not critical, but shouldn&#8217;t happen. But what&#8217;s worse is that at line 12 the code is now wrong.<\/p>\r\n\r\n\r\n\r\n<p>The line <code>if (hello + world == \"\")<\/code> was changed into <code>if (hello + world.empty())<\/code>. This changes the meaning of the statement!<\/p>\r\n\r\n\r\n\r\n<p>When applying fixes, be sure<strong> to review code and check<\/strong> if this is what you expected.<\/p>\r\n\r\n\r\n\r\n<p>I hope you now get a general idea of how to use the tool and what its output is.<\/p>\r\n\r\n\r\n\r\n<p>Running this tool from a command line might not be the best choice. Fortunately, there are many options on how we can plug it inside our build system or IDE.<\/p>\r\n\r\n\r\n\r\n<p>For example, you can\u00c2\u00a0learn how to make use of it with CMake on a KDAB blog or&#8230; <a href=\"https:\/\/www.kdab.com\/clang-tidy-part-1-modernize-source-code-using-c11c14\/\">Clang-Tidy, part 1: Modernise your source code using C++11\/C++14 &#8211; KDAB<\/a><\/p>\r\n\r\n\r\n\r\n<p>Let&#8217;s now have a brief look at the options in Visual Studio.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Clang_Tools_in_Visual_Studio\"><\/span>Clang Tools in Visual Studio<span class=\"ez-toc-section-end\"><\/span><\/h2>\r\n\r\n\r\n\r\n<p><strong>clang-tidy<\/strong> support is available starting with Visual Studio 2019 version 16.4.<\/p>\r\n\r\n\r\n\r\n<p><strong>clang-tidy<\/strong> is the default analysis tool when using the LLVM\/clang-cl toolset, but you can also enable it with MSVC compiler:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_enable.png?ssl=1\"><img decoding=\"async\" width=\"786\" height=\"544\" data-attachment-id=\"1759\" data-permalink=\"https:\/\/stage.wholetomato.com\/blog\/clang_tidy_vs_enable\/\" data-orig-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_enable.png?fit=786%2C544&amp;ssl=1\" data-orig-size=\"786,544\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"clang_tidy_vs_enable\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_enable.png?fit=300%2C208&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_enable.png?fit=786%2C544&amp;ssl=1\" class=\"wp-image-1759\" src=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_enable.png?resize=786%2C544&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_enable.png?w=786&amp;ssl=1 786w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_enable.png?resize=300%2C208&amp;ssl=1 300w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_enable.png?resize=768%2C532&amp;ssl=1 768w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_enable.png?resize=360%2C249&amp;ssl=1 360w\" sizes=\"(max-width: 786px) 100vw, 786px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\r\n\r\n\r\n\r\n<p>And you can configure it with a separate property panel:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_setup.png?ssl=1\"><img decoding=\"async\" width=\"774\" height=\"532\" data-attachment-id=\"1760\" data-permalink=\"https:\/\/stage.wholetomato.com\/blog\/clang_tidy_vs_setup\/\" data-orig-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_setup.png?fit=774%2C532&amp;ssl=1\" data-orig-size=\"774,532\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"clang_tidy_vs_setup\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_setup.png?fit=300%2C206&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_setup.png?fit=774%2C532&amp;ssl=1\" class=\"wp-image-1760\" src=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_setup.png?resize=774%2C532&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_setup.png?w=774&amp;ssl=1 774w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_setup.png?resize=300%2C206&amp;ssl=1 300w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_setup.png?resize=768%2C528&amp;ssl=1 768w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/11\/clang_tidy_vs_setup.png?resize=360%2C247&amp;ssl=1 360w\" sizes=\"(max-width: 774px) 100vw, 774px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\r\n\r\n\r\n\r\n<p>And when you run a code analysis you can get results to your Error\/Warning list:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-vs-output.png?ssl=1\"><img decoding=\"async\" width=\"1140\" height=\"272\" data-attachment-id=\"1782\" data-permalink=\"https:\/\/stage.wholetomato.com\/blog\/clang-vs-output\/\" data-orig-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-vs-output.png?fit=1143%2C273&amp;ssl=1\" data-orig-size=\"1143,273\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"clang-vs-output\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-vs-output.png?fit=300%2C72&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-vs-output.png?fit=1024%2C245&amp;ssl=1\" class=\"wp-image-1782\" src=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-vs-output.png?resize=1140%2C272&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-vs-output.png?w=1143&amp;ssl=1 1143w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-vs-output.png?resize=300%2C72&amp;ssl=1 300w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-vs-output.png?resize=1024%2C245&amp;ssl=1 1024w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-vs-output.png?resize=768%2C183&amp;ssl=1 768w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-vs-output.png?resize=1140%2C273&amp;ssl=1 1140w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-vs-output.png?resize=360%2C86&amp;ssl=1 360w\" sizes=\"(max-width: 1140px) 100vw, 1140px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\r\n\r\n\r\n\r\n<p>Unfortunately, the current version of Visual Studio doesn&#8217;t offer a way to apply fixes, so you need to modify the code on your own.<\/p>\r\n\r\n\r\n\r\n<p>Luckily, with the help of Visual Assist, you can change it. See below.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_Visual_Assist_Makes_Things_Much_Safer_And_Easier\"><\/span>How Visual Assist Makes Things Much Safer And Easier<span class=\"ez-toc-section-end\"><\/span><\/h2>\r\n\r\n\r\n\r\n<p>Visual Assist offers a service called &#8220;Code Inspection&#8221;, which is based on a standalone LLVM\/Clang embedded into the VA system. You can enable it (even in Visual studio 2008!), and for our simple example you might get the following results (in a separate VA Code Inspection Window):<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output.png?ssl=1\"><img decoding=\"async\" width=\"708\" height=\"179\" data-attachment-id=\"1775\" data-permalink=\"https:\/\/stage.wholetomato.com\/blog\/clang-va-output\/\" data-orig-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output.png?fit=708%2C179&amp;ssl=1\" data-orig-size=\"708,179\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"clang-va-output\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output.png?fit=300%2C76&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output.png?fit=708%2C179&amp;ssl=1\" class=\"wp-image-1775\" src=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output.png?resize=708%2C179&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output.png?w=708&amp;ssl=1 708w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output.png?resize=300%2C76&amp;ssl=1 300w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output.png?resize=360%2C91&amp;ssl=1 360w\" sizes=\"(max-width: 708px) 100vw, 708px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\r\n\r\n\r\n\r\n<p>And what&#8217;s best is that for many of them you can apply a fix!<\/p>\r\n\r\n\r\n\r\n<p>See the following context menu:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output-fix-menu.png?ssl=1\"><img decoding=\"async\" width=\"1017\" height=\"246\" data-attachment-id=\"1776\" data-permalink=\"https:\/\/stage.wholetomato.com\/blog\/clang-va-output-fix-menu\/\" data-orig-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output-fix-menu.png?fit=1017%2C246&amp;ssl=1\" data-orig-size=\"1017,246\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"clang-va-output-fix-menu\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output-fix-menu.png?fit=300%2C73&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output-fix-menu.png?fit=1017%2C246&amp;ssl=1\" class=\"wp-image-1776\" src=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output-fix-menu.png?resize=1017%2C246&#038;ssl=1\" alt=\"\" srcset=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output-fix-menu.png?w=1017&amp;ssl=1 1017w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output-fix-menu.png?resize=300%2C73&amp;ssl=1 300w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output-fix-menu.png?resize=768%2C186&amp;ssl=1 768w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2020\/12\/clang-va-output-fix-menu.png?resize=360%2C87&amp;ssl=1 360w\" sizes=\"(max-width: 1017px) 100vw, 1017px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\r\n\r\n\r\n\r\n<p>This is great! Thanks to the embedded LLVM\/Clang subsystem, Visual Assist can perform the analysis and help you with many tasks related to code modernisation and fundamental code analysis!<\/p>\r\n\r\n\r\n\r\n<p>But what&#8217;s best is that Visual Assist cleans up the output from clang-tidy and <strong>makes sure the fixes are correct and safe<\/strong>. Here&#8217;s the code after I applied all suggestions:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-syntaxhighlighter-code\">#include &lt;vector&gt;\r\n#include &lt;memory&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n\r\nint main() {\r\n\tint a;\r\n\r\n\tstd::string hello{ \"hello\" };\r\n\tstd::string world{ \"world\" };\r\n\tif ((hello + world).empty())\r\n\t\tstd::cout &lt;&lt; \"empty!\\n\";\r\n\r\n\tstd::unique_ptr&lt;std::string&gt; ptr{ new std::string { \"abc\" } };\r\n\tptr = std::make_unique&lt;std::string&gt;( \"xyz\" );\r\n\tptr.reset(nullptr);\r\n\r\n\tstd::vector&lt;int&gt; vec{ 1, 2, 3, 4 };\r\n\tfor (int &amp; it : vec)\r\n\t\tstd::cout &lt;&lt; it &lt;&lt; '\\n';\r\n}<\/pre>\r\n\r\n\r\n\r\n<p>Nice!<\/p>\r\n\r\n\r\n\r\n<p>As you can see there&#8217;s no extra include statement. And what&#8217;s most important is in line 12. VA added extra brackets, so the whole expression <strong>is now correct and safe!<\/strong><\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\r\n\r\n\r\n\r\n<p>In this article, we covered <strong>clang-tidy<\/strong> &#8211; a handy tool for code analysis that can (experimentally) fix your code automatically! The tool is quite verbose and might be hard to configure to work with large projects. In addition, please make sure you review code when applying fixes.<\/p>\r\n\r\n\r\n\r\n<p>By default, you can download it and launch from a command line, but it&#8217;s much better to use it from Visual Studio (limited).<\/p>\r\n\r\n\r\n\r\n<p>To get the best experience and safety have a look at the embedded clang-tidy inside Visual Assist &#8211; in the form of &#8220;VA Code Inspections&#8221;. This extra feature makes sure the results of code analysis are easy to read and meaningful, and the fixes are correct.<\/p>\r\n\r\n\r\n\r\n<p>Today we only scratched the surface of this exciting topic. In two upcoming blog posts you&#8217;ll see some more use cases where Visual Assist can help you with code refactoring and modernisation (also leveraging embedded <strong>clang-tidy<\/strong>). Stay tuned.<\/p>\r\n\r\n\r\n\r\n<p>For now you can read more in:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><a href=\"https:\/\/clang.llvm.org\/extra\/clang-tidy\/\">Clang-Tidy \u00e2\u20ac\u201d Extra Clang Tools 12 documentation<\/a><\/li>\r\n<li><a href=\"https:\/\/docs.wholetomato.com\/default.asp?W760\">Introduction to Code Inspection<\/a> Visual Assist Documentation<\/li>\r\n<li><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/code-analysis-with-clang-tidy-in-visual-studio\/\">Code analysis with clang-tidy in Visual Studio | C++ Team Blog<\/a><\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<div class=\"wp-block-image\">\r\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.wholetomato.com\/\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"600\" height=\"200\" data-attachment-id=\"1811\" data-permalink=\"https:\/\/stage.wholetomato.com\/blog\/007_banner_wholetomato_resize_emiliano_600x200\/\" data-orig-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/01\/007_banner_wholetomato_resize_emiliano_600x200.png?fit=600%2C200&amp;ssl=1\" data-orig-size=\"600,200\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"007_banner_wholetomato_resize_emiliano_600x200\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/01\/007_banner_wholetomato_resize_emiliano_600x200.png?fit=300%2C100&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/01\/007_banner_wholetomato_resize_emiliano_600x200.png?fit=600%2C200&amp;ssl=1\" class=\"wp-image-1811\" src=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/01\/007_banner_wholetomato_resize_emiliano_600x200.png?resize=600%2C200&#038;ssl=1\" alt=\"Bulk up &amp; build faster in Visual Studio\" srcset=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/01\/007_banner_wholetomato_resize_emiliano_600x200.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/01\/007_banner_wholetomato_resize_emiliano_600x200.png?resize=300%2C100&amp;ssl=1 300w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/01\/007_banner_wholetomato_resize_emiliano_600x200.png?resize=360%2C120&amp;ssl=1 360w\" sizes=\"(max-width: 600px) 100vw, 600px\" data-recalc-dims=\"1\" \/><\/a>\r\n<figcaption>https:\/\/www.wholetomato.com\/<\/figcaption>\r\n<\/figure>\r\n<\/div>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>As a developer, you probably know that parsing C++ source code is very complicated. This is also the reason why there might be fewer good tools for coding assistance than we have in other &#8220;lighter&#8221;&#8230;<\/p>\n","protected":false},"author":183830964,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_jetpack_newsletter_access":"","_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[672],"tags":[],"class_list":["post-1755","post","type-post","status-publish","format-standard","hentry","category-tips-and-tricks"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pfhqii-sj","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/posts\/1755","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/users\/183830964"}],"replies":[{"embeddable":true,"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/comments?post=1755"}],"version-history":[{"count":25,"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/posts\/1755\/revisions"}],"predecessor-version":[{"id":2564,"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/posts\/1755\/revisions\/2564"}],"wp:attachment":[{"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/media?parent=1755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/categories?post=1755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/tags?post=1755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}