public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/86769] g++ destroys condition variable in for statement too early
       [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
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-10-28 15:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gcc-bugzilla at contacts dot eelis
                   |                            |.net

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
*** Bug 68003 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/86769] g++ destroys condition variable in for statement too early
       [not found] <bug-86769-4@http.gcc.gnu.org/bugzilla/>
  2020-10-28 15:38 ` [Bug c++/86769] g++ destroys condition variable in for statement too early 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
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: arthur.j.odwyer at gmail dot com @ 2020-10-28 16:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

Arthur O'Dwyer <arthur.j.odwyer at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |arthur.j.odwyer at gmail dot com

--- Comment #3 from Arthur O'Dwyer <arthur.j.odwyer at gmail dot com> ---
Confirmed. https://godbolt.org/z/MfbrcG

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/86769] g++ destroys condition variable in for statement too early
       [not found] <bug-86769-4@http.gcc.gnu.org/bugzilla/>
  2020-10-28 15:38 ` [Bug c++/86769] g++ destroys condition variable in for statement too early 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
  2021-01-14 16:44 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-11-30 20:10 UTC (permalink / raw)
  To: gcc-bugs

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)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/86769] g++ destroys condition variable in for statement too early
       [not found] <bug-86769-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2020-11-30 20:10 ` mpolacek at gcc dot gnu.org
@ 2021-01-14 16:44 ` mpolacek at gcc dot gnu.org
  2021-01-14 23:50 ` mpolacek at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-01-14 16:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/86769] g++ destroys condition variable in for statement too early
       [not found] <bug-86769-4@http.gcc.gnu.org/bugzilla/>
                   ` (3 preceding siblings ...)
  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
  6 siblings, 0 replies; 7+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-01-14 23:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Further poking revealed that the patch above mishandles

// PR c++/86769
// { dg-do run }

#define assert(X) do { if (!(X)) __builtin_abort(); } while(0)

int g;

struct X {
  X() { g++; }
  ~X() { g--; }
  operator bool() { return g == 0; }
};

void
check_live ()
{
  assert (g > 0);
}

void
check_dead ()
{
  assert (g == 0);
}

void f(X &) { assert (!g); }

int
main ()
{
  for (int i = 0; i < 1; ++i, check_dead ())
    {
      X x = X();
      check_live ();
    }
}

So saving it here lest I lose it.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/86769] g++ destroys condition variable in for statement too early
       [not found] <bug-86769-4@http.gcc.gnu.org/bugzilla/>
                   ` (4 preceding siblings ...)
  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
  6 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-12 21:49 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 57684
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57684&action=edit
Testcase from godbolt

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/86769] g++ destroys condition variable in for statement too early
       [not found] <bug-86769-4@http.gcc.gnu.org/bugzilla/>
                   ` (5 preceding siblings ...)
  2024-03-12 21:49 ` pinskia at gcc dot gnu.org
@ 2024-03-12 21:50 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-12 21:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gnu4u at flonatel dot org

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 36016 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-03-12 21:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-86769-4@http.gcc.gnu.org/bugzilla/>
2020-10-28 15:38 ` [Bug c++/86769] g++ destroys condition variable in for statement too early 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
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

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).