skip to Main Content

Visual Studio (Desktop) has a clang-tidy integration. you can make VS invoke clang-tidy with a list of checks.
however, I could not find a way to make it use an existing .clang-tidy configuration file.

The documentation hints that it is possible:

Clang-Tidy configuration By default, Clang-Tidy does not set any
checks when enabled. To see the list of checks in the command-line
version, run clang-tidy -list-checks in a developer command prompt.
You can configure the checks that Clang-Tidy runs inside Visual
Studio. In the project Property Pages dialog, open the Configuration
Properties > Code Analysis > Clang-Tidy page. Enter checks to run in
the Clang-Tidy Checks property. A good default set is
clang-analyzer-*. This property value 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.

This is what I tried to do manually via the VS property pages:

enter image description here

But when running analyses on a file, it doesn’t work.

So How to get Visual Studio use a .clang-tidy configuration file when invoking clang-tidy?

2

Answers


  1. Chosen as BEST ANSWER

    It seems Visual Studio may invoke clang-tidy from a directory outside the source tree in cases where the build is generated out-of-source (as commonly happens when using CMake).

    I found a small hack around it. Basically I trick Visual Studio into thinking I am giving it a list of checks, where in fact I give it the path to the config file along with whatever arguments I want. (note the extra " characters).

    enter image description here

    This is of course a hack. But still might serve somebody.

    Here is a way to do that with CMake:

    set_target_properties(MyTarget PROPERTIES
        VS_GLOBAL_RunCodeAnalysis false
    
        # Use visual studio core guidelines
        VS_GLOBAL_EnableMicrosoftCodeAnalysis false
        #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 "-* ""--config-file=${MY_CLANG_TIDY_CONFIG_PATH} --header-filter=.*" 
    )
    

  2. The property pages are for setting clang-tidy checks directly (not a path to the .clang-tidy file). Visual Studio should automatically detect the .clang-tidy file in your workspace, as long as it’s in the same or a parent folder of your source files.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search