llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.14k stars 12.02k forks source link

clang-tidy has no way to conditionally apply checks to headers #59425

Open thehans opened 1 year ago

thehans commented 1 year ago

I have a project where I want to enable most of clang-tidy's modernize-* checks, including modernize-use-nodiscard. However there seems to be a problem where Qt MOC cannot parse [[nodiscard]] when building this on older platforms like Ubuntu 18.04 and 20.04.

The Qt specific files in this project are all under src/gui/ path, so I thought that I could place a .clang-tidy file there with contents:

Checks:          '-modernize-use-nodiscard'
InheritParentConfig: true

To me this means that it should use all the other top-level-enabled checks except modernize-use-nodiscard. But for whatever reason, headers don't seem to follow .clang-tidy settings (only the top level one though?)

I don't understand why a header filter exists but not a general filename filter. Why is the interface for checking headers so obscure and difficult compared to the rest of source files?

llvmbot commented 1 year ago

@llvm/issue-subscribers-clang-tidy

njames93 commented 1 year ago

Clang tidy uses the check options from the file it was invoked on, so header files take the options of source files that include them.

thehans commented 1 year ago

Is it not possible to process headers independently from the sources which include them?

njames93 commented 1 year ago

To do that you'd need to disable clang-tidy running on header files, then invoke clang-tidy on the header. For this to work properly you'd need all your header files to be self contained.