aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/except.c
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2022-01-05 17:01:12 -0500
committerJason Merrill <jason@redhat.com>2022-01-06 19:25:43 -0500
commitb10e031458d541f794dfaa08ba606487603a4bb6 (patch)
tree84098539237e443920e3c21668370bb63a27c841 /gcc/cp/except.c
parent4c6afbbd48f0c40ddf949bc403d9bd5f5e14204f (diff)
downloadgcc-b10e031458d541f794dfaa08ba606487603a4bb6.zip
gcc-b10e031458d541f794dfaa08ba606487603a4bb6.tar.gz
gcc-b10e031458d541f794dfaa08ba606487603a4bb6.tar.bz2
c++: destroy retval on throwing cleanup in try [PR33799]
My earlier attempt to fix this bug didn't handle the case where both the return and the throwing cleanup are within a try-block that catches and discards the exception. Fixed by adding the retval cleanup to any try-blocks as well as the function body. PR102191 pointed out that we also weren't handling templates properly, so I moved the call out of the parser. PR c++/33799 PR c++/102191 gcc/cp/ChangeLog: * except.c (maybe_splice_retval_cleanup): Check current_binding_level. * semantics.c (do_poplevel): Call it here. * parser.c (cp_parser_compound_statement): Not here. gcc/testsuite/ChangeLog: * g++.dg/eh/return1.C: Add temporary in try block case. * g++.dg/cpp2a/constexpr-dtor11.C: New test.
Diffstat (limited to 'gcc/cp/except.c')
-rw-r--r--gcc/cp/except.c29
1 files changed, 19 insertions, 10 deletions
diff --git a/gcc/cp/except.c b/gcc/cp/except.c
index 5c7eeff..bcd9f84 100644
--- a/gcc/cp/except.c
+++ b/gcc/cp/except.c
@@ -1294,26 +1294,35 @@ maybe_set_retval_sentinel ()
current_retval_sentinel, boolean_true_node);
}
-/* COMPOUND_STMT is the STATEMENT_LIST for the current function body. If
- current_retval_sentinel was set in this function, wrap the body in a
- CLEANUP_STMT to destroy the return value on throw. */
+/* COMPOUND_STMT is the STATEMENT_LIST for some block. If COMPOUND_STMT is the
+ current function body or a try block, and current_retval_sentinel was set in
+ this function, wrap the block in a CLEANUP_STMT to destroy the return value
+ on throw. */
void
maybe_splice_retval_cleanup (tree compound_stmt)
{
- /* If need_retval_cleanup set current_retval_sentinel, wrap the function body
- in a CLEANUP_STMT to handle destroying the return value. */
- if (!DECL_CONSTRUCTOR_P (current_function_decl)
+ /* If we need a cleanup for the return value, add it in at the same level as
+ pushdecl_outermost_localscope. And also in try blocks. */
+ bool function_body
+ = (current_binding_level->level_chain
+ && current_binding_level->level_chain->kind == sk_function_parms);
+
+ if ((function_body || current_binding_level->kind == sk_try)
+ && !DECL_CONSTRUCTOR_P (current_function_decl)
&& !DECL_DESTRUCTOR_P (current_function_decl)
&& current_retval_sentinel)
{
location_t loc = DECL_SOURCE_LOCATION (current_function_decl);
-
- /* Add a DECL_EXPR for current_retval_sentinel. */
tree_stmt_iterator iter = tsi_start (compound_stmt);
tree retval = DECL_RESULT (current_function_decl);
- tree decl_expr = build_stmt (loc, DECL_EXPR, current_retval_sentinel);
- tsi_link_before (&iter, decl_expr, TSI_SAME_STMT);
+
+ if (function_body)
+ {
+ /* Add a DECL_EXPR for current_retval_sentinel. */
+ tree decl_expr = build_stmt (loc, DECL_EXPR, current_retval_sentinel);
+ tsi_link_before (&iter, decl_expr, TSI_SAME_STMT);
+ }
/* Skip past other decls, they can't contain a return. */
while (TREE_CODE (tsi_stmt (iter)) == DECL_EXPR)