c++: be permissive about eh spec mismatch for op new

Message ID 20250109212342.4075856-1-jason@redhat.com
State New
Headers
Series c++: be permissive about eh spec mismatch for op new |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply

Commit Message

Jason Merrill Jan. 9, 2025, 9:23 p.m. UTC
  Tested x86_64-pc-linux-gnu, applying to trunk.

-- 8< --

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.
---
 gcc/cp/decl.cc                          | 11 +++++++++--
 gcc/testsuite/g++.dg/cpp0x/noexcept88.C |  9 +++++++++
 2 files changed, 18 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept88.C


base-commit: 424a9ac45ab1a80359b22ee30d2815fb0f2c5149
  

Patch

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 288da65fd8d..5c6a4996a89 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 00000000000..9b57fddbf0a
--- /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 }