now works with -fno-exceptions and -fno-rtti

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@110828 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/__config b/include/__config
index c3cf8c7..a19b297 100644
--- a/include/__config
+++ b/include/__config
@@ -90,6 +90,10 @@
 #define _LIBCPP_NO_EXCEPTIONS
 #endif
 
+#if !(__has_feature(cxx_rtti))
+#define _LIBCPP_NO_RTTI
+#endif
+
 #define _LIBCPP_HAS_NO_ADVANCED_SFINAE
 #define _LIBCPP_HAS_NO_STRONG_USING
 #define _LIBCPP_HAS_NO_TEMPLATE_ALIASES
@@ -139,6 +143,10 @@
 #define _LIBCPP_NO_EXCEPTIONS
 #endif
 
+#ifndef __GXX_RTTI
+#define _LIBCPP_NO_RTTI
+#endif
+
 #define _LIBCPP_HAS_NO_TEMPLATE_ALIASES
 
 #ifndef __GXX_EXPERIMENTAL_CXX0X__
diff --git a/include/__functional_03 b/include/__functional_03
index ffbb165..fa318df 100644
--- a/include/__functional_03
+++ b/include/__functional_03
@@ -243,8 +243,10 @@
     virtual void destroy() = 0;
     virtual void destroy_deallocate() = 0;
     virtual _R operator()() = 0;
+#ifndef _LIBCPP_NO_RTTI
     virtual const void* target(const type_info&) const = 0;
     virtual const std::type_info& target_type() const = 0;
+#endif
 };
 
 template<class _R, class _A0>
@@ -260,8 +262,10 @@
     virtual void destroy() = 0;
     virtual void destroy_deallocate() = 0;
     virtual _R operator()(_A0) = 0;
+#ifndef _LIBCPP_NO_RTTI
     virtual const void* target(const type_info&) const = 0;
     virtual const std::type_info& target_type() const = 0;
+#endif
 };
 
 template<class _R, class _A0, class _A1>
@@ -277,8 +281,10 @@
     virtual void destroy() = 0;
     virtual void destroy_deallocate() = 0;
     virtual _R operator()(_A0, _A1) = 0;
+#ifndef _LIBCPP_NO_RTTI
     virtual const void* target(const type_info&) const = 0;
     virtual const std::type_info& target_type() const = 0;
+#endif
 };
 
 template<class _R, class _A0, class _A1, class _A2>
@@ -294,8 +300,10 @@
     virtual void destroy() = 0;
     virtual void destroy_deallocate() = 0;
     virtual _R operator()(_A0, _A1, _A2) = 0;
+#ifndef _LIBCPP_NO_RTTI
     virtual const void* target(const type_info&) const = 0;
     virtual const std::type_info& target_type() const = 0;
+#endif
 };
 
 template<class _FD, class _Alloc, class _FB> class __func;
@@ -313,8 +321,10 @@
     virtual void destroy();
     virtual void destroy_deallocate();
     virtual _R operator()();
+#ifndef _LIBCPP_NO_RTTI
     virtual const void* target(const type_info&) const;
     virtual const std::type_info& target_type() const;
+#endif
 };
 
 template<class _F, class _Alloc, class _R>
@@ -360,6 +370,8 @@
     return __invoke<_R>(__f_.first());
 }
 
+#ifndef _LIBCPP_NO_RTTI
+
 template<class _F, class _Alloc, class _R>
 const void*
 __func<_F, _Alloc, _R()>::target(const type_info& __ti) const
@@ -376,6 +388,8 @@
     return typeid(_F);
 }
 
+#endif
+
 template<class _F, class _Alloc, class _R, class _A0>
 class __func<_F, _Alloc, _R(_A0)>
     : public  __base<_R(_A0)>
@@ -389,8 +403,10 @@
     virtual void destroy();
     virtual void destroy_deallocate();
     virtual _R operator()(_A0);
+#ifndef _LIBCPP_NO_RTTI
     virtual const void* target(const type_info&) const;
     virtual const std::type_info& target_type() const;
+#endif
 };
 
 template<class _F, class _Alloc, class _R, class _A0>
@@ -436,6 +452,8 @@
     return __invoke(__f_.first(), __a0);
 }
 
+#ifndef _LIBCPP_NO_RTTI
+
 template<class _F, class _Alloc, class _R, class _A0>
 const void*
 __func<_F, _Alloc, _R(_A0)>::target(const type_info& __ti) const
@@ -452,6 +470,8 @@
     return typeid(_F);
 }
 
+#endif
+
 template<class _F, class _Alloc, class _R, class _A0, class _A1>
 class __func<_F, _Alloc, _R(_A0, _A1)>
     : public  __base<_R(_A0, _A1)>
