Skip to content

Commit

Permalink
Add an _LIBCPP_NORETURN inline function named __throw_XXX for each ex…
Browse files Browse the repository at this point in the history
…ception type we define. They either construct and throw the exception, or abort() (if exceptions are disabled). Use these functions everywhere instead of assert()ing when exceptions are disabled. WARNING: This is a behavior change - but only with exceptions disabled. Reviewed as: https://reviews.llvm.org/D23855.

llvm-svn: 279744
  • Loading branch information
mclow committed Aug 25, 2016
1 parent b41b990 commit d437fa5
Show file tree
Hide file tree
Showing 30 changed files with 272 additions and 288 deletions.
16 changes: 4 additions & 12 deletions libcxx/include/__functional_03
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,8 @@ template<class _Rp>
_Rp
function<_Rp()>::operator()() const
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__f_ == 0)
throw bad_function_call();
#endif // _LIBCPP_NO_EXCEPTIONS
__throw_bad_function_call();
return (*__f_)();
}

Expand Down Expand Up @@ -955,10 +953,8 @@ template<class _Rp, class _A0>
_Rp
function<_Rp(_A0)>::operator()(_A0 __a0) const
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__f_ == 0)
throw bad_function_call();
#endif // _LIBCPP_NO_EXCEPTIONS
__throw_bad_function_call();
return (*__f_)(__a0);
}

Expand Down Expand Up @@ -1231,10 +1227,8 @@ template<class _Rp, class _A0, class _A1>
_Rp
function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__f_ == 0)
throw bad_function_call();
#endif // _LIBCPP_NO_EXCEPTIONS
__throw_bad_function_call();
return (*__f_)(__a0, __a1);
}

Expand Down Expand Up @@ -1507,10 +1501,8 @@ template<class _Rp, class _A0, class _A1, class _A2>
_Rp
function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__f_ == 0)
throw bad_function_call();
#endif // _LIBCPP_NO_EXCEPTIONS
__throw_bad_function_call();
return (*__f_)(__a0, __a1, __a2);
}

Expand Down
7 changes: 2 additions & 5 deletions libcxx/include/__locale
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,9 @@ template <class _Facet>
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
__throw_runtime_error("locale::combine: locale missing facet");

return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
}

Expand Down Expand Up @@ -1184,8 +1183,6 @@ _LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<wchar_t, char, mb
_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)

_LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);

template <size_t _Np>
struct __narrow_to_utf8
{
Expand Down
1 change: 0 additions & 1 deletion libcxx/include/any
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ namespace std {
#include <typeinfo>
#include <type_traits>
#include <cstdlib>
#include <cassert>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
Expand Down
16 changes: 3 additions & 13 deletions libcxx/include/array
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexce
#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
Expand Down Expand Up @@ -209,11 +206,8 @@ typename array<_Tp, _Size>::reference
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
__throw_out_of_range("array::at");

return __elems_[__n];
}

Expand All @@ -223,11 +217,7 @@ typename array<_Tp, _Size>::const_reference
array<_Tp, _Size>::at(size_type __n) const
{
if (__n >= _Size)
#ifndef _LIBCPP_NO_EXCEPTIONS
throw out_of_range("array::at");
#else
assert(!"array::at out_of_range");
#endif
__throw_out_of_range("array::at");
return __elems_[__n];
}

Expand Down
66 changes: 18 additions & 48 deletions libcxx/include/bitset
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ template <size_t N> struct hash<std::bitset<N>>;
#include <stdexcept>
#include <iosfwd>
#include <__functional_base>
#if defined(_LIBCPP_NO_EXCEPTIONS)
#include <cassert>
#endif

#include <__undef_min_max>

Expand Down Expand Up @@ -326,11 +323,8 @@ __bitset<_N_words, _Size>::to_ulong(false_type) const
const_iterator __e = __make_iter(_Size);
const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
if (__i != __e)
#ifndef _LIBCPP_NO_EXCEPTIONS
throw overflow_error("bitset to_ulong overflow error");
#else
assert(!"bitset to_ulong overflow error");
#endif
__throw_overflow_error("bitset to_ulong overflow error");

return __first_[0];
}

Expand All @@ -349,11 +343,8 @@ __bitset<_N_words, _Size>::to_ullong(false_type) const
const_iterator __e = __make_iter(_Size);
const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
if (__i != __e)
#ifndef _LIBCPP_NO_EXCEPTIONS
throw overflow_error("bitset to_ullong overflow error");
#else
assert(!"bitset to_ullong overflow error");
#endif
__throw_overflow_error("bitset to_ullong overflow error");

return to_ullong(true_type());
}

Expand Down Expand Up @@ -763,11 +754,8 @@ bitset<_Size>::bitset(const _CharT* __str,
size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
for (size_t __i = 0; __i < __rlen; ++__i)
if (__str[__i] != __zero && __str[__i] != __one)
#ifndef _LIBCPP_NO_EXCEPTIONS
throw invalid_argument("bitset string ctor has invalid argument");
#else
assert(!"bitset string ctor has invalid argument");
#endif
__throw_invalid_argument("bitset string ctor has invalid argument");

size_t _Mp = _VSTD::min(__rlen, _Size);
size_t __i = 0;
for (; __i < _Mp; ++__i)
Expand All @@ -789,19 +777,13 @@ bitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
_CharT __zero, _CharT __one)
{
if (__pos > __str.size())
#ifndef _LIBCPP_NO_EXCEPTIONS
throw out_of_range("bitset string pos out of range");
#else
assert(!"bitset string pos out of range");
#endif
__throw_out_of_range("bitset string pos out of range");

size_t __rlen = _VSTD::min(__n, __str.size() - __pos);
for (size_t __i = __pos; __i < __pos + __rlen; ++__i)
if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
#ifndef _LIBCPP_NO_EXCEPTIONS
throw invalid_argument("bitset string ctor has invalid argument");
#else
assert(!"bitset string ctor has invalid argument");
#endif
__throw_invalid_argument("bitset string ctor has invalid argument");

size_t _Mp = _VSTD::min(__rlen, _Size);
size_t __i = 0;
for (; __i < _Mp; ++__i)
Expand Down Expand Up @@ -876,11 +858,8 @@ bitset<_Size>&
bitset<_Size>::set(size_t __pos, bool __val)
{
if (__pos >= _Size)
#ifndef _LIBCPP_NO_EXCEPTIONS
throw out_of_range("bitset set argument out of range");
#else
assert(!"bitset set argument out of range");
#endif
__throw_out_of_range("bitset set argument out of range");

(*this)[__pos] = __val;
return *this;
}
Expand All @@ -899,11 +878,8 @@ bitset<_Size>&
bitset<_Size>::reset(size_t __pos)
{
if (__pos >= _Size)
#ifndef _LIBCPP_NO_EXCEPTIONS
throw out_of_range("bitset reset argument out of range");
#else
assert(!"bitset reset argument out of range");
#endif
__throw_out_of_range("bitset reset argument out of range");

(*this)[__pos] = false;
return *this;
}
Expand Down Expand Up @@ -932,11 +908,8 @@ bitset<_Size>&
bitset<_Size>::flip(size_t __pos)
{
if (__pos >= _Size)
#ifndef _LIBCPP_NO_EXCEPTIONS
throw out_of_range("bitset flip argument out of range");
#else
assert(!"bitset flip argument out of range");
#endif
__throw_out_of_range("bitset flip argument out of range");

reference r = base::__make_ref(__pos);
r = ~r;
return *this;
Expand Down Expand Up @@ -1027,11 +1000,8 @@ bool
bitset<_Size>::test(size_t __pos) const
{
if (__pos >= _Size)
#ifndef _LIBCPP_NO_EXCEPTIONS
throw out_of_range("bitset test argument out of range");
#else
assert(!"bitset test argument out of range");
#endif
__throw_out_of_range("bitset test argument out of range");

return (*this)[__pos];
}

Expand Down
3 changes: 0 additions & 3 deletions libcxx/include/complex
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,6 @@ template<class T, class charT, class traits>
#include <stdexcept>
#include <cmath>
#include <sstream>
#if defined(_LIBCPP_NO_EXCEPTIONS)
#include <cassert>
#endif

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
Expand Down
12 changes: 4 additions & 8 deletions libcxx/include/deque
Original file line number Diff line number Diff line change
Expand Up @@ -895,26 +895,22 @@ template <bool>
class __deque_base_common
{
protected:
void __throw_length_error() const;
void __throw_out_of_range() const;
_LIBCPP_NORETURN void __throw_length_error() const;
_LIBCPP_NORETURN void __throw_out_of_range() const;
};

template <bool __b>
void
__deque_base_common<__b>::__throw_length_error() const
{
#ifndef _LIBCPP_NO_EXCEPTIONS
throw length_error("deque");
#endif
_VSTD::__throw_length_error("deque");
}

template <bool __b>
void
__deque_base_common<__b>::__throw_out_of_range() const
{
#ifndef _LIBCPP_NO_EXCEPTIONS
throw out_of_range("deque");
#endif
_VSTD::__throw_out_of_range("deque");
}

template <class _Tp, class _Allocator>
Expand Down
5 changes: 2 additions & 3 deletions libcxx/include/experimental/any
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ inline namespace fundamentals_v1 {
#include <typeinfo>
#include <type_traits>
#include <cstdlib>
#include <cassert>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
Expand All @@ -98,13 +97,13 @@ public:

#if _LIBCPP_STD_VER > 11 // C++ > 11

_LIBCPP_NORETURN _LIBCPP_INLINE_VISIBILITY
_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
inline void __throw_bad_any_cast()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
throw bad_any_cast();
#else
assert(!"bad_any_cast");
_VSTD::abort();
#endif
}

Expand Down
31 changes: 6 additions & 25 deletions libcxx/include/experimental/dynarray
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ public:

#include <__undef___deallocate>

#if defined(_LIBCPP_NO_EXCEPTIONS)
#include <cassert>
#endif

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif
Expand Down Expand Up @@ -142,13 +138,8 @@ private:
static inline _LIBCPP_INLINE_VISIBILITY value_type* __allocate ( size_t count )
{
if ( numeric_limits<size_t>::max() / sizeof (value_type) <= count )
{
#ifndef _LIBCPP_NO_EXCEPTIONS
throw bad_array_length();
#else
assert(!"dynarray::allocation");
#endif
}
__throw_bad_array_length();

return static_cast<value_type *> (_VSTD::__allocate (sizeof(value_type) * count));
}

Expand Down Expand Up @@ -283,13 +274,8 @@ typename dynarray<_Tp>::reference
dynarray<_Tp>::at(size_type __n)
{
if (__n >= __size_)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
throw out_of_range("dynarray::at");
#else
assert(!"dynarray::at out_of_range");
#endif
}
__throw_out_of_range("dynarray::at");

return data()[__n];
}

Expand All @@ -299,13 +285,8 @@ typename dynarray<_Tp>::const_reference
dynarray<_Tp>::at(size_type __n) const
{
if (__n >= __size_)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
throw out_of_range("dynarray::at");
#else
assert(!"dynarray::at out_of_range");
#endif
}
__throw_out_of_range("dynarray::at");

return data()[__n];
}

Expand Down
Loading

0 comments on commit d437fa5

Please sign in to comment.