21

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:

  1. 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

  2. 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

  3. 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.*")
mpatro
  • 293
  • 2
  • 10
  • Why doesn't header-filter or HeaderFilterRegex solve the problem? Would --header-filter='Eigen/src/Core/.*' work? – Mikel Rychliski Apr 09 '20 at 17:27
  • 5
    Actually `-header-filter` is a whitelist not a blacklist. I've tried - it doesn't solve the problem. Similarly as `-isystem` that 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 in `a_file.cpp` and clang-tidy treats this warning as problem in `.cpp` file itself not actually problem of `.hpp` file. – mpatro Apr 10 '20 at 18:33

3 Answers3

1

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

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
0
  1. GetRandom and fDummy code can you add it in here? It is not clear with what you have written.
  2. Also, include directories handle all these warnings in CMAKE. (yes i meant inclusion as system headers. )
  • I don't have the code for `GetRandom` and `fDummy` at hand (this part of question was written two years ago) but it also didn't matter it could be just float numbers. The full-blown example is visible in `EDIT2` and https://github.com/MaciejPatro/test_clang_tidy. I don't understand second point (cmake doesn't handle any of clang-tidy warnings - if you mean inclusion as system headers - it doesn't work for me in case the warning is in macro - problem shown in the repository with doctest and `CHECK` macro | For other kind of problems it works like TODOs in example) – mpatro Jan 19 '22 at 14:22
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 19 '22 at 17:19
  • a. yes i meant inclusion as system headers. NOLINT, is one horrible way. b. Potential leak of memory pointed to by field 'ptr' -- this is the problem. Memory leak can only be treated with how we use the library. there is no problem in 3rd party library it seems. Kindly check the usage of ptr. – Aditya Thakekar Jan 20 '22 at 11:59
  • This issue (potential leak) is somewhere inside of `CHECK` macro of `doctest`. I don't really use the pointer at any point of time - see usage in code snippet `CHECK(5 != 7);` and where the clang-tidy warning points you to: `.../doctest.h:1239:9`. If I would even use `CHECK(true)` the same issue would happen. – mpatro Jan 20 '22 at 12:46
  • need to backtrace this issue from the original code. final try just in case if there is a variable with same header file name. change the header guards to standard preprocessor directives. eg. ifndef _SOME_HEADER_ – Aditya Thakekar Jan 21 '22 at 12:28
-1

.clang-tidy

...
HeaderFilterRegex: '.*'
...

or

clang-tidy ... --header-filter='.*' ...

From clang-tidy output when analyzing a lot of code:

Suppressed ... warnings (... in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
Francisco Aguilera
  • 3,099
  • 6
  • 31
  • 57