@@ -465,8 +485,10 @@
     virtual void destroy();
     virtual void destroy_deallocate();
     virtual _R operator()(_A0, _A1);
+#ifndef _LIBCPP_NO_RTTI
     virtual const void* target(const type_info&) const;
     virtual const std::type_info& target_type() const;
+#endif
 };
 
 template<class _F, class _Alloc, class _R, class _A0, class _A1>
@@ -512,6 +534,8 @@
     return __invoke(__f_.first(), __a0, __a1);
 }
 
+#ifndef _LIBCPP_NO_RTTI
+
 template<class _F, class _Alloc, class _R, class _A0, class _A1>
 const void*
 __func<_F, _Alloc, _R(_A0, _A1)>::target(const type_info& __ti) const
@@ -528,6 +552,8 @@
     return typeid(_F);
 }
 
+#endif
+
 template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
 class __func<_F, _Alloc, _R(_A0, _A1, _A2)>
     : public  __base<_R(_A0, _A1, _A2)>
@@ -541,8 +567,10 @@
     virtual void destroy();
     virtual void destroy_deallocate();
     virtual _R operator()(_A0, _A1, _A2);
+#ifndef _LIBCPP_NO_RTTI
     virtual const void* target(const type_info&) const;
     virtual const std::type_info& target_type() const;
+#endif
 };
 
 template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
@@ -588,6 +616,8 @@
     return __invoke(__f_.first(), __a0, __a1, __a2);
 }
 
+#ifndef _LIBCPP_NO_RTTI
+
 template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
 const void*
 __func<_F, _Alloc, _R(_A0, _A1, _A2)>::target(const type_info& __ti) const
@@ -604,6 +634,8 @@
     return typeid(_F);
 }
 
+#endif
+
 }  // __function
 
 template<class _R>
@@ -669,10 +701,12 @@
     // 20.7.16.2.4, function invocation:
     _R operator()() const;
 
+#ifndef _LIBCPP_NO_RTTI
     // 20.7.16.2.5, function target access:
     const std::type_info& target_type() const;
     template <typename _T> _T* target();
     template <typename _T> const _T* target() const;
+#endif
 };
 
 template<class _R>
@@ -797,11 +831,15 @@
 _R
 function<_R()>::operator()() const
 {
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__f_ == 0)
         throw bad_function_call();
+#endif
     return (*__f_)();
 }
 
+#ifndef _LIBCPP_NO_RTTI
+
 template<class _R>
 const std::type_info&
 function<_R()>::target_type() const
@@ -831,6 +869,8 @@
     return (const _T*)__f_->target(typeid(_T));
 }
 
+#endif
+
 template<class _R, class _A0>
 class function<_R(_A0)>
     : public unary_function<_A0, _R>
@@ -905,10 +945,12 @@
     // 20.7.16.2.4, function invocation:
     _R operator()(_A0) const;
 
+#ifndef _LIBCPP_NO_RTTI
     // 20.7.16.2.5, function target access:
     const std::type_info& target_type() const;
     template <typename _T> _T* target();
     template <typename _T> const _T* target() const;
+#endif
 };
 
 template<class _R, class _A0>
@@ -1033,11 +1075,15 @@
 _R
 function<_R(_A0)>::operator()(_A0 __a0) const
 {
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__f_ == 0)
         throw bad_function_call();
+#endif
     return (*__f_)(__a0);
 }
 
+#ifndef _LIBCPP_NO_RTTI
+
 template<class _R, class _A0>
 const std::type_info&
 function<_R(_A0)>::target_type() const
@@ -1067,6 +1113,8 @@
     return (const _T*)__f_->target(typeid(_T));
 }
 
+#endif
+
 template<class _R, class _A0, class _A1>
 class function<_R(_A0, _A1)>
     : public binary_function<_A0, _A1, _R>
@@ -1141,10 +1189,12 @@
     // 20.7.16.2.4, function invocation:
     _R operator()(_A0, _A1) const;
 
+#ifndef _LIBCPP_NO_RTTI
     // 20.7.16.2.5, function target access:
     const std::type_info& target_type() const;
     template <typename _T> _T* target();
     template <typename _T> const _T* target() const;
+#endif
 };
 
 template<class _R, class _A0, class _A1>
@@ -1269,11 +1319,15 @@
 _R
 function<_R(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
 {
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__f_ == 0)
         throw bad_function_call();
+#endif
     return (*__f_)(__a0, __a1);
 }
 
+#ifndef _LIBCPP_NO_RTTI
+
 template<class _R, class _A0, class _A1>
 const std::type_info&
 function<_R(_A0, _A1)>::target_type() const
@@ -1303,6 +1357,8 @@
     return (const _T*)__f_->target(typeid(_T));
 }
 
