我正在为我的项目使用 CMake,并且我想向项目引入 clang-tidy 检查。
我用于此目的CMAKE_CXX_CLANG_TIDY
and .clang-tidy
用于检查设置的文件。
我想在 CI 中使用警告作为错误来可靠地检查提交是否引入了一些新的违规行为。不幸的是,由于第三方库的原因,我在启用检查时遇到了一些问题。
请看编辑2!
例如我使用Eigen
这是纯头文件库。由于这个事实,我在代码中收到一些警告,例如。 “a_文件.cpp”
/snap/cmake/301/bin/cmake -E __run_co_compile --tidy="clang-tidy;--extra-arg-before=--driver-mode=g++" --source=../../a_file.cpp -- /usr/bin/clang++ -DEIGEN_MPL2_ONLY -DEIGEN_STACK_ALLOCATION_LIMIT=16384 -I../../SomePath -I../../SomePath2 -isystem ../../path_to/include/Eigen -m64 -stdlib=libc++ -g -fPIC -std=c++11 -MD -MT a_file.cpp.o -MF a_file.cpp.o.d -o a_file.cpp.o -c a_file.cpp
../../path_to/include/Eigen/Eigen/src/Core/Swap.h:89:36: error: Assigned value is garbage or undefined [clang-analyzer-core.uninitialized.Assign,-warnings-as-errors]
a_file.cpp:279:5: note: Loop condition is true. Entering loop body
for( unsigned int i = 0; i < 100; ++i )
^
a_file.cpp:282:13: note: Calling move assignment operator for 'Matrix<float, 3, 1, 0, 3, 1>'
some_name = Vector3f( GetRandom( fDummy ),GetRandom( fDummy ), GetRandom( fDummy ) );
我有点不知道如何忽略此类问题header-filter
似乎没有解决这个问题 - 对于其他检查 [bugprone-xxx] 我也有类似的问题。除了添加之外我还有什么选择//NO-LINT
到处?
编辑:为错误添加了一些上下文。
EDIT2:
因为我仍然在努力正确处理clang-tidy
我准备了一个存储库来显示示例问题。
https://github.com/MaciejPatro/test_clang_tidy https://github.com/MaciejPatro/test_clang_tidy
这是一个最小的存储库,包含 2 个头文件和一个使用的 cpp 文件doctest
。我在那里使用两项检查:clang-analyzer-cplusplus*,google-readability-todo
- 第一个演示问题doctest
包含和第二个,因为它是创建“错误”的最简单的一种。
一些_header.h
void else_after_return() {
// TODO wrong hpp some
}
other_header.h
void wrong_function() {
// TODO wrong hpp other
}
my_test.cpp
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>
#include <some_header.h>
#include <other_header.h>
TEST_CASE("a test")
{
// TODO wrong cpp
else_after_return();
wrong_function();
CHECK(5 != 7);
}
有 3 个测试可以给出这些结果:
-
忽略头文件(未指定 --header-filter)。
我可以看到:
/home/pmac/projects/test_clang_tidy/source/tests/my_test.cpp:9:3: warning: missing username/bug in TODO [google-readability-todo]
这是预期的
-
允许所有头文件 ( --header-filter=.* )
我可以看到:
/home/pmac/projects/test_clang_tidy/source/include/other_header.h:5:3: warning: missing username/bug in TODO [google-readability-todo]
/home/pmac/projects/test_clang_tidy/source/include/some_header.h:5:3: warning: missing username/bug in TODO [google-readability-todo]
/home/pmac/projects/test_clang_tidy/source/tests/my_test.cpp:9:3: warning: missing username/bug in TODO [google-readability-todo]
这对我来说很有意义
-
仅具有“名称中的某些”的头文件(--header-filter=.some.)
/home/pmac/projects/test_clang_tidy/source/include/some_header.h:5:3: warning: missing username/bug in TODO [google-readability-todo]
/home/pmac/projects/test_clang_tidy/source/tests/my_test.cpp:9:3: warning: missing username/bug in TODO [google-readability-todo]
一切看起来都很好
接下来的 3 个测试添加第二个检查clang-analyzer-cplusplus
这是可见的doctest
。现在无论提供给的设置如何clang-tidy
我还收到来自的警告doctest
MACRO DOCTEST_DO_BINARY_EXPRESSION_COMPARISON
其扩展自CHECK
:
/home/pmac/.conan/data/doctest/2.4.6/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include/doctest/doctest.h:1239:9: warning: Potential leak of memory pointed to by field 'ptr' [clang-analyzer-cplusplus.NewDeleteLeaks]
我想要来自的警告doctest
被过滤掉 - 不幸的是没有任何设置(--header-filter,也不将其包含为系统标头-isystem
)允许我忽略它。
这是如何编译 my_test.cpp 的完整命令行(以确认doctest
标头包含在-isystem
)
/home/pmac/.local/lib/python3.8/site-packages/cmake/data/bin/cmake -E __run_co_compile --tidy="clang-tidy-12;-checks=-*,clang-analyzer-cplusplus*,google-readability-todo;--header-filter=.*some.*;--extra-arg-before=--driver-mode=g++" --source=../source/tests/my_test.cpp -- /usr/bin/c++ -I../source/include -isystem /home/pmac/.conan/data/doctest/2.4.6/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include -g -std=gnu++2a -MD -MT source/CMakeFiles/test_clang_tidy_tests.dir/tests/my_test.cpp.o -MF source/CMakeFiles/test_clang_tidy_tests.dir/tests/my_test.cpp.o.d -o source/CMakeFiles/test_clang_tidy_tests.dir/tests/my_test.cpp.o -c ../source/tests/my_test.cpp
有没有其他方法可以过滤掉第三方库中包含的宏生成的警告?我不想因为 3rdparty 库而删除测试中的检查。
要更改存储库注释/取消注释行中的“测试”https://github.com/MaciejPatro/test_clang_tidy/blob/master/CMakeLists.txt
set(CMAKE_CXX_CLANG_TIDY "clang-tidy-12;-checks=-*,google-readability-todo")
#set(CMAKE_CXX_CLANG_TIDY "clang-tidy-12;-checks=-*,google-readability-todo;--header-filter=.*")
#set(CMAKE_CXX_CLANG_TIDY "clang-tidy-12;-checks=-*,google-readability-todo;--header-filter=.*some.*")
# Something works wrong here!
#set(CMAKE_CXX_CLANG_TIDY "clang-tidy-12;-checks=-*,clang-analyzer-cplusplus*,google-readability-todo)
#set(CMAKE_CXX_CLANG_TIDY "clang-tidy-12;-checks=-*,clang-analyzer-cplusplus*,google-readability-todo;--header-filter=.*")
#set(CMAKE_CXX_CLANG_TIDY "clang-tidy-12;-checks=-*,clang-analyzer-cplusplus*,google-readability-todo;--header-filter=.*some.*")