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

            Bug ID: 127990
           Summary: -g changes the source location of a TARGET_EXPR
                    initializer in a statement expression
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: compare-debug-failure
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yqtian668 at gmail dot com
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu
             Build: x86_64-pc-linux-gnu

Created attachment 65888
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=65888&action=edit
patch and test

The following valid C++ program fails `-fcompare-debug` at `-O1` and above:

```c++
struct D { int i; ~D (); };
D g ();
int
f ()
{
  return ({ g (); }).i;
}
```

Command:

```text
g++ -O1 -g -fcompare-debug -S source.C
```

Tested with GCC 17.0.0 20261018 (experimental), revision
2d1f1bc18a59c0f106c551304ac459ad7781a711, configured for
x86_64-pc-linux-gnu.

The generated instructions are identical with `-g` and `-g0`, but the source
location attached to the call and the load of its return slot differs. In the
reduced input it is column 15 with `-g` and column 10 with `-g0`. This makes
`-fcompare-debug` fail and can make a later diagnostic refer to a different
column.

With `-g`, statement-frontier markers turn the `TARGET_EXPR` initializer into
a `STATEMENT_LIST`. `gimplify_target_expr` builds a location-less `INIT_EXPR`
with `build2`, so the two tree shapes cause it to inherit different
`input_location` values.

A candidate patch builds the `INIT_EXPR` with
`rexpr_location (init, EXPR_LOCATION (targ))`. This existing helper skips the
leading debug marker and selects the real inner-expression location, while
keeping the `TARGET_EXPR` location as a fallback. The regression test checks
both `-fcompare-debug` and the precise location of a multi-line statement
expression.

The focused test reports 18 expected passes. An x86_64-pc-linux-gnu C/C++
bootstrap completed successfully, and complete regression testing found no new
failures. `git diff --check` and `contrib/check_GNU_style.sh` are clean.

Note that this bug is found by our non-AI-based automated testing technique,
and then verfied by me with the assistnat of Chatgpt.

Any suggestion would be apprecaited. If it looks good to you, I am happy submit
the patch to gcc-patches

Reply via email to