[Bug c++/86769] g++ destroys condition variable in for statement too early

2024-03-12 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

Andrew Pinski  changed:

   What|Removed |Added

 CC||gnu4u at flonatel dot org

--- Comment #7 from Andrew Pinski  ---
*** Bug 36016 has been marked as a duplicate of this bug. ***

[Bug c++/86769] g++ destroys condition variable in for statement too early

2024-03-12 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

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

[Bug c++/86769] g++ destroys condition variable in for statement too early

2021-01-14 Thread mpolacek at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

--- Comment #5 from Marek Polacek  ---
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.

[Bug c++/86769] g++ destroys condition variable in for statement too early

2021-01-14 Thread mpolacek at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

Marek Polacek  changed:

   What|Removed |Added

 Status|NEW |ASSIGNED
   Assignee|unassigned at gcc dot gnu.org  |mpolacek at gcc dot 
gnu.org

[Bug c++/86769] g++ destroys condition variable in for statement too early

2020-11-30 Thread mpolacek at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

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) == 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 (, 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, _list);
 }

+  if (maybe_inject_incr_to_body (body, incr))
+incr = NULL_TREE;
+
   append_to_statement_list (body, _list);
   finish_bc_block (_list, bc_continue, clab);
   if (incr)

[Bug c++/86769] g++ destroys condition variable in for statement too early

2020-10-28 Thread arthur.j.odwyer at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

Arthur O'Dwyer  changed:

   What|Removed |Added

 CC||arthur.j.odwyer at gmail dot 
com

--- Comment #3 from Arthur O'Dwyer  ---
Confirmed. https://godbolt.org/z/MfbrcG

[Bug c++/86769] g++ destroys condition variable in for statement too early

2020-10-28 Thread mpolacek at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

Marek Polacek  changed:

   What|Removed |Added

 CC||gcc-bugzilla at contacts dot 
eelis
   ||.net

--- Comment #2 from Marek Polacek  ---
*** Bug 68003 has been marked as a duplicate of this bug. ***

[Bug c++/86769] g++ destroys condition variable in for statement too early

2018-08-01 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

Jonathan Wakely  changed:

   What|Removed |Added

   Keywords||wrong-code
 Status|UNCONFIRMED |NEW
   Last reconfirmed||2018-08-01
 Ever confirmed|0   |1

[Bug c++/86769] g++ destroys condition variable in for statement too early

2018-07-31 Thread richard-gccbugzilla at metafoo dot co.uk
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86769

--- Comment #1 from Richard Smith  ---
For easy reproduction: https://godbolt.org/g/dfZFxz

This does not appear to be a regression: this example appears to be miscompiled
by every version of GCC back to at least 4.1.