I'm using CMake for my project and I wanted to introduce clang-tidy checks to the project.
I'm using for this purpose CMAKE_CXX_CLANG_TIDY
and .clang-tidy
file for checks setup.
I wanted to use warnings-as-errors to have reliable way in CI to check whether commit introduces some new violations. Unfortunately I have some problems with enabling checks due to 3rd-party libraries.
PLEASE LOOK AT EDIT2!
For example I use Eigen
which is header-only library. Due to this fact I get some warnings in my code eg. "a_file.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 ) );
I'm a bit out of ideas how to ignore this kind of problems as header-filter
doesn't seem to resolve this issue - for other checks [bugprone-xxx] I have similar issues. What are my options besides adding //NO-LINT
everywhere?
Edit: added a bit of context to error.
EDIT2:
As I still struggle with correct handling for clang-tidy
I've prepared a repository to show the example problem.
https://github.com/MaciejPatro/test_clang_tidy
This is a minimal repository with 2 header files, and one cpp files that uses doctest
. I use two checks there: clang-analyzer-cplusplus*,google-readability-todo
- first one demonstrating the problem with doctest
inclusion and second one because it's the simplest one to create a "bug".
some_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);
}
There are 3 tests that give these results:
Ignore header files (no --header-filter specified). I can see:
/home/pmac/projects/test_clang_tidy/source/tests/my_test.cpp:9:3: warning: missing username/bug in TODO [google-readability-todo]
Which is expected
Allow all header files ( --header-filter=.* ) I can see:
/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]
Which makes sense for me
Only header files with "some in name" (--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]
Everything seems fine
Next 3 tests add the second check clang-analyzer-cplusplus
which is visible in doctest
. Now regardless of the settings provided to clang-tidy
I get additionally a warning from doctest
MACRO DOCTEST_DO_BINARY_EXPRESSION_COMPARISON
which is expanded from 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]
I want the warning from doctest
being filtered out - unfortunately none of settings (--header-filter, nor including it as system header -isystem
) allows me to ignore it.
Here is the full command line how the my_test.cpp is compiled (to confirm that doctest
header is included with -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
Is there any other way to filter out the warning generated by MACROs included from a 3rd party library? I don't want to remove checks in my tests because of 3rdparty library.
To change "Tests" in repository comment/uncomment lines in 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.*")
The error you have posted does not look spurious. The problem likely isn't with the 3rd-party library, but with your usage of it. You have not supplied enough code to give a fully correct answer as it's not clear what fDummy
and GetRandom
are. If fDummy
is being moved into GetRandom
, you have a real error here.
The comments have already listed the ways to ignore errors contained within header files: using -isystem
(in CMake, imported targets use this by default, otherwise you can manually apply SYSTEM
to target_include_directories
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With