Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use clang-tidy with Visual Studio and CMake?

My ultimate goal is to do a one-off automated modernization of some code and apply our identifier capitalization rules that are expressed in .clang-tidy. Being able to repeat this over time would be nice. We use Visual Studio 2015, CMake 3.13, and I've installed LLVM 9 to get clang-tidy.

I'm fine with running clang-tidy on the command line, or from CMake, or Visual Studio, but I need help for any of those.

For running on the command line I need a compile_commands.json file from CMake, but passing -DCMAKE_EXPORT_COMPILE_COMMANDS=ON will not create one. I've heard this does not work for CMake Visual Studio generators. I tried a Ninja generator with "cmake .. -G Ninja" but I got errors. I'm not familiar with Ninja and don't know if is expected to just work. I also don't know whether the compile_commands.json file that it generates would be relevant if I'm really building with Visual Studio 2015. What is the most accepted way to generate this file on my platform?

I have access to Visual Studio 2019, 16.4, which has built-in clang-tidy support, but the blog says that this doesn't apply fixes, which is my main goal here.

like image 641
All The Rage Avatar asked Oct 18 '25 12:10

All The Rage


1 Answers

The way I did this was via Clang Power Tools - it's an extension for Visual Studio that integrates clang-tidy and can apply fixes.

It also helped me in getting my codebase compileable by clang (necessary precondition for clang-tidy) as it provides basic clang compilation (just syntax, no output).

like image 147
pablo285 Avatar answered Oct 21 '25 02:10

pablo285



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!