How to suppress nesting patterns with gcov

I am using GCC 4.9 with GCOV to get the code and span the branches. However, the results for spanning branches are completely useless for my C ++ code. GCC seems to be building templates, despite using all the -fno-*-inline flags that I know of.

Here is a small sample application that illustrates the problem:

 #include <string> #include <iostream> using namespace std; int main() { string foo; foo = "abc"; cout << foo << endl; } 

I will compile the program with g++ -O0 -fno-inline -fno-inline-small-functions -fno-default-inline --coverage -fprofile-arcs test.cpp -o test

After running test , gcovr -r . -b gcovr -r . -b prints:

 ------------------------------------------------------------------------------ GCC Code Coverage Report Directory: . ------------------------------------------------------------------------------ File Branches Taken Cover Missing ------------------------------------------------------------------------------ test.cpp 14 7 50% 7,8,9,10 ------------------------------------------------------------------------------ TOTAL 14 7 50% ------------------------------------------------------------------------------ 

There is no branch in our main function. For example, line 7 contains string foo; . The std::basic_string<...> constructor seems to have some if statement in it, but this is not useful information when viewing the main coverage.

The problem is that all of these built-in branches are summarized, and the branch coverage calculated for my actual unit tests is about 40%. I'm interested in the coverage of my code, as opposed to how many branches I got into the C ++ standard library.

Is there a way to completely close inlining in the compiler or say GCOV so as not to consider inline branches? I could not find any guidance on the GCOV homepage or anywhere else on this topic.

Any help is greatly appreciated.

+7
c ++ gcc templates gcov
source share
1 answer

Well, you should always double check your expectations. Many thanks to @Useless for pointing out the gcov output gcov . However, you were not quite right: the branches did not belong to the test.cpp file. Running gcovr with -k and looking at all the intermediate files shows that gcov correctly creates files, such as #usr#include#c++#4.9#bits#basic_string.h.gcov , which show coverage for the standard C ++ library library.

However, the reason for all branches in test.cpp not an attachment. These are exceptions. Each call to the standard library is a branch due to potential exceptions (e.g. std::bad_alloc ). Adding -fno-exceptions to the compiler flags gives the following result:

 ------------------------------------------------------------------------------ GCC Code Coverage Report Directory: . ------------------------------------------------------------------------------ File Branches Taken Cover Missing ------------------------------------------------------------------------------ test.cpp 4 2 50% 10 ------------------------------------------------------------------------------ TOTAL 4 2 50% ------------------------------------------------------------------------------ 

Dig deeper into gcov output via cat foo.cpp.gcov fingerprints:

  -: 0:Source:test.cpp -: 0:Graph:/home/neverlord/gcov/test.gcno -: 0:Data:/home/neverlord/gcov/test.gcda -: 0:Runs:1 -: 0:Programs:1 -: 1:#include <string> -: 2:#include <iostream> -: 3: -: 4:using namespace std; -: 5: function main called 1 returned 100% blocks executed 100% 1: 6:int main() { 1: 7: string foo; call 0 returned 1 1: 8: foo = "abc"; call 0 returned 1 1: 9: cout << foo << endl; call 0 returned 1 call 1 returned 1 call 2 returned 1 function _GLOBAL__sub_I_main called 1 returned 100% blocks executed 100% function _Z41__static_initialization_and_destruction_0ii called 1 returned 100% blocks executed 100% 4: 10:} call 0 returned 1 branch 1 taken 1 (fallthrough) branch 2 taken 0 branch 3 taken 1 (fallthrough) branch 4 taken 0 

Sorry for the noise.

+4
source share

All Articles