Getting clang-tidy to fix header files
Asked Answered
S

3

15

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.
Sacrilegious answered 8/2, 2016 at 3:15 Comment(0)
S
12

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
Sacrilegious answered 14/2, 2016 at 4:59 Comment(3)
With regards to the "fixes ended up being applied several times": That's why there's a run-clang-tidy script which first collects the fixes and then applies them in one-go afterwards. See clang-developers.42468.n3.nabble.com/…Merovingian
I see. Not very well documented though. If you write that up as an answer or a suggested edit, I'll be more than happy to accept it. (I've stopped doing much c++ development, so I probably won't test it though)Sacrilegious
Using run-clang-tidy.py does not solve this problem. It will still process header files and apply fixes once for each compilation unit the header file is included in. The only workaround is to run clang-tidy -fix on each compilation unit sequentially as this answer suggests, so it never sees an unfixed header file more than once.Somnifacient
S
0

Be careful when using "-header-filter" correctly. There is no two "minus" characters before "header" ! Unlike in nishantjr reply.

As shown here : http://releases.llvm.org/3.9.0/tools/clang/tools/extra/docs/clang-tidy/index.html

This is still true in clang-tidy 9.0.

Substructure answered 5/3, 2019 at 15:7 Comment(1)
Does it really care if you use two "minus" characters instead of one? Usually it doesn't matter whether you use one or two with most applications.Sporozoite
U
-1

There is a bug in the run-clang-tidy.py script. It doesn't correctly merge the fixes because the file path is not normalized to a unique path. Try this patched version that uses pathlib to normalize the path correctly on linux, windows and macos...

https://github.com/dyollb/modernize_cpp/blob/main/clang_tidy/run-clang-tidy.py

Unseasoned answered 17/5, 2021 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.