CMAKE_CXX_CLANG_TIDY in MSVC

You can use clang-tidy in visual studio now apparently. I really don’t like the cmake setting json they have, so I don’t want to use that to use clang-tidy, I prefer the old fashioned way. Would be nice if cmake would apply CMAKE_CXX_CLANG_TIDY to visual studio project generation.

Switching to clang-tidy for code analysis adds something like this to a vcxproj

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <RunCodeAnalysis>true</RunCodeAnalysis>
    <EnableMicrosoftCodeAnalysis>false</EnableMicrosoftCodeAnalysis>
    <EnableClangTidyCodeAnalysis>true</EnableClangTidyCodeAnalysis>
    <ClangTidyChecks>cppcoreguidelines-*</ClangTidyChecks>
  </PropertyGroup>

CMAKE_CXX_CLANG_TIDY takes the path to the clang-tidy tool and optionally arguments for it. That is too low-level to map directly to the .vcxproj settings you describe. We’ll likely need some higher-level interface for it.

You can insert that code snippet into your visual studio project by daisy chaining the user properties file (can be changed with a target property)

I agree I also didn’t want to use CMakeSettings.json
So I figured this out.

set_target_properties(foo PROPERTIES
    VS_GLOBAL_RunCodeAnalysis true

    # Use visual studio core guidelines
    VS_GLOBAL_EnableMicrosoftCodeAnalysis true
    VS_GLOBAL_CodeAnalysisRuleSet ${CMAKE_CURRENT_SOURCE_DIR}/foo.ruleset
    VS_GLOBAL_CodeAnalysisRuleSet ${CMAKE_CURRENT_SOURCE_DIR}/foo.ruleset

    # Use clangtidy
    VS_GLOBAL_EnableClangTidyCodeAnalysis true
    VS_GLOBAL_ClangTidyChecks -checks=-*,modernize-*,-modernize-use-trailing-return-type
)
1 Like

This has worked fine for me. Only complaint is it could potentially break.

Also MicrosoftCodeAnalysis is slow.

That worked! thanks.

Di you know if VS can use the external clang-tidy control file (.clang-tidy) which allows better control?

VS can use a .clang-tidy file according to the ms-docs (https://docs.microsoft.com/en-us/cpp/code-quality/clang-tidy?view=vs-2019)

Clang-Tidy configuration

You can configure the checks that clang-tidy runs inside Visual Studio via the Clang-Tidy Checks option. 
This input is provided to the --checks argument of the tool. 
Any further configuration can be included in custom .clang-tidy files. 
For more information, see the Clang-Tidy documentation on LLVM.org.

Ok, I also tested it in my project but I did not have clang-tidy installed with Visual Studio. Instead, I added another property VS_GLOBAL_ClangTidyToolExe and set it to “${CLANG_TIDY_EXECUTABLE}” which I previously searched with find_program. I make it easier for my users by searching in the default install location of LLVM windows installer:
ENV ProgramFiles
and
ENV ProgramFiles(x86
with PATH_SUFFIXES set to LLVM/bin.
This way, users/developers only have to flip a switch instead of lots of typing.

Thanks for the inspiration.

On trying it out:
Hell, this is slow! I like the direct integration into the editor like in QtCreator much better. Additionally, aborting a build does not abort the clang-tidy program.

It is crazy slow. I went ahead and turned off VS_GLOBAL_RunCodeAnalysis in my project. I also left VS_GLOBAL_EnableMicrosoftCodeAnalysis off, so clang tidy only. I’ll just run it once in a while manually. With the rest of the settings, “Analyze->Run code Analysis” runs clang-tidy manually. Once I have continuous integration going on this project, I’ll set it up to run there too. But too slow to run on a normal local build.

Did you actually manage to get VS use a .clang-tidy configuration file?