This patch fixes PR 53148, a regression where a conditional expression
between signed and unsigned, the evaluated half involving division by
zero but both halves having only integer constants as operands,
resulted in an ICE because C_MAYBE_CONST_EXPRs were wrongly nested
(they should never be nested).  The problem was the code dealing with
signed/unsigned warnings folded both halves, and then wrapped them in
C_MAYBE_CONST_EXPRs as needed to avoid repeated recursive folding, but
the code dealing with nonconstant expressions with integer constant
operands (such as those involving an evaluated division by zero - such
expressions may appear in unevaluated parts of an integer constant
expression, but not in evaluated parts) failed to deal with this
before adding another C_MAYBE_CONST_EXPR to mark the expression with
integer constant operands as such.

Bootstrapped with no regressions on x86_64-unknown-linux-gnu.  Applied
to mainline.  Will also test for 4.7 and 4.6 branches.

2012-05-21  Joseph Myers  <jos...@codesourcery.com>

        PR c/53148
        * c-typeck.c (build_conditional_expr): Remove C_MAYBE_CONST_EXPR
        from folded operands before wrapping another around the
        conditional expression.

testsuite:
2012-05-21  Joseph Myers  <jos...@codesourcery.com>

        PR c/53148
        * gcc.c-torture/compile/pr53418-1.c,
        gcc.c-torture/compile/pr53418-2.c: New tests.

Index: testsuite/gcc.c-torture/compile/pr53418-1.c
===================================================================
--- testsuite/gcc.c-torture/compile/pr53418-1.c (revision 0)
+++ testsuite/gcc.c-torture/compile/pr53418-1.c (revision 0)
@@ -0,0 +1,5 @@
+void
+f (void)
+{
+  int i = (0 ? 1 : 0U / 0);
+}
Index: testsuite/gcc.c-torture/compile/pr53418-2.c
===================================================================
--- testsuite/gcc.c-torture/compile/pr53418-2.c (revision 0)
+++ testsuite/gcc.c-torture/compile/pr53418-2.c (revision 0)
@@ -0,0 +1,5 @@
+void
+f (void)
+{
+  int i = (1 ? 0U / 0 : 1);
+}
Index: c-typeck.c
===================================================================
--- c-typeck.c  (revision 187704)
+++ c-typeck.c  (working copy)
@@ -4408,6 +4408,11 @@ build_conditional_expr (location_t colon
     ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
   else
     {
+      if (int_operands)
+       {
+         op1 = remove_c_maybe_const_expr (op1);
+         op2 = remove_c_maybe_const_expr (op2);
+       }
       ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
       if (int_operands)
        ret = note_integer_operands (ret);

-- 
Joseph S. Myers
jos...@codesourcery.com

Reply via email to