Search code examples
clang-tidy

Using clang-tidy with compile-commands.json to parse multiple files


I am having trouble getting clang-tidy to read my compilation-database. If I try:

clang-tidy --config-file ./.clang-tidy -checks=* -p ./target

or

clang-tidy --config-file ./.clang-tidy -checks=* -p ./target/compile-commands.json

I get

Error: no input files specified.
USAGE: clang-tidy [options] <source0> [... <sourceN>]
...

But according to the --help -p is the way to specify the path to it. It only seems to work if I specify individual files as in:

clang-tidy --config-file ./.clang-tidy -checks=* path/to/file.cpp

Solution

  • When you say, "according to the --help -p is the way to specify the path to it", I think you're referring to this passage from the Clang-Tidy page:

    -p <build-path> is used to read a compile command database.
    
            For example, it can be a CMake build directory in which a file named
            compile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
            CMake option to get this output). When no build path is specified,
            a search for compile_commands.json will be attempted through all
            parent paths of the first input file . See:
            https://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an
            example of setting up Clang Tooling on a source tree.
    

    As it says, -p helps clang-tidy find the compilation database, which tells clang-tidy how to compile the listed source files, but it does not actually specify what files to analyze. (In general, the database can say how to compile many things, but you might only want to analyze a subset.)

    To specify what to analyze, you have to list all of the file names on the command line after the options. There is, perhaps unfortunately, no way to ask clang-tidy to analyze everything in the compilation database.