]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: extended temp cleanups [PR118856]
authorJason Merrill <jason@redhat.com>
Fri, 14 Feb 2025 09:53:01 +0000 (10:53 +0100)
committerJason Merrill <jason@redhat.com>
Fri, 14 Feb 2025 16:12:48 +0000 (17:12 +0100)
commite96e1bb69c7b46db18e747ee379a62681bc8c82d
tree1a032d19f87f80b4ea6bde443272be6c2ec9651a
parent823de62f5f66f052193aa74520dec94fb3488958
c++: extended temp cleanups [PR118856]

A later testcase in PR118856 highlights a preexisting problem with multiple
reference-extended temporaries in a single declaration; if initializing a
later one throws, the cleanup for the earlier one is not in scope yet.
Let's deal with this by keeping a dummy TARGET_EXPR to hold the EH cleanup
until all other initialization is complete.  See the comment for various
other considered approaches.

We now avoid extending TARGET_EXPRs with CLEANUP_EH_ONLY set; all such
TARGET_EXPRs were already only internal iterator/flag variables that don't
want to be extended, as they are dead after initialization is complete even
if other temporaries are extended.  But some other internal temporaries did
not have the flag set because they don't have TARGET_EXPR_CLEANUP; I
introduce a get_internal_target_expr function to set the flag rather than
directly set the flag (and add a comment) in such places.  The places
changed to call get_internal_target_expr either already set the flag, or
have no cleanup at all.

PR c++/118856

gcc/cp/ChangeLog:

* call.cc (set_up_extended_ref_temp): Retain a TARGET_EXPR for
cleanups if something later in initialization throws.
(extend_temps_r): Don't extend eliding or EH-only TARGET_EXPRs.
* cp-tree.h (get_internal_target_expr): Declare.
* tree.cc (get_internal_target_expr): New.
* decl.cc (cp_finish_decomp, expand_static_init): Use it.
* except.cc (build_throw): Likewise.
* init.cc (build_new_1, build_vec_init, build_delete): Likewise.
(build_vec_delete): Likewise.
* typeck2.cc (maybe_push_temp_cleanup): Likewise.

gcc/testsuite/ChangeLog:

* g++.dg/eh/ref-temp3.C: New test.
* g++.dg/eh/ref-temp4.C: New test.
gcc/cp/call.cc
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/cp/except.cc
gcc/cp/init.cc
gcc/cp/tree.cc
gcc/cp/typeck2.cc
gcc/testsuite/g++.dg/eh/ref-temp3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/eh/ref-temp4.C [new file with mode: 0644]