9

I have a dependency as source in my project that I have no control over. I'm using cmake's clang-tidy integration to analyze my code, and this dependency is firing A LOT of warnings. Is there a way to tell cmake not to run clang-tidy on specific files ?
I tried to add the files to the -line-filter option of clang-tidy, but this doesn't work:

set_target_properties(target PROPERTIES
CXX_CLANG_TIDY "${clang_tidy_loc};\
${TIDY_CONFIG} \
-line-filter=\"[\
{\"name\":\"path/to/file.cpp\"},\
{\"name\":\"path/to/file.h\"}\
]\"")

If the solution could work with other static analyzers like cppcheck it would be really nice. Thanks.

Niverton
  • 163
  • 1
  • 2
  • 6

2 Answers2

19

If some property - like CXX_CLANG_TIDY - is only available on target level, you have to move the files you want to have different settings for into a separate new target itself.

This can be done by using OBJECT libraries.

In your case something like:

add_library(
    target_no_static_code_analysis
    OBJECT
        path/to/file.cpp
        path/to/file.h
)

# NOTE: Resetting only needed if you have a global CMAKE_CXX_CLANG_TIDY
set_target_properties(
    target_no_static_code_analysis
    PROPERTIES
         CXX_CLANG_TIDY ""
)

...
add_library(target ${other_srcs} $<TARGET_OBJECTS:target_no_static_code_analysis>)

References

Florian
  • 39,996
  • 9
  • 133
  • 149
  • I didn't think about that yesterday but this seems so obvious now. Thanks for the clear and well documented answer, if it's a dependency it should have already been a different target... – Niverton Apr 01 '18 at 07:21
  • Learned about OBJECT library thanks to this answer. Thank you! – rpattabi Oct 08 '20 at 06:55
0

In case you have a header only library I use SYSTEM (should also be possible for OBJECT libraries)

add_library(
  header_only_library_no_static_code_analysis 
  INTERFACE
)

target_include_directories(
  header_only_library_no_static_code_analysis 
  SYSTEM # Adds -isystem instead of -I and this tells clang-tidy not to analyze these includes
  INTERFACE
    path/to
)

I couldn't use this approach for a long time due to following bug

https://bugs.launchpad.net/gcc-arm-embedded/+bug/1698539

But with GNU Arm Embedded Toolchain Version 9-2020-q2-update it seems resolved :)

ge45mue
  • 677
  • 8
  • 23
  • This is liable to trigger https://releases.llvm.org/11.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/llvmlibc-restrict-system-libc-headers.html – Mikhail Mar 05 '21 at 00:57