+#endif
+
 template<class _R, class _A0, class _A1, class _A2>
 class function<_R(_A0, _A1, _A2)>
 {
@@ -1376,10 +1432,12 @@
     // 20.7.16.2.4, function invocation:
     _R operator()(_A0, _A1, _A2) const;
 
+#ifndef _LIBCPP_NO_RTTI
     // 20.7.16.2.5, function target access:
     const std::type_info& target_type() const;
     template <typename _T> _T* target();
     template <typename _T> const _T* target() const;
+#endif
 };
 
 template<class _R, class _A0, class _A1, class _A2>
@@ -1504,11 +1562,15 @@
 _R
 function<_R(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
 {
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__f_ == 0)
         throw bad_function_call();
+#endif
     return (*__f_)(__a0, __a1, __a2);
 }
 
+#ifndef _LIBCPP_NO_RTTI
+
 template<class _R, class _A0, class _A1, class _A2>
 const std::type_info&
 function<_R(_A0, _A1, _A2)>::target_type() const
@@ -1538,6 +1600,8 @@
     return (const _T*)__f_->target(typeid(_T));
 }
 
+#endif
+
 template <class _F> 
 inline _LIBCPP_INLINE_VISIBILITY
 bool
diff --git a/include/__locale b/include/__locale
index c2146d9..743b393 100644
--- a/include/__locale
+++ b/include/__locale
@@ -134,8 +134,10 @@
 locale
 locale::combine(const locale& __other) const
 {
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (!_STD::has_facet<_Facet>(__other))
         throw runtime_error("locale::combine: locale missing facet");
+#endif
     return locale(*this, &const_cast<_Facet&>(_STD::use_facet<_Facet>(__other)));
 }
 
diff --git a/include/__tree b/include/__tree
index 53c1e1f..18363dc 100644
--- a/include/__tree
+++ b/include/__tree
@@ -1102,8 +1102,10 @@
     if (size() != 0)
     {
         __node_pointer __cache = __detach();
+#ifndef _LIBCPP_NO_EXCEPTIONS
         try
         {
+#endif
             for (; __cache != nullptr && __first != __last; ++__first)
             {
                 __cache->__value_ = *__first;
@@ -1111,6 +1113,7 @@
                 __node_insert_unique(__cache);
                 __cache = __next;
             }
+#ifndef _LIBCPP_NO_EXCEPTIONS
         }
         catch (...)
         {
@@ -1119,6 +1122,7 @@
             destroy(__cache);
             throw;
         }
+#endif
         if (__cache != nullptr)
         {
             while (__cache->__parent_ != nullptr)
@@ -1138,8 +1142,10 @@
     if (size() != 0)
     {
         __node_pointer __cache = __detach();
+#ifndef _LIBCPP_NO_EXCEPTIONS
         try
         {
+#endif
             for (; __cache != nullptr && __first != __last; ++__first)
             {
                 __cache->__value_ = *__first;
@@ -1147,6 +1153,7 @@
                 __node_insert_multi(__cache);
                 __cache = __next;
             }
+#ifndef _LIBCPP_NO_EXCEPTIONS
         }
         catch (...)
         {
@@ -1155,6 +1162,7 @@
             destroy(__cache);
             throw;
         }
+#endif
         if (__cache != nullptr)
         {
             while (__cache->__parent_ != nullptr)
@@ -1253,8 +1261,10 @@
         if (size() != 0)
         {
             __node_pointer __cache = __detach();
+#ifndef _LIBCPP_NO_EXCEPTIONS
             try
             {
+#endif
                 while (__cache != nullptr && __t.size() != 0)
                 {
                     __cache->__value_ = _STD::move(__t.remove(__t.begin())->__value_);
@@ -1262,6 +1272,7 @@
                     __node_insert_multi(__cache);
                     __cache = __next;
                 }
+#ifndef _LIBCPP_NO_EXCEPTIONS
             }
             catch (...)
             {
@@ -1270,6 +1281,7 @@
                 destroy(__cache);
                 throw;
             }
+#endif
             if (__cache != nullptr)
             {
                 while (__cache->__parent_ != nullptr)
diff --git a/include/exception b/include/exception
index 51e8313..772ada4 100644
--- a/include/exception
+++ b/include/exception
@@ -141,6 +141,7 @@
 exception_ptr
 make_exception_ptr(_E __e)
 {
+#ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
         throw __e;
@@ -149,6 +150,7 @@
     {
         return current_exception();
     }
+#endif
 }
 
 // nested_exception
@@ -188,7 +190,9 @@
                                     >::type* = 0)
 #endif
 {
+#ifndef _LIBCPP_NO_EXCEPTIONS
     throw __nested<typename remove_reference<_Tp>::type>(_STD::forward<_Tp>(__t));
+#endif
 }
 
 template <class _Tp>
@@ -204,7 +208,9 @@
                                     >::type* = 0)
 #endif
 {
+#ifndef _LIBCPP_NO_EXCEPTIONS
     throw _STD::forward<_Tp>(__t);
+#endif
 }
 
 template <class _E>
diff --git a/include/functional b/include/functional
index e645a55..430f325 100644
--- a/include/functional
+++ b/include/functional
@@ -1000,8 +1000,10 @@
     virtual void destroy() = 0;
     virtual void destroy_deallocate() = 0;
     virtual _R operator()(_ArgTypes&& ...) = 0;
+#ifndef _LIBCPP_NO_RTTI
     virtual const void* target(const type_info&) const = 0;
     virtual const std::type_info& target_type() const = 0;
+#endif
 };
 
 template<class _FD, class _Alloc, class _FB> class __func;
@@ -1019,8 +1021,10 @@
     virtual void destroy();
     virtual void destroy_deallocate();
     virtual _R operator()(_ArgTypes&& ... __arg);
+#ifndef _LIBCPP_NO_RTTI
     virtual const void* target(const type_info&) const;
     virtual const std::type_info& target_type() const;
+#endif
 };
 
 template<class _F, class _Alloc, class _R, class ..._ArgTypes>
@@ -1066,6 +1070,8 @@
     return __invoke(__f_.first(), _STD::forward<_ArgTypes>(__arg)...);
 }
 
+#ifndef _LIBCPP_NO_RTTI
+
 template<class _F, class _Alloc, class _R, class ..._ArgTypes>
 const void*
 __func<_F, _Alloc, _R(_ArgTypes...)>::target(const type_info& __ti) const
@@ -1082,6 +1088,8 @@
     return typeid(_F);
 }
 
