Support lcov for cfamily code coverage reports

lcov is easy to use to upload code coverage reports to services such as CodeCov.io, and can generate a single file with coverage reports with system and installed libraries filtered out, as detailed here:

Here are the steps to produce a pretty readable result:

# ENABLE_COVERAGE_BOOL triggers settings in https://github.com/acgetchell/CDT-plusplus/blob/develop/cmake/Sanitizers.cmake
cmake -G Ninja -D CMAKE_BUILD_TYPE=Debug -D ENABLE_COVERAGE_BOOL=TRUE -S . -B build
build-wrapper-linux-x86-64 --out-dir ${{ env.BUILD_WRAPPER_OUT_DIR }} cmake --build build
cd build
ctest --schedule-random -j2
lcov --capture --directory . --output-file coverage.info
# Remove coverage results from system and vcpkg libraries
lcov --remove coverage.info '/usr/*' '*/usr/include/*' '*/vcpkg_installed/*' --output-file coverage.info
lcov --list coverage.info

Which produces:

Reading tracefile coverage.info
                                          |Lines       |Functions  |Branches    
Filename                                  |Rate     Num|Rate    Num|Rate     Num
================================================================================
[/Users/adam/projects/CDT-plusplus/]
include/Apply_move.hpp                    | 100%      3| 100%     4|    -      0
include/Ergodic_moves_3.hpp               |98.1%    212| 100%    15|    -      0
include/Foliated_triangulation.hpp        |96.1%    483| 100%    92|    -      0
include/Geometry.hpp                      | 100%     36| 100%     5|    -      0
include/Manifold.hpp                      |96.1%    153| 100%    63|    -      0
include/Metropolis.hpp                    | 100%     18| 100%     9|    -      0
include/Move_always.hpp                   | 100%     71|57.1%    14|    -      0
include/Move_command.hpp                  |79.2%     77| 100%    13|    -      0
include/Move_tracker.hpp                  |92.3%     26|75.0%    28|    -      0
include/S3Action.hpp                      | 100%    144| 100%     3|    -      0
include/Sphere_d.hpp                      |76.9%     13| 100%     2|    -      0
include/Torus_d.hpp                       | 100%      7| 100%     1|    -      0
include/Utilities.hpp                     |83.8%     99|93.3%    15|    -      0
src/cdt-gv.cpp                            |20.0%     30| 100%     1|    -      0
src/cdt-opt.cpp                           |90.5%     21| 100%     1|    -      0
src/initialize.cpp                        | 9.8%     51| 100%     1|    -      0
tests/Apply_move_test.cpp                 | 100%     89| 100%     2|    -      0
tests/Ergodic_moves_3_test.cpp            |95.1%    102| 100%     2|    -      0
tests/Foliated_triangulation_test.cpp     | 100%    404| 100%    22|    -      0
tests/Function_ref_test.cpp               |97.6%     84| 100%    10|    -      0
tests/Geometry_test.cpp                   | 100%    101| 100%     6|    -      0
tests/Manifold_test.cpp                   | 100%    466| 100%    21|    -      0
tests/Metropolis_test.cpp                 | 100%     84| 100%     6|    -      0
tests/Move_always_test.cpp                |74.2%    120|83.3%     6|    -      0
tests/Move_command_test.cpp               |98.6%    276| 100%    15|    -      0
tests/Move_tracker_test.cpp               | 100%     66| 100%     4|    -      0
tests/S3Action_test.cpp                   | 100%     59| 100%     2|    -      0
tests/Settings_test.cpp                   | 100%     30| 100%     2|    -      0
tests/Sphere_test.cpp                     | 100%     15| 100%     2|    -      0
tests/Tetrahedron_test.cpp                | 100%    149| 100%     8|    -      0
tests/Torus_test.cpp                      | 100%     31| 100%     2|    -      0
tests/Utilities_test.cpp                  | 100%    231| 100%    10|    -      0
tests/Vertex_test.cpp                     | 100%     76| 100%     2|    -      0
tests/main.cpp                            |87.5%      8| 100%     1|    -      0
================================================================================
                                    Total:|95.3%   3835|96.2%   390|    -      0

Thanks for your consideration!

Hi @acgetchell ,

thank you for your post.

May I ask what is the benefit of processing coverage reports with lcov?

codecov.io works with raw files too:

Before considering adding support for lcov format, it would be nice to understand the benefits. Is it because lcov is easier to use to generate a report rather than using simple gcov?

lcov is usable with complex projects.

The example given is extremely simple. In my case, I’m using over a hundred libraries with many different directories containing gcda files, with nested CMake directories + vcpkg to manage dependencies.

Complex filtering also needs to be done to eliminate coverage reports on these included libraries.

I am not able to use gcov to get coverage reports for my project, and the question I asked about how to get my project working with gcov and SonarCloud has not had any answers.

If there is a way to get coverage reports on my project using gcov and upload to SonarCloud, I’d be happy to implement it.

Hi @acgetchell,

there is, I am going to reply in the other topic, let me apologize if you haven’t got an answer yet.

Thanks @mpaladin for your reply!

As I posted in my question, using gcov as suggested generates 226 coverage files out of 21 that are actually in my project. lcov allows filtering and distills this down to a single file, and unlike gcovr is already included with LLVM.