From 61680cfaf1eef26a5953f36ab82a1cc13f9b2f2c Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Tue, 25 Aug 2020 07:19:41 +0200 Subject: [PATCH] c++: Fix up ptr.~PTR () handling [PR96721] The following testcase is miscompiled, because build_trivial_dtor_call handles the case when instance is a pointer by adding a clobber to what the pointer points to (which is desirable e.g. for delete) rather than the pointer itself. That is I think always desirable behavior for references, but for pointers for the pseudo dtor case it is not. 2020-08-25 Jakub Jelinek PR c++/96721 * cp-tree.h (build_trivial_dtor_call): Add bool argument defaulted to false. * call.c (build_trivial_dtor_call): Add NO_PTR_DEREF argument. If instance is a pointer and NO_PTR_DEREF is true, clobber the pointer rather than what it points to. * semantics.c (finish_call_expr): Call build_trivial_dtor_call with true as NO_PTR_DEREF. * g++.dg/opt/flifetime-dse8.C: New test. --- gcc/cp/call.c | 9 ++++++--- gcc/cp/cp-tree.h | 2 +- gcc/cp/semantics.c | 2 +- gcc/testsuite/g++.dg/opt/flifetime-dse8.C | 12 ++++++++++++ 4 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/opt/flifetime-dse8.C diff --git a/gcc/cp/call.c b/gcc/cp/call.c index c62d9e227d3..4726e57a30d 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -8430,10 +8430,12 @@ conv_binds_ref_to_prvalue (conversion *c) } /* Call the trivial destructor for INSTANCE, which can be either an lvalue of - class type or a pointer to class type. */ + class type or a pointer to class type. If NO_PTR_DEREF is true and + INSTANCE has pointer type, clobber the pointer rather than what it points + to. */ tree -build_trivial_dtor_call (tree instance) +build_trivial_dtor_call (tree instance, bool no_ptr_deref) { gcc_assert (!is_dummy_object (instance)); @@ -8443,7 +8445,8 @@ build_trivial_dtor_call (tree instance) return fold_convert (void_type_node, instance); } - if (INDIRECT_TYPE_P (TREE_TYPE (instance))) + if (INDIRECT_TYPE_P (TREE_TYPE (instance)) + && (!no_ptr_deref || TYPE_REF_P (TREE_TYPE (instance)))) { if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance)))) goto no_clobber; diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 3f3717a6bb5..2b7c9b9913a 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -6248,7 +6248,7 @@ extern bool null_member_pointer_value_p (tree); extern bool sufficient_parms_p (const_tree); extern tree type_decays_to (tree); extern tree extract_call_expr (tree); -extern tree build_trivial_dtor_call (tree); +extern tree build_trivial_dtor_call (tree, bool = false); extern tree build_user_type_conversion (tree, tree, int, tsubst_flags_t); extern tree build_new_function_call (tree, vec **, diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index b71ca0729a8..3877a0e536a 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -2713,7 +2713,7 @@ finish_call_expr (tree fn, vec **args, bool disallow_virtual, denoted by the object expression of the class member access. */ tree ob = TREE_OPERAND (fn, 0); if (obvalue_p (ob)) - result = build_trivial_dtor_call (ob); + result = build_trivial_dtor_call (ob, true); else /* No location to clobber. */ result = convert_to_void (ob, ICV_STATEMENT, complain); diff --git a/gcc/testsuite/g++.dg/opt/flifetime-dse8.C b/gcc/testsuite/g++.dg/opt/flifetime-dse8.C new file mode 100644 index 00000000000..eeb814e03e3 --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/flifetime-dse8.C @@ -0,0 +1,12 @@ +// PR c++/96721 +// { dg-do run } +// { dg-options "-O2 -flifetime-dse" } + +typedef int *T; + +int +main () +{ + T a = T (); + a.~T (); +} -- 2.30.2