[gcc r16-3962] c++: clobber class new-expressions

Jason Merrill jason@gcc.gnu.org
Thu Sep 18 21:47:07 GMT 2025


https://gcc.gnu.org/g:12975b8aa2dc38ed5b3d6e2c6181a522b4f655e3

commit r16-3962-g12975b8aa2dc38ed5b3d6e2c6181a522b4f655e3
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Sep 18 13:10:55 2025 +0200

    c++: clobber class new-expressions
    
    Since Jakub's P2795R5 work removes the initial clobber from constructors,
    let's add a clobber for new of all non-empty classes.
    
    gcc/cp/ChangeLog:
    
            * init.cc (build_new_1): Clobber classes.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/analyzer/pr97116.C: Adjust diagnostic.
            * g++.dg/warn/Warray-bounds-20.C: Likewise.

Diff:
---
 gcc/cp/init.cc                               | 4 +---
 gcc/testsuite/g++.dg/analyzer/pr97116.C      | 5 ++---
 gcc/testsuite/g++.dg/warn/Warray-bounds-20.C | 4 ++--
 3 files changed, 5 insertions(+), 8 deletions(-)

diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
index c950c363f59c..3fe476d7eec7 100644
--- a/gcc/cp/init.cc
+++ b/gcc/cp/init.cc
@@ -3566,9 +3566,7 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
     = (std_placement && flag_lifetime_dse > 1
        && !processing_template_decl
        && !is_empty_type (elt_type)
-       && !*init
-       && (!CLASS_TYPE_P (elt_type)
-	   || type_has_non_user_provided_default_constructor (elt_type)));
+       && (!*init || CLASS_TYPE_P (elt_type)));
 
   /* In the simple case, we can stop now.  */
   pointer_type = build_pointer_type (type);
diff --git a/gcc/testsuite/g++.dg/analyzer/pr97116.C b/gcc/testsuite/g++.dg/analyzer/pr97116.C
index 1c404c2ceb23..06ee8623f43a 100644
--- a/gcc/testsuite/g++.dg/analyzer/pr97116.C
+++ b/gcc/testsuite/g++.dg/analyzer/pr97116.C
@@ -3,7 +3,7 @@
 
 struct foo
 {
-  foo (int i) : m_i (i) {} // { dg-message "argument 'this' of 'foo::foo\\(int\\)' must be non-null" "note" }
+  foo (int i) : m_i (i) {}
 
   int get () const { return m_i; } // { dg-message "argument 'this' of '\[^\n\]*' must be non-null" "note" }
   
@@ -15,8 +15,7 @@ struct foo
 
 void test_1 (void)
 {
-  foo *p = new(NULL) foo (42); // { dg-warning "non-null expected" "warning" }
-  // { dg-message "argument 'this'( \\(\[^\n\]*\\))? NULL where non-null expected" "final event" { target *-*-* } .-1 }
+  foo *p = new(NULL) foo (42); // { dg-warning "dereference of NULL" "warning" }
 }
 
 int test_2 (void)
diff --git a/gcc/testsuite/g++.dg/warn/Warray-bounds-20.C b/gcc/testsuite/g++.dg/warn/Warray-bounds-20.C
index 5fc552930747..36f8046fed7b 100644
--- a/gcc/testsuite/g++.dg/warn/Warray-bounds-20.C
+++ b/gcc/testsuite/g++.dg/warn/Warray-bounds-20.C
@@ -39,7 +39,7 @@ void warn_derived_ctor_access_new_decl ()
                               // { dg-message "at offset 1 into object 'a' of size 20" "LP64 note" { target ilp32} .-1 }
   char *p = a;
   ++p;
-  D1 *q = new (p) D1;
+  D1 *q = new (p) D1;		// { dg-warning "-Warray-bounds" }
   sink (q);
 }
 
@@ -47,7 +47,7 @@ void warn_derived_ctor_access_new_alloc ()
 {
   char *p = (char*)operator new (sizeof (D1));    // { dg-message "at offset 1 into object of size \\d+ allocated by '\[^\n\r]*operator new\[^\n\r]*'" "note" }
   ++p;
-  D1 *q = new (p) D1;
+  D1 *q = new (p) D1;		// { dg-warning "-Warray-bounds" }
   sink (q);
 }


More information about the Gcc-cvs mailing list