Skip to content

Commit

Permalink
[libcxx] Use __libcpp_verbose_abort for error messages
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
petrhosek committed Sep 16, 2024
1 parent 9fc789d commit fcaed43
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
11 changes: 4 additions & 7 deletions libcxx/src/support/runtime/exception_fallback.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//
//===----------------------------------------------------------------------===//

#include <cstdio>
#include <__verbose_abort>

namespace std {

Expand Down Expand Up @@ -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
}
Expand All @@ -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 {}
Expand Down
9 changes: 3 additions & 6 deletions libcxx/src/support/runtime/exception_msvc.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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)();
Expand Down Expand Up @@ -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
}
Expand Down
24 changes: 8 additions & 16 deletions libcxx/src/support/runtime/exception_pointer_unimplemented.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {}
Expand All @@ -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();
Expand All @@ -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

0 comments on commit fcaed43

Please sign in to comment.