diff options
author | Jason Merrill <jason@redhat.com> | 2022-01-05 09:49:37 -0500 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2022-01-06 19:24:31 -0500 |
commit | 2fbc45486e13facfeb05bd6ddf70ff9973a30a3c (patch) | |
tree | b3e12dc5d275caa8a5c4a443a15353387b867edd /gcc/cp | |
parent | 40140b734487cbe03cd897dca47120317f898004 (diff) | |
download | gcc-2fbc45486e13facfeb05bd6ddf70ff9973a30a3c.zip gcc-2fbc45486e13facfeb05bd6ddf70ff9973a30a3c.tar.gz gcc-2fbc45486e13facfeb05bd6ddf70ff9973a30a3c.tar.bz2 |
c++: keep destroying array after one dtor throws [PR66451]
When we're cleaning up an array, if one destructor throws, we should still
try to clean up the rest of the array. We can use TRY_CATCH_EXPR for this,
instead of a TARGET_EXPR like my other recent patches, because a destructor
call can't involve any temporaries that need to live longer.
I thought about only doing this when we call build_vec_delete_1 from
build_vec_init, but it seems appropriate for delete-expressions as well;
we've said that the array's lifetime is over, it makes sense to keep trying
to destroy it. The standard isn't clear, but clang seems to agree with me.
PR c++/66451
gcc/cp/ChangeLog:
* init.c (build_vec_delete_1): Handle throwing dtor.
(build_vec_init): Tell it we're in a cleanup already.
gcc/testsuite/ChangeLog:
* g++.dg/eh/array3.C: New test.
* g++.dg/eh/array1.C: Mark destructor as throw().
* g++.dg/ipa/devirt-40.C: Likewise.
* g++.dg/warn/pr83054.C: Likewise.
* g++.dg/eh/delete1.C: Shorten array to one element.
Diffstat (limited to 'gcc/cp')
-rw-r--r-- | gcc/cp/init.c | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 7c7b810..df63e61 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -4006,7 +4006,8 @@ build_new (location_t loc, vec<tree, va_gc> **placement, tree type, static tree build_vec_delete_1 (location_t loc, tree base, tree maxindex, tree type, special_function_kind auto_delete_vec, - int use_global_delete, tsubst_flags_t complain) + int use_global_delete, tsubst_flags_t complain, + bool in_cleanup = false) { tree virtual_size; tree ptype = build_pointer_type (type = complete_type (type)); @@ -4109,6 +4110,18 @@ build_vec_delete_1 (location_t loc, tree base, tree maxindex, tree type, body = build_compound_expr (loc, body, tmp); loop = build1 (LOOP_EXPR, void_type_node, body); + + /* If one destructor throws, keep trying to clean up the rest, unless we're + already in a build_vec_init cleanup. */ + if (flag_exceptions && !in_cleanup && !expr_noexcept_p (tmp, tf_none)) + { + loop = build2 (TRY_CATCH_EXPR, void_type_node, loop, + unshare_expr (loop)); + /* Tell honor_protect_cleanup_actions to discard this on the + exceptional path. */ + TRY_CATCH_IS_CLEANUP (loop) = true; + } + loop = build_compound_expr (loc, tbase_init, loop); no_destructor: @@ -4490,7 +4503,8 @@ build_vec_init (tree base, tree maxindex, tree init, e = build_vec_delete_1 (input_location, rval, m, inner_elt_type, sfk_complete_destructor, - /*use_global_delete=*/0, complain); + /*use_global_delete=*/0, complain, + /*in_cleanup*/true); if (e == error_mark_node) errors = true; TARGET_EXPR_CLEANUP (iterator_targ) = e; |