]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: be permissive about eh spec mismatch for op new
authorJason Merrill <jason@redhat.com>
Thu, 9 Jan 2025 20:12:07 +0000 (15:12 -0500)
committerJason Merrill <jason@redhat.com>
Thu, 9 Jan 2025 21:23:05 +0000 (16:23 -0500)
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
gcc/testsuite/g++.dg/cpp0x/noexcept88.C [new file with mode: 0644]

index 288da65fd8db82ad0fde3ea3e805d969ffbfa9a8..5c6a4996a89edcdc5a0fcb04668507872e635b2b 100644 (file)
@@ -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 (file)
index 0000000..9b57fdd
--- /dev/null
@@ -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 }