[llvm] r312066 - [Error] Add an optional error message to cantFail.
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 29 16:29:09 PDT 2017
Author: lhames
Date: Tue Aug 29 16:29:09 2017
New Revision: 312066
URL: http://llvm.org/viewvc/llvm-project?rev=312066&view=rev
Log:
[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".
Modified:
llvm/trunk/include/llvm/Support/Error.h
llvm/trunk/unittests/Support/ErrorTest.cpp
Modified: llvm/trunk/include/llvm/Support/Error.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Error.h?rev=312066&r1=312065&r2=312066&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Error.h (original)
+++ llvm/trunk/include/llvm/Support/Error.h Tue Aug 29 16:29:09 2017
@@ -676,9 +676,12 @@ LLVM_ATTRIBUTE_NORETURN void report_fata
///
/// 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 <typename T>
-T cantFail(Expected<T> ValOrErr) {
+T cantFail(Expected<T> 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<T> ValOrErr) {
/// Bar &X = cantFail(foo(false));
/// @endcode
template <typename T>
-T& cantFail(Expected<T&> ValOrErr) {
+T& cantFail(Expected<T&> 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<ErrT>)'.
+/// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'.
template <typename ErrT>
class ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> {
public:
@@ -813,7 +822,7 @@ class ErrorHandlerTraits<RetT (C::*)(con
: public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
/// Specialization for member functions of the form
-/// 'RetT (std::unique_ptr<ErrT>) const'.
+/// 'RetT (std::unique_ptr<ErrT>)'.
template <typename C, typename RetT, typename ErrT>
class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)>
: public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {};
Modified: llvm/trunk/unittests/Support/ErrorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ErrorTest.cpp?rev=312066&r1=312065&r2=312066&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ErrorTest.cpp (original)
+++ llvm/trunk/unittests/Support/ErrorTest.cpp Tue Aug 29 16:29:09 2017
@@ -486,8 +486,9 @@ TEST(Error, CantFailSuccess) {
#if LLVM_ENABLE_ABI_BREAKING_CHECKS && !defined(NDEBUG)
TEST(Error, CantFailDeath) {
EXPECT_DEATH(
- cantFail(make_error<StringError>("foo", inconvertibleErrorCode())),
- "Failure value returned from cantFail wrapped call")
+ cantFail(make_error<StringError>("foo", inconvertibleErrorCode()),
+ "Cantfail call failed"),
+ "Cantfail call failed")
<< "cantFail(Error) did not cause an abort for failure value";
EXPECT_DEATH(
More information about the llvm-commits
mailing list