RFA: PATCH to initializer_constant_valid_p for c++/38880
Ian Lance Taylor
iant@google.com
Thu Feb 19 05:02:00 GMT 2009
Jason Merrill <jason@redhat.com> writes:
> Fixing this bug in either fold or the front end had the same problem
> of making the pseudo-offsetof expression look like a valid C++
> constant-expression, when it should not be. We just want it to be
> accepted by initializer_constant_valid_p. As it turns out, we just
> need to make the existing code accept PLUS_EXPR as well as
> POINTER_PLUS_EXPR, now that we no longer fold the former into the
> latter in this case.
>
> Ian, was there any particular reason for limiting narrowing support to
> POINTER_PLUS_EXPR and MINUS_EXPR, but not PLUS_EXPR? The testcase was
> failing because we fold the MINUS_EXPR to a PLUS_EXPR with the second
> operand negated.
Only recognizing POINTER_PLUS_EXPR was a conservative change. The old
code only handled MINUS_EXPR. With the introduction of
POINTER_PLUS_EXPR, ptr - constant was handled as POINTER_PLUS_EXPR
rather than MINUS_EXPR. So I extended the old code to handle
POINTER_PLUS_EXPR as well as MINUS_EXPR. I did not handle PLUS_EXPR
because the old code didn't handle it.
If we can get a PLUS_EXPR for something which subtracts a constant from
a pointer (adds a negative constant to a pointer) then handling
PLUS_EXPR makes sense to me.
However, your patch does mean that
char *p1;
short a[] = { p1 + p1 };
may do something unexpected. It would be conservative to only handle
PLUS_EXPR when the second operand is a constant. And to then wonder why
POINTER_PLUS_EXPR wasn't used instead of PLUS_EXPR.
Ian
More information about the Gcc-patches
mailing list