In this post I show how to use clang-tidy from within VS2015. The prerequisites are that you must be using CMake to generate your project. During the install of CMake I checked the option to add CMake to my PATH environment variable. I also installed Clang and added it to the PATH environment variable as well. Finally, get Ninja and put it in some directory. Ninja is needed because when I tried to use Visual Studio 14 2015 Win64 for the G option, CMake kept switching the compiler to cl.exe despite setting the compiler in the command-line options. I assume that you are doing out of source builds, and that all your source is under the src
directory. An example source tree would be:
c:\projects\projectA
|----buildx64_VS2015
|----buildx64_Ninja
|----src
|----srcA1
|----srcA2
CMakeLists.txt
For clang-tidy to work one has to create the compile_commands.json
file. With CMake it is easy to do. I created a batch file to do just that. Make sure to edit the file where it specifies the architecture to be amd64 to meet your needs.
Batch file for building compile_commands.json
@echo off
if [%1]==[] goto usage
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
pushd %1
cmake -DCMAKE_C_COMPILER="clang-cl.exe" -DCMAKE_CXX_COMPILER="clang-cl.exe" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_MAKE_PROGRAM="c:/Program Files/Ninja/ninja.exe" -G "Ninja" ..
popd
@echo Done.
goto :eof
:usage
@echo Usage: %0 ^<full path to ninja build directory^>
exit /B 1
:eof
This batch file should be invoked from the command prompt. Note that you will have to edit the batch file to enter the correct path of the Ninja binaries, and I am using a /
not a \
. If you switch them CMake will interpret it as an escape sequence.
gen_compile_commands.bat <full path to Ninja build directory>
The batch file takes the full path to the Ninja build directory (relative paths will not work) which is the first parameter(%1
). Once the json file is built one can use that to run clang-tidy. I happen to have many sub-directories with lots of C++ files. Therefore I opted to run clang-tidy from a batch file that would tidy up all my C++ files.
Batch file for running clang-tidy across all C++ files in the various sub-directories
@echo off
if [%1]==[] goto usage
setlocal
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
set F=%1\cppfiles.txt
pushd %1
echo. 2>%F%
pushd ..\src
forfiles /s /m _cpp /c "cmd /c echo @path >> %F%"
popd
for /F "tokens=*" %%L in (cppfiles.txt) do clang-tidy -p .\compile_commands.json -header-filter=.* %%L
popd
del %F%
endlocal
@echo Done.
goto :eof
:usage
@echo Usage: %0 ^<full path to ninja build directory^>
exit /B 1
:eof
This batch file should be invoked from the command prompt or VS2015, more on that later.
ctall.bat <full path to Ninja build directory>
The batch file takes the full path to the Ninja build directory (relative paths will not work) which is the first parameter(%1
). The output of the batch file can be redirected to another file and then one can go through each suggestion made by clang-tidy and fix it. However, that is somewhat tedious, which leads me to the next section.
Running the batch file to tidy up from VS2015
Most VS versions support external tools, so this may apply to versions other than 2015. Choose from the menu Tools > External Tools... >
. This brings up a dialog box where new tools may be added. Hit the Add button and enter a suitable title. For the Command value enter the file name with the full path to the batch file ctall.bat. For Arguments use the value $(SolutionDir)..\buildx64_ninja
. This is the path to my ninja build directory so be sure to change it for yours. The Initial directory can be the same as the directory that contains ctall.bat. Make sure to check Use Output window. Then use the OK button to save. You should now see a new entry in Tools that has the same name as was entered in the previous dialog in the Title entry field. When you choose this menu option it will run ctall.bat and its output will end up in the Output window in VS. When you double-click on a line in the output, VS will load the file (but alas) not bring you to the line of the error. You will have to jump to the line manually. I think this has to do with the way clang-tidy outputs file names and line numbers.
Some of us may want to run clang-tidy on individual files from VS. In that case use the following batch file.
Batch file for running clang-tidy on an individual C++ source file
@echo off
if [%1]==[] goto usage
if [%2]==[] goto usage
setlocal
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
set F=%1\cppfiles.txt
pushd %1
clang-tidy -p .\compile_commands.json -header-filter=.* %2
popd
endlocal
@echo Done.
goto :eof
:usage
@echo Usage: %0 ^<full path to ninja build directory^> ^<full path to file to tidy^>
exit /B 1
:eof
Choose from the menu Tools > External Tools... >
. This brings up a dialog box where new tools may be added. Hit the Add button and enter a suitable title. For the Command value enter the file name with the full path to the batch file ctsingle.bat. For Arguments use the value $(SolutionDir)..\buildx64_ninja $(ItemPath)
. This is the path to my ninja build directory so be sure to change it for yours. The Initial directory can be the same as the directory that contains ctsingle.bat. Make sure to check Use Output window. Then use the OK button to save. You should now see a new entry in Tools that has the same name as was entered in the previous dialog in the Title entry field. When you choose this menu option it will run ctsingle.bat on the current file in the editor window and its output will end up in the Output window in VS.