public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-5814] tree: Fix up try_catch_may_fallthru [PR112619]
@ 2023-11-24  7:54 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2023-11-24  7:54 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:1c44bd92a86db3fcdeb4a66ce2f3222d13af0681

commit r14-5814-g1c44bd92a86db3fcdeb4a66ce2f3222d13af0681
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Nov 24 08:44:28 2023 +0100

    tree: Fix up try_catch_may_fallthru [PR112619]
    
    The following testcase ICEs with -std=c++98 since r14-5086 because
    block_may_fallthru is called on a TRY_CATCH_EXPR whose second operand
    is a MODIFY_EXPR rather than STATEMENT_LIST, which try_catch_may_fallthru
    apparently expects.
    I've been wondering whether that isn't some kind of FE bug and whether
    there isn't some unwritten rule that second operand of TRY_CATCH_EXPR
    must be a STATEMENT_LIST.  Looking at the FEs, the C++ FE uses mostly its
    own trees, TRY_BLOCK (TRY_CATCH_EXPR replacement) with HANDLER in it (CATCH_EXPR
    replacement) - but HANDLER can be immediate second operand rather than nested
    in STATEMENT_LIST, EH_SPEC_BLOCK (this one stands for both TRY_CATCH_EXPR
    and EH_FILTER_EXPR in its second argument); both of these are only replaced
    by the generic trees during gimplification though, so will unlikely be seen
    by block_may_fallthru; and then CLEANUP_STMT, which is genericized
    into TRY_CATCH_EXPR with non-CATCH_EXPR/EH_FILTER_EXPR in its body (this is
    the one that causes the ICE on this testcase).
    The Go and Rust FEs create TRY_CATCH_EXPR with CATCH_EXPR immediately in its
    second argument (but either are unlucky that block_may_fallthru isn't called
    or the body can always fallthru, or latent ICE), while the D FE most likely
    hit this ICE and attempts to work around it, by checking at TRY_CATCH_EXPR
    creation time if the second argument from pop_stmt_list is STATEMENT_LIST and
    if not, forcefully wraps it into a STATEMENT_LIST.
    
    Unfortunately, I don't see an easy way to create an artificial tree iterator
    from just a single tree statement, so the patch duplicates what the loops
    later do (after all, it is very simple, just didn't want to duplicate
    also the large comments explaning it, so the 3 See below. comments).
    
    2023-11-24  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/112619
            * tree.cc (try_catch_may_fallthru): If second operand of
            TRY_CATCH_EXPR is not a STATEMENT_LIST, handle it as if it was a
            STATEMENT_LIST containing a single statement.
    
            * g++.dg/eh/pr112619.C: New test.

Diff:
---
 gcc/testsuite/g++.dg/eh/pr112619.C | 15 +++++++++++++++
 gcc/tree.cc                        | 18 ++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/gcc/testsuite/g++.dg/eh/pr112619.C b/gcc/testsuite/g++.dg/eh/pr112619.C
new file mode 100644
index 00000000000..f04e1c38c8e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/eh/pr112619.C
@@ -0,0 +1,15 @@
+// PR c++/112619
+// { dg-do compile }
+
+struct S { S (); ~S (); };
+
+S
+foo (int a, int b)
+{
+  if (a || b)
+    {
+      S s;
+      return s;
+    }
+  return S ();
+}
diff --git a/gcc/tree.cc b/gcc/tree.cc
index a3d907a4ac0..e9f703e7237 100644
--- a/gcc/tree.cc
+++ b/gcc/tree.cc
@@ -12573,6 +12573,24 @@ try_catch_may_fallthru (const_tree stmt)
   if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
     return true;
 
+  switch (TREE_CODE (TREE_OPERAND (stmt, 1)))
+    {
+    case CATCH_EXPR:
+      /* See below.  */
+      return block_may_fallthru (CATCH_BODY (TREE_OPERAND (stmt, 1)));
+
+    case EH_FILTER_EXPR:
+      /* See below.  */
+      return block_may_fallthru (EH_FILTER_FAILURE (TREE_OPERAND (stmt, 1)));
+
+    case STATEMENT_LIST:
+      break;
+
+    default:
+      /* See below.  */
+      return false;
+    }
+
   i = tsi_start (TREE_OPERAND (stmt, 1));
   switch (TREE_CODE (tsi_stmt (i)))
     {

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-11-24  7:54 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-24  7:54 [gcc r14-5814] tree: Fix up try_catch_may_fallthru [PR112619] Jakub Jelinek

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