Search code examples
c++clang++llvm-clangautomated-refactoringclang-tidy

Getting clang-tidy to fix header files


I'm in the process of moving a project currently compiling with gcc to clang, and have a bunch of warnings that gcc didn't generate (-Winconsistent-missing-override). clang-tidy works for fixing these errors in the *.cpp files, however it doesn't touch the hpp files because a compile command wasn't found in the database (as I would expect).

I'm using ninja to build the project and ninja -t compdb cc cxx > .build/compile_commands.json to generate the compilation database. I've tried running:

clang-tidy-3.6 -p .build/      \
      $(find src/ -name *.cpp) \
      $(find src/ -name *.hpp) \
      --checks=misc-use-override --fix

to fix the errors. It refuses to touch header files complaining:

Skipping .../src/header/file.hpp. Compile command not found.

Solution

  • I got it working by specifying the --header-filter=src/ option. Interestingly fixes ended up being applied several times causing output like this:

    void f() override override override override override;
    

    I worked around this by running clang-tidy on each source file separately. Also note the <build-path> specified with -p must also contain the .clang-format configuration for styling to be applied.

    This is my current iteration of the command:

    find src/ -name '*.cpp' -exec \
         clang-tidy-3.6 -p . --header-filter=src/ {}
                   --checks=misc-use-override --fix