]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: minor EXPR_STMT cleanup
authorJason Merrill <jason@redhat.com>
Mon, 3 Feb 2025 15:35:33 +0000 (10:35 -0500)
committerJason Merrill <jason@redhat.com>
Sat, 19 Apr 2025 14:01:53 +0000 (10:01 -0400)
I think it was around PR118574 that I noticed a few cases where we were
unnecessarily wrapping a statement tree in a further EXPR_STMT.  Let's avoid
that and also use finish_expr_stmt in a few places in the coroutines code
that were building EXPR_STMT directly.

gcc/cp/ChangeLog:

* coroutines.cc (coro_build_expr_stmt)
(coro_build_cvt_void_expr_stmt): Remove.
(build_actor_fn): Use finish_expr_stmt.
* semantics.cc (finish_expr_stmt): Avoid wrapping statement in
EXPR_STMT.
(finish_stmt_expr_expr): Add comment.

gcc/cp/coroutines.cc
gcc/cp/semantics.cc

index b92d09fa4ead001b82381b4dbbec8efb662044aa..743da068e35247d499db76f7e3eb38371a1dfe4a 100644 (file)
@@ -1852,21 +1852,6 @@ coro_build_frame_access_expr (tree coro_ref, tree member_id, bool preserve_ref,
   return expr;
 }
 
-/* Helpers to build EXPR_STMT and void-cast EXPR_STMT, common ops.  */
-
-static tree
-coro_build_expr_stmt (tree expr, location_t loc)
-{
-  return maybe_cleanup_point_expr_void (build_stmt (loc, EXPR_STMT, expr));
-}
-
-static tree
-coro_build_cvt_void_expr_stmt (tree expr, location_t loc)
-{
-  tree t = build1 (CONVERT_EXPR, void_type_node, expr);
-  return coro_build_expr_stmt (t, loc);
-}
-
 /* Helpers to build an artificial var, with location LOC, NAME and TYPE, in
    CTX, and with initializer INIT.  */
 
@@ -2582,8 +2567,7 @@ build_actor_fn (location_t loc, tree coro_frame_type, tree actor, tree fnbody,
   tree hfa = build_new_method_call (ash, hfa_m, &args, NULL_TREE, LOOKUP_NORMAL,
                                    NULL, tf_warning_or_error);
   r = cp_build_init_expr (ash, hfa);
-  r = coro_build_cvt_void_expr_stmt (r, loc);
-  add_stmt (r);
+  finish_expr_stmt (r);
   release_tree_vector (args);
 
   /* Now we know the real promise, and enough about the frame layout to
@@ -2678,8 +2662,7 @@ build_actor_fn (location_t loc, tree coro_frame_type, tree actor, tree fnbody,
      we must tail call them.  However, some targets do not support indirect
      tail calls to arbitrary callees.  See PR94359.  */
   CALL_EXPR_TAILCALL (resume) = true;
-  resume = coro_build_cvt_void_expr_stmt (resume, loc);
-  add_stmt (resume);
+  finish_expr_stmt (resume);
 
   r = build_stmt (loc, RETURN_EXPR, NULL);
   gcc_checking_assert (maybe_cleanup_point_expr_void (r) == r);
index 7f23efd4a11eca9fa1d157c8614d7ec80b6d7703..1aa35d3861ea8f93ad85b9a1e8dd8b7a0b743207 100644 (file)
@@ -1180,10 +1180,13 @@ finish_expr_stmt (tree expr)
         expr = error_mark_node;
 
       /* Simplification of inner statement expressions, compound exprs,
-        etc can result in us already having an EXPR_STMT.  */
+        etc can result in us already having an EXPR_STMT or other statement
+        tree.  Don't wrap them in EXPR_STMT.  */
       if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
        {
-         if (TREE_CODE (expr) != EXPR_STMT)
+         if (TREE_CODE (expr) != EXPR_STMT
+             && !STATEMENT_CLASS_P (expr)
+             && TREE_CODE (expr) != STATEMENT_LIST)
            expr = build_stmt (loc, EXPR_STMT, expr);
          expr = maybe_cleanup_point_expr_void (expr);
        }
@@ -3082,6 +3085,7 @@ finish_stmt_expr_expr (tree expr, tree stmt_expr)
        }
       else if (processing_template_decl)
        {
+         /* Not finish_expr_stmt because we don't want convert_to_void.  */
          expr = build_stmt (input_location, EXPR_STMT, expr);
          expr = add_stmt (expr);
          /* Mark the last statement so that we can recognize it as such at