3

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.

3
  • 1
    gcov is telling you that bar() was executed and baz() was not. That is correct. Do I misunderstand your question? Jun 20, 2014 at 6:29
  • @Drew Dorman I've updated the question to tackle your comment. Jun 20, 2014 at 6:33
  • Are you sure the line is executable? I imagine that a function that is never called is omitted from the final binary.
    – user743382
    Jun 20, 2014 at 6:36

1 Answer 1

2

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.

5
  • Oh! Thanks a lot! Would you happen to know the flag that prevents this kind of behavior, making the linker include all functions? Jun 20, 2014 at 6:41
  • 1
    I believe it's --whole-archive, sent to the linker. Jun 20, 2014 at 6:46
  • Thanks! However, it seems like the linker is not the problem. When I only compile (-c) then baz is already missing from the object file. Any idea how I can prevent this? Jun 20, 2014 at 7:04
  • I'm sorry that didn't work. I suggest posting a new question to get more eyes on your new question. Good luck! Jun 20, 2014 at 7:06
  • 3
    If the function is inline, the compiler will eliminate any "not called" functions if the function isn't reachable from outside the source file. Since the class is defined in the source file, there is no way to call this function externally, and the compiler simply removes it. This is what I expect from the compiler. You can possibly work around this by adding a function that calls the baz function, but in itself is never called (but not inline or static). Jun 20, 2014 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.