From: Lang Hames Date: Tue, 29 Aug 2017 23:29:09 +0000 (+0000) Subject: [Error] Add an optional error message to cantFail. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cfcd4a35478076a79343337aacb97062e0e0dd18;p=llvm [Error] Add an optional error message to cantFail. cantFail is the moral equivalent of an assertion that the wrapped call must return a success value. This patch allows clients to include an associated error message (the same way they would for an assertion for llvm_unreachable). If the error message is not specified it will default to: "Failure value returned from cantFail wrapped call". git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@312066 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Support/Error.h b/include/llvm/Support/Error.h index 6aca32bf076..bb40dbee053 100644 --- a/include/llvm/Support/Error.h +++ b/include/llvm/Support/Error.h @@ -676,9 +676,12 @@ LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, /// /// cantFail(foo(false)); /// @endcode -inline void cantFail(Error Err) { - if (Err) - llvm_unreachable("Failure value returned from cantFail wrapped call"); +inline void cantFail(Error Err, const char *Msg = nullptr) { + if (Err) { + if (!Msg) + Msg = "Failure value returned from cantFail wrapped call"; + llvm_unreachable(Msg); + } } /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and @@ -695,11 +698,14 @@ inline void cantFail(Error Err) { /// int X = cantFail(foo(false)); /// @endcode template -T cantFail(Expected ValOrErr) { +T cantFail(Expected ValOrErr, const char *Msg = nullptr) { if (ValOrErr) return std::move(*ValOrErr); - else - llvm_unreachable("Failure value returned from cantFail wrapped call"); + else { + if (!Msg) + Msg = "Failure value returned from cantFail wrapped call"; + llvm_unreachable(Msg); + } } /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and @@ -716,11 +722,14 @@ T cantFail(Expected ValOrErr) { /// Bar &X = cantFail(foo(false)); /// @endcode template -T& cantFail(Expected ValOrErr) { +T& cantFail(Expected ValOrErr, const char *Msg = nullptr) { if (ValOrErr) return *ValOrErr; - else - llvm_unreachable("Failure value returned from cantFail wrapped call"); + else { + if (!Msg) + Msg = "Failure value returned from cantFail wrapped call"; + llvm_unreachable(Msg); + } } /// Helper for testing applicability of, and applying, handlers for @@ -775,7 +784,7 @@ public: } }; -/// Specialization for functions of the form 'Error (std::unique_ptr)'. +/// Specialization for functions of the form 'void (std::unique_ptr)'. template class ErrorHandlerTraits)> { public: @@ -813,7 +822,7 @@ class ErrorHandlerTraits : public ErrorHandlerTraits {}; /// Specialization for member functions of the form -/// 'RetT (std::unique_ptr) const'. +/// 'RetT (std::unique_ptr)'. template class ErrorHandlerTraits)> : public ErrorHandlerTraits)> {}; diff --git a/unittests/Support/ErrorTest.cpp b/unittests/Support/ErrorTest.cpp index db332ebff7a..6010122a9a0 100644 --- a/unittests/Support/ErrorTest.cpp +++ b/unittests/Support/ErrorTest.cpp @@ -486,8 +486,9 @@ TEST(Error, CantFailSuccess) { #if LLVM_ENABLE_ABI_BREAKING_CHECKS && !defined(NDEBUG) TEST(Error, CantFailDeath) { EXPECT_DEATH( - cantFail(make_error("foo", inconvertibleErrorCode())), - "Failure value returned from cantFail wrapped call") + cantFail(make_error("foo", inconvertibleErrorCode()), + "Cantfail call failed"), + "Cantfail call failed") << "cantFail(Error) did not cause an abort for failure value"; EXPECT_DEATH(