https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128827
Bug ID: 128827
Summary: [gcov / c] Statement expression initialization line
reported as unexecuted (count 0) when inner statement
returns or branches early
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: gcov-profile
Assignee: unassigned at gcc dot gnu.org
Reporter: durgeshdarshil1234 at gmail dot com
Target Milestone: ---
Problem Description:
--------------------
When a GNU C statement expression ({ ... }) initializes a variable and an inner
statement returns early (or branches out via goto), gcov reports the
declaration/initialization line as completely UNEXECUTED (count 0 / "#####").
This creates a serious false negative in code coverage audits: the function was
entered (count 1), the statement expression was entered and evaluated, and the
inner return was executed (count 1), yet gcov claims the outer statement line
was never reached.
Minimal Standalone Reproducer (test.c, 6 lines):
------------------------------------------------
int f(int a) {
int res = ({
if (a) return 42;
0;
});
return res;
}
int main(void) {
return f(1) != 42;
}
Commands to Reproduce:
----------------------
gcc -std=gnu11 -O0 -g --coverage test.c -o test
./test
gcov test.c
cat test.c.gcov
Actual gcov Output (GCC 11 through GCC 15 trunk):
-------------------------------------------------
-: 0:Source:test.c
-: 0:Graph:test.gcno
-: 0:Data:test.gcda
-: 0:Runs:1
1: 1:int f(int a) {
#####: 2: int res = ({ <-- BUG: Count 0 (#####), expected
1!
1: 3: if (a) return 42;
#####: 4: 0;
-: 5: });
#####: 6: return res;
-: 7:}
-: 8:
1: 9:int main(void) {
1: 10: return f(1) != 42;
-: 11:}
Expected Output (as correctly reported by Clang/llvm-cov):
----------------------------------------------------------
Line 1: count = 1
Line 2: count = 1 <-- Statement expression declaration was entered and
executed
Line 3: count = 1 <-- Inner guard evaluated true and returned
Line 4: count = 0
Line 6: count = 0
Root Cause Analysis (-fdump-tree-gimple-lineno):
------------------------------------------------
In the GIMPLE lowering dump:
int f (int a)
[test.c:1:14] {
int D.2835;
int D.2839;
int res;
[test.c:2:16] {
[test.c:3:12] {
[test.c:3:12] if (a != 0) goto <D.2837>; else goto <D.2838>;
<D.2837>:
[test.c:3:23] D.2839 = 42;
[test.c:3:23] return D.2839;
<D.2838>:
}
[test.c:4:9] D.2835 = 0;
}
[test.c:2:9] res = D.2835;
[test.c:6:12] D.2839 = res;
[test.c:6:12] return D.2839;
}
The C frontend binds the source locus [test.c:2:9] solely to the final
assignment instruction `res = D.2835;` placed AFTER the statement expression
body.
When `return 42;` executes, control jumps straight to the function exit,
completely bypassing `res = D.2835;`. Because no GIMPLE statement carrying the
location of line 2 is ever executed on the entry path into the statement
expression, gcov marks line 2 as unexecuted dead code (count 0 / #####).
The entry locus of the statement expression should be emitted at the start of
the block so that entering the statement expression records execution of the
statement line.