public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Fix up ptr.~PTR () handling [PR96721]
@ 2020-08-24 21:34 Jakub Jelinek
  2020-08-25  2:37 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2020-08-24 21:34 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

Hi!

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.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk?

2020-08-24  Jakub Jelinek  <jakub@redhat.com>

	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/cp-tree.h.jj	2020-08-24 10:00:01.383257572 +0200
+++ gcc/cp/cp-tree.h	2020-08-24 12:28:02.698410896 +0200
@@ -6248,7 +6248,7 @@ extern bool null_member_pointer_value_p
 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<tree, va_gc> **,
--- gcc/cp/call.c.jj	2020-08-18 07:50:18.564922335 +0200
+++ gcc/cp/call.c	2020-08-24 12:30:55.211961352 +0200
@@ -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;
--- gcc/cp/semantics.c.jj	2020-08-18 07:50:18.653921086 +0200
+++ gcc/cp/semantics.c	2020-08-24 12:31:06.564800148 +0200
@@ -2713,7 +2713,7 @@ finish_call_expr (tree fn, vec<tree, va_
 	 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);
--- gcc/testsuite/g++.dg/opt/flifetime-dse8.C.jj	2020-08-24 12:36:24.585285134 +0200
+++ gcc/testsuite/g++.dg/opt/flifetime-dse8.C	2020-08-24 12:36:20.341345380 +0200
@@ -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 ();
+}

	Jakub


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c++: Fix up ptr.~PTR () handling [PR96721]
  2020-08-24 21:34 [PATCH] c++: Fix up ptr.~PTR () handling [PR96721] Jakub Jelinek
@ 2020-08-25  2:37 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2020-08-25  2:37 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On 8/24/20 5:34 PM, Jakub Jelinek wrote:
> Hi!
> 
> 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.
> 
> Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
> trunk?

OK.

> 2020-08-24  Jakub Jelinek  <jakub@redhat.com>
> 
> 	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/cp-tree.h.jj	2020-08-24 10:00:01.383257572 +0200
> +++ gcc/cp/cp-tree.h	2020-08-24 12:28:02.698410896 +0200
> @@ -6248,7 +6248,7 @@ extern bool null_member_pointer_value_p
>   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<tree, va_gc> **,
> --- gcc/cp/call.c.jj	2020-08-18 07:50:18.564922335 +0200
> +++ gcc/cp/call.c	2020-08-24 12:30:55.211961352 +0200
> @@ -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;
> --- gcc/cp/semantics.c.jj	2020-08-18 07:50:18.653921086 +0200
> +++ gcc/cp/semantics.c	2020-08-24 12:31:06.564800148 +0200
> @@ -2713,7 +2713,7 @@ finish_call_expr (tree fn, vec<tree, va_
>   	 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);
> --- gcc/testsuite/g++.dg/opt/flifetime-dse8.C.jj	2020-08-24 12:36:24.585285134 +0200
> +++ gcc/testsuite/g++.dg/opt/flifetime-dse8.C	2020-08-24 12:36:20.341345380 +0200
> @@ -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 ();
> +}
> 
> 	Jakub
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-08-25  2:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-24 21:34 [PATCH] c++: Fix up ptr.~PTR () handling [PR96721] Jakub Jelinek
2020-08-25  2:37 ` Jason Merrill

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