From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id ABC04393C85E; Mon, 30 Nov 2020 20:10:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ABC04393C85E From: "mpolacek at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/86769] g++ destroys condition variable in for statement too early Date: Mon, 30 Nov 2020 20:10:23 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 8.2.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: mpolacek at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 30 Nov 2020 20:10:23 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D86769 Marek Polacek changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |mpolacek at gcc dot gnu.org --- Comment #4 from Marek Polacek --- Unfinished/untested patch: commit f873acfa7ed1956a58d02cc383b8d709c446f656 (HEAD -> PR86769) Author: Marek Polacek 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) =3D=3D BIND_EXPR) + body =3D BIND_EXPR_BODY (body); + if (TREE_CODE (body) =3D=3D STATEMENT_LIST) + { + tree_stmt_iterator i =3D tsi_last (body); + tree t =3D tsi_stmt (i); + if (TREE_CODE (t) =3D=3D TRY_FINALLY_EXPR) + { + if (TREE_CODE (TREE_OPERAND (t, 0)) =3D=3D STATEMENT_LIST) + { + i =3D 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 =3D NULL_TREE; + append_to_statement_list (body, &stmt_list); finish_bc_block (&stmt_list, bc_continue, clab); if (incr)=