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.

llvm-svn: 290651
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: 687d3213f0c26fdd06f54544ef74cfd4d628a35a
diff --git a/src/debug.cpp b/src/debug.cpp
index d46935c..fe27dc8 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()