Delay folding of bool conversion into COND_EXPR.
authorJason Merrill <jason@redhat.com>
Sat, 8 Oct 2016 17:40:22 +0000 (13:40 -0400)
committerJason Merrill <jason@gcc.gnu.org>
Sat, 8 Oct 2016 17:40:22 +0000 (13:40 -0400)
gcc/c-family/
* c-common.c (c_common_truthvalue_conversion): Don't distribute
into COND_EXPR in C++.
gcc/cp/
* cp-gimplify.c (cp_fold): Distribute cp_truthvalue_conversion
into COND_EXPR.

From-SVN: r240893

gcc/c-family/ChangeLog
gcc/c-family/c-common.c
gcc/cp/ChangeLog
gcc/cp/cp-gimplify.c

index 9f6b16d7306cac4b1ebc259593e6ee98803a2b10..21985c796fb77d462290845bc9cddd1e33914c23 100644 (file)
@@ -1,3 +1,8 @@
+2016-10-08  Jason Merrill  <jason@redhat.com>
+
+       * c-common.c (c_common_truthvalue_conversion): Don't distribute
+       into COND_EXPR in C++.
+
 2016-10-08  Jakub Jelinek  <jakub@redhat.com>
 
        * c-lex.c (c_lex_with_flags) <case CPP_COMMENT>: For CPP_COMMENT
index f7a5d62012bb84871f02a1e993f41738058a8a1b..dbdb276f9536d90d229b9494d22d4406b18f3d0e 100644 (file)
@@ -4694,21 +4694,8 @@ c_common_truthvalue_conversion (location_t location, tree expr)
        }
       /* Distribute the conversion into the arms of a COND_EXPR.  */
       if (c_dialect_cxx ())
-       {
-         tree op1 = TREE_OPERAND (expr, 1);
-         tree op2 = TREE_OPERAND (expr, 2);
-         int w = warn_int_in_bool_context;
-         warn_int_in_bool_context = 0;
-         /* In C++ one of the arms might have void type if it is throw.  */
-         if (!VOID_TYPE_P (TREE_TYPE (op1)))
-           op1 = c_common_truthvalue_conversion (location, op1);
-         if (!VOID_TYPE_P (TREE_TYPE (op2)))
-           op2 = c_common_truthvalue_conversion (location, op2);
-         expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
-                                 TREE_OPERAND (expr, 0), op1, op2);
-         warn_int_in_bool_context = w;
-         goto ret;
-       }
+       /* Avoid premature folding.  */
+       break;
       else
        {
          int w = warn_int_in_bool_context;
index 0bd0457f01a7c283cb57f93fe453c3319af83f95..32b011f346261abd126a51c0839fc84eea73a9e0 100644 (file)
@@ -1,3 +1,8 @@
+2016-10-08  Jason Merrill  <jason@redhat.com>
+
+       * cp-gimplify.c (cp_fold): Distribute cp_truthvalue_conversion
+       into COND_EXPR.
+
 2016-10-07  Jason Merrill  <jason@redhat.com>
 
        Further P0135 refinement.
index 5aca8f2a3b0da3d160e01efe57872d289c47f3bd..48796323523294ec13a0291a33262fcec68edbb6 100644 (file)
@@ -2253,6 +2253,15 @@ cp_fold (tree x)
       op1 = cp_fold (TREE_OPERAND (x, 1));
       op2 = cp_fold (TREE_OPERAND (x, 2));
 
+      if (TREE_CODE (TREE_TYPE (x)) == BOOLEAN_TYPE)
+       {
+         warning_sentinel (warn_int_in_bool_context);
+         if (!VOID_TYPE_P (TREE_TYPE (op1)))
+           op1 = cp_truthvalue_conversion (op1);
+         if (!VOID_TYPE_P (TREE_TYPE (op2)))
+           op2 = cp_truthvalue_conversion (op2);
+       }
+
       if (op0 != TREE_OPERAND (x, 0)
          || op1 != TREE_OPERAND (x, 1)
          || op2 != TREE_OPERAND (x, 2))