Code analysis with clang-tidy in Visual Studio
[Updated on 11/6/2019] – Removed “Setup: Installing Clang tools” 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 “linter” 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 Clang/LLVM for MSBuild projects and for CMake projects.
Code Analysis defaults to the respective tool as dictated by your platform toolset and compiler: Microsoft Code Analysis if using MSVC (“Visual Studio 2019”) 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.
We’ve redesigned the Code Analysis section of project Property Pages, allowing you to better configure and define which tool you’re using with each of your projects. In the General tab, you can select which tool(s) to run when running analysis – whether that’s via the Analyze menu, on build, or automatically in the background. (If you explicitly run Code Analysis without either tool enabled, you’ll simply get a warning returned in the Error List.)
Rule sets for the MSVC Code Analysis engine are configurable under the “Microsoft” tab, while the “Clang-Tidy” tab allows you to specify which specific clang-tidy checks to enable or disable, i.e. the input to be provided to the –checks option of the tool.
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. See the LLVM.org documentation for more details.
In CMake configurations targeting Windows, you can customize checks by specifying the clangTidyChecks key in the JSON view of CMakeSettings.json, with a string value to be passed to the tool’s –checks option like above. Starting in Preview 2, you can also configure which tool(s) to use via the enableMicrosoftCodeAnalysis and enableClangTidyCodeAnalysis keys, with in-editor warnings and the Error List updating on file save.
We do not currently support configuration via the CMAKE_<LANG>_CLANG_TIDY 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.
Code Analysis can be further configured (e.g. toggling squiggle display or background runs) within Tools > Options > Text Editor > C/C++ > Advanced.
Error List & editor integration
Running clang-tidy results in itemized warnings populating the Error List, from where you can quickly navigate around your code. The “Category” column provides information on the type of warning based on the check prefix, e.g. cppcoreguidelines, readability, or clang-diagnostic.
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.
The UI doesn’t currently support pointing Visual Studio to use a custom clang-tidy.exe, which may be desired in the case of using custom checks, but we’re exploring this as an option going forward. We’re also working toward allowing you to run clang-tidy with the –fix flag and applying resulting quick fixes.
Send us feedback
Your feedback is a key part of ensuring we’re able to deliver the best Code Analysis experience to all! We’d love for you to try out the latest Preview version of Visual Studio 2019 version 16.4 and let us know how it’s working for you, either in the comments below or via email. If you encounter problems or have suggestions, please Report A Problem or reach out via Developer Community. You can also find us on Twitter @VisualC.