[libcxx-commits] [libcxx] [libcxx] Use __libcpp_verbose_abort for error messages (PR #108873)

Petr Hosek via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 16 12:05:59 PDT 2024


https://github.com/petrhosek created https://github.com/llvm/llvm-project/pull/108873

Rather than using the following sequence of calls:

```
fprintf(stderr, "...");
::abort();
```

We should use the following:

```
__libcpp_verbose_abort("...")
```

This simplifies the code and ensures the behavior is consistent across all call sites. Since `__libcpp_verbose_abort` is defined as weak, it also allows users to provide their implementation if they want to change the behavior which may be helpful in environments like embedded where error reporting usually requires special handling.

>From fcaed4301d88c6be8f77a8fdcee2e6e8e893c57e Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Mon, 16 Sep 2024 11:35:29 -0700
Subject: [PATCH] [libcxx] Use __libcpp_verbose_abort for error messages

Rather than using the following sequence of calls:

```
fprintf(stderr, "...");
::abort();
```

We should use the following:

```
__libcpp_verbose_abort("...")
```

This simplifies the code and ensures the behavior is consistent across
all call sites. Since `__libcpp_verbose_abort` is defined as weak, it
also allows users to provide their implementation if they want to change
the behavior which may be helpful in environments like embedded where
error reporting usually requires special handling.
---
 .../support/runtime/exception_fallback.ipp    | 11 ++++-----
 libcxx/src/support/runtime/exception_msvc.ipp |  9 +++----
 .../exception_pointer_unimplemented.ipp       | 24 +++++++------------
 3 files changed, 15 insertions(+), 29 deletions(-)

diff --git a/libcxx/src/support/runtime/exception_fallback.ipp b/libcxx/src/support/runtime/exception_fallback.ipp
index ca542c9497214f..bedb16e58566bc 100644
--- a/libcxx/src/support/runtime/exception_fallback.ipp
+++ b/libcxx/src/support/runtime/exception_fallback.ipp
@@ -7,7 +7,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <cstdio>
+#include <__verbose_abort>
 
 namespace std {
 
@@ -39,13 +39,11 @@ terminate_handler get_terminate() noexcept { return __libcpp_atomic_load(&__term
 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
     (*get_terminate())();
     // handler should not return
-    fprintf(stderr, "terminate_handler unexpectedly returned\n");
-    ::abort();
+    __libcpp_verbose_abort("terminate_handler unexpectedly returned\n");
 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
   } catch (...) {
     // handler should not throw exception
-    fprintf(stderr, "terminate_handler unexpectedly threw an exception\n");
-    ::abort();
+    __libcpp_verbose_abort("terminate_handler unexpectedly threw an exception\n");
   }
 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
 }
@@ -54,8 +52,7 @@ bool uncaught_exception() noexcept { return uncaught_exceptions() > 0; }
 
 int uncaught_exceptions() noexcept {
 #warning uncaught_exception not yet implemented
-  fprintf(stderr, "uncaught_exceptions not yet implemented\n");
-  ::abort();
+  __libcpp_verbose_abort("uncaught_exceptions not yet implemented\n");
 }
 
 exception::~exception() noexcept {}
diff --git a/libcxx/src/support/runtime/exception_msvc.ipp b/libcxx/src/support/runtime/exception_msvc.ipp
index 163aec057d9b5c..869b64d2f533e4 100644
--- a/libcxx/src/support/runtime/exception_msvc.ipp
+++ b/libcxx/src/support/runtime/exception_msvc.ipp
@@ -11,8 +11,7 @@
 #  error this header can only be used when targeting the MSVC ABI
 #endif
 
-#include <stdio.h>
-#include <stdlib.h>
+#include <__verbose_abort>
 
 extern "C" {
 typedef void(__cdecl* terminate_handler)();
@@ -48,13 +47,11 @@ terminate_handler get_terminate() noexcept { return ::_get_terminate(); }
 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
     (*get_terminate())();
     // handler should not return
-    fprintf(stderr, "terminate_handler unexpectedly returned\n");
-    ::abort();
+    __libcpp_verbose_abort("terminate_handler unexpectedly returned\n");
 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
   } catch (...) {
     // handler should not throw exception
-    fprintf(stderr, "terminate_handler unexpectedly threw an exception\n");
-    ::abort();
+    __libcpp_verbose_abort("terminate_handler unexpectedly threw an exception\n");
   }
 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
 }
diff --git a/libcxx/src/support/runtime/exception_pointer_unimplemented.ipp b/libcxx/src/support/runtime/exception_pointer_unimplemented.ipp
index 1fe3127f18b0bb..05a71ce34e5ac8 100644
--- a/libcxx/src/support/runtime/exception_pointer_unimplemented.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_unimplemented.ipp
@@ -7,33 +7,28 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <stdio.h>
-#include <stdlib.h>
+#include <__verbose_abort>
 
 namespace std {
 
 exception_ptr::~exception_ptr() noexcept {
 #warning exception_ptr not yet implemented
-  fprintf(stderr, "exception_ptr not yet implemented\n");
-  ::abort();
+  __libcpp_verbose_abort("exception_ptr not yet implemented\n");
 }
 
 exception_ptr::exception_ptr(const exception_ptr& other) noexcept : __ptr_(other.__ptr_) {
 #warning exception_ptr not yet implemented
-  fprintf(stderr, "exception_ptr not yet implemented\n");
-  ::abort();
+  __libcpp_verbose_abort("exception_ptr not yet implemented\n");
 }
 
 exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept {
 #warning exception_ptr not yet implemented
-  fprintf(stderr, "exception_ptr not yet implemented\n");
-  ::abort();
+  __libcpp_verbose_abort("exception_ptr not yet implemented\n");
 }
 
 exception_ptr exception_ptr::__from_native_exception_pointer(void *__e) noexcept {
 #warning exception_ptr not yet implemented
-  fprintf(stderr, "exception_ptr not yet implemented\n");
-  ::abort();
+  __libcpp_verbose_abort("exception_ptr not yet implemented\n");
 }
 
 nested_exception::nested_exception() noexcept : __ptr_(current_exception()) {}
@@ -46,8 +41,7 @@ nested_exception::~nested_exception() noexcept {}
 
 [[noreturn]] void nested_exception::rethrow_nested() const {
 #warning exception_ptr not yet implemented
-  fprintf(stderr, "exception_ptr not yet implemented\n");
-  ::abort();
+  __libcpp_verbose_abort("exception_ptr not yet implemented\n");
 #if 0
   if (__ptr_ == nullptr)
       terminate();
@@ -57,14 +51,12 @@ nested_exception::~nested_exception() noexcept {}
 
 exception_ptr current_exception() noexcept {
 #warning exception_ptr not yet implemented
-  fprintf(stderr, "exception_ptr not yet implemented\n");
-  ::abort();
+  __libcpp_verbose_abort("exception_ptr not yet implemented\n");
 }
 
 [[noreturn]] void rethrow_exception(exception_ptr p) {
 #warning exception_ptr not yet implemented
-  fprintf(stderr, "exception_ptr not yet implemented\n");
-  ::abort();
+  __libcpp_verbose_abort("exception_ptr not yet implemented\n");
 }
 
 } // namespace std



More information about the libcxx-commits mailing list