This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Clarify that -fwrapv covers all signed arithmetic overflow


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]