]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: -Wmismatched-new-delete, -O0, new (nothrow)
authorJason Merrill <jason@redhat.com>
Thu, 18 Sep 2025 11:10:54 +0000 (13:10 +0200)
committerJason Merrill <jason@redhat.com>
Thu, 18 Sep 2025 21:45:09 +0000 (23:45 +0200)
With new (nothrow) we need to check whether the allocation returned null,
and the resulting COND_EXPR was confusing -Wmismatched-new-delete-2 at -O0.
Let's help it out by appending the address if it's the same on both arms.

gcc/cp/ChangeLog:

* init.cc (build_new_1): Append alloc_node to checking COND_EXPR.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wmismatched-new-delete-2.C: Remove xfail.

gcc/cp/init.cc
gcc/testsuite/g++.dg/warn/Wmismatched-new-delete-2.C

index f19794caadcb6812dc6b18f86c17ca7d6c586d25..8db84eb5e38cfb0ad95b5cd6af82852215342423 100644 (file)
@@ -3930,6 +3930,11 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
                                           complain);
          rval = build_conditional_expr (input_location, ifexp, rval,
                                         alloc_node, complain);
+         /* If there's no offset between data_addr and alloc_node, append it
+            to help -Wmismatched-new-delete at -O0.  */
+         if (!cookie_size)
+           rval = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_node),
+                          rval, alloc_node);
        }
 
       /* Perform the allocation before anything else, so that ALLOC_NODE
index d0d53b38b93891fa5bce59ead6de2d83be5c7642..0f1877ee3e53941403e3a47d0a0606b3f87c29e0 100644 (file)
@@ -140,9 +140,8 @@ void warn_x1_array_mismatch ()
     delete p;                 // { dg-warning "'static void X1::operator delete\\(void\\*\\)' called on pointer returned from a mismatched allocation function" }
   }
   {
-    // The following requires optimization (see warn_x1_array_mismatch()).
     X1 *p = new (std::nothrow) X1[3];
-    delete p;                 // { dg-warning "'static void X1::operator delete\\(void\\*\\)' called on pointer returned from a mismatched allocation function" "pr?????" { xfail *-*-* } }
+    delete p;                 // { dg-warning "'static void X1::operator delete\\(void\\*\\)' called on pointer returned from a mismatched allocation function" }
   }
 }