diff --git a/include/fmt/core.h b/include/fmt/core.h index 44cd025d..e6bec17e 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -117,6 +117,42 @@ # endif #endif +#ifndef FMT_THROW +# if FMT_EXCEPTIONS +# if FMT_MSC_VER || FMT_NVCC +FMT_BEGIN_NAMESPACE +namespace internal { +template inline void do_throw(const Exception& x) { + // Silence unreachable code warnings in MSVC and NVCC because these + // are nearly impossible to fix in a generic code. + volatile bool b = true; + if (b) throw x; +} +} // namespace internal +FMT_END_NAMESPACE +# define FMT_THROW(x) internal::do_throw(x) +# else +# define FMT_THROW(x) throw x +# endif +# define FMT_RETHROW() throw +# else +# define FMT_THROW(x) \ + do { \ + static_cast(sizeof(x)); \ + FMT_ASSERT(false, ""); \ + } while (false) +# define FMT_RETHROW() FMT_ASSERT(false, "") +# endif +#endif + +#if FMT_EXCEPTIONS +# define FMT_TRY try +# define FMT_CATCH(x) catch (x) +#else +# define FMT_TRY if (true) +# define FMT_CATCH(x) if (false) +#endif + // Define FMT_USE_NOEXCEPT to make fmt use noexcept (C++11 feature). #ifndef FMT_USE_NOEXCEPT # define FMT_USE_NOEXCEPT 0 @@ -1516,7 +1552,8 @@ class dynamic_format_arg_store unsigned long long get_types() const { return internal::is_unpacked_bit | data_.size() | - (named_info_.empty() ? 0ULL : internal::has_named_args_bit); + (named_info_.empty() ? 0ULL : + static_cast(internal::has_named_args_bit)); } const basic_format_arg* data() const { @@ -1532,12 +1569,12 @@ class dynamic_format_arg_store if (named_info_.empty()) data_.insert(data_.begin(), basic_format_arg{}); data_.emplace_back(internal::make_arg(arg)); - try { + FMT_TRY { named_info_.push_back({arg.name, static_cast(data_.size() - 2u)}); data_[0].value_.named_args = {named_info_.data(), named_info_.size()}; - } catch (...) { + } FMT_CATCH(...) { data_.pop_back(); - throw; + FMT_RETHROW(); } } diff --git a/include/fmt/format.h b/include/fmt/format.h index a087ca31..fd891ca8 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -88,40 +88,6 @@ # define FMT_FALLTHROUGH #endif -#ifndef FMT_THROW -# if FMT_EXCEPTIONS -# if FMT_MSC_VER || FMT_NVCC -FMT_BEGIN_NAMESPACE -namespace internal { -template inline void do_throw(const Exception& x) { - // Silence unreachable code warnings in MSVC and NVCC because these - // are nearly impossible to fix in a generic code. - volatile bool b = true; - if (b) throw x; -} -} // namespace internal -FMT_END_NAMESPACE -# define FMT_THROW(x) internal::do_throw(x) -# else -# define FMT_THROW(x) throw x -# endif -# else -# define FMT_THROW(x) \ - do { \ - static_cast(sizeof(x)); \ - FMT_ASSERT(false, ""); \ - } while (false) -# endif -#endif - -#if FMT_EXCEPTIONS -# define FMT_TRY try -# define FMT_CATCH(x) catch (x) -#else -# define FMT_TRY if (true) -# define FMT_CATCH(x) if (false) -#endif - #ifndef FMT_USE_USER_DEFINED_LITERALS // For Intel and NVIDIA compilers both they and the system gcc/msc support UDLs. # if (FMT_HAS_FEATURE(cxx_user_literals) || FMT_GCC_VERSION >= 407 || \