I started to use Gcov to analyze my C programs. So I read, in the introduction of GCov documentation,this: "because gcov accumulates statistics by line (at the lowest resolution), it works best with a programming style that places only one statement on each line." GCov Documentation
Writing trivial programs, I noted problems in the calculation of number of times that a statement, contained in a "for" clause, is executed.
I'm going to show you two examples and the GCov output of these:
-: 0:Source:example1.c
-: 0:Graph:example1.gcno
-: 0:Data:example1.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include <stdio.h>
-: 2:#include <stdlib.h>
-: 3:
-: 4:
1: 5:int main()
-: 6:{
1: 7: int i = 0;
-: 8:
1: 9: for(; i < 4; i++);
-: 10:
1: 11: return 0;
-: 12:}
-: 13:
GCov counts the for clause as a unique element. It is not wrong?. As told by documentation GCov counts correctly the number of times that the statements in for clause are executed if I put these on one line, like in this example:
-: 0:Source:example1.c
-: 0:Graph:example1.gcno
-: 0:Data:example1.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:
-: 2:#include <stdio.h>
-: 3:#include <stdlib.h>
-: 4:
-: 5:
1: 6:int main()
-: 7:{
1: 8: int i = 0;
-: 9:
6: 10: for(;
-: 11: i < 4;
4: 12: i++);
-: 13:
1: 14: return 0;
-: 15:}
-: 16:
We have that (i = 0) is executed 1 time, (i < 4) 5 times and (i++) 4 times. It is correct for me.
I have two questions:
1-I don't want to adapt my coding style in order to do statistics with GCov. Are there many tools or flags that transform my code in the correct format in order to get correct statistics with GCov?
2- Are there other constructs that have the same problem of "for" loop?
Thanks in advance for answers.