This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: C as intermediate language, signed integer overflow and -ftrapv
- From: "Joseph S. Myers" <joseph at codesourcery dot com>
- To: Richard Biener <richard dot guenther at gmail dot com>
- Cc: Thomas Mertes <thomas dot mertes at gmx dot at>, GCC Development <gcc at gcc dot gnu dot org>
- Date: Fri, 25 Jul 2014 13:24:40 +0000
- Subject: Re: C as intermediate language, signed integer overflow and -ftrapv
- Authentication-results: sourceware.org; auth=none
- References: <trinity-ef56f8a7-8da6-40a9-aea6-658df9967fbc-1406127365897 at 3capp-gmx-bs23> <53D01359 dot 9060708 at LimeGreenSocks dot com> <CAFiYyc3oEdfEcqQvucp-DHGanCZ4Q+oV++Ggb9cypwyfskE++g at mail dot gmail dot com> <trinity-cd88fe45-1800-4151-8a0b-cf48a7b97d33-1406277813651 at 3capp-gmx-bs47> <CAFiYyc0A3qqz7zqN6Fym177YC29pm-9gEmPes4LR-XerEMfo=A at mail dot gmail dot com>
On Fri, 25 Jul 2014, Richard Biener wrote:
> Note the slight complication with static initializers that _do_ have
> to simplify to something (well, maybe not with -ftrapv ...). Joseph,
> I can easily make fold fail if you don't explicitely use a constant
> folding API (int_const_binop) and -ftrapv is set. Is it valid to
> reject
>
> static int x = __INT_MAX__ + 1;
>
> with an initializer-not-constant error?
Yes - -pedantic-errors already gives an error there, as it should. Of
course
static int x = 1 ? 0 : __INT_MAX__ + 1;
must not give an error, whereas one is OK (and required with
-pedantic-errors) for
static int x = 0 * (__INT_MAX__ + 1);
(one of the cases where the answer you get is the same as if C integers
had an infinite range, where some people are less concerned about ensuring
runtime errors occur - like e.g. x * 2 / 2 - I think the starting point
should be generating such errors whenever overflow occurs in the
abstract machine and then possibly having options to omit some such
errors).
(I tend to the view that -ftrapv should map to
-fsanitize=signed-integer-overflow -fsanitize-undefined-trap-on-error so
we only have the one implementation of these checks to get right and to
optimize as much as possible.)
--
Joseph S. Myers
joseph@codesourcery.com