public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "mpolacek at gcc dot gnu.org" <gcc-bugzilla@gcc.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	[thread overview]
Message-ID: <bug-86769-4-OUbtviOwiw@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-86769-4@http.gcc.gnu.org/bugzilla/>

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 <polacek@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)

  parent reply	other threads:[~2020-11-30 20:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-86769-4@http.gcc.gnu.org/bugzilla/>
2020-10-28 15:38 ` mpolacek at gcc dot gnu.org
2020-10-28 16:15 ` arthur.j.odwyer at gmail dot com
2020-11-30 20:10 ` mpolacek at gcc dot gnu.org [this message]
2021-01-14 16:44 ` mpolacek at gcc dot gnu.org
2021-01-14 23:50 ` mpolacek at gcc dot gnu.org
2024-03-12 21:49 ` pinskia at gcc dot gnu.org
2024-03-12 21:50 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-86769-4-OUbtviOwiw@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).