https://gcc.gnu.org/g:3cae3a80695e5aabd7353c02e179c997158eef30
commit r15-6750-g3cae3a80695e5aabd7353c02e179c997158eef30 Author: Jason Merrill <ja...@redhat.com> Date: Thu Jan 9 15:12:07 2025 -0500 c++: be permissive about eh spec mismatch for op new r15-3532 made us more strict about exception-specification mismatches with the standard library, but let's still be permissive about operator new, since previously you needed to say throw(std::bad_alloc). gcc/cp/ChangeLog: * decl.cc (check_redeclaration_exception_specification): Be more lenient about ::operator new. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/noexcept88.C: New test. Diff: --- gcc/cp/decl.cc | 11 +++++++++-- gcc/testsuite/g++.dg/cpp0x/noexcept88.C | 9 +++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 288da65fd8db..5c6a4996a89e 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -1398,7 +1398,14 @@ check_redeclaration_exception_specification (tree new_decl, location_t new_loc = DECL_SOURCE_LOCATION (new_decl); auto_diagnostic_group d; - if (DECL_IN_SYSTEM_HEADER (old_decl) && DECL_EXTERN_C_P (old_decl)) + /* Be permissive about C++98 vs C++11 operator new declarations. */ + bool global_new = (IDENTIFIER_NEW_OP_P (DECL_NAME (new_decl)) + && CP_DECL_CONTEXT (new_decl) == global_namespace + && (nothrow_spec_p (new_exceptions) + == nothrow_spec_p (old_exceptions))); + + if (DECL_IN_SYSTEM_HEADER (old_decl) + && (global_new || DECL_EXTERN_C_P (old_decl))) /* Don't fuss about the C library; the C library functions are not specified to have exception specifications (just behave as if they have them), but some implementations include them. */ @@ -1407,7 +1414,7 @@ check_redeclaration_exception_specification (tree new_decl, /* We used to silently permit mismatched eh specs with -fno-exceptions, so only complain if -pedantic. */ complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl); - else if (!new_exceptions) + else if (!new_exceptions || global_new) /* Reduce to pedwarn for omitted exception specification. No warning flag for this; silence the warning by correcting the code. */ complained = pedwarn (new_loc, 0, msg, new_decl); diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept88.C b/gcc/testsuite/g++.dg/cpp0x/noexcept88.C new file mode 100644 index 000000000000..9b57fddbf0a9 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept88.C @@ -0,0 +1,9 @@ +// { dg-options -Wsystem-headers } + +#include <cstdlib> +#include <new> + +void *operator new (std::size_t) throw (std::bad_alloc); // { dg-line decl } +// { dg-error "dynamic exception spec" "" { target c++17 } decl } +// { dg-warning "dynamic exception spec" "" { target { c++11 && { ! c++17 } } } decl } +// { dg-warning "different exception spec" "" { target { c++11 && { ! c++17 } } } decl }