[libc++] Generalize the customizeable assertion handler
Instead of taking a fixed set of arguments, use variadics so that
we can pass arbitrary arguments to the handler. This is the first
step towards using the handler to handle other non-assertion-related
failures, like std::unreachable and an exception being thrown in
-fno-exceptions mode, which would improve user experience by including
additional information in crashes (right now, we call abort() without
additional information).
Differential Revision: https://reviews.llvm.org/D130507
NOKEYCHECK=True
GitOrigin-RevId: 7de5aca84c541edd9f11b9316cb61c5f350ee724
diff --git a/docs/UsingLibcxx.rst b/docs/UsingLibcxx.rst
index aa58120..40a8d9a 100644
--- a/docs/UsingLibcxx.rst
+++ b/docs/UsingLibcxx.rst
@@ -163,7 +163,7 @@
.. code-block:: cpp
- void __libcpp_assertion_handler(char const* file, int line, char const* expression, char const* message)
+ void __libcpp_assertion_handler(char const* format, ...)
This mechanism is similar to how one can replace the default definition of ``operator new``
and ``operator delete``. For example:
@@ -173,8 +173,12 @@
// In HelloWorldHandler.cpp
#include <version> // must include any libc++ header before defining the handler (C compatibility headers excluded)
- void std::__libcpp_assertion_handler(char const* file, int line, char const* expression, char const* message) {
- std::printf("Assertion %s failed at %s:%d, more info: %s", expression, file, line, message);
+ void std::__libcpp_assertion_handler(char const* format, ...) {
+ va_list list;
+ va_start(list, format);
+ std::vfprintf(stderr, format, list);
+ va_end(list);
+
std::abort();
}