+#endif
+
 }  // __function
 
 template<class _R, class ..._ArgTypes>
@@ -1163,10 +1171,12 @@
     // 20.7.16.2.4, function invocation:
     _R operator()(_ArgTypes...) const;
 
+#ifndef _LIBCPP_NO_RTTI
     // 20.7.16.2.5, function target access:
     const std::type_info& target_type() const;
     template <typename _T> _T* target();
     template <typename _T> const _T* target() const;
+#endif
 };
 
 template<class _R, class ..._ArgTypes>
@@ -1331,11 +1341,15 @@
 _R
 function<_R(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
 {
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__f_ == 0)
         throw bad_function_call();
+#endif
     return (*__f_)(_STD::forward<_ArgTypes>(__arg)...);
 }
 
+#ifndef _LIBCPP_NO_RTTI
+
 template<class _R, class ..._ArgTypes>
 const std::type_info&
 function<_R(_ArgTypes...)>::target_type() const
@@ -1365,6 +1379,8 @@
     return (const _T*)__f_->target(typeid(_T));
 }
 
+#endif
+
 template <class _R, class... _ArgTypes> 
 inline _LIBCPP_INLINE_VISIBILITY
 bool
diff --git a/include/locale b/include/locale
index f48a75e..b86143e 100644
--- a/include/locale
+++ b/include/locale
@@ -3690,8 +3690,10 @@
         if (__r == codecvt_base::ok)
             return __ws;
     }
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__wide_err_string_.empty())
         throw range_error("wstring_convert: from_bytes error");
+#endif
     return __wide_err_string_;
 }
 
@@ -3776,8 +3778,10 @@
                 return __bs;
         }
     }
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__byte_err_string_.empty())
         throw range_error("wstring_convert: to_bytes error");
+#endif
     return __byte_err_string_;
 }
 
diff --git a/include/map b/include/map
index 3998fdf..fd06c21 100644
--- a/include/map
+++ b/include/map
@@ -1136,8 +1136,10 @@
 {
     __node_base_pointer __parent;
     __node_base_pointer& __child = __find_equal_key(__parent, __k);
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__child == nullptr)
         throw out_of_range("map::at:  key not found");
+#endif
     return static_cast<__node_pointer>(__child)->__value_.second;
 }
 
@@ -1147,8 +1149,10 @@
 {
     __node_base_const_pointer __parent;
     __node_base_const_pointer __child = __find_equal_key(__parent, __k);
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__child == nullptr)
         throw out_of_range("map::at:  key not found");
+#endif
     return static_cast<__node_const_pointer>(__child)->__value_.second;
 }
 
diff --git a/include/memory b/include/memory
index 3358a19..5aa2fad 100644
--- a/include/memory
+++ b/include/memory
@@ -2642,7 +2642,9 @@
     long use_count() const {return __shared_count::use_count();}
     __shared_weak_count* lock();
 
+#ifndef _LIBCPP_NO_RTTI
     virtual const void* __get_deleter(const type_info&) const;
