Code analysis with clang-tidy in Visual Studio

eli fessler

[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.

Configuring clang-tidy

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.

MSBuild projects

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 documentation for more details.

CMake projects

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.

Future work

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.


Discussion is closed. Login to edit/delete existing comments.

  • Mike Diack 0

    Hi Eli
    Thanks for this. I’ve now installed the Clang tools, and all is mostly working well.
    I can use Clang-Tidy as part of code analysis on build, together with Microsoft’s own analysis tool.
    e.g. with the example trivial code:

    int main()
    // Spot the bug – null pointer deref
    char* p = NULL;
    *p = ‘a’;
    std::cout << "Hello World!\n";

    Both MS's analyzer and Clang-Tidy catch this fine.

    The MS analyzer also catches it with a squiggle in the background if I close the solution and open it. But the same "background analysis" is not occuring with clang tidy (the squiggle doesn't appear in the background). I've turned all the options on to enable it but background "clang-tidy" squiggles don't work for me.

    Am I the only one, have I found a bug?


    • Denis Bezykornov 0

      Same for me

      • Mike Diack 0

        I’ve raised a bug on this via VS 2019 Update 4 Preview 1’s feedback btw.

  • Mike Diack 0

    Although I’ve had to raise a bug on this, please take heart Eli and the code analysis team. You are doing a great job. I’ve been a big ambassador for static analysis since finding Gimpel’s PC-Lint in 1992, and beginning to use it in earnest in the mid 90s. But for a long term I was very much in the minority

    10 years ago, code analysis, was rarely mentioned in many environments, and choices were limited, Gimpel’s Lint demanded time and effort to get it right (but God it works well when it is set up!), and then there were the prohibitively expensive tools like Klocwork etc.

    Now we’re spoilt for choice with a mass of free or low cost static analysis tools, Microsoft and others can take a lot of credit for that.
    I’m thinking of CppCheck, PVS Studio, Clang-Tidy and the Clang Power Tools, Jetbrains Free Resharper Command Line Tools (inspectcode.x86.exe), PC-Lint, PC-Lint Plus, MS’s own analysis tools. Many of which are either free of cost or low cost. You and many others have made static analysis mainstream, and hopefully C/C++ code quality and reliability in the real world will improve as a result.

    Certainly after a compiler and a debugger, a decent static analysis tool is the best tool to get and learn inside out for any C/C++ developer. If you can learn and use multiple tools (all have their strengths and weaknesses), so much the better!

    But kudos and thanks!

  • James Cheng 0

    Hi eli,
    I use vs2019 enterprise and install the clang tool for windows as you mentioned.

    But in the “code analysis” tab, it only show “general” without “clang tidy” , nothing else… How could I do next? Thank you.

    • Mike Diack 0

      Are you sure you are using the VS 2019 Update 4 Preview 1 (or later edition)? (Version 16.4.0 Preview 1.0)? This feature is not in 16.3.3 or any of the lower numbered versions?

  • James Cheng 0

    Another question is, how can I generate compilation database from MSbuild?

    I want to run clang tidy from command line for all the source files… but encounter this problem,


  • Austen Stone 0

    How can I enable clang-tidy for all projects by default? Is there a default value for “Checks to Enable or Disable”?

  • 0

    Hi Eli
    It seems “performance-for-range-copy” is not activated by default?

    std::vector< std::string > v;
    for (auto s : v) {
    std::cout << s;
    s can be made a const reference.
    Thank you.

  • Sangjin Han 0

    Thanks for this wonderful feature.
    I could use custom clang-tidy by replacing the one in the directory ‘2019/Preview/vc/tools/llvm/bin’.
    I’m looking forward to using the quick fixes (–fix) feature in visual studio.

  • Roman Petrov 0

    Thank you, clang-tidy support in Visual Studio is a great feature. We develop cross-platform projects in Visual Studio / C++ and use “Shared Items” projects to share cross platform code between platform-dependent projects. When I enable clang-tidy for the project, Visual Studio enables code analysis only for own project files and dependent “Shared items” projects are not analyzed.

    Is there any way to enable clang-tidy checks for “Shared Items” C++ projects?

  • Verstappen, Jos 0

    Nice new feature! I am trying it out with VS 16.4.0.

    I want to try to enable only one check: readability-inconsistent-declaration-parameter-name. So in the project properties, Code Analysis > Clang-Tidy I filled in “-*,readability-inconsistent-declaration-parameter-name” (without the quotes) to enable only that check. When I then run it on the project, or on a single file, it still performs all checks.

    Someone know how to fix this?

Feedback usabilla icon