[libcxx-commits] [libcxx] [libcxx] Use __libcpp_verbose_abort for error messages (PR #108873)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Sep 16 12:10:40 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Petr Hosek (petrhosek)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/108873.diff
3 Files Affected:
- (modified) libcxx/src/support/runtime/exception_fallback.ipp (+4-7)
- (modified) libcxx/src/support/runtime/exception_msvc.ipp (+3-6)
- (modified) libcxx/src/support/runtime/exception_pointer_unimplemented.ipp (+8-16)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/108873
More information about the libcxx-commits
mailing list