+#endif
 private:
     virtual void __on_zero_shared_weak() = 0;
 };
@@ -2656,13 +2658,17 @@
     __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
         :  __data_(__compressed_pair<_Tp, _Dp>(__p, _STD::move(__d)), _STD::move(__a)) {}
 
+#ifndef _LIBCPP_NO_RTTI
     virtual const void* __get_deleter(const type_info&) const;
+#endif
 
 private:
     virtual void __on_zero_shared();
     virtual void __on_zero_shared_weak();
 };
 
+#ifndef _LIBCPP_NO_RTTI
+
 template <class _Tp, class _Dp, class _Alloc>
 const void*
 __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const
@@ -2670,6 +2676,8 @@
     return __t == typeid(_Dp) ? &__data_.first().second() : 0;
 }
 
+#endif
+
 template <class _Tp, class _Dp, class _Alloc>
 void
 __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared()
@@ -2834,9 +2842,11 @@
     template <class _U> bool owner_before(weak_ptr<_U> const& __p) const
         {return __cntrl_ < __p.__cntrl_;}
 
+#ifndef _LIBCPP_NO_RTTI
     template <class _Dp>
         _Dp* __get_deleter() const
             {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
+#endif
 
 #ifndef _LIBCPP_HAS_NO_VARIADICS
 
@@ -3591,6 +3601,8 @@
     return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
 }
 
+#ifndef _LIBCPP_NO_RTTI
+
 template<class _Dp, class _Tp>
 inline _LIBCPP_INLINE_VISIBILITY
 _Dp*
@@ -3599,6 +3611,8 @@
     return __p.template __get_deleter<_Dp>();
 }
 
+#endif
+
 template<class _Tp>
 class weak_ptr
 {
diff --git a/include/regex b/include/regex
index 51ba1ea..be49cd2 100644
--- a/include/regex
+++ b/include/regex
@@ -2263,8 +2263,10 @@
             }
             else
             {
+#ifndef _LIBCPP_NO_EXCEPTIONS
                 if (__b.size() != 1 || __e.size() != 1)
                     throw regex_error(regex_constants::error_collate);
+#endif
                 if (__icase_)
                 {
                     __b[0] = __traits_.translate_nocase(__b[0]);
@@ -2894,8 +2896,10 @@
     case egrep:
         __first = __parse_egrep(__first, __last);
         break;
+#ifndef _LIBCPP_NO_EXCEPTIONS
     default:
         throw regex_error(regex_constants::__re_err_grammar);
+#endif
     }
     return __first;
 }
@@ -2926,8 +2930,10 @@
                 }
             }
         }
+#ifndef _LIBCPP_NO_EXCEPTIONS
         if (__first != __last)
             throw regex_error(regex_constants::__re_err_empty);
+#endif
     }
     return __first;
 }
@@ -2940,15 +2946,19 @@
 {
     __owns_one_state<_CharT>* __sa = __end_;
     _ForwardIterator __temp = __parse_ERE_branch(__first, __last);
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__temp == __first)
         throw regex_error(regex_constants::__re_err_empty);
+#endif
     __first = __temp;
     while (__first != __last && *__first == '|')
     {
         __owns_one_state<_CharT>* __sb = __end_;
         __temp = __parse_ERE_branch(++__first, __last);
+#ifndef _LIBCPP_NO_EXCEPTIONS
         if (__temp == __first)
             throw regex_error(regex_constants::__re_err_empty);
+#endif
         __push_alternation(__sa, __sb);
         __first = __temp;
     }
@@ -2962,8 +2972,10 @@
                                                  _ForwardIterator __last)
 {
     _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__temp == __first)
         throw regex_error(regex_constants::__re_err_empty);
+#endif
     do
     {
         __first = __temp;
@@ -2998,8 +3010,10 @@
             unsigned __temp_count = __marked_count_;
             ++__open_count_;
             __temp = __parse_extended_reg_exp(++__temp, __last);
+#ifndef _LIBCPP_NO_EXCEPTIONS
             if (__temp == __last || *__temp != ')')
                 throw regex_error(regex_constants::error_paren);
+#endif
             __push_end_marked_subexpression(__temp_count);
             --__open_count_;
             ++__temp;
@@ -3064,8 +3078,10 @@
             unsigned __temp_count = __marked_count_;
             __first = __parse_RE_expression(__temp, __last);
             __temp = __parse_Back_close_paren(__first, __last);
+#ifndef _LIBCPP_NO_EXCEPTIONS
             if (__temp == __first)
                 throw regex_error(regex_constants::error_paren);
+#endif
             __push_end_marked_subexpression(__temp_count);
             __first = __temp;
         }
@@ -3374,16 +3390,22 @@
                 int __min = 0;
                 __first = __temp;
                 __temp = __parse_DUP_COUNT(__first, __last, __min);
+#ifndef _LIBCPP_NO_EXCEPTIONS
                 if (__temp == __first)
                     throw regex_error(regex_constants::error_badbrace);
+#endif
                 __first = __temp;
+#ifndef _LIBCPP_NO_EXCEPTIONS
                 if (__first == __last)
                     throw regex_error(regex_constants::error_brace);
+#endif
                 if (*__first != ',')
                 {
                     __temp = __parse_Back_close_brace(__first, __last);
+#ifndef _LIBCPP_NO_EXCEPTIONS
                     if (__temp == __first)
                         throw regex_error(regex_constants::error_brace);
+#endif
                     __push_loop(__min, __min, __s, __mexp_begin, __mexp_end,
                                     true);
                     __first = __temp;
@@ -3394,14 +3416,18 @@
                     int __max = -1;
                     __first = __parse_DUP_COUNT(__first, __last, __max);
                     __temp = __parse_Back_close_brace(__first, __last);
+#ifndef _LIBCPP_NO_EXCEPTIONS
                     if (__temp == __first)
                         throw regex_error(regex_constants::error_brace);
+#endif
                     if (__max == -1)
                         __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
                     else
                     {
+#ifndef _LIBCPP_NO_EXCEPTIONS
                         if (__max < __min)
                             throw regex_error(regex_constants::error_badbrace);
+#endif
                         __push_loop(__min, __max, __s, __mexp_begin, __mexp_end,
                                     true);
                     }
@@ -3461,11 +3487,15 @@
             {
                 int __min;
                 _ForwardIterator __temp = __parse_DUP_COUNT(++__first, __last, __min);
+#ifndef _LIBCPP_NO_EXCEPTIONS
                 if (__temp == __first)
                     throw regex_error(regex_constants::error_badbrace);
+#endif
                 __first = __temp;
+#ifndef _LIBCPP_NO_EXCEPTIONS
                 if (__first == __last)
                     throw regex_error(regex_constants::error_brace);
+#endif
                 switch (*__first)
                 {
                 case '}':
@@ -3479,8 +3509,11 @@
                         __push_loop(__min, __min, __s, __mexp_begin, __mexp_end);
                     break;
                 case ',':
-                    if (++__first == __last)
+                    ++__first;
+#ifndef _LIBCPP_NO_EXCEPTIONS
+                    if (__first == __last)
                         throw regex_error(regex_constants::error_badbrace);
+#endif
                     if (*__first == '}')
                     {
                         ++__first;
@@ -3496,14 +3529,20 @@
                     {
                         int __max = -1;
                         __temp = __parse_DUP_COUNT(__first, __last, __max);
+#ifndef _LIBCPP_NO_EXCEPTIONS
                         if (__temp == __first)
                             throw regex_error(regex_constants::error_brace);
+#endif
                         __first = __temp;
+#ifndef _LIBCPP_NO_EXCEPTIONS
                         if (__first == __last || *__first != '}')
                             throw regex_error(regex_constants::error_brace);
+#endif
                         ++__first;
+#ifndef _LIBCPP_NO_EXCEPTIONS
                         if (__max < __min)
                             throw regex_error(regex_constants::error_badbrace);
+#endif
                         if (__grammar == ECMAScript && __first != __last && *__first == '?')
                         {
                             ++__first;
@@ -3513,8 +3552,10 @@
                             __push_loop(__min, __max, __s, __mexp_begin, __mexp_end);
                     }
                     break;
+#ifndef _LIBCPP_NO_EXCEPTIONS
                 default:
                     throw regex_error(regex_constants::error_badbrace);
+#endif
                 }
             }
             break;
@@ -3531,8 +3572,11 @@
 {
     if (__first != __last && *__first == '[')
     {
-        if (++__first == __last)
+        ++__first;
+#ifndef _LIBCPP_NO_EXCEPTIONS
+        if (__first == __last)
             throw regex_error(regex_constants::error_brack);
+#endif
         bool __negate = false;
         if (*__first == '^')
         {
@@ -3541,23 +3585,29 @@
         }
         __bracket_expression<_CharT, _Traits>* __ml = __start_matching_list(__negate);
         // __ml owned by *this
+#ifndef _LIBCPP_NO_EXCEPTIONS
         if (__first == __last)
             throw regex_error(regex_constants::error_brack);
+#endif
         if ((__flags_ & 0x1F0) != ECMAScript && *__first == ']')
         {
             __ml->__add_char(']');
             ++__first;
         }
         __first = __parse_follow_list(__first, __last, __ml);
+#ifndef _LIBCPP_NO_EXCEPTIONS
         if (__first == __last)
             throw regex_error(regex_constants::error_brack);
+#endif
         if (*__first == '-')
         {
             __ml->__add_char('-');
             ++__first;
         }
+#ifndef _LIBCPP_NO_EXCEPTIONS
         if (__first == __last || *__first != ']')
             throw regex_error(regex_constants::error_brack);
+#endif
         ++__first;
     }
     return __first;
@@ -3677,8 +3727,10 @@
                           basic_string<_CharT>& __str,
                           __bracket_expression<_CharT, _Traits>* __ml)
 {
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__first == __last)
         throw regex_error(regex_constants::error_escape);
+#endif
     switch (*__first)
     {
     case 0:
@@ -3719,8 +3771,10 @@
                           _ForwardIterator __last,
                           basic_string<_CharT>* __str)
 {
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__first == __last)
         throw regex_error(regex_constants::error_escape);
+#endif
     switch (*__first)
     {
     case '\\':
@@ -3788,8 +3842,10 @@
         else
             __push_char(_CharT(__val));
     }
+#ifndef _LIBCPP_NO_EXCEPTIONS
     else
         throw regex_error(regex_constants::error_escape);
+#endif
     return __first;
 }
 
@@ -3805,14 +3861,18 @@
     value_type _Equal_close[2] = {'=', ']'};
     _ForwardIterator __temp = _STD::search(__first, __last, _Equal_close,
                                                             _Equal_close+2);
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__temp == __last)
         throw regex_error(regex_constants::error_brack);
+#endif
     // [__first, __temp) contains all text in [= ... =]
     typedef typename _Traits::string_type string_type;
     string_type __collate_name =
         __traits_.lookup_collatename(__first, __temp);
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__collate_name.empty())
         throw regex_error(regex_constants::error_collate);
