how to use clang-tidy modernize with visual studio

9.9k views Asked by At

I'm trying to apply clang modernization on files either from within visual studio, or from the command line, making use of the includes paths and settings in my visual studio 2015 C++ projects.

I created a clang-tidy settings file, like this:

clang-tidy -dump-config=.clang-tidy 
  -checks="modernize-loop-convert,modernize-deprecated-headers"

and verified that it works on a single file, from (cygwin) command line: clang-tidy.exe -explain-config lists (among other things)

'modernize-deprecated-headers' is enabled in the C:\abc\.clang-tidy.

and I can run it on individual files by setting up the includes by hand: clang-tidy.exe someFile.cpp -- -Ic:/abc -I. -IIncludes

I installed the visual studio clang-format VS plugin, and created an example .clang-format file, { BasedOnStyle: "LLVM", IndentWidth: 20 } which is picked up by the VS plugin. I then naively tried to feed clang-format with the clang-tidy configuration, but unsurprisingly that does not work at all.

To summarize: how to run clang-tidy on files and visual studio projects?

Perhaps a workaround would be to dump out the includes for each file from visual studio? Any way forward will do.

For reference:

How can I enable clang-tidy's "modernize" checks?

How to feed Visual Studio Clang-Format plugin with clang-format file?

3

There are 3 answers

3
Michel de Ruiter On

Visual Studio 2019 version 16.4+ has native support for clang-tidy!

0
Daniel Marjamäki On

I am a Cppcheck developer.

You can run clang-tidy on visual studio solutions in the Cppcheck GUI.

It is a free open source (GPL) tool. You can download it here: http://cppcheck.sf.net

Some screenshots: http://cppcheck.sourceforge.net/clangtidy/index.html

It doesn't currently integrate other clang tools (for instance clang-format) but it is possible to extract the clang-tidy commands used by Cppcheck GUI.. maybe you can edit those.

0
Ionut Enache On

To use Clang Tidy modernize directly inside Visual Studio you can try my Clang Power Tools vs-extension. It has the Clang Tidy-Fix feature which can modernize your code automatically.

You mentioned that you run Clang Tidy on a single file. You can use the tidy command from vs-toolbar, which will run tidy on the editor's active file. If you want to execute tidy on multiple files use the commands from the context menu or run it on the entire project, but I'm not sure if this is what you want to do because the code changes will be harder to saw.

Regarding the additional includes, you can choose between: includes directories and system includes.