From 4e7efffdcadf373d1ae7d17722634d3a849c1f7a Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Sun, 2 May 2021 12:05:49 -0700 Subject: [PATCH] revise exceptionStr Summary: Revise all overloads of `folly::exceptionStr` in terms of `type_info_of`, `exception_ptr_get_type`, and `exception_ptr_get_object`. No longer rely on `catch_exception` and no longer have inline preprocessor conditionals. Reviewed By: Orvid, luciang Differential Revision: D26333081 fbshipit-source-id: 318ce83b9f15a12d5a33f528134e6fb38bb78a62 --- folly/ExceptionString.cpp | 65 ++++++++---------------------- folly/ExceptionString.h | 4 +- folly/Executor.cpp | 12 ++---- folly/Executor.h | 6 +-- folly/test/ExceptionStringTest.cpp | 2 +- 5 files changed, 24 insertions(+), 65 deletions(-) diff --git a/folly/ExceptionString.cpp b/folly/ExceptionString.cpp index b762cf316c6..356586c6650 100644 --- a/folly/ExceptionString.cpp +++ b/folly/ExceptionString.cpp @@ -18,67 +18,34 @@ #include -#include -#include #include -#include #include +#include namespace folly { +namespace { + +fbstring exception_string_type(std::type_info const* ti) { + return ti ? demangle(*ti) : ""; +} + +} // namespace + /** * Debug string for an exception: include type and what(), if * defined. */ -fbstring exceptionStr(const std::exception& e) { -#if FOLLY_HAS_RTTI - fbstring rv(demangle(typeid(e))); - rv += ": "; -#else - fbstring rv("Exception (no RTTI available): "); -#endif - rv += e.what(); - return rv; +fbstring exceptionStr(std::exception const& e) { + auto prefix = exception_string_type(folly::type_info_of(e)); + return std::move(prefix) + ": " + e.what(); } -namespace { - -FOLLY_CREATE_MEMBER_INVOKER(invoke_cxa_exception_type_fn, __cxa_exception_type); - -struct fallback_cxa_exception_type_fn { - FOLLY_MAYBE_UNUSED FOLLY_ERASE_HACK_GCC std::type_info const* operator()( - std::exception_ptr const&) const noexcept { - return nullptr; - } -}; - -using invoke_or_fallback_cxa_exception_type_fn = std::conditional_t< - is_invocable_r_v< - std::type_info const*, - invoke_cxa_exception_type_fn, - std::exception_ptr const&>, - invoke_cxa_exception_type_fn, - fallback_cxa_exception_type_fn>; - -FOLLY_INLINE_VARIABLE constexpr invoke_or_fallback_cxa_exception_type_fn - invoke_or_fallback_cxa_exception_type; - -} // namespace - -fbstring exceptionStr(std::exception_ptr ep) { - if (!kHasExceptions) { - return "Exception (catch unavailable)"; +fbstring exceptionStr(std::exception_ptr const& ep) { + if (auto ex = exception_ptr_get_object(ep)) { + return exceptionStr(*ex); } - auto type = invoke_or_fallback_cxa_exception_type(ep); - return catch_exception( - [&]() -> fbstring { - return catch_exception( - [&]() -> fbstring { std::rethrow_exception(std::move(ep)); }, - static_cast(exceptionStr)); - }, - [&]() -> fbstring { - return type ? demangle(*type) : ""; - }); + return exception_string_type(exception_ptr_get_type(ep)); } } // namespace folly diff --git a/folly/ExceptionString.h b/folly/ExceptionString.h index f287ceea7c1..078c6c26cd5 100644 --- a/folly/ExceptionString.h +++ b/folly/ExceptionString.h @@ -26,8 +26,8 @@ namespace folly { * Debug string for an exception: include type and what(), if * defined. */ -fbstring exceptionStr(const std::exception& e); +fbstring exceptionStr(std::exception const& e); -fbstring exceptionStr(std::exception_ptr ep); +fbstring exceptionStr(std::exception_ptr const& ep); } // namespace folly diff --git a/folly/Executor.cpp b/folly/Executor.cpp index bc6e02aff8f..42adb28341b 100644 --- a/folly/Executor.cpp +++ b/folly/Executor.cpp @@ -26,15 +26,9 @@ namespace folly { -void Executor::invokeCatchingExnsLog( - char const* const prefix, std::exception const* const ex) { - auto const message = " threw unhandled "; - if (ex) { - LOG(ERROR) << prefix << message << exceptionStr(*ex); - } else { - auto ep = std::current_exception(); - LOG(ERROR) << prefix << message << exceptionStr(ep); - } +void Executor::invokeCatchingExnsLog(char const* const prefix) noexcept { + auto ep = std::current_exception(); + LOG(ERROR) << prefix << " threw unhandled " << exceptionStr(ep); } void Executor::addWithPriority(Func, int8_t /* priority */) { diff --git a/folly/Executor.h b/folly/Executor.h index 45b74a75ed3..42e7d5d38f6 100644 --- a/folly/Executor.h +++ b/folly/Executor.h @@ -232,8 +232,7 @@ class Executor { template FOLLY_ERASE static void invokeCatchingExns(char const* p, F f) noexcept { - auto h = [p](auto&... e) noexcept { invokeCatchingExnsLog(p, &e...); }; - catch_exception([&] { catch_exception(f, h); }, h); + catch_exception(f, invokeCatchingExnsLog, p); } protected: @@ -269,8 +268,7 @@ class Executor { } private: - static void invokeCatchingExnsLog( - char const* prefix, std::exception const* ex = nullptr); + static void invokeCatchingExnsLog(char const* prefix) noexcept; template static KeepAlive makeKeepAliveDummy(ExecutorT* executor) { diff --git a/folly/test/ExceptionStringTest.cpp b/folly/test/ExceptionStringTest.cpp index 39dfd080668..7209a99c3a5 100644 --- a/folly/test/ExceptionStringTest.cpp +++ b/folly/test/ExceptionStringTest.cpp @@ -30,7 +30,7 @@ TEST_F(ExceptionStringTest, exception_ptr) { TEST_F(ExceptionStringTest, exception_ptr_unknown) { auto ptr = std::make_exception_ptr(7); - auto expected = folly::kIsLibstdcpp ? "int" : ""; + auto expected = "int"; auto actual = folly::exceptionStr(ptr).toStdString(); EXPECT_EQ(expected, actual); }