weimingz created this revision.
weimingz added reviewers: rmaprath, grandinj.
weimingz added a subscriber: cfe-commits.

Add macros to wrapper for throw or assert.

http://reviews.llvm.org/D21706

Files:
  include/__config
  include/__locale
  include/array
  include/experimental/optional
  include/fstream

Index: include/fstream
===================================================================
--- include/fstream
+++ include/fstream
@@ -618,10 +618,8 @@
             size_t __nr = fread((void*)__extbufnext_, 1, __nmemb, __file_);
             if (__nr != 0)
             {
-#ifndef _LIBCPP_NO_EXCEPTIONS
                 if (!__cv_)
-                    throw bad_cast();
-#endif
+                    _LIBCXX_THROW_OR_ASSERT(bad_cast(), "bad cast");
                 __extbufend_ = __extbufnext_ + __nr;
                 char_type*  __inext;
                 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
Index: include/experimental/optional
===================================================================
--- include/experimental/optional
+++ include/experimental/optional
@@ -517,11 +517,7 @@
     constexpr value_type const& value() const
     {
         if (!this->__engaged_)
-#ifndef _LIBCPP_NO_EXCEPTIONS
-            throw bad_optional_access();
-#else
-            assert(!"bad optional access");
-#endif
+          _LIBCXX_THROW_OR_ASSERT(bad_optional_access(), "bad optional access");
         return this->__val_;
     }
 
@@ -529,11 +525,7 @@
     value_type& value()
     {
         if (!this->__engaged_)
-#ifndef _LIBCPP_NO_EXCEPTIONS
-            throw bad_optional_access();
-#else
-            assert(!"bad optional access");
-#endif
+            _LIBCXX_THROW_OR_ASSERT(bad_optional_access(), "bad optional access");
         return this->__val_;
     }
 
Index: include/array
===================================================================
--- include/array
+++ include/array
@@ -108,9 +108,6 @@
 #include <iterator>
 #include <algorithm>
 #include <stdexcept>
-#if defined(_LIBCPP_NO_EXCEPTIONS)
-    #include <cassert>
-#endif
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #pragma GCC system_header
@@ -209,11 +206,7 @@
 array<_Tp, _Size>::at(size_type __n)
 {
     if (__n >= _Size)
-#ifndef _LIBCPP_NO_EXCEPTIONS
-        throw out_of_range("array::at");
-#else
-        assert(!"array::at out_of_range");
-#endif
+        _LIBCXX_THROW_OR_ASSERT(out_of_range("array::at"), "array::at out_of_range");
     return __elems_[__n];
 }
 
Index: include/__locale
===================================================================
--- include/__locale
+++ include/__locale
@@ -165,10 +165,8 @@
 locale
 locale::combine(const locale& __other) const
 {
-#ifndef _LIBCPP_NO_EXCEPTIONS
     if (!_VSTD::has_facet<_Facet>(__other))
-        throw runtime_error("locale::combine: locale missing facet");
-#endif  // _LIBCPP_NO_EXCEPTIONS
+        _LIBCXX_THROW_OR_ASSERT_RUNTIME("locale::combine: locale missing facet");
     return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
 }
 
Index: include/__config
===================================================================
--- include/__config
+++ include/__config
@@ -890,6 +890,18 @@
 #define _LIBCPP_HAS_THREAD_SAFETY_ANNOTATIONS
 #endif
 
+#if (defined(_LIBCPP_NO_EXCEPTIONS))
+#include <cassert>
+#define _LIBCXX_THROW_OR_ASSERT(e, msg) assert(! msg) // generic wrapper.
+#define _LIBCXX_THROW_OR_ASSERT_MSG(e, msg) assert (! msg) // same msg for exception and assert.
+#define _LIBCXX_THROW_OR_ASSERT_RUNTIME(msg) assert(! msg) // wrapper for runtime error.
+#else
+#define _LIBCXX_THROW_OR_ASSERT(e, msg) throw e
+#define _LIBCXX_THROW_OR_ASSERT_MSG(e, msg) throw e(msg)
+#define _LIBCXX_THROW_OR_ASSERT_RUNTIME(msg)  _LIBCXX_THROW_OR_ASSERT_MSG(runtime_error, msg)
+
+#endif
+
 #endif // __cplusplus
 
 #endif // _LIBCPP_CONFIG
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to