https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769
Marek Polacek <mpolacek at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |mpolacek at gcc dot gnu.org --- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> --- Unfinished/untested patch: commit f873acfa7ed1956a58d02cc383b8d709c446f656 (HEAD -> PR86769) Author: Marek Polacek <pola...@redhat.com> Date: Fri Nov 20 16:40:50 2020 -0500 c++: Condition in for statement destroyed too early [PR86769] diff --git a/gcc/c-family/c-gimplify.c b/gcc/c-family/c-gimplify.c index a7c0ec3be0d..f0a19456715 100644 --- a/gcc/c-family/c-gimplify.c +++ b/gcc/c-family/c-gimplify.c @@ -205,6 +205,30 @@ expr_loc_or_loc (const_tree expr, location_t or_loc) return loc; } +/* TODO */ + +static bool +maybe_inject_incr_to_body (tree body, tree incr) +{ + if (TREE_CODE (body) == BIND_EXPR) + body = BIND_EXPR_BODY (body); + if (TREE_CODE (body) == STATEMENT_LIST) + { + tree_stmt_iterator i = tsi_last (body); + tree t = tsi_stmt (i); + if (TREE_CODE (t) == TRY_FINALLY_EXPR) + { + if (TREE_CODE (TREE_OPERAND (t, 0)) == STATEMENT_LIST) + { + i = tsi_last (TREE_OPERAND (t, 0)); + tsi_link_after (&i, incr, TSI_CONTINUE_LINKING); + return true; + } + } + } + return false; +} + /* Build a generic representation of one of the C loop forms. COND is the loop condition or NULL_TREE. BODY is the (possibly compound) statement controlled by the loop. INCR is the increment expression of a for-loop, @@ -291,6 +315,9 @@ genericize_c_loop (tree *stmt_p, location_t start_locus, tree cond, tree body, append_to_statement_list (top, &stmt_list); } + if (maybe_inject_incr_to_body (body, incr)) + incr = NULL_TREE; + append_to_statement_list (body, &stmt_list); finish_bc_block (&stmt_list, bc_continue, clab); if (incr)