clang-tidy co-compile appends compiler command line, but uses wrong compiler headers
The current implementation of the clang-tidy co-compile appends --
to the clang-tidy command, followed by the full compile command line. The comments in the code at the start of the HandleTidy()
function state the following:
// Construct the clang-tidy command line by taking what was given
// and adding our compiler command line. The clang-tidy tool will
// automatically skip over the compiler itself and extract the
// options.
The clang-tidy docs say this is supported:
clang-tidy is a LibTooling-based tool, and it’s easier to work with if you set up a compile command database for your project.... You can also specify compilation options on the command line after --:
$ clang-tidy test.cpp -- -Imy_project/include -DMY_DEFINES ...
Noting the latter, it seems this was intended for when you don't have a compile_commands.json
file. I would have expected that if CMAKE_EXPORT_COMPILE_COMMANDS
is true, then we should add a -p /path/to/build/dir
instead of appending the compiler command line. Passing the command line directly seems redundant.
But why this really matters is that I have a real-world project where using a custom compiler results in clang-tidy picking up the wrong headers with the current implementation (it picks up the system default headers). If I manually construct the clang-tidy command line to use the -p /path/to/build/dir
instead of appending the compiler command, then it does use the correct compiler headers. Sadly, if I pass the -p
option with the existing implementation, that seems to get ignored and the compiler command given on the command line is still used.
I don't know why the wrong headers are being picked up in my case, the custom compiler is also the one that should be found on the PATH. There's no reason it should be finding the system headers at all. Putting that aside, is there any reason we shouldn't use -p /path/to/build/dir
instead when we know we have a compile_commands.json
file being generated in the build directory?
If we did decide to switch to using -p /path/to/build/dir
instead, we would have to check if there is already such an argument in the target's <LANG>_CLANG_TIDY
property and only add our own if there isn't already one there. Projects may already be specifying that (this was the case for me). Even if CMAKE_EXPORT_COMPILE_COMMANDS
wasn't set, if there is a -p
option included in the <LANG>_CLANG_TIDY
property, we probably should assume that should take precedence and not add compiler commands in that case either, but I could be convinced not to go that far if there are backward compatibility concerns for some corner case.
CC: @bill-hoffman (original author of the clang-tidy support)