Re: [C++ PATCH] Detect UB in shifts in constexpr functions

2014-12-03 Thread Marek Polacek
On Wed, Dec 03, 2014 at 02:45:50PM +0100, Andreas Schwab wrote: > Marek Polacek writes: > > > * constexpr.c (cxx_eval_check_shift_p): New function. > > (cxx_eval_binary_expression): Call it. Set NON_CONSTANT_P if it > > returns true. > > > > * g++.dg/cpp0x/constexpr-shift1.C: New

Re: [C++ PATCH] Detect UB in shifts in constexpr functions

2014-12-03 Thread Andreas Schwab
Marek Polacek writes: > * constexpr.c (cxx_eval_check_shift_p): New function. > (cxx_eval_binary_expression): Call it. Set NON_CONSTANT_P if it > returns true. > > * g++.dg/cpp0x/constexpr-shift1.C: New test. > * g++.dg/cpp1y/constexpr-shift1.C: New test. > *

Re: [C++ PATCH] Detect UB in shifts in constexpr functions

2014-12-02 Thread Jason Merrill
OK, thanks. Jason

Re: [C++ PATCH] Detect UB in shifts in constexpr functions

2014-12-02 Thread Marek Polacek
On Tue, Dec 02, 2014 at 09:59:26AM -0500, Jason Merrill wrote: > Thanks. These errors also need to be conditional on (!ctx->quiet), and if > we see one of these conditions we need to set *non_constant_p. Ah, sorry. Fixed in the following. Regtested on ppc64-linux, bootstrap in progress. 2014-1

Re: [C++ PATCH] Detect UB in shifts in constexpr functions

2014-12-02 Thread Jason Merrill
On 12/02/2014 09:14 AM, Marek Polacek wrote: On Fri, Nov 28, 2014 at 09:28:03AM -0500, Jason Merrill wrote: I was thinking even more detailed: one diagnostic for negative count, one diagnostic for count larger than the precision of the lhs, and then a third for overflow. Alright, done. Thank

Re: [C++ PATCH] Detect UB in shifts in constexpr functions

2014-12-02 Thread Marek Polacek
On Fri, Nov 28, 2014 at 09:28:03AM -0500, Jason Merrill wrote: > I was thinking even more detailed: one diagnostic for negative count, one > diagnostic for count larger than the precision of the lhs, and then a third > for overflow. Alright, done. > >+ /* For signed x << y the following: >

Re: [C++ PATCH] Detect UB in shifts in constexpr functions

2014-11-28 Thread Jason Merrill
On 11/27/2014 09:26 AM, Marek Polacek wrote: On Wed, Nov 26, 2014 at 11:56:26AM -0500, Jason Merrill wrote: Please give diagnostics explaining what's wrong with the shift rather than the generic "is not a constant expression". Done. I was thinking even more detailed: one diagnostic for negat

Re: [C++ PATCH] Detect UB in shifts in constexpr functions

2014-11-27 Thread Marek Polacek
On Wed, Nov 26, 2014 at 11:56:26AM -0500, Jason Merrill wrote: > Please give diagnostics explaining what's wrong with the shift rather than > the generic "is not a constant expression". Done. > >+ tree t = build_int_cst (unsigned_type_node, uprec - 1); > >+ t = fold_build2 (MINUS_EXPR,

Re: [C++ PATCH] Detect UB in shifts in constexpr functions

2014-11-26 Thread Jason Merrill
Please give diagnostics explaining what's wrong with the shift rather than the generic "is not a constant expression". + tree t = build_int_cst (unsigned_type_node, uprec - 1); + t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs); + tree ulhs = fold_convert (unsigned_type_f

Re: [C++ PATCH] Detect UB in shifts in constexpr functions

2014-11-24 Thread Marek Polacek
On Mon, Nov 24, 2014 at 05:05:08PM +0100, Jakub Jelinek wrote: > On Mon, Nov 24, 2014 at 04:58:22PM +0100, Marek Polacek wrote: > > > Consider say: > > > > > > constexpr int p = 1; > > > constexpr int foo (int a) > > > { > > > return a << (int) &p; > > > } > > > constexpr int bar (int a) > > > {

Re: [C++ PATCH] Detect UB in shifts in constexpr functions

2014-11-24 Thread Jakub Jelinek
On Mon, Nov 24, 2014 at 04:58:22PM +0100, Marek Polacek wrote: > > Consider say: > > > > constexpr int p = 1; > > constexpr int foo (int a) > > { > > return a << (int) &p; > > } > > constexpr int bar (int a) > > { > > return ((int) &p) << a; > > } > > constexpr int q = foo (5); > > constexpr i

Re: [C++ PATCH] Detect UB in shifts in constexpr functions

2014-11-24 Thread Marek Polacek
On Mon, Nov 24, 2014 at 04:49:25PM +0100, Jakub Jelinek wrote: > On Mon, Nov 24, 2014 at 02:51:14PM +0100, Marek Polacek wrote: > > --- gcc/cp/constexpr.c > > +++ gcc/cp/constexpr.c > > @@ -1451,6 +1451,43 @@ verify_constant (tree t, bool allow_non_constant, > > bool *non_constant_p, > >return

Re: [C++ PATCH] Detect UB in shifts in constexpr functions

2014-11-24 Thread Jakub Jelinek
On Mon, Nov 24, 2014 at 02:51:14PM +0100, Marek Polacek wrote: > --- gcc/cp/constexpr.c > +++ gcc/cp/constexpr.c > @@ -1451,6 +1451,43 @@ verify_constant (tree t, bool allow_non_constant, bool > *non_constant_p, >return *non_constant_p; > } > > +/* Return true if the shift operation on LHS