This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Clarify that -fwrapv covers all signed arithmetic overflow
- From: Joseph Myers <joseph at codesourcery dot com>
- To: Paolo Bonzini <bonzini at gnu dot org>
- Cc: <gcc-patches at gcc dot gnu dot org>, <gerald at pfeifer dot com>
- Date: Tue, 17 Nov 2015 17:21:01 +0000
- Subject: Re: [PATCH] Clarify that -fwrapv covers all signed arithmetic overflow
- Authentication-results: sourceware.org; auth=none
- References: <1447763566-2529-1-git-send-email-bonzini at gnu dot org> <alpine dot DEB dot 2 dot 10 dot 1511171254190 dot 4472 at digraph dot polyomino dot org dot uk> <564B27AE dot 6060506 at gnu dot org> <alpine dot DEB dot 2 dot 10 dot 1511171526510 dot 14808 at digraph dot polyomino dot org dot uk> <564B4CBB dot 5040103 at gnu dot org> <alpine dot DEB dot 2 dot 10 dot 1511171557420 dot 14808 at digraph dot polyomino dot org dot uk> <564B5FD3 dot 801 at gnu dot org>
On Tue, 17 Nov 2015, Paolo Bonzini wrote:
> On 17/11/2015 17:02, Joseph Myers wrote:
> > On Tue, 17 Nov 2015, Paolo Bonzini wrote:
> >
> >> * it doesn't promise that GCC will never rely on undefined behavior
> >> rules for signed left shifts
> >
> > I think we should remove the ", but this is subject to change" in
> > implement-c.texi (while replacing it with noting that ubsan will still
> > diagnose such cases, and they will also be diagnosed where constant
> > expressions are required).
>
> ... hmm, are you sure? None of the following warn for me
>
> int x = -1 << 2;
> int y = 1 << 31;
> int z = 2 << 31;
>
> I tried with any combination of -ansi, -pedantic, -std=cXX,
> -fsanitize=undefined.
With a recent trunk build I get:
$ ./build/gcc/xgcc -B./build/gcc/ -S -o /dev/null -pedantic -std=c11 t.c
t.c:1:9: warning: initializer element is not a constant expression [-Wpedantic]
int x = -1 << 2;
^
t.c:2:9: warning: initializer element is not a constant expression [-Wpedantic]
int y = 1 << 31;
^
t.c:3:11: warning: result of '2 << 31' requires 34 bits to represent, but 'int' only has 32 bits [-Wshift-overflow=]
int z = 2 << 31;
^
t.c:3:9: warning: initializer element is not a constant expression [-Wpedantic]
int z = 2 << 31;
^
(and -pedantic-errors produces errors for the "not a constant expression"
cases, as expected).
--
Joseph S. Myers
joseph@codesourcery.com