Here is an example. In the attached file, foo.c contains only two functions, 'sum' and 'main'. The function 'sum' gets inlined into 'main' (with -O2).
gcc --coverage -O2 foo.c ./a.out gcov -b foo.c Now the coverage data for the 'if' condition in 'sum' looks like this: (in attached file foo.c.gcov) 8: 8: if (v[i]) total += 1; branch 0 never executed branch 1 never executed branch 2 taken 75% (fallthrough) branch 3 taken 25% Thus a simple conditional looks like a four-way branch. It is due to early inlining where a couple of basic blocks get eliminated but the branch coverage still gets attributed to the conditional. Similarly the coverage data for the loop in 'sum' looks like this 9: 7: for (i = 0; i < N; ++i) { branch 0 never executed branch 1 never executed branch 2 taken 89% branch 3 taken 11% (fallthrough) After disabling early inlining, the coverage data looks saner. Of course, in general, the coverage data cannot be accurate in presence of optimizations. However, this improves the situation somewhat and improves usability when compiling without optimization is not feasible. Sharad On Fri, Sep 30, 2011 at 9:15 PM, Xinliang David Li <davi...@google.com> wrote: > > Yes, this will improve test coverage option's usability, but please > provide the example to explain the issues. > > David > > On Fri, Sep 30, 2011 at 6:12 PM, Sharad Singhai <sing...@google.com> wrote: > > This patch disables early inlining when --coverage option is > > specified. This improves coverage data in presence of other > > optimizations, specially with -O2 where early inlining changes the > > control flow graph sufficiently enough to generate seemingly very odd > > source coverage. > > > > Bootstrapped okay and regression tests passed. > > > > Okay for google/gcc-4_6? > > > > 2011-09-30 Sharad Singhai <sing...@google.com> > > > > * gcc.c (cc1_options): Added -fno-early-inlining for coverage. > > > > Index: gcc.c > > =================================================================== > > --- gcc.c (revision 179402) > > +++ gcc.c (working copy) > > @@ -776,7 +776,7 @@ > > %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %b.s}}}\ > > %{fsyntax-only:-o %j} %{-param*}\ > > %{fmudflap|fmudflapth:-fno-builtin -fno-merge-constants}\ > > - %{coverage:-fprofile-arcs -ftest-coverage}"; > > + %{coverage:-fprofile-arcs -ftest-coverage -fno-early-inlining}"; > > > > /* If an assembler wrapper is used to invoke post-assembly tools > > like MAO, --save-temps need to be passed to save the output of > > > > -- > > This patch is available for review at http://codereview.appspot.com/5173042 > >