[英]How to use SYSTEM headers with CMake and clang-tidy?
I am trying to use clang-tidy
in my CMake (3.17.1) project however it crashes on the Catch2
test library header.我试图在我的 CMake (3.17.1) 项目中使用
clang-tidy
,但是它在Catch2
测试库 header 上崩溃。 Setting the Catch2 as a system header does not seem to help.将 Catch2 设置为系统 header 似乎没有帮助。 The command invoked for clang-tidy contains the path to
Catch2
as a system include directory yet the diagnostics is still printed for it.为 clang-tidy 调用的命令包含
Catch2
的路径作为系统包含目录,但仍为其打印诊断信息。 When trying to isolate it I have discovered that this does not actually work with clang-tidy:当试图隔离它时,我发现这实际上不适用于clang-tidy:
clang-tidy src.cpp -- -Isystem/Path/to/header
It results in the header not being found at all.这导致根本找不到 header。 What I have learned somewhere (cannot find it now) was to make it actually two
--extra-arg
parameters of the clang-tidy instead:我在某处学到的(现在找不到)实际上是把它变成了 clang-tidy 的两个
--extra-arg
参数:
clang-tidy --extra-arg=-Isystem --extra-arg=/Path/to/header src.cpp
This however does not work everywhere.然而,这并不适用于任何地方。 On Windows I was able to make it work but on Linux it never worked in any form (together, separate, after
--
).在 Windows 上,我能够使其工作,但在 Linux 上,它从未以任何形式工作(一起,分开,之后
--
)。 How does one use the -isystem
headers with clang-tidy
on Linux?如何在 Linux 上使用带有
clang-tidy
的-isystem
标头? It is very confusing and inconsistent.这是非常混乱和不一致的。 Furthermore how to do it with CMake?
此外,如何使用 CMake 做到这一点?
I have this:我有这个:
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_CLANG_TIDY clang-tidy)
add_library(Catch2 STATIC Catch2/Catch2.cpp Catch2/include/Catch2/catch.hpp)
target_include_directories(Catch2 SYSTEM PUBLIC Catch2/include)
add_executable(SomeTest SomeTest/test.cpp)
target_link_libraries(Catch2)
The generated command line is rather convoluted (wrapping is mine for readability):生成的命令行相当复杂(包装是我的可读性):
cmake
-E __run_co_compile
--tidy="clang-tidy-10;--extra-arg-before=--driver-mode=g++"
--source=../Sometest/test.cpp
--
/usr/bin/clang++-10
-isystem ../Catch2/include
-g
-std=gnu++17
-MD
-MT CMakeFiles/SomeTest.dir/projects/SomeTest/test.cpp.o
-MF CMakeFiles/SomeTest.dir/projects/SomeTest/FileTest.cpp.o.d
-o CMakeFiles/SomeTest.dir/projects/SomeTest/test.cpp.o
-c
../projects/SomeTest/test.cpp
In the output there are warnings from the Catch2 so the system
in the include is just ignored seemingly.在 output 中有来自 Catch2 的警告,因此包含中的
system
似乎只是被忽略了。 I have tried to force the --extra-arg
via the CMAKE_CXX_CLANG_TIDY
property:我试图通过
CMAKE_CXX_CLANG_TIDY
属性强制--extra-arg
:
set(CMAKE_CXX_CLANG_TIDY clang-tidy --extra-arg=-isystem --extra-arg=../Catch2/include)
but that does not seem to do the trick either.但这似乎也不起作用。
I am following your repro as posted on LLVM bugtracker.我正在关注您在 LLVM bugtracker 上发布的复制品。
You are doing everything correctly: that is, marking Catch2 as system include with SYSTEM
.您所做的一切都是正确的:即将 Catch2 标记为带有
SYSTEM
的系统包含。 clang-tidy
is also behaving correctly: it only checks your source file test.cpp
and doesn't fully check catch.hpp
, only the macro expansion. clang-tidy
的行为也正确:它只检查你的源文件test.cpp
并且不完全检查catch.hpp
,只检查宏扩展。
The problem is the outdated version of Catch2.问题是 Catch2 的过时版本。
hicpp-vararg
warning has been silenced as of Catch2 2.12.2, so you need to update to at least that version.自 Catch2 2.12.2 起,
hicpp-vararg
警告已被静音,因此您需要至少更新到该版本。 Moreover, apparently the core issue that hicpp-vararg
reported upon has been fixed and this change is expected to be present in clang-tidy 11 release.此外,显然
hicpp-vararg
报告的核心问题已得到修复,预计此更改将出现在 clang-tidy 11 版本中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.