clang-tidy cmake 从检查中排除文件

clang-tidy cmake exclude file from check

我的项目中有一个我无法控制的依赖项作为源。 我正在使用 cmake 的 clang-tidy 集成来分析我的代码,这种依赖性会引发很多警告。有没有办法告诉 cmake 不要 运行 clang-tidy 特定文件?
我试图将文件添加到 clang-tidy 的 -line-filter 选项,但这不起作用:

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\"}\
]\"")

如果该解决方案可以与 cppcheck 等其他静态分析器一起使用,那就太好了。 谢谢

如果某些 属性 - 如 CXX_CLANG_TIDY - 仅在目标级别可用,您必须将要对其进行不同设置的文件移动到单独的新目标本身。

这可以通过使用 OBJECT libraries 来完成。

在你的情况下是这样的:

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>)

参考资料

如果你只有 header 库,我使用 SYSTEM(OBJECT 库应该也可以)

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
)

由于以下错误,我很长时间无法使用这种方法

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

但使用 GNU Arm 嵌入式工具链版本 9-2020-q2-update 似乎已解决 :)