summaryrefslogtreecommitdiffstats
path: root/llvm/include
diff options
context:
space:
mode:
authorLang Hames <lhames@gmail.com>2017-06-20 22:18:02 +0000
committerLang Hames <lhames@gmail.com>2017-06-20 22:18:02 +0000
commitcd22753689f6e91e0a2dc19b58a499d5c0b71790 (patch)
treefe772f535b7c7c03e9a23416365f8a83742140a9 /llvm/include
parent15ed29290643d2ee25e6b2368cda8c594abc6d7b (diff)
downloadbcm5719-llvm-cd22753689f6e91e0a2dc19b58a499d5c0b71790.tar.gz
bcm5719-llvm-cd22753689f6e91e0a2dc19b58a499d5c0b71790.zip
Add a cantFail overload for Expected-reference (Expected<T&>) types.
llvm-svn: 305863
Diffstat (limited to 'llvm/include')
-rw-r--r--llvm/include/llvm/Support/Error.h21
1 files changed, 21 insertions, 0 deletions
diff --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h
index 1e27e0b821f..9a7fa0ae635 100644
--- a/llvm/include/llvm/Support/Error.h
+++ b/llvm/include/llvm/Support/Error.h
@@ -1076,6 +1076,27 @@ T cantFail(Expected<T> ValOrErr) {
llvm_unreachable("Failure value returned from cantFail wrapped call");
}
+/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and
+/// returns the contained reference.
+///
+/// This function can be used to wrap calls to fallible functions ONLY when it
+/// is known that the Error will always be a success value. E.g.
+///
+/// @code{.cpp}
+/// // foo only attempts the fallible operation if DoFallibleOperation is
+/// // true. If DoFallibleOperation is false then foo always returns a Bar&.
+/// Expected<Bar&> foo(bool DoFallibleOperation);
+///
+/// Bar &X = cantFail(foo(false));
+/// @endcode
+template <typename T>
+T& cantFail(Expected<T&> ValOrErr) {
+ if (ValOrErr)
+ return *ValOrErr;
+ else
+ llvm_unreachable("Failure value returned from cantFail wrapped call");
+}
+
} // end namespace llvm
#endif // LLVM_SUPPORT_ERROR_H
OpenPOWER on IntegriCloud