# CMAKE\_CXX\_CLANG\_TIDY in MSVC

**URL:** https://discourse.cmake.org/t/cmake-cxx-clang-tidy-in-msvc/890
**Category:** Development
**Tags:** comp:msvc, gen:vs
**Created:** [March 31, 2020, 12:31pm UTC](https://discourse.cmake.org/t/cmake-cxx-clang-tidy-in-msvc/890 "2020-03-31T12:31:00Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![duhone](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/a3d4f5/32.png) [@duhone](https://discourse.cmake.org/u/duhone)
#### Post date: [March 31, 2020, 12:31pm UTC](https://discourse.cmake.org/t/cmake-cxx-clang-tidy-in-msvc/890/1 "2020-03-31T12:31:00Z")

</div>

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>
```

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [March 31, 2020, 1:53pm UTC](https://discourse.cmake.org/t/cmake-cxx-clang-tidy-in-msvc/890/2 "2020-03-31T13:53:30Z")

</div>

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

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [March 31, 2020, 5:52pm UTC](https://discourse.cmake.org/t/cmake-cxx-clang-tidy-in-msvc/890/3 "2020-03-31T17:52:04Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ymba9g8j9cjp0wloqf5y/32/1244_2.png) [@YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/u/YMba9g8j9CJp0wLoQf5y)
#### Post date: [April 1, 2020, 9:44pm UTC](https://discourse.cmake.org/t/cmake-cxx-clang-tidy-in-msvc/890/4 "2020-04-01T21:44:42Z")

</div>

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

```auto
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
)

```

---

<div class="post-metadata">

### Author: ![YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ymba9g8j9cjp0wloqf5y/32/1244_2.png) [@YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/u/YMba9g8j9CJp0wLoQf5y)
#### Post date: [April 1, 2020, 9:46pm UTC](https://discourse.cmake.org/t/cmake-cxx-clang-tidy-in-msvc/890/5 "2020-04-01T21:46:27Z")

</div>

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

Also MicrosoftCodeAnalysis is slow.

---

<div class="post-metadata">

### Author: ![duhone](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/a3d4f5/32.png) [@duhone](https://discourse.cmake.org/u/duhone)
#### Post date: [April 2, 2020, 2:29am UTC](https://discourse.cmake.org/t/cmake-cxx-clang-tidy-in-msvc/890/6 "2020-04-02T02:29:48Z")

</div>

> [@YMba9g8j9CJp0wLoQf5y](#):
>
> 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 )

That worked! thanks.

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [April 2, 2020, 3:49am UTC](https://discourse.cmake.org/t/cmake-cxx-clang-tidy-in-msvc/890/7 "2020-04-02T03:49:04Z")

</div>

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

---

<div class="post-metadata">

### Author: ![YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ymba9g8j9cjp0wloqf5y/32/1244_2.png) [@YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/u/YMba9g8j9CJp0wLoQf5y)
#### Post date: [April 2, 2020, 7:06pm UTC](https://discourse.cmake.org/t/cmake-cxx-clang-tidy-in-msvc/890/8 "2020-04-02T19:06:26Z")

</div>

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](https://docs.microsoft.com/en-us/cpp/code-quality/clang-tidy?view=vs-2019))

```auto
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.

```

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [April 3, 2020, 6:33am UTC](https://discourse.cmake.org/t/cmake-cxx-clang-tidy-in-msvc/890/9 "2020-04-03T06:33:42Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![duhone](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/a3d4f5/32.png) [@duhone](https://discourse.cmake.org/u/duhone)
#### Post date: [April 4, 2020, 3:59pm UTC](https://discourse.cmake.org/t/cmake-cxx-clang-tidy-in-msvc/890/10 "2020-04-04T15:59:44Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Elad\_M](https://discourse.cmake.org/user_avatar/discourse.cmake.org/elad_m/32/3185_2.png) [@Elad\_M](https://discourse.cmake.org/u/Elad_M)
#### Post date: [February 18, 2023, 9:05pm UTC](https://discourse.cmake.org/t/cmake-cxx-clang-tidy-in-msvc/890/11 "2023-02-18T21:05:32Z")

</div>

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