https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128805
Bug ID: 128805
Summary: Multi-line compound literal designated initializers
discard source locations, causing GDB breakpoints to
slide past initialization
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 66197
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66197&action=edit
Minimal reproducer demonstrating GDB breakpoint sliding in multi-line compound
literal
Under ISO C11 §6.5.2.5, a compound literal provides an unnamed object whose
value is given by an initializer list. When occurring inside the body of a
function, it has automatic storage duration associated with the enclosing
block, and its value is initialized at runtime execution.
When lowering compound literals with designated initializers (e.g., `(struct
Point){ .x = a, .y = b }`) in `gcc/gimplify.cc` (`gimplify_init_ctor_eval`),
GCC stamps the generated field assignment statements with `input_location`—the
source location of the enclosing statement (`p = ...`).
When the initializer values are simple variables or constants, no intermediate
temporary expressions are evaluated. Consequently, all field writes receive the
line number of the assignment operator, and the source locations of lines
containing the individual designated initializers (`.x = a,` and `.y = b`) are
discarded.
Impact:
1. DWARF line tables contain no entries for the designated initializer lines.
Setting breakpoints on lines inside the compound literal slides past the field
initialization completely to the subsequent statement.
2. In `gcov`, lines containing designated initializers are omitted from the
instrumented line table (`-:`), whereas Clang creates independent executable
regions for each designated initializer line.
Minimal Reproducer (test_compound_literal_loc.c):
struct Point {
int x;
int y;
};
void use(struct Point *p) {}
void test_pt(int a, int b) {
struct Point p;
p = (struct Point){
.x = a,
.y = b
};
use(&p);
}
int main(void) {
test_pt(5, 10);
return 0;
}
Exact Reproduction Commands:
gcc -O0 -g -fdump-tree-gimple-lineno test_compound_literal_loc.c -o
test_compound_literal_loc
gdb -batch -ex "b test_compound_literal_loc.c:11" -ex "b
test_compound_literal_loc.c:12" -ex "r" test_compound_literal_loc
Observed GIMPLE Lowering (-fdump-tree-gimple-lineno):
void test_pt (int a, int b)
[test_compound_literal_loc.c:8:28] {
struct Point p;
struct Point D.1953 = {.x=a, .y=b};
try
{
[test_compound_literal_loc.c:10:7] p.x = a;
[test_compound_literal_loc.c:10:7] p.y = b;
[test_compound_literal_loc.c:14:5] use (&p);
}
Notice that both `p.x = a;` and `p.y = b;` are stamped with line 10 column 7.
Lines 11 and 12 produce zero GIMPLE statements.
Observed GDB Breakpoint Sliding:
Breakpoint 1 at 0x1185: file test_compound_literal_loc.c, line 14.
Breakpoint 2 at 0x1185: file test_compound_literal_loc.c, line 14.
[Thread debugging using libthread_db enabled]
Breakpoint 1, test_pt (a=5, b=10) at test_compound_literal_loc.c:14
14 use(&p);
A developer attempting to break on lines 11 or 12 cannot inspect state before
`p.x` or `p.y` is initialized; execution slides directly past the stores to
`use(&p)` on line 14.
Expression Asymmetry:
If the designated initializer contains a function call or arithmetic expression
(e.g. `.x = compute(a)`), the gimplifier emits an evaluation statement for the
RHS which retains the
line 11 location, but if the value is a scalar variable, the location is lost.
Comparison with Clang:
Clang 16 preserves source locations for each designated initializer line in
DWARF. In GDB:
Breakpoint 1 at 0x23f0: file test_compound_literal_loc.c, line 11.
Breakpoint 1, test_pt (a=5, b=10) at test_compound_literal_loc.c:11
11 .x = a,
Tested on GCC 11.4.0 and GCC 15.2.0.