{"id":25052,"date":"2019-10-08T18:34:42","date_gmt":"2019-10-08T18:34:42","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=25052"},"modified":"2019-11-06T23:48:03","modified_gmt":"2019-11-06T23:48:03","slug":"code-analysis-with-clang-tidy-in-visual-studio","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/code-analysis-with-clang-tidy-in-visual-studio\/","title":{"rendered":"Code analysis with clang-tidy in Visual Studio"},"content":{"rendered":"<p><span style=\"color: #808080;\"><em><strong>[Updated on 11\/6\/2019]<\/strong> &#8211; Removed &#8220;Setup: Installing Clang tools&#8221; section; this is no longer required starting in Visual Studio 2019 version 16.4 Preview 3.<\/em><\/span><\/p>\n<p><a href=\"https:\/\/visualstudio.microsoft.com\/vs\/preview\/\">Visual Studio 2019 version 16.4 Preview 1<\/a> brings a significant improvement to the C++ code analysis experience: native support for <a href=\"https:\/\/clang.llvm.org\/extra\/clang-tidy\/\">clang-tidy<\/a>, a Clang-based \u201clinter\u201d tool developed by the LLVM Project that delivers a variety of code improvements such as modernization and standards conformance, static analysis, and automatic formatting. For more information about using Clang\/LLVM as a compiler in your project, see our past blog posts on <a href=\"https:\/\/aka.ms\/cpp\/clangmsbuild\">Clang\/LLVM for MSBuild projects<\/a> and <a href=\"https:\/\/aka.ms\/cpp\/clangcmake\">for CMake projects<\/a>.<\/p>\n<h2>Configuring clang-tidy<\/h2>\n<p>Code Analysis defaults to the respective tool as dictated by your <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/how-to-modify-the-target-framework-and-platform-toolset?view=vs-2019\">platform toolset<\/a> and compiler: Microsoft Code Analysis if using MSVC (\u201cVisual Studio 2019\u201d) and clang-tidy if using LLVM\/clang-cl. While Code Analysis will run automatically in the background on files opened in the Editor, by default it will not run at build time on all your files. Read on to learn about further configuration options.<\/p>\n<h3>MSBuild projects<\/h3>\n<p>We\u2019ve redesigned the Code Analysis section of project Property Pages, allowing you to better configure and define which tool you\u2019re using with each of your projects. In the General tab, you can select which tool(s) to run when running analysis \u2013 whether that\u2019s via the Analyze menu, on build, or <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/in-editor-code-analysis-in-visual-studio-2019-preview-2\/\">automatically in the background<\/a>. (If you explicitly run Code Analysis without either tool enabled, you\u2019ll simply get a warning returned in the Error List.)<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-25056 size-full\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/CAPropertyPages.png\" alt=\"\" width=\"1025\" height=\"701\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/CAPropertyPages.png 1025w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/CAPropertyPages-300x205.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/CAPropertyPages-768x525.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/CAPropertyPages-1024x700.png 1024w\" sizes=\"(max-width: 1025px) 100vw, 1025px\" \/><\/p>\n<p>Rule sets for the MSVC Code Analysis engine are configurable under the \u201cMicrosoft\u201d tab, while the \u201cClang-Tidy\u201d tab allows you to specify which specific clang-tidy checks to enable or disable, i.e. the input to be provided to the <span class=\"lang:default decode:true crayon-inline \">&#8211;checks<\/span>\u00a0 option of the tool.<\/p>\n<p>Further configuration is possible via .clang-tidy files, from where the tool attempts to read additional settings and in which you can provide further configuration, e.g. via options such as Checks, HeaderFilterRegex, and SystemHeaders. \u00a0See the <a href=\"https:\/\/clang.llvm.org\/extra\/clang-tidy\/\">LLVM.org documentation<\/a> for more details.<\/p>\n<h3>CMake projects<\/h3>\n<p>In CMake configurations targeting Windows, you can customize checks by specifying the <span class=\"lang:default decode:true crayon-inline \">clangTidyChecks<\/span>\u00a0 key in the JSON view of CMakeSettings.json, with a string value to be passed to the tool\u2019s <span class=\"lang:default decode:true crayon-inline \">&#8211;checks<\/span>\u00a0 option like above. Starting in Preview 2, you can also configure which tool(s) to use via the <span class=\"lang:default decode:true crayon-inline \">enableMicrosoftCodeAnalysis<\/span> and <span class=\"lang:default decode:true crayon-inline \">enableClangTidyCodeAnalysis<\/span>\u00a0keys, with in-editor warnings and the Error List updating on file save.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-25057\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/CMakeClangTidyChecks.png\" alt=\"\" width=\"559\" height=\"305\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/CMakeClangTidyChecks.png 559w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/CMakeClangTidyChecks-300x164.png 300w\" sizes=\"(max-width: 559px) 100vw, 559px\" \/><\/p>\n<p>We do not currently support configuration via the <span class=\"lang:default decode:true crayon-inline \">CMAKE_&lt;LANG&gt;_CLANG_TIDY<\/span>\u00a0 variable in CMakeLists.txt in the IDE. In addition, Clang-Tidy support for CMake configurations only extends to Windows, i.e. we do not yet support WSL or remote Linux targeting.<\/p>\n<p>Code Analysis can be further configured (e.g. toggling squiggle display or background runs) within Tools &gt; Options &gt; Text Editor &gt; C\/C++ &gt; Advanced.<\/p>\n<h2>Error List &amp; editor integration<\/h2>\n<p>Running clang-tidy results in itemized warnings populating the Error List, from where you can quickly navigate around your code. The \u201cCategory\u201d column provides information on the type of warning based on the check prefix, e.g. cppcoreguidelines, readability, or clang-diagnostic.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-25058\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/ClangTidyErrorList.png\" alt=\"\" width=\"1297\" height=\"312\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/ClangTidyErrorList.png 1297w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/ClangTidyErrorList-300x72.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/ClangTidyErrorList-768x185.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/ClangTidyErrorList-1024x246.png 1024w\" sizes=\"(max-width: 1297px) 100vw, 1297px\" \/><\/p>\n<p>Warnings also display as in-editor squiggles so you can easily view the location and context of discovered issues. Squiggles show underneath relevant sections of code and hovering over these displays a tooltip with information about the issue, just like with Microsoft Code Analysis warnings.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-25061\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/ClangTidySquiggles.png\" alt=\"\" width=\"575\" height=\"135\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/ClangTidySquiggles.png 575w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/10\/ClangTidySquiggles-300x70.png 300w\" sizes=\"(max-width: 575px) 100vw, 575px\" \/><\/p>\n<h2>Future work<\/h2>\n<p>The UI doesn\u2019t currently support pointing Visual Studio to use a <strong>custom clang-tidy.exe<\/strong>, which may be desired in the case of using custom checks, but we\u2019re exploring this as an option going forward. We\u2019re also working toward allowing you to <strong>run clang-tidy with the <span class=\"lang:default decode:true crayon-inline \">&#8211;fix<\/span>\u00a0 flag<\/strong> and applying resulting quick fixes.<\/p>\n<h2>Send us feedback<\/h2>\n<p>Your feedback is a key part of ensuring we\u2019re able to deliver the best Code Analysis experience to all! We\u2019d love for you to try out the latest Preview version of <a href=\"https:\/\/visualstudio.microsoft.com\/vs\/preview\/\">Visual Studio 2019 version 16.4<\/a> and let us know how it\u2019s working for you, either in the comments below or\u00a0<a href=\"mailto:visualcpp@microsoft.com\">via email<\/a>. If you encounter problems or have\u00a0suggestions,\u00a0please\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/how-to-report-a-problem-with-visual-studio?view=vs-2019\">Report A Problem<\/a> or reach out via <a href=\"https:\/\/developercommunity.visualstudio.com\/spaces\/62\/index.html\">Developer Community<\/a>. You can also find us on Twitter <a href=\"https:\/\/twitter.com\/visualc\">@VisualC<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Updated on 11\/6\/2019] &#8211; Removed &#8220;Setup: Installing Clang tools&#8221; section; this is no longer required starting in Visual Studio 2019 version 16.4 Preview 3. Visual Studio 2019 version 16.4 Preview 1 brings a significant improvement to the C++ code analysis experience: native support for clang-tidy, a Clang-based \u201clinter\u201d tool developed by the LLVM Project that [&hellip;]<\/p>\n","protected":false},"author":1079,"featured_media":25058,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[270],"tags":[119],"class_list":["post-25052","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcement","tag-code-analysis"],"acf":[],"blog_post_summary":"<p>[Updated on 11\/6\/2019] &#8211; Removed &#8220;Setup: Installing Clang tools&#8221; section; this is no longer required starting in Visual Studio 2019 version 16.4 Preview 3. Visual Studio 2019 version 16.4 Preview 1 brings a significant improvement to the C++ code analysis experience: native support for clang-tidy, a Clang-based \u201clinter\u201d tool developed by the LLVM Project that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/25052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/1079"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=25052"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/25052\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/25058"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=25052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=25052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=25052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}