+#endif
     string_type __equiv_name =
         __traits_.transform_primary(__collate_name.begin(),
                                     __collate_name.end());
@@ -3828,8 +3888,10 @@
         case 2:
             __ml->__add_digraph(__collate_name[0], __collate_name[1]);
             break;
+#ifndef _LIBCPP_NO_EXCEPTIONS
         default:
             throw regex_error(regex_constants::error_collate);
+#endif
         }
     }
     __first = next(__temp, 2);
@@ -3848,14 +3910,18 @@
     value_type _Colon_close[2] = {':', ']'};
     _ForwardIterator __temp = _STD::search(__first, __last, _Colon_close,
                                                             _Colon_close+2);
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__temp == __last)
         throw regex_error(regex_constants::error_brack);
+#endif
     // [__first, __temp) contains all text in [: ... :]
     typedef typename _Traits::char_class_type char_class_type;
     char_class_type __class_type =
         __traits_.lookup_classname(__first, __temp, __flags_ & icase);
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__class_type == 0)
         throw regex_error(regex_constants::error_brack);
+#endif
     __ml->__add_class(__class_type);
     __first = next(__temp, 2);
     return __first;
@@ -3873,8 +3939,10 @@
     value_type _Dot_close[2] = {'.', ']'};
     _ForwardIterator __temp = _STD::search(__first, __last, _Dot_close,
                                                             _Dot_close+2);
+#ifndef _LIBCPP_NO_EXCEPTIONS
     if (__temp == __last)
         throw regex_error(regex_constants::error_brack);
+#endif
     // [__first, __temp) contains all text in [. ... .]
     typedef typename _Traits::string_type string_type;
     __col_sym = __traits_.lookup_collatename(__first, __temp);
@@ -3883,8 +3951,10 @@
     case 1:
     case 2:
         break;
+#ifndef _LIBCPP_NO_EXCEPTIONS
     default:
         throw regex_error(regex_constants::error_collate);
+#endif
     }
     __first = next(__temp, 2);
     return __first;
@@ -4022,8 +4092,10 @@
                                 __temp = __exp.__parse(++__temp, __last);
                                 __exp.__push_l_anchor();
                                 __push_lookahead(_STD::move(__exp), false);
+#ifndef _LIBCPP_NO_EXCEPTIONS
                                 if (__temp == __last || *__temp != ')')
                                     throw regex_error(regex_constants::error_paren);
+#endif
                                 __first = ++__temp;
                             }
                             break;
@@ -4034,8 +4106,10 @@
                                 __temp = __exp.__parse(++__temp, __last);
                                 __exp.__push_l_anchor();
                                 __push_lookahead(_STD::move(__exp), true);
+#ifndef _LIBCPP_NO_EXCEPTIONS
                                 if (__temp == __last || *__temp != ')')
                                     throw regex_error(regex_constants::error_paren);
+#endif
                                 __first = ++__temp;
                             }
                             break;
