For an if-statement contain two expressions such as "if(a>b && c>d)",
there will be three branches: branch 0, branch 1, and branch 2 in the
corresponding .gcov file. What do these branches mean?
For a switch-statement, if there are 5 case-statement and 1 default-
statement, then there will be 7 (not 6) branches. Why?
Could you please help me figure out the meaning of these branches?
Thanks a lot!
I use GCC coverage system and GCOV report for my job. I hope my
mention helps you. At first, my GCC version is "gcc version 3.4.6
20060404 (Red Hat 3.4.6-3)". And GCOV is "gcov (GCC) 3.4.6 20060404
(Red Hat 3.4.6-3)". Please remain the versions.
I try to your code as "if(a>b && c>d)". And I got GCOV report as
below:
| 1: 32: a=1;b=0;c=1;d=0;
| 1: 33: if( a>b && c>d ) {
| branch 0 taken 100% (fallthrough)
| branch 1 taken 0%
| branch 2 taken 100% (fallthrough)
| branch 3 taken 0%
| 1: 34: printf("hit\n");
| call 0 returned 100%
| -: 35: }
In your report, you got only 3 branches. But I got 4 branches. I can
explain you of my case. The statement "if( a>b && c>d )" will generate
4 branches. Because GCC may compile to following program flow.
| [1] compare a>b, if true branch to [2] else branch to [4]
| [2] compare c>d, if true branch to [3] else branch to [4]
| [3] call printf()
| [4] next statement
There are 4 branches. if statement seems only 2 branches, just true or
false. But compiler will resolve conditional expressions. Therefore
the condisinal expression makes more complexed branches.