https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128800
Bug ID: 128800
Summary: Short-circuit ||/&& RHS condition gets operator
location; RHS line missing from line table and gcov
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: durgeshdarshil1234 at gmail dot com
Target Milestone: ---
When a logical short-circuit operator (|| or &&) is placed across lines in a
multi-line conditional expression, GCC attributes the generated GIMPLE
conditional statement for the right-hand operand to the source location of the
binary operator token, rather than to the source location of the RHS operand
expression.
Consequently, whenever the operator and the RHS operand appear on different
lines, the source line containing the RHS operand receives no GIMPLE statements
and no DWARF .loc entries.
This results in:
1. Complete omission from gcov reports (-: uninstrumented) despite
executing at runtime.
2. Inability to set breakpoints in GDB on the RHS line (breakpoints slide
to the next line).
This affects both C (gcc) and C++, and occurs identically with || and &&.
### Reproducer 1: Trailing operator (Drops line 3 from coverage and DWARF)
Save as test_trailing.c:
int test(const char *a, const char *b) {
if (a == 0 ||
b == 0) {
return 1;
}
return 0;
}
int main(void) {
test("x", "y");
return 0;
}
Commands:
$ gcc -O0 --coverage test_trailing.c -o test_trailing
$ ./test_trailing
$ gcov test_trailing.c
Observed gcov output:
-: 0:Source:test_trailing.c
-: 0:Graph:test_trailing.gcno
-: 0:Data:test_trailing.gcda
-: 0:Runs:1
1: 1:int test(const char *a, const char *b) {
1: 2: if (a == 0 ||
-: 3: b == 0) {
#####: 4: return 1;
-: 5: }
1: 6: return 0;
-: 7:}
1: 8:int main(void) {
1: 9: test("x", "y");
1: 10: return 0;
-: 11:}
Notice that line 3 is marked "-:" (uninstrumented), even though `b == 0` was
evaluated at runtime.
GIMPLE dump ($ gcc -O0 -fdump-tree-gimple-lineno -c test_trailing.c):
[test_trailing.c:2:8] if (a == 0B) goto <D.1950>; else goto <D.1952>;
<D.1952>:
[test_trailing.c:2:16] if (b == 0B) goto <D.1950>; else goto <D.1951>;
Both checks are attributed to line 2 (column 8 for LHS, column 16 for `||`).
Line 3 is completely absent from GIMPLE.
Assembly listing ($ gcc -O0 -g -S -masm=intel test_trailing.c):
.loc 1 2 8
cmp QWORD PTR [rbp-8], 0
je .L2
.loc 1 2 16 discriminator 1
cmp QWORD PTR [rbp-16], 0
jne .L3
Debugger impact (GDB):
$ gdb -batch -ex "b test_trailing.c:3" -ex "r" ./test_trailing
Breakpoint 1 at 0x1147: file test_trailing.c, line 4.
GDB cannot bind to line 3 and slides the breakpoint to line 4 (`return 1;`).
Because `return 1` is not reached, the breakpoint is never hit.
(Identical behavior verified with &&:
if (a != 0 &&
b != 0) {
)
### Reproducer 2: 3-line layout (Confirms RHS inherits operator location)
Save as test_multiline.c:
int test(const char *a, const char *b) {
if (a == 0
||
b == 0) {
return 1;
}
return 0;
}
int main(void) {
test("x", "y");
return 0;
}
Commands:
$ gcc -O0 --coverage test_multiline.c -o test_multiline
$ ./test_multiline
$ gcov test_multiline.c
Observed gcov output (excerpt):
1: 2: if (a == 0
1: 3: ||
-: 4: b == 0) {
GIMPLE dump ($ gcc -O0 -fdump-tree-gimple-lineno -c test_multiline.c):
[test_multiline.c:2:8] if (a == 0B) goto <D.1947>; else goto <D.1949>;
<D.1949>:
[test_multiline.c:3:9] if (b == 0B) goto <D.1947>; else goto <D.1948>;
Assembly (summary of .loc directives):
.loc 1 2 8 (LHS condition on line 2)
.loc 1 3 9 (|| operator on line 3)
(No .loc for line 4)
This confirms the RHS condition always inherits the location of the binary
operator (`||` at line 3 column 9) rather than its own expression location on
line 4. Placing `||` at the start of the RHS line only masked this behavior
because the operator and operand shared a line.
### Expected Behavior
The GIMPLE condition for the RHS operand should carry the RHS expression's own
source location, so that line 3 (Reproducer 1) and line 4 (Reproducer 2) appear
in the line table, are instrumented by gcov, and accept breakpoints.
Presumably the short-circuit condition lowering in gcc/gimplify.cc (e.g.,
shortcut_cond_expr / shortcut_cond_r) should preserve and attach the RHS
operand expression's source location to the generated GIMPLE condition, rather
than the binary operator's location.
### Environment & Versions Verified
- GCC 11.4.0 (x86_64-linux-gnu)
- GCC 14.1.0 (x86_64-pc-linux-gnu)
- GCC 17.0.0 20261116 (experimental trunk, x86_64-pc-linux-gnu)
- Verified on both gcc and g++