Why does gcov report in-class function definitions

2019-04-15 01:29发布

问题:

It seems like gcov does not report inline definitions of class methods as executable lines. Example:

#include <iostream>

struct Foo {
  void bar() {}
  void baz() {}
};

int main() {
  Foo foo;
  foo.bar();
}

If I compile the above program with g++ -g -O0 -ftest-coverage -fprofile-arcs -o main main.cpp, run it, and call gcov on it, I get the following report:

        -:    0:Source:main.cpp
        -:    0:Graph:main.gcno
        -:    0:Data:main.gcda
        -:    0:Runs:1
        -:    0:Programs:1
        -:    1:#include <iostream>
        -:    2:
        -:    3:struct Foo {
        1:    4:  void bar() {}
        -:    5:  void baz() {}
        -:    6:};
        -:    7:
        1:    8:int main() {
        -:    9:  Foo foo;
        1:   10:  foo.bar();
        4:   11:}

Why is line 5 reported as non-executable, even though the method above was reported correctly as executed once?

Update

According to the gcov documentation (https://gcc.gnu.org/onlinedocs/gcc/Invoking-Gcov.html#Invoking-Gcov), - denotes a non-executable line while ##### and ==== mark lines that can be executed but weren't.

回答1:

gcov is reporting that after linking your binary, there was never any possibility of Foo::baz() being executed.

The linker completely removed that function, so no executable was code associated with that line anymore.