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: [C++ PATCH] Detect UB in shifts in constexpr functions


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 negative count, one diagnostic for count larger than the precision of the lhs, and then a third for overflow.

+      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_for (lhstype), lhs);
+      t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
+      if (tree_int_cst_lt (integer_one_node, t))

This could also use a comment explaining the logic.

+      /* For signed x << y the following:
+	 (unsigned) x >> ((prec (lhs) - 1) - y)
+	 if > 1, is undefined.  */

I meant briefly explaining where that formula comes from.

Jason


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