diff options
author | Eric Fiselier <eric@efcs.ca> | 2016-12-28 04:58:52 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2016-12-28 04:58:52 +0000 |
commit | 01eb99ac7bb111520d011748fb9fff176d2e64d7 (patch) | |
tree | ddacda4800b22c6a0f9266bc5d6290ff45badf6f /src/debug.cpp | |
parent | 046a71be67e5c65c3425a85416edf852fc7d1887 (diff) |
Implement a throwing version of _LIBCPP_ASSERT.
This patch implements changes to allow _LIBCPP_ASSERT to throw on failure
instead of aborting. The main changes needed to do this are:
1. Change _LIBCPP_ASSERT to call a handler via a replacable function pointer
instead of calling abort directly. Additionally this patch implements two
handler functions, one which aborts and another that throws an exception.
2. Add _NOEXCEPT_DEBUG macro for disabling noexcept spec on function which
contain _LIBCPP_ASSERT. This is required in order to prevent assertion
failures throwing through a noexcept function. This macro has no effect
unless _LIBCPP_DEBUG_USE_EXCEPTIONS is defined.
Having a non-aborting _LIBCPP_ASSERT is very important to allow sane testing of
debug mode. Currently we can only have one test case per file, since the test
case will cause the program to abort. Testing debug mode this way would require
thousands of test files, most of which would be 95% boiler plate. I don't think
this is a feasible strategy. Fortunately using a throwing debug handler solves
these issues.
Additionally this patch rewrites the documentation for debug mode.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@290651 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'src/debug.cpp')
-rw-r--r-- | src/debug.cpp | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/src/debug.cpp b/src/debug.cpp index d46935cc9..fe27dc82c 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -7,16 +7,75 @@ // //===----------------------------------------------------------------------===// -#define _LIBCPP_DEBUG 1 #include "__config" #include "__debug" #include "functional" #include "algorithm" +#include "string" +#include "cstdio" #include "__hash_table" #include "mutex" _LIBCPP_BEGIN_NAMESPACE_STD +static std::string make_what_str(__libcpp_debug_info const& info) { + string msg = info.__file_; + msg += ":" + to_string(info.__line_) + ": _LIBCPP_ASSERT '"; + msg += info.__pred_; + msg += "' failed. "; + msg += info.__msg_; + return msg; +} + +_LIBCPP_SAFE_STATIC __libcpp_debug_function_type + __libcpp_debug_function = __libcpp_abort_debug_function; + +bool __libcpp_set_debug_function(__libcpp_debug_function_type __func) { + __libcpp_debug_function = __func; + return true; +} + +_LIBCPP_NORETURN void __libcpp_abort_debug_function(__libcpp_debug_info const& info) { + std::fprintf(stderr, "%s\n", make_what_str(info).c_str()); + std::abort(); +} + +_LIBCPP_NORETURN void __libcpp_throw_debug_function(__libcpp_debug_info const& info) { + throw __libcpp_debug_exception(info); +} + +struct __libcpp_debug_exception::__libcpp_debug_exception_imp { + __libcpp_debug_info __info_; + std::string __what_str_; +}; + +__libcpp_debug_exception::__libcpp_debug_exception() _NOEXCEPT + : __imp_(nullptr) { +} + +__libcpp_debug_exception::__libcpp_debug_exception( + __libcpp_debug_info const& info) : __imp_(new __libcpp_debug_exception_imp) +{ + __imp_->__info_ = info; + __imp_->__what_str_ = make_what_str(info); +} +__libcpp_debug_exception::__libcpp_debug_exception( + __libcpp_debug_exception const& other) : __imp_(nullptr) { + if (other.__imp_) + __imp_ = new __libcpp_debug_exception_imp(*other.__imp_); +} + +__libcpp_debug_exception::~__libcpp_debug_exception() _NOEXCEPT { + if (__imp_) + delete __imp_; +} + +const char* __libcpp_debug_exception::what() const _NOEXCEPT { + if (__imp_) + return __imp_->__what_str_.c_str(); + return "__libcpp_debug_exception"; +} + _LIBCPP_FUNC_VIS __libcpp_db* __get_db() |