https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128804

            Bug ID: 128804
           Summary: Multi-line COND_EXPR attributes both branches to
                    colon_loc, dropping false arm from DWARF 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: ---

Created attachment 66196
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66196&action=edit
Minimal reproducer: multi-line ternary drops false arm

When a ternary conditional expression (? :) spans multiple lines, GCC
attributes the generated GIMPLE assignments for both the true arm and the false
arm to the source location of the colon (:) token, rather than to the source
locations of the respective operand expressions.

Consequently, whenever the colon and the false arm appear on different lines,
the source line containing the false arm receives no GIMPLE statements and no
DWARF .loc entries. This causes two severe diagnostic defects:

1. In gcov, when the condition is false (so only the false arm is evaluated at
runtime), line coverage inverts: the unexecuted true arm is marked as executed
(1*), while the executed false arm is marked uninstrumented (-:).

2. In GDB at -O0, setting a breakpoint on the false arm line fails to bind and
slides forward to the next line.

This persists across GCC 11.4, GCC 14.1, GCC 15.2, and GCC 17.0 trunk.

---

### Minimal Standalone Reproducer

Save as test_ternary_loc.c:

    int test_tern(int a, int b, int c) {
        int x = a ?
            b :
            c;
        return x;
    }

    int main(void) {
        test_tern(0, 2, 3);
        return 0;
    }

Commands to compile and view coverage:
    $ gcc -O0 --coverage test_ternary_loc.c -o test_ternary_loc
    $ ./test_ternary_loc
    $ gcov test_ternary_loc.c

Observed gcov output:
        -:    0:Source:test_ternary_loc.c
        -:    0:Graph:test_ternary_loc.gcno
        -:    0:Data:test_ternary_loc.gcda
        -:    0:Runs:1
        1:    1:int test_tern(int a, int b, int c) {
        1:    2:    int x = a ?
        1*:    3:        b :
        -:    4:        c;
        1:    5:    return x;
        -:    6:}
        1:    7:int main(void) {
        1:    8:    test_tern(0, 2, 3);
        1:    9:    return 0;
        -:   10:}

Notice that:
- Line 3 (`b :`) is marked "1*" (executed), even though `a == 0` and `b` was
never evaluated.
- Line 4 (`c;`) is marked "-:" (uninstrumented), even though `c` was the only
arm evaluated at runtime.

---

### GIMPLE Dump ($ gcc -O0 -fdump-tree-gimple-lineno -c test_ternary_loc.c)

    int test_tern (int a, int b, int c)
    [test_ternary_loc.c:1:36] {
        [test_ternary_loc.c:3:11] if (a != 0) goto <D.1953>; else goto
<D.1954>;
        <D.1953>:
        [test_ternary_loc.c:3:11] iftmp.0 = b;
        goto <D.1955>;
        <D.1954>:
        [test_ternary_loc.c:3:11] iftmp.0 = c;
        <D.1955>:
        [test_ternary_loc.c:2:9] x = iftmp.0;
        [test_ternary_loc.c:5:12] return D.1956;
    }

Notice that `[test_ternary_loc.c:3:11]` (column 11 is the colon `:` on line 3)
is assigned to:
- The condition `if (a != 0)`
- The true arm assignment `iftmp.0 = b`
- The false arm assignment `iftmp.0 = c`

Line 4 (`c;`) has 0 statements or instructions in GIMPLE.

---

### Debugger Impact (GDB at -O0)

    $ gcc -O0 -g test_ternary_loc.c -o test_ternary_loc
    $ gdb -batch -ex "b test_ternary_loc.c:4" -ex "r" ./test_ternary_loc
    Breakpoint 1 at 0x114b: file test_ternary_loc.c, line 5.

GDB cannot bind to line 4 and slides the breakpoint to line 5 (`return x;`).

---

### Layout Sensitivity Analysis

The issue depends on the placement of the colon:
1. Trailing colon (`b : \n c;`): Line 4 (`c;`) is dropped (-:).
2. Leading colon (`? b \n : c;`): Line 2 (`? b`) is dropped (-:).
3. Isolated colon (`b \n : \n c;`): Both `b` and `c` are dropped (-:), and only
the isolated colon line is marked executed.

---

### Contrast with Clang
Clang generates accurate line mapping and DWARF for the identical code:
    2|      1|    int x = a ?
    3|      0|        b :
    4|      1|        c;
Line 3 is marked count 0, Line 4 is marked count 1, and GDB sets a breakpoint
on line 4 without sliding.

---

### Expected Behavior
In gcc/gimplify.cc (e.g. gimplify_cond_expr), the GIMPLE assignments for the
true and false arms should preserve and carry the source locations of `then_`
(line 3) and `else_` (line
4) respectively, rather than overwriting both with `EXPR_LOCATION (expr)` (the
colon location).

---

### Environment & Versions Verified
- GCC 11.4.0 (x86_64-linux-gnu)
- GCC 14.1.0 (x86_64-pc-linux-gnu)
- GCC 15.2.0 (x86_64-linux-gnu)
- GCC 17.0.0 20261116 (experimental trunk, x86_64-pc-linux-gnu)

Reply via email to