@@ -4071,15 +4145,20 @@
             break;
         case '(':
             {
-                if (++__first == __last)
+                ++__first;
+#ifndef _LIBCPP_NO_EXCEPTIONS
+                if (__first == __last)
                     throw regex_error(regex_constants::error_paren);
+#endif
                 _ForwardIterator __temp = _STD::next(__first);
                 if (__temp != __last && *__first == '?' && *__temp == ':')
                 {
                     ++__open_count_;
                     __first = __parse_ecma_exp(++__temp, __last);
+#ifndef _LIBCPP_NO_EXCEPTIONS
                     if (__first == __last || *__first != ')')
                         throw regex_error(regex_constants::error_paren);
+#endif
                     --__open_count_;
                     ++__first;
                 }
@@ -4089,8 +4168,10 @@
                     unsigned __temp_count = __marked_count_;
                     ++__open_count_;
                     __first = __parse_ecma_exp(__first, __last);
+#ifndef _LIBCPP_NO_EXCEPTIONS
                     if (__first == __last || *__first != ')')
                         throw regex_error(regex_constants::error_paren);
+#endif
                     __push_end_marked_subexpression(__temp_count);
                     --__open_count_;
                     ++__first;
@@ -4151,8 +4232,10 @@
             unsigned __v = *__first - '0';
             for (++__first; '0' <= *__first && *__first <= '9'; ++__first)
                 __v = 10 * __v + *__first - '0';
+#ifndef _LIBCPP_NO_EXCEPTIONS
             if (__v > mark_count())
                 throw regex_error(regex_constants::error_backref);
+#endif
             __push_back_ref(__v);
         }
     }
@@ -4270,31 +4353,51 @@
             }
             break;
         case 'u':
-            if (++__first == __last)
+            ++__first;
+#ifndef _LIBCPP_NO_EXCEPTIONS
+            if (__first == __last)
                 throw regex_error(regex_constants::error_escape);
+#endif
             __hd = __traits_.value(*__first, 16);
+#ifndef _LIBCPP_NO_EXCEPTIONS
             if (__hd == -1)
                 throw regex_error(regex_constants::error_escape);
+#endif
             __sum = 16 * __sum + __hd;
-            if (++__first == __last)
+            ++__first;
+#ifndef _LIBCPP_NO_EXCEPTIONS
+            if (__first == __last)
                 throw regex_error(regex_constants::error_escape);
+#endif
             __hd = __traits_.value(*__first, 16);
+#ifndef _LIBCPP_NO_EXCEPTIONS
             if (__hd == -1)
                 throw regex_error(regex_constants::error_escape);
+#endif
             __sum = 16 * __sum + __hd;
             // drop through
         case 'x':
-            if (++__first == __last)
+            ++__first;
+#ifndef _LIBCPP_NO_EXCEPTIONS
+            if (__first == __last)
                 throw regex_error(regex_constants::error_escape);
+#endif
             __hd = __traits_.value(*__first, 16);
+#ifndef _LIBCPP_NO_EXCEPTIONS
             if (__hd == -1)
                 throw regex_error(regex_constants::error_escape);
+#endif
             __sum = 16 * __sum + __hd;
-            if (++__first == __last)
+            ++__first;
+#ifndef _LIBCPP_NO_EXCEPTIONS
+            if (__first == __last)
                 throw regex_error(regex_constants::error_escape);
+#endif
             __hd = __traits_.value(*__first, 16);
+#ifndef _LIBCPP_NO_EXCEPTIONS
             if (__hd == -1)
                 throw regex_error(regex_constants::error_escape);
+#endif
             __sum = 16 * __sum + __hd;
             if (__str)
                 *__str = _CharT(__sum);
@@ -4311,8 +4414,10 @@
                     __push_char(*__first);
                 ++__first;
             }
+#ifndef _LIBCPP_NO_EXCEPTIONS
             else if (__str)
                 throw regex_error(regex_constants::error_escape);
+#endif
             break;
         }
     }
@@ -5206,8 +5311,11 @@
                 __states.pop_back();
                 break;
             default:
+#ifndef _LIBCPP_NO_EXCEPTIONS
                 throw regex_error(regex_constants::__re_err_unknown);
+#endif
                 break;
+
             }
         } while (!__states.empty());
     }
@@ -5274,7 +5382,9 @@
                 __states.pop_back();
                 break;
             default:
+#ifndef _LIBCPP_NO_EXCEPTIONS
                 throw regex_error(regex_constants::__re_err_unknown);
+#endif
                 break;
             }
         } while (!__states.empty());
@@ -5354,7 +5464,9 @@
                 __states.pop_back();
                 break;
             default:
+#ifndef _LIBCPP_NO_EXCEPTIONS
                 throw regex_error(regex_constants::__re_err_unknown);
+#endif
                 break;
             }
         } while (!__states.empty());