From: Jason Merrill Date: Thu, 18 Sep 2025 11:10:54 +0000 (+0200) Subject: c++: -Wmismatched-new-delete, -O0, new (nothrow) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c91e508a52d4441b6c081ea50b0a08d0f4775675;p=thirdparty%2Fgcc.git c++: -Wmismatched-new-delete, -O0, new (nothrow) 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. --- diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index f19794caadc..8db84eb5e38 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -3930,6 +3930,11 @@ build_new_1 (vec **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 diff --git a/gcc/testsuite/g++.dg/warn/Wmismatched-new-delete-2.C b/gcc/testsuite/g++.dg/warn/Wmismatched-new-delete-2.C index d0d53b38b93..0f1877ee3e5 100644 --- a/gcc/testsuite/g++.dg/warn/Wmismatched-new-delete-2.C +++ b/gcc/testsuite/g++.dg/warn/Wmismatched-new-delete-2.C @@ -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" } } }