Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake + clang_tidy - disable checking in directory

I have large project using CMake. I want to add clang_tidy-8 support with following code:

set(BORG_CLANG_TIDY OFF CACHE STRING "If enabled, clang-tidy will be used. If set to 'fix', fixes will be done on source")
set_property(CACHE BORG_CLANG_TIDY PROPERTY STRINGS ON OFF fix)
if(BORG_CLANG_TIDY)
    if (BORG_CLANG_TIDY STREQUAL "fix")
        set(maybe_fix -fix)
    endif()

    set(CMAKE_CXX_CLANG_TIDY clang-tidy-8 -extra-arg=-Wno-unknown-warning-option -format-style=file ${maybe_fix} )
endif()

I put proper .clang-tidy in root directory of project (proper = with desired checks). However, there are directories that I don't want clang tidy to check/fix (3rdparty and legacy code that can't be modified because it is brittle). So I tried putting empty .clang-tidy file in those directories (empty = with -checks=-*). This doesn't work because Error: no checks enabled.

I hoped to find some some fake -checks=-*,hello-world-do-nothing-check but nothing presented itself.

Is there other way to disable checks in selected subdirectories (/subtrees)? Those directories are static and may be hardcoded in CMake if needed.

like image 204
MateuszL Avatar asked Oct 11 '19 09:10

MateuszL


People also ask

How do I use CMake_CXX_Clang_tidy?

You can set CMAKE_CXX_CLANG_TIDY either in your CMakeList.txt or you can do it from the command line. These two example above will run Clang-Tidy with all checks enabled. The string you pass the CMAKE_CXX_CLANG_TIDY is your different arguments to Clang-Tidy. So the example above passes -checks=* to Clang-Tidy.

How do I run Clang-tidy with all checks enabled?

These two example above will run Clang-Tidy with all checks enabled. The string you pass the CMAKE_CXX_CLANG_TIDY is your different arguments to Clang-Tidy. So the example above passes -checks=* to Clang-Tidy. By default Clang-Tidy will not check your header files for problems.

How to use Clang-tidy to check header files for problems?

By default Clang-Tidy will not check your header files for problems. But you might be interested in running Clang-Tidy on your headers as well. This is done with the argument -header-filter=.. For example: The above snippet from CMakeLists.txt will make sure to also check header in the . -folder, the project folder.

How do I run Clang-tidy from the command line?

Run clang-tidy from the command line with our new check: The -checks option accepts a comma-separated mini-language which is used to enable and disable checks to run on the specified source file. clang-tidy has several checks which are enabled by default.


2 Answers

If you want a dummy check that would do nothing there's at least one that's pretty easy to disable by its options: misc-definitions-in-headers

The HeaderFileExtensions option can be used to make the check work with only certain header file suffixes. If you set it to something non-existent line "x" then you have a hello-world-do-nothing-check alternative. Your clang-tidy file would then look something like this:

Checks: '-*,misc-definitions-in-headers'
CheckOptions:
  - { key: HeaderFileExtensions,          value: "x" }

You can also check https://stackoverflow.com/a/56319752/9874699 and try to adapt the line-filter to filter out files from certain directories.

like image 127
pablo285 Avatar answered Sep 29 '22 23:09

pablo285


Is there other way to disable checks in selected subdirectories (/subtrees)?

In CMakeList.txt files contained in those subdirectories, add the following line:

set(CMAKE_CXX_CLANG_TIDY "")

But this is not a good solution: it creates a binding between the build system and a toolchain-specific tool. CMAKE_CXX_CLANG_TIDY should only ever be set via the configuration command (or possibly via a tool-chain file).

like image 25
John McFarlane Avatar answered Sep 29 '22 23:09

John McFarlane