Skip to content

Running clang-tidy with compile command database containing multiple entries for the same source file #171139

@adambadura

Description

@adambadura

According to the documentation clang-tidy can be run with -p command line argument described (at 22.0.0) as:

-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.

The "compile command database" may contain multiple entries for the same source file. It is even documented in the JSON Compilation Database Format Specification (again, at 22.0.0):

  • file: The main translation unit source processed by this compilation step. This is used by tools as the key into the compilation database. There can be multiple command objects for the same file, for example if the same source file is compiled with different configurations.

Although, when reading it now I wonder what would it mean that it "is used by tools as the key into the compilation database" when it can show up more than once. And then whether the "There can be multiple command objects for the same file" means multiple entries in the database or multiple command elements (array? same for arguments?) within single entry?

Either way, in my case I have a CMake-based project and some source files show up multiple times, with different compilation options. So, they should be checked multiple times as each such check might yield different issues.

Yet, I see no way to either make clang-tidy pick a specific entry nor to even see which entry it picked. My wild guess would be that it picks the first entry, but results seem to indicate it picks the last perhaps?

For now what I though of was to either don't use -p and instead provide the compile options directly or to split the compile_commands.json file into each entry separately and then feed clang-tidy with such mono-entry files instead, to have full control over which options get used.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions