Commit 1ea94b5e authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

throw_exception to support -fno-exceptions

Summary:
[Folly] `throw_exception` to support `-fno-exceptions`, switching all invocations of `std::__throw_....`.

Removes the need to use the non-portable `bits/functexcept.h`.

Reviewed By: mzlee, ot

Differential Revision: D7050335

fbshipit-source-id: 53d15639baa268a51f703816f04f314454d64979
parent 740a9b01
...@@ -56,8 +56,8 @@ ...@@ -56,8 +56,8 @@
#include <folly/Traits.h> #include <folly/Traits.h>
#include <folly/hash/Hash.h> #include <folly/hash/Hash.h>
#include <folly/lang/Exception.h>
#include <folly/memory/Malloc.h> #include <folly/memory/Malloc.h>
#include <folly/portability/BitsFunctexcept.h>
// When used in folly, assertions are not disabled. // When used in folly, assertions are not disabled.
#define FBSTRING_ASSERT(expr) assert(expr) #define FBSTRING_ASSERT(expr) assert(expr)
...@@ -83,7 +83,7 @@ FOLLY_GCC_DISABLE_WARNING("-Wshadow") ...@@ -83,7 +83,7 @@ FOLLY_GCC_DISABLE_WARNING("-Wshadow")
FOLLY_GCC_DISABLE_WARNING("-Warray-bounds") FOLLY_GCC_DISABLE_WARNING("-Warray-bounds")
// FBString cannot use throw when replacing std::string, though it may still // FBString cannot use throw when replacing std::string, though it may still
// use std::__throw_* // use folly::throw_exception
// nolint // nolint
#define throw FOLLY_FBSTRING_MAY_NOT_USE_THROW #define throw FOLLY_FBSTRING_MAY_NOT_USE_THROW
...@@ -1052,12 +1052,10 @@ template < ...@@ -1052,12 +1052,10 @@ template <
class Storage = fbstring_core<E>> class Storage = fbstring_core<E>>
#endif #endif
class basic_fbstring { class basic_fbstring {
static void enforce( template <typename Ex, typename... Args>
bool condition, FOLLY_ALWAYS_INLINE static void enforce(bool condition, Args&&... args) {
void (*throw_exc)(const char*),
const char* msg) {
if (!condition) { if (!condition) {
throw_exc(msg); throw_exception<Ex>(static_cast<Args&&>(args)...);
} }
} }
...@@ -1338,7 +1336,7 @@ class basic_fbstring { ...@@ -1338,7 +1336,7 @@ class basic_fbstring {
size_type capacity() const { return store_.capacity(); } size_type capacity() const { return store_.capacity(); }
void reserve(size_type res_arg = 0) { void reserve(size_type res_arg = 0) {
enforce(res_arg <= max_size(), std::__throw_length_error, ""); enforce<std::length_error>(res_arg <= max_size(), "");
store_.reserve(res_arg); store_.reserve(res_arg);
} }
...@@ -1364,12 +1362,12 @@ class basic_fbstring { ...@@ -1364,12 +1362,12 @@ class basic_fbstring {
} }
const_reference at(size_type n) const { const_reference at(size_type n) const {
enforce(n <= size(), std::__throw_out_of_range, ""); enforce<std::out_of_range>(n <= size(), "");
return (*this)[n]; return (*this)[n];
} }
reference at(size_type n) { reference at(size_type n) {
enforce(n < size(), std::__throw_out_of_range, ""); enforce<std::out_of_range>(n < size(), "");
return (*this)[n]; return (*this)[n];
} }
...@@ -1454,13 +1452,13 @@ class basic_fbstring { ...@@ -1454,13 +1452,13 @@ class basic_fbstring {
basic_fbstring& insert(size_type pos1, const basic_fbstring& str, basic_fbstring& insert(size_type pos1, const basic_fbstring& str,
size_type pos2, size_type n) { size_type pos2, size_type n) {
enforce(pos2 <= str.length(), std::__throw_out_of_range, ""); enforce<std::out_of_range>(pos2 <= str.length(), "");
procrustes(n, str.length() - pos2); procrustes(n, str.length() - pos2);
return insert(pos1, str.data() + pos2, n); return insert(pos1, str.data() + pos2, n);
} }
basic_fbstring& insert(size_type pos, const value_type* s, size_type n) { basic_fbstring& insert(size_type pos, const value_type* s, size_type n) {
enforce(pos <= length(), std::__throw_out_of_range, ""); enforce<std::out_of_range>(pos <= length(), "");
insert(begin() + pos, s, s + n); insert(begin() + pos, s, s + n);
return *this; return *this;
} }
...@@ -1470,7 +1468,7 @@ class basic_fbstring { ...@@ -1470,7 +1468,7 @@ class basic_fbstring {
} }
basic_fbstring& insert(size_type pos, size_type n, value_type c) { basic_fbstring& insert(size_type pos, size_type n, value_type c) {
enforce(pos <= length(), std::__throw_out_of_range, ""); enforce<std::out_of_range>(pos <= length(), "");
insert(begin() + pos, n, c); insert(begin() + pos, n, c);
return *this; return *this;
} }
...@@ -1536,7 +1534,7 @@ class basic_fbstring { ...@@ -1536,7 +1534,7 @@ class basic_fbstring {
basic_fbstring& erase(size_type pos = 0, size_type n = npos) { basic_fbstring& erase(size_type pos = 0, size_type n = npos) {
Invariant checker(*this); Invariant checker(*this);
enforce(pos <= length(), std::__throw_out_of_range, ""); enforce<std::out_of_range>(pos <= length(), "");
procrustes(n, length() - pos); procrustes(n, length() - pos);
std::copy(begin() + pos + n, end(), begin() + pos); std::copy(begin() + pos + n, end(), begin() + pos);
resize(length() - n); resize(length() - n);
...@@ -1545,7 +1543,7 @@ class basic_fbstring { ...@@ -1545,7 +1543,7 @@ class basic_fbstring {
iterator erase(iterator position) { iterator erase(iterator position) {
const size_type pos(position - begin()); const size_type pos(position - begin());
enforce(pos <= size(), std::__throw_out_of_range, ""); enforce<std::out_of_range>(pos <= size(), "");
erase(pos, 1); erase(pos, 1);
return begin() + pos; return begin() + pos;
} }
...@@ -1568,7 +1566,7 @@ class basic_fbstring { ...@@ -1568,7 +1566,7 @@ class basic_fbstring {
basic_fbstring& replace(size_type pos1, size_type n1, basic_fbstring& replace(size_type pos1, size_type n1,
const basic_fbstring& str, const basic_fbstring& str,
size_type pos2, size_type n2) { size_type pos2, size_type n2) {
enforce(pos2 <= str.length(), std::__throw_out_of_range, ""); enforce<std::out_of_range>(pos2 <= str.length(), "");
return replace(pos1, n1, str.data() + pos2, return replace(pos1, n1, str.data() + pos2,
std::min(n2, str.size() - pos2)); std::min(n2, str.size() - pos2));
} }
...@@ -1590,7 +1588,7 @@ class basic_fbstring { ...@@ -1590,7 +1588,7 @@ class basic_fbstring {
StrOrLength s_or_n2, NumOrChar n_or_c) { StrOrLength s_or_n2, NumOrChar n_or_c) {
Invariant checker(*this); Invariant checker(*this);
enforce(pos <= size(), std::__throw_out_of_range, ""); enforce<std::out_of_range>(pos <= size(), "");
procrustes(n1, length() - pos); procrustes(n1, length() - pos);
const iterator b = begin() + pos; const iterator b = begin() + pos;
return replace(b, b + n1, s_or_n2, n_or_c); return replace(b, b + n1, s_or_n2, n_or_c);
...@@ -1674,7 +1672,7 @@ class basic_fbstring { ...@@ -1674,7 +1672,7 @@ class basic_fbstring {
} }
size_type copy(value_type* s, size_type n, size_type pos = 0) const { size_type copy(value_type* s, size_type n, size_type pos = 0) const {
enforce(pos <= size(), std::__throw_out_of_range, ""); enforce<std::out_of_range>(pos <= size(), "");
procrustes(n, size() - pos); procrustes(n, size() - pos);
if (n != 0) { if (n != 0) {
...@@ -1792,12 +1790,12 @@ class basic_fbstring { ...@@ -1792,12 +1790,12 @@ class basic_fbstring {
} }
basic_fbstring substr(size_type pos = 0, size_type n = npos) const& { basic_fbstring substr(size_type pos = 0, size_type n = npos) const& {
enforce(pos <= size(), std::__throw_out_of_range, ""); enforce<std::out_of_range>(pos <= size(), "");
return basic_fbstring(data() + pos, std::min(n, size() - pos)); return basic_fbstring(data() + pos, std::min(n, size() - pos));
} }
basic_fbstring substr(size_type pos = 0, size_type n = npos) && { basic_fbstring substr(size_type pos = 0, size_type n = npos) && {
enforce(pos <= size(), std::__throw_out_of_range, ""); enforce<std::out_of_range>(pos <= size(), "");
erase(0, pos); erase(0, pos);
if (n < size()) { if (n < size()) {
resize(n); resize(n);
...@@ -1822,7 +1820,7 @@ class basic_fbstring { ...@@ -1822,7 +1820,7 @@ class basic_fbstring {
int compare(size_type pos1, size_type n1, int compare(size_type pos1, size_type n1,
const value_type* s, size_type n2) const { const value_type* s, size_type n2) const {
enforce(pos1 <= size(), std::__throw_out_of_range, ""); enforce<std::out_of_range>(pos1 <= size(), "");
procrustes(n1, size() - pos1); procrustes(n1, size() - pos1);
// The line below fixed by Jean-Francois Bastien, 04-23-2007. Thanks! // The line below fixed by Jean-Francois Bastien, 04-23-2007. Thanks!
const int r = traits_type::compare(pos1 + data(), s, std::min(n1, n2)); const int r = traits_type::compare(pos1 + data(), s, std::min(n1, n2));
...@@ -1832,7 +1830,7 @@ class basic_fbstring { ...@@ -1832,7 +1830,7 @@ class basic_fbstring {
int compare(size_type pos1, size_type n1, int compare(size_type pos1, size_type n1,
const basic_fbstring& str, const basic_fbstring& str,
size_type pos2, size_type n2) const { size_type pos2, size_type n2) const {
enforce(pos2 <= str.size(), std::__throw_out_of_range, ""); enforce<std::out_of_range>(pos2 <= str.size(), "");
return compare(pos1, n1, str.data() + pos2, return compare(pos1, n1, str.data() + pos2,
std::min(n2, str.size() - pos2)); std::min(n2, str.size() - pos2));
} }
...@@ -1855,7 +1853,7 @@ template <typename E, class T, class A, class S> ...@@ -1855,7 +1853,7 @@ template <typename E, class T, class A, class S>
FOLLY_MALLOC_NOINLINE inline typename basic_fbstring<E, T, A, S>::size_type FOLLY_MALLOC_NOINLINE inline typename basic_fbstring<E, T, A, S>::size_type
basic_fbstring<E, T, A, S>::traitsLength(const value_type* s) { basic_fbstring<E, T, A, S>::traitsLength(const value_type* s) {
return s ? traits_type::length(s) return s ? traits_type::length(s)
: (std::__throw_logic_error( : (throw_exception<std::logic_error>(
"basic_fbstring: null pointer initializer not valid"), "basic_fbstring: null pointer initializer not valid"),
0); 0);
} }
...@@ -1941,7 +1939,7 @@ template <typename E, class T, class A, class S> ...@@ -1941,7 +1939,7 @@ template <typename E, class T, class A, class S>
inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::append( inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::append(
const basic_fbstring& str, const size_type pos, size_type n) { const basic_fbstring& str, const size_type pos, size_type n) {
const size_type sz = str.size(); const size_type sz = str.size();
enforce(pos <= sz, std::__throw_out_of_range, ""); enforce<std::out_of_range>(pos <= sz, "");
procrustes(n, sz - pos); procrustes(n, sz - pos);
return append(str.data() + pos, n); return append(str.data() + pos, n);
} }
...@@ -1992,7 +1990,7 @@ template <typename E, class T, class A, class S> ...@@ -1992,7 +1990,7 @@ template <typename E, class T, class A, class S>
inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::assign( inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::assign(
const basic_fbstring& str, const size_type pos, size_type n) { const basic_fbstring& str, const size_type pos, size_type n) {
const size_type sz = str.size(); const size_type sz = str.size();
enforce(pos <= sz, std::__throw_out_of_range, ""); enforce<std::out_of_range>(pos <= sz, "");
procrustes(n, sz - pos); procrustes(n, sz - pos);
return assign(str.data() + pos, n); return assign(str.data() + pos, n);
} }
......
...@@ -38,8 +38,8 @@ ...@@ -38,8 +38,8 @@
#include <folly/FormatTraits.h> #include <folly/FormatTraits.h>
#include <folly/Likely.h> #include <folly/Likely.h>
#include <folly/Traits.h> #include <folly/Traits.h>
#include <folly/lang/Exception.h>
#include <folly/memory/Malloc.h> #include <folly/memory/Malloc.h>
#include <folly/portability/BitsFunctexcept.h>
//============================================================================= //=============================================================================
// forward declaration // forward declaration
...@@ -1073,7 +1073,8 @@ class fbvector { ...@@ -1073,7 +1073,8 @@ class fbvector {
} }
const_reference at(size_type n) const { const_reference at(size_type n) const {
if (UNLIKELY(n >= size())) { if (UNLIKELY(n >= size())) {
std::__throw_out_of_range("fbvector: index is greater than size."); throw_exception<std::out_of_range>(
"fbvector: index is greater than size.");
} }
return (*this)[n]; return (*this)[n];
} }
......
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
#include <folly/Portability.h> #include <folly/Portability.h>
#include <folly/Range.h> #include <folly/Range.h>
#include <folly/Utility.h> #include <folly/Utility.h>
#include <folly/portability/BitsFunctexcept.h> #include <folly/lang/Exception.h>
#include <folly/portability/Constexpr.h> #include <folly/portability/Constexpr.h>
namespace folly { namespace folly {
...@@ -63,8 +63,9 @@ using FixedStringBase = FixedStringBase_<>; ...@@ -63,8 +63,9 @@ using FixedStringBase = FixedStringBase_<>;
// it's testing for fails. In this way, precondition violations are reported // it's testing for fails. In this way, precondition violations are reported
// at compile-time instead of at runtime. // at compile-time instead of at runtime.
[[noreturn]] inline void assertOutOfBounds() { [[noreturn]] inline void assertOutOfBounds() {
assert(false && "Array index out of bounds in BasicFixedString"); assert(!"Array index out of bounds in BasicFixedString");
std::__throw_out_of_range("Array index out of bounds in BasicFixedString"); throw_exception<std::out_of_range>(
"Array index out of bounds in BasicFixedString");
} }
constexpr std::size_t checkOverflow(std::size_t i, std::size_t max) { constexpr std::size_t checkOverflow(std::size_t i, std::size_t max) {
...@@ -79,9 +80,7 @@ constexpr std::size_t checkOverflowOrNpos(std::size_t i, std::size_t max) { ...@@ -79,9 +80,7 @@ constexpr std::size_t checkOverflowOrNpos(std::size_t i, std::size_t max) {
// Intentionally NOT constexpr. See note above for assertOutOfBounds // Intentionally NOT constexpr. See note above for assertOutOfBounds
[[noreturn]] inline void assertNotNullTerminated() noexcept { [[noreturn]] inline void assertNotNullTerminated() noexcept {
assert( assert(!"Non-null terminated string used to initialize a BasicFixedString");
false &&
"Non-null terminated string used to initialize a BasicFixedString");
std::terminate(); // Fail hard, fail fast. std::terminate(); // Fail hard, fail fast.
} }
...@@ -1104,20 +1103,20 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase { ...@@ -1104,20 +1103,20 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase {
* \throw std::out_of_range when i > size() * \throw std::out_of_range when i > size()
*/ */
FOLLY_CPP14_CONSTEXPR Char& at(std::size_t i) noexcept(false) { FOLLY_CPP14_CONSTEXPR Char& at(std::size_t i) noexcept(false) {
return i <= size_ return i <= size_ ? data_[i]
? data_[i] : (throw_exception<std::out_of_range>(
: (std::__throw_out_of_range("Out of range in BasicFixedString::at"), "Out of range in BasicFixedString::at"),
data_[size_]); data_[size_]);
} }
/** /**
* \overload * \overload
*/ */
constexpr const Char& at(std::size_t i) const noexcept(false) { constexpr const Char& at(std::size_t i) const noexcept(false) {
return i <= size_ return i <= size_ ? data_[i]
? data_[i] : (throw_exception<std::out_of_range>(
: (std::__throw_out_of_range("Out of range in BasicFixedString::at"), "Out of range in BasicFixedString::at"),
data_[size_]); data_[size_]);
} }
/** /**
......
...@@ -328,6 +328,7 @@ nobase_follyinclude_HEADERS = \ ...@@ -328,6 +328,7 @@ nobase_follyinclude_HEADERS = \
lang/Assume.h \ lang/Assume.h \
lang/Bits.h \ lang/Bits.h \
lang/ColdClass.h \ lang/ColdClass.h \
lang/Exception.h \
lang/Launder.h \ lang/Launder.h \
lang/RValueReferenceWrapper.h \ lang/RValueReferenceWrapper.h \
lang/SafeAssert.h \ lang/SafeAssert.h \
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
#include <folly/Portability.h> #include <folly/Portability.h>
#include <folly/hash/SpookyHashV2.h> #include <folly/hash/SpookyHashV2.h>
#include <folly/portability/BitsFunctexcept.h> #include <folly/lang/Exception.h>
#include <folly/portability/Constexpr.h> #include <folly/portability/Constexpr.h>
#include <folly/portability/String.h> #include <folly/portability/String.h>
...@@ -224,7 +224,7 @@ class Range : private boost::totally_ordered<Range<Iter>> { ...@@ -224,7 +224,7 @@ class Range : private boost::totally_ordered<Range<Iter>> {
template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0> template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
Range(const std::string& str, std::string::size_type startFrom) { Range(const std::string& str, std::string::size_type startFrom) {
if (UNLIKELY(startFrom > str.size())) { if (UNLIKELY(startFrom > str.size())) {
std::__throw_out_of_range("index out of range"); throw_exception<std::out_of_range>("index out of range");
} }
b_ = str.data() + startFrom; b_ = str.data() + startFrom;
e_ = str.data() + str.size(); e_ = str.data() + str.size();
...@@ -236,7 +236,7 @@ class Range : private boost::totally_ordered<Range<Iter>> { ...@@ -236,7 +236,7 @@ class Range : private boost::totally_ordered<Range<Iter>> {
std::string::size_type startFrom, std::string::size_type startFrom,
std::string::size_type size) { std::string::size_type size) {
if (UNLIKELY(startFrom > str.size())) { if (UNLIKELY(startFrom > str.size())) {
std::__throw_out_of_range("index out of range"); throw_exception<std::out_of_range>("index out of range");
} }
b_ = str.data() + startFrom; b_ = str.data() + startFrom;
if (str.size() - startFrom < size) { if (str.size() - startFrom < size) {
...@@ -274,7 +274,7 @@ class Range : private boost::totally_ordered<Range<Iter>> { ...@@ -274,7 +274,7 @@ class Range : private boost::totally_ordered<Range<Iter>> {
auto const cdata = container.data(); auto const cdata = container.data();
auto const csize = container.size(); auto const csize = container.size();
if (UNLIKELY(startFrom > csize)) { if (UNLIKELY(startFrom > csize)) {
std::__throw_out_of_range("index out of range"); throw_exception<std::out_of_range>("index out of range");
} }
b_ = cdata + startFrom; b_ = cdata + startFrom;
e_ = cdata + csize; e_ = cdata + csize;
...@@ -296,7 +296,7 @@ class Range : private boost::totally_ordered<Range<Iter>> { ...@@ -296,7 +296,7 @@ class Range : private boost::totally_ordered<Range<Iter>> {
auto const cdata = container.data(); auto const cdata = container.data();
auto const csize = container.size(); auto const csize = container.size();
if (UNLIKELY(startFrom > csize)) { if (UNLIKELY(startFrom > csize)) {
std::__throw_out_of_range("index out of range"); throw_exception<std::out_of_range>("index out of range");
} }
b_ = cdata + startFrom; b_ = cdata + startFrom;
if (csize - startFrom < size) { if (csize - startFrom < size) {
...@@ -521,14 +521,14 @@ class Range : private boost::totally_ordered<Range<Iter>> { ...@@ -521,14 +521,14 @@ class Range : private boost::totally_ordered<Range<Iter>> {
value_type& at(size_t i) { value_type& at(size_t i) {
if (i >= size()) { if (i >= size()) {
std::__throw_out_of_range("index out of range"); throw_exception<std::out_of_range>("index out of range");
} }
return b_[i]; return b_[i];
} }
const value_type& at(size_t i) const { const value_type& at(size_t i) const {
if (i >= size()) { if (i >= size()) {
std::__throw_out_of_range("index out of range"); throw_exception<std::out_of_range>("index out of range");
} }
return b_[i]; return b_[i];
} }
...@@ -564,21 +564,21 @@ class Range : private boost::totally_ordered<Range<Iter>> { ...@@ -564,21 +564,21 @@ class Range : private boost::totally_ordered<Range<Iter>> {
void advance(size_type n) { void advance(size_type n) {
if (UNLIKELY(n > size())) { if (UNLIKELY(n > size())) {
std::__throw_out_of_range("index out of range"); throw_exception<std::out_of_range>("index out of range");
} }
b_ += n; b_ += n;
} }
void subtract(size_type n) { void subtract(size_type n) {
if (UNLIKELY(n > size())) { if (UNLIKELY(n > size())) {
std::__throw_out_of_range("index out of range"); throw_exception<std::out_of_range>("index out of range");
} }
e_ -= n; e_ -= n;
} }
Range subpiece(size_type first, size_type length = npos) const { Range subpiece(size_type first, size_type length = npos) const {
if (UNLIKELY(first > size())) { if (UNLIKELY(first > size())) {
std::__throw_out_of_range("index out of range"); throw_exception<std::out_of_range>("index out of range");
} }
return Range(b_ + first, std::min(length, size() - first)); return Range(b_ + first, std::min(length, size() - first));
...@@ -775,7 +775,7 @@ class Range : private boost::totally_ordered<Range<Iter>> { ...@@ -775,7 +775,7 @@ class Range : private boost::totally_ordered<Range<Iter>> {
} else if (e == e_) { } else if (e == e_) {
e_ = b; e_ = b;
} else { } else {
std::__throw_out_of_range("index out of range"); throw_exception<std::out_of_range>("index out of range");
} }
} }
...@@ -841,7 +841,7 @@ class Range : private boost::totally_ordered<Range<Iter>> { ...@@ -841,7 +841,7 @@ class Range : private boost::totally_ordered<Range<Iter>> {
*/ */
size_t replaceAll(const_range_type source, const_range_type dest) { size_t replaceAll(const_range_type source, const_range_type dest) {
if (source.size() != dest.size()) { if (source.size() != dest.size()) {
throw std::invalid_argument( throw_exception<std::invalid_argument>(
"replacement must have the same size as source"); "replacement must have the same size as source");
} }
......
...@@ -249,7 +249,7 @@ void* SimpleAllocator::allocateHard() { ...@@ -249,7 +249,7 @@ void* SimpleAllocator::allocateHard() {
// Allocate a new slab. // Allocate a new slab.
mem_ = static_cast<uint8_t*>(folly::aligned_malloc(allocSize_, allocSize_)); mem_ = static_cast<uint8_t*>(folly::aligned_malloc(allocSize_, allocSize_));
if (!mem_) { if (!mem_) {
std::__throw_bad_alloc(); throw_exception<std::bad_alloc>();
} }
end_ = mem_ + allocSize_; end_ = mem_ + allocSize_;
blocks_.push_back(mem_); blocks_.push_back(mem_);
......
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
#include <folly/Portability.h> #include <folly/Portability.h>
#include <folly/hash/Hash.h> #include <folly/hash/Hash.h>
#include <folly/lang/Align.h> #include <folly/lang/Align.h>
#include <folly/portability/BitsFunctexcept.h> #include <folly/lang/Exception.h>
#include <folly/system/ThreadId.h> #include <folly/system/ThreadId.h>
namespace folly { namespace folly {
...@@ -437,7 +437,7 @@ class CoreAllocator { ...@@ -437,7 +437,7 @@ class CoreAllocator {
void* mem = void* mem =
aligned_malloc(size, hardware_destructive_interference_size); aligned_malloc(size, hardware_destructive_interference_size);
if (!mem) { if (!mem) {
std::__throw_bad_alloc(); throw_exception<std::bad_alloc>();
} }
return mem; return mem;
} }
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
#include <boost/iterator/iterator_adaptor.hpp> #include <boost/iterator/iterator_adaptor.hpp>
#include <boost/utility.hpp> #include <boost/utility.hpp>
#include <folly/portability/BitsFunctexcept.h> #include <folly/lang/Exception.h>
namespace folly { namespace folly {
...@@ -214,7 +214,7 @@ class EvictingCacheMap { ...@@ -214,7 +214,7 @@ class EvictingCacheMap {
TValue& get(const TKey& key) { TValue& get(const TKey& key) {
auto it = find(key); auto it = find(key);
if (it == end()) { if (it == end()) {
std::__throw_out_of_range("Key does not exist"); throw_exception<std::out_of_range>("Key does not exist");
} }
return it->second; return it->second;
} }
...@@ -246,7 +246,7 @@ class EvictingCacheMap { ...@@ -246,7 +246,7 @@ class EvictingCacheMap {
const TValue& getWithoutPromotion(const TKey& key) const { const TValue& getWithoutPromotion(const TKey& key) const {
auto it = findWithoutPromotion(key); auto it = findWithoutPromotion(key);
if (it == end()) { if (it == end()) {
std::__throw_out_of_range("Key does not exist"); throw_exception<std::out_of_range>("Key does not exist");
} }
return it->second; return it->second;
} }
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
#include <folly/Format.h> #include <folly/Format.h>
#include <folly/hash/Hash.h> #include <folly/hash/Hash.h>
#include <folly/lang/Assume.h> #include <folly/lang/Assume.h>
#include <folly/portability/BitsFunctexcept.h> #include <folly/lang/Exception.h>
namespace folly { namespace folly {
...@@ -246,7 +246,7 @@ const dynamic* dynamic::get_ptr(dynamic const& idx) const& { ...@@ -246,7 +246,7 @@ const dynamic* dynamic::get_ptr(dynamic const& idx) const& {
[[noreturn]] static void throwOutOfRangeAtMissingKey(dynamic const& idx) { [[noreturn]] static void throwOutOfRangeAtMissingKey(dynamic const& idx) {
auto msg = sformat("couldn't find key {} in dynamic object", idx.asString()); auto msg = sformat("couldn't find key {} in dynamic object", idx.asString());
std::__throw_out_of_range(msg.c_str()); throw_exception<std::out_of_range>(msg);
} }
dynamic const& dynamic::at(dynamic const& idx) const& { dynamic const& dynamic::at(dynamic const& idx) const& {
...@@ -255,7 +255,7 @@ dynamic const& dynamic::at(dynamic const& idx) const& { ...@@ -255,7 +255,7 @@ dynamic const& dynamic::at(dynamic const& idx) const& {
throwTypeError_("int64", idx.type()); throwTypeError_("int64", idx.type());
} }
if (idx < 0 || idx >= parray->size()) { if (idx < 0 || idx >= parray->size()) {
std::__throw_out_of_range("out of range in dynamic array"); throw_exception<std::out_of_range>("out of range in dynamic array");
} }
return (*parray)[size_t(idx.asInt())]; return (*parray)[size_t(idx.asInt())];
} else if (auto* pobject = get_nothrow<ObjectImpl>()) { } else if (auto* pobject = get_nothrow<ObjectImpl>()) {
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
#include <stdexcept> #include <stdexcept>
#include <folly/Executor.h> #include <folly/Executor.h>
#include <folly/portability/BitsFunctexcept.h> #include <folly/lang/Exception.h>
namespace folly { namespace folly {
// An executor that supports timed scheduling. Like RxScheduler. // An executor that supports timed scheduling. Like RxScheduler.
...@@ -48,7 +48,7 @@ namespace folly { ...@@ -48,7 +48,7 @@ namespace folly {
/// Schedule a Func to be executed at time t, or as soon afterward as /// Schedule a Func to be executed at time t, or as soon afterward as
/// possible. Expect millisecond resolution at best. Must be threadsafe. /// possible. Expect millisecond resolution at best. Must be threadsafe.
virtual void scheduleAt(Func&& /* a */, TimePoint const& /* t */) { virtual void scheduleAt(Func&& /* a */, TimePoint const& /* t */) {
std::__throw_logic_error("unimplemented"); throw_exception<std::logic_error>("unimplemented");
} }
/// Get this executor's notion of time. Must be threadsafe. /// Get this executor's notion of time. Must be threadsafe.
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
#include <folly/Utility.h> #include <folly/Utility.h>
#include <folly/futures/FutureException.h> #include <folly/futures/FutureException.h>
#include <folly/futures/detail/FSM.h> #include <folly/futures/detail/FSM.h>
#include <folly/portability/BitsFunctexcept.h> #include <folly/lang/Exception.h>
#include <folly/io/async/Request.h> #include <folly/io/async/Request.h>
...@@ -161,7 +161,7 @@ class Core final { ...@@ -161,7 +161,7 @@ class Core final {
case State::OnlyCallback: case State::OnlyCallback:
case State::Armed: case State::Armed:
case State::Done: case State::Done:
std::__throw_logic_error("setCallback called twice"); throw_exception<std::logic_error>("setCallback called twice");
FSM_END FSM_END
// we could always call this, it is an optimization to only call it when // we could always call this, it is an optimization to only call it when
...@@ -188,7 +188,7 @@ class Core final { ...@@ -188,7 +188,7 @@ class Core final {
case State::OnlyResult: case State::OnlyResult:
case State::Armed: case State::Armed:
case State::Done: case State::Done:
std::__throw_logic_error("setResult called twice"); throw_exception<std::logic_error>("setResult called twice");
FSM_END FSM_END
if (transitionToArmed) { if (transitionToArmed) {
......
...@@ -60,7 +60,7 @@ std::string CursorBase<Derived, BufType>::readTerminatedString( ...@@ -60,7 +60,7 @@ std::string CursorBase<Derived, BufType>::readTerminatedString(
auto result = readWhile(keepReading); auto result = readWhile(keepReading);
// skip over the terminator character // skip over the terminator character
if (isAtEnd()) { if (isAtEnd()) {
std::__throw_out_of_range("terminator not found"); throw_exception<std::out_of_range>("terminator not found");
} }
skip(1); skip(1);
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
#include <folly/io/IOBuf.h> #include <folly/io/IOBuf.h>
#include <folly/io/IOBufQueue.h> #include <folly/io/IOBufQueue.h>
#include <folly/lang/Bits.h> #include <folly/lang/Bits.h>
#include <folly/portability/BitsFunctexcept.h> #include <folly/lang/Exception.h>
/** /**
* Cursor class for fast iteration over IOBuf chains. * Cursor class for fast iteration over IOBuf chains.
...@@ -449,13 +449,13 @@ class CursorBase { ...@@ -449,13 +449,13 @@ class CursorBase {
void clone(std::unique_ptr<folly::IOBuf>& buf, size_t len) { void clone(std::unique_ptr<folly::IOBuf>& buf, size_t len) {
if (UNLIKELY(cloneAtMost(buf, len) != len)) { if (UNLIKELY(cloneAtMost(buf, len) != len)) {
std::__throw_out_of_range("underflow"); throw_exception<std::out_of_range>("underflow");
} }
} }
void clone(folly::IOBuf& buf, size_t len) { void clone(folly::IOBuf& buf, size_t len) {
if (UNLIKELY(cloneAtMost(buf, len) != len)) { if (UNLIKELY(cloneAtMost(buf, len) != len)) {
std::__throw_out_of_range("underflow"); throw_exception<std::out_of_range>("underflow");
} }
} }
...@@ -526,13 +526,13 @@ class CursorBase { ...@@ -526,13 +526,13 @@ class CursorBase {
} }
if (otherBuf == other.buffer_) { if (otherBuf == other.buffer_) {
std::__throw_out_of_range("wrap-around"); throw_exception<std::out_of_range>("wrap-around");
} }
len += crtPos_ - crtBegin_; len += crtPos_ - crtBegin_;
} else { } else {
if (crtPos_ < other.crtPos_) { if (crtPos_ < other.crtPos_) {
std::__throw_out_of_range("underflow"); throw_exception<std::out_of_range>("underflow");
} }
len += crtPos_ - other.crtPos_; len += crtPos_ - other.crtPos_;
...@@ -552,7 +552,7 @@ class CursorBase { ...@@ -552,7 +552,7 @@ class CursorBase {
len += curBuf->length(); len += curBuf->length();
curBuf = curBuf->next(); curBuf = curBuf->next();
if (curBuf == buf || curBuf == buffer_) { if (curBuf == buf || curBuf == buffer_) {
std::__throw_out_of_range("wrap-around"); throw_exception<std::out_of_range>("wrap-around");
} }
} }
...@@ -629,7 +629,7 @@ class CursorBase { ...@@ -629,7 +629,7 @@ class CursorBase {
for (size_t available; (available = length()) < len; ) { for (size_t available; (available = length()) < len; ) {
str->append(reinterpret_cast<const char*>(data()), available); str->append(reinterpret_cast<const char*>(data()), available);
if (UNLIKELY(!tryAdvanceBuffer())) { if (UNLIKELY(!tryAdvanceBuffer())) {
std::__throw_out_of_range("string underflow"); throw_exception<std::out_of_range>("string underflow");
} }
len -= available; len -= available;
} }
...@@ -658,7 +658,7 @@ class CursorBase { ...@@ -658,7 +658,7 @@ class CursorBase {
void pullSlow(void* buf, size_t len) { void pullSlow(void* buf, size_t len) {
if (UNLIKELY(pullAtMostSlow(buf, len) != len)) { if (UNLIKELY(pullAtMostSlow(buf, len) != len)) {
std::__throw_out_of_range("underflow"); throw_exception<std::out_of_range>("underflow");
} }
} }
...@@ -678,7 +678,7 @@ class CursorBase { ...@@ -678,7 +678,7 @@ class CursorBase {
void skipSlow(size_t len) { void skipSlow(size_t len) {
if (UNLIKELY(skipAtMostSlow(len) != len)) { if (UNLIKELY(skipAtMostSlow(len) != len)) {
std::__throw_out_of_range("underflow"); throw_exception<std::out_of_range>("underflow");
} }
} }
...@@ -697,7 +697,7 @@ class CursorBase { ...@@ -697,7 +697,7 @@ class CursorBase {
void retreatSlow(size_t len) { void retreatSlow(size_t len) {
if (UNLIKELY(retreatAtMostSlow(len) != len)) { if (UNLIKELY(retreatAtMostSlow(len) != len)) {
std::__throw_out_of_range("underflow"); throw_exception<std::out_of_range>("underflow");
} }
} }
...@@ -745,13 +745,13 @@ class Writable { ...@@ -745,13 +745,13 @@ class Writable {
void push(const uint8_t* buf, size_t len) { void push(const uint8_t* buf, size_t len) {
Derived* d = static_cast<Derived*>(this); Derived* d = static_cast<Derived*>(this);
if (d->pushAtMost(buf, len) != len) { if (d->pushAtMost(buf, len) != len) {
std::__throw_out_of_range("overflow"); throw_exception<std::out_of_range>("overflow");
} }
} }
void push(ByteRange buf) { void push(ByteRange buf) {
if (this->pushAtMost(buf) != buf.size()) { if (this->pushAtMost(buf) != buf.size()) {
std::__throw_out_of_range("overflow"); throw_exception<std::out_of_range>("overflow");
} }
} }
...@@ -767,7 +767,7 @@ class Writable { ...@@ -767,7 +767,7 @@ class Writable {
*/ */
void push(Cursor cursor, size_t len) { void push(Cursor cursor, size_t len) {
if (this->pushAtMost(cursor, len) != len) { if (this->pushAtMost(cursor, len) != len) {
std::__throw_out_of_range("overflow"); throw_exception<std::out_of_range>("overflow");
} }
} }
...@@ -996,7 +996,7 @@ class Appender : public detail::Writable<Appender> { ...@@ -996,7 +996,7 @@ class Appender : public detail::Writable<Appender> {
// Waste the rest of the current buffer and allocate a new one. // Waste the rest of the current buffer and allocate a new one.
// Don't make it too small, either. // Don't make it too small, either.
if (growth_ == 0) { if (growth_ == 0) {
std::__throw_out_of_range("can't grow buffer chain"); throw_exception<std::out_of_range>("can't grow buffer chain");
} }
n = std::max(n, growth_); n = std::max(n, growth_);
......
/*
* Copyright 2018-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <exception>
#include <folly/CPortability.h>
#include <folly/CppAttributes.h>
namespace folly {
/// throw_exception
///
/// Throw an exception if exceptions are enabled, or terminate if compiled with
/// -fno-exceptions.
template <typename Ex>
[[noreturn]] FOLLY_NOINLINE FOLLY_COLD void throw_exception(Ex&& ex) {
#if !__GNUC || __EXCEPTIONS
throw static_cast<Ex&&>(ex);
#else
std::terminate();
#endif
}
/// throw_exception
///
/// Construct and throw an exception if exceptions are enabled, or terminate if
/// compiled with -fno-exceptions.
template <typename Ex, typename... Args>
[[noreturn]] FOLLY_NOINLINE FOLLY_COLD void throw_exception(Args&&... args) {
throw_exception(Ex(static_cast<Args&&>(args)...));
}
} // namespace folly
...@@ -46,8 +46,8 @@ ...@@ -46,8 +46,8 @@
// includes and uses fbstring. // includes and uses fbstring.
#if defined(_GLIBCXX_USE_FB) && !defined(_LIBSTDCXX_FBSTRING) #if defined(_GLIBCXX_USE_FB) && !defined(_LIBSTDCXX_FBSTRING)
#include <folly/lang/Exception.h>
#include <folly/memory/detail/MallocImpl.h> #include <folly/memory/detail/MallocImpl.h>
#include <folly/portability/BitsFunctexcept.h>
#include <string> #include <string>
...@@ -93,14 +93,12 @@ extern "C" int mallctlbymib(const size_t*, size_t, void*, size_t*, void*, ...@@ -93,14 +93,12 @@ extern "C" int mallctlbymib(const size_t*, size_t, void*, size_t*, void*,
size_t) size_t)
__attribute__((__weak__)); __attribute__((__weak__));
#include <bits/functexcept.h>
#define FOLLY_HAVE_MALLOC_H 1 #define FOLLY_HAVE_MALLOC_H 1
#else // !defined(_LIBSTDCXX_FBSTRING) #else // !defined(_LIBSTDCXX_FBSTRING)
#include <folly/lang/Exception.h> /* nolint */
#include <folly/memory/detail/MallocImpl.h> /* nolint */ #include <folly/memory/detail/MallocImpl.h> /* nolint */
#include <folly/portability/BitsFunctexcept.h> /* nolint */
#endif #endif
...@@ -229,7 +227,7 @@ static const size_t jemallocMinInPlaceExpandable = 4096; ...@@ -229,7 +227,7 @@ static const size_t jemallocMinInPlaceExpandable = 4096;
inline void* checkedMalloc(size_t size) { inline void* checkedMalloc(size_t size) {
void* p = malloc(size); void* p = malloc(size);
if (!p) { if (!p) {
std::__throw_bad_alloc(); throw_exception<std::bad_alloc>();
} }
return p; return p;
} }
...@@ -237,7 +235,7 @@ inline void* checkedMalloc(size_t size) { ...@@ -237,7 +235,7 @@ inline void* checkedMalloc(size_t size) {
inline void* checkedCalloc(size_t n, size_t size) { inline void* checkedCalloc(size_t n, size_t size) {
void* p = calloc(n, size); void* p = calloc(n, size);
if (!p) { if (!p) {
std::__throw_bad_alloc(); throw_exception<std::bad_alloc>();
} }
return p; return p;
} }
...@@ -245,7 +243,7 @@ inline void* checkedCalloc(size_t n, size_t size) { ...@@ -245,7 +243,7 @@ inline void* checkedCalloc(size_t n, size_t size) {
inline void* checkedRealloc(void* ptr, size_t size) { inline void* checkedRealloc(void* ptr, size_t size) {
void* p = realloc(ptr, size); void* p = realloc(ptr, size);
if (!p) { if (!p) {
std::__throw_bad_alloc(); throw_exception<std::bad_alloc>();
} }
return p; return p;
} }
......
...@@ -50,8 +50,8 @@ ...@@ -50,8 +50,8 @@
#include <folly/Portability.h> #include <folly/Portability.h>
#include <folly/Traits.h> #include <folly/Traits.h>
#include <folly/lang/Assume.h> #include <folly/lang/Assume.h>
#include <folly/lang/Exception.h>
#include <folly/memory/Malloc.h> #include <folly/memory/Malloc.h>
#include <folly/portability/BitsFunctexcept.h>
#include <folly/portability/Malloc.h> #include <folly/portability/Malloc.h>
#include <folly/portability/TypeTraits.h> #include <folly/portability/TypeTraits.h>
...@@ -881,14 +881,14 @@ class small_vector : public detail::small_vector_base< ...@@ -881,14 +881,14 @@ class small_vector : public detail::small_vector_base<
reference at(size_type i) { reference at(size_type i) {
if (i >= size()) { if (i >= size()) {
std::__throw_out_of_range("index out of range"); throw_exception<std::out_of_range>("index out of range");
} }
return (*this)[i]; return (*this)[i];
} }
const_reference at(size_type i) const { const_reference at(size_type i) const {
if (i >= size()) { if (i >= size()) {
std::__throw_out_of_range("index out of range"); throw_exception<std::out_of_range>("index out of range");
} }
return (*this)[i]; return (*this)[i];
} }
......
...@@ -72,7 +72,7 @@ ...@@ -72,7 +72,7 @@
#include <folly/Traits.h> #include <folly/Traits.h>
#include <folly/Utility.h> #include <folly/Utility.h>
#include <folly/portability/BitsFunctexcept.h> #include <folly/lang/Exception.h>
namespace folly { namespace folly {
...@@ -737,7 +737,7 @@ class sorted_vector_map ...@@ -737,7 +737,7 @@ class sorted_vector_map
if (it != end()) { if (it != end()) {
return it->second; return it->second;
} }
std::__throw_out_of_range("sorted_vector_map::at"); throw_exception<std::out_of_range>("sorted_vector_map::at");
} }
const mapped_type& at(const key_type& key) const { const mapped_type& at(const key_type& key) const {
...@@ -745,7 +745,7 @@ class sorted_vector_map ...@@ -745,7 +745,7 @@ class sorted_vector_map
if (it != end()) { if (it != end()) {
return it->second; return it->second;
} }
std::__throw_out_of_range("sorted_vector_map::at"); throw_exception<std::out_of_range>("sorted_vector_map::at");
} }
size_type count(const key_type& key) const { size_type count(const key_type& key) const {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment