http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58943

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
So something like:
--- gcc/c/c-typeck.c.jj    2013-12-03 08:27:22.000000000 +0100
+++ gcc/c/c-typeck.c    2013-12-04 11:53:57.160894632 +0100
@@ -5192,6 +5192,7 @@ build_modify_expr (location_t location,
 {
   tree result;
   tree newrhs;
+  tree rhseval = NULL_TREE;
   tree rhs_semantic_type = NULL_TREE;
   tree lhstype = TREE_TYPE (lhs);
   tree olhstype = lhstype;
@@ -5253,8 +5254,17 @@ build_modify_expr (location_t location,
       /* Construct the RHS for any non-atomic compound assignemnt. */
       if (!is_atomic_op)
         {
+      /* If in LHS op= RHS the RHS has side-effects, ensure they
+         are preevaluated before the rest of the assignment expression's
+         side-effects, because RHS could contain e.g. function calls
+         that modify LHS.  */
+      if (TREE_SIDE_EFFECTS (rhs))
+        {
+          newrhs = c_save_expr (rhs);
+          rhseval = newrhs;
+        }
       newrhs = build_binary_op (location,
-                    modifycode, lhs, rhs, 1);
+                    modifycode, lhs, newrhs, 1);

       /* The original type of the right hand side is no longer
          meaningful.  */
@@ -5268,7 +5278,7 @@ build_modify_expr (location_t location,
      if so, we need to generate setter calls.  */
       result = objc_maybe_build_modify_expr (lhs, newrhs);
       if (result)
-    return result;
+    goto return_result;

       /* Else, do the check that we postponed for Objective-C.  */
       if (!lvalue_or_else (location, lhs, lv_assign))
@@ -5362,7 +5372,7 @@ build_modify_expr (location_t location,
       if (result)
     {
       protected_set_expr_location (result, location);
-      return result;
+      goto return_result;
     }
     }

@@ -5383,11 +5393,15 @@ build_modify_expr (location_t location,
      as the LHS argument.  */

   if (olhstype == TREE_TYPE (result))
-    return result;
+    goto return_result;

   result = convert_for_assignment (location, olhstype, result, rhs_origtype,
                    ic_assign, false, NULL_TREE, NULL_TREE, 0);
   protected_set_expr_location (result, location);
+
+return_result:
+  if (rhseval)
+    result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
   return result;
 }


?  In any case, it will have various consequences on OpenMP and perhaps
vectorization.

Reply via email to