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.*")
-header-filteris a whitelist not a blacklist. I've tried - it doesn't solve the problem. Similarly as-isystemthat makes include treated as system-headers from which all warnings are ignored by default (without this option I get way more warnings). My understanding here is that this part of code is inlined ina_file.cppand clang-tidy treats this warning as problem in.cppfile itself not actually problem of.hppfile.doctest.hinclude in my tests. Did you ever manage to solve this issue for yourself? I used therun-clang-tidy.pyscript from the LLVM sources and had to adjust it to accept more args, one which simply removes specific includes if specified. But that still irks me as it doesn't feel like the proper way, if there is any at all.