Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class member initialization in header files generated wrong lcov coverage report #63

Closed
yuriy-mochurad opened this issue Mar 29, 2019 · 3 comments

Comments

@yuriy-mochurad
Copy link

I have created a simple example to illustrate the issue:

test.h

#ifndef TEST_H
#define TEST_H

class TestCoverage{
public:
/*
 *
 *
 *
 */
    int a = 5;
    int b = 10;
    void testfunc();
    explicit TestCoverage();
};

#endif // TEST_H

test.cpp

#include "test.h"
#include <iostream>

TestCoverage::TestCoverage()
{
    a=15;
    b=20;
}

void TestCoverage::testfunc(){
/*
 *
 * */
    std::cout<<"hey";
}

And added a simple test case (which checks nothing) using catch2:

#include "test.h"
#include <catch2/catch.hpp>

TEST_CASE("test", "[whatever]")
{
    auto testCoverage = new TestCoverage();
    testCoverage->testfunc();
}

Afterwards I generated a code coverage report using lcov and generated html file (I have built it with -O0 flag). Html file shows some strange results: https://i.stack.imgur.com/WxSxe.png

The question is: why lines 11 and 12 are displayed as covered? It looks like they are completely mirroring the class member initialization in the header file (a and b initialization). If I move initialization across the header file - the covered lines in the cpp file also move. Is this a bug in lcov or am I missing something?

@oberpar
Copy link
Contributor

oberpar commented Apr 4, 2019

Could you post gcov output files for this test? This can be obtained using a command like gcov -abc <source.cpp>.

@yuriy-mochurad
Copy link
Author

The file test.cpp.gcov looks like this:

        -:    0:Source:../../test/test.cpp
        -:    0:Graph:test.gcno
        -:    0:Data:test.gcda
        -:    0:Runs:1
        -:    0:Programs:1
        -:    1:#include "test.h"
        -:    2:#include <iostream>
        -:    3:
function _ZN12TestCoverageC2Ev called 1 returned 100% blocks executed 100%
function _ZN12TestCoverageC1Ev called 1 returned 100% blocks executed 100%
        -:    4:TestCoverage::TestCoverage()
        1:    5:{
        1:    6:    a=15;
        1:    7:    b=20;
        1:    8:}
        1:    8-block  0
        1:    8-block  1
        -:    9:
function _ZN12TestCoverage8testfuncEv called 1 returned 100% blocks executed 100%
        -:   10:void TestCoverage::testfunc(){
        1:   11:    /*
        1:   12:     *
        -:   13:     * */
        1:   14:    std::cout<<"hey";
        1:   15:}
        1:   15-block  0

@oberpar
Copy link
Contributor

oberpar commented Aug 4, 2020

The output shows that lines 11 and 12 are already covered in gcov output, so there is little that lcov can do here since it obtains the coverage data from gcov. Therefore I'm closing this issue.

@oberpar oberpar closed this as completed Aug 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants