<div><div dir="auto">Very cool, thanks...</div><br><div class="gmail_quote"><div>On Tue, Aug 29, 2017 at 4:30 PM Lang Hames via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: lhames<br>
Date: Tue Aug 29 16:29:09 2017<br>
New Revision: 312066<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=312066&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=312066&view=rev</a><br>
Log:<br>
[Error] Add an optional error message to cantFail.<br>
<br>
cantFail is the moral equivalent of an assertion that the wrapped call must<br>
return a success value. This patch allows clients to include an associated<br>
error message (the same way they would for an assertion for llvm_unreachable).<br>
<br>
If the error message is not specified it will default to: "Failure value<br>
returned from cantFail wrapped call".<br>
<br>
<br>
Modified:<br>
llvm/trunk/include/llvm/Support/Error.h<br>
llvm/trunk/unittests/Support/ErrorTest.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/Support/Error.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Error.h?rev=312066&r1=312065&r2=312066&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Error.h?rev=312066&r1=312065&r2=312066&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Support/Error.h (original)<br>
+++ llvm/trunk/include/llvm/Support/Error.h Tue Aug 29 16:29:09 2017<br>
@@ -676,9 +676,12 @@ LLVM_ATTRIBUTE_NORETURN void report_fata<br>
///<br>
/// cantFail(foo(false));<br>
/// @endcode<br>
-inline void cantFail(Error Err) {<br>
- if (Err)<br>
- llvm_unreachable("Failure value returned from cantFail wrapped call");<br>
+inline void cantFail(Error Err, const char *Msg = nullptr) {<br>
+ if (Err) {<br>
+ if (!Msg)<br>
+ Msg = "Failure value returned from cantFail wrapped call";<br>
+ llvm_unreachable(Msg);<br>
+ }<br>
}<br>
<br>
/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and<br>
@@ -695,11 +698,14 @@ inline void cantFail(Error Err) {<br>
/// int X = cantFail(foo(false));<br>
/// @endcode<br>
template <typename T><br>
-T cantFail(Expected<T> ValOrErr) {<br>
+T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) {<br>
if (ValOrErr)<br>
return std::move(*ValOrErr);<br>
- else<br>
- llvm_unreachable("Failure value returned from cantFail wrapped call");<br>
+ else {<br>
+ if (!Msg)<br>
+ Msg = "Failure value returned from cantFail wrapped call";<br>
+ llvm_unreachable(Msg);<br>
+ }<br>
}<br>
<br>
/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and<br>
@@ -716,11 +722,14 @@ T cantFail(Expected<T> ValOrErr) {<br>
/// Bar &X = cantFail(foo(false));<br>
/// @endcode<br>
template <typename T><br>
-T& cantFail(Expected<T&> ValOrErr) {<br>
+T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) {<br>
if (ValOrErr)<br>
return *ValOrErr;<br>
- else<br>
- llvm_unreachable("Failure value returned from cantFail wrapped call");<br>
+ else {<br>
+ if (!Msg)<br>
+ Msg = "Failure value returned from cantFail wrapped call";<br>
+ llvm_unreachable(Msg);<br>
+ }<br>
}<br>
<br>
/// Helper for testing applicability of, and applying, handlers for<br>
@@ -775,7 +784,7 @@ public:<br>
}<br>
};<br>
<br>
-/// Specialization for functions of the form 'Error (std::unique_ptr<ErrT>)'.<br>
+/// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'.<br>
template <typename ErrT><br>
class ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> {<br>
public:<br>
@@ -813,7 +822,7 @@ class ErrorHandlerTraits<RetT (C::*)(con<br>
: public ErrorHandlerTraits<RetT (&)(ErrT &)> {};<br>
<br>
/// Specialization for member functions of the form<br>
-/// 'RetT (std::unique_ptr<ErrT>) const'.<br>
+/// 'RetT (std::unique_ptr<ErrT>)'.<br>
template <typename C, typename RetT, typename ErrT><br>
class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)><br>
: public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {};<br>
<br>
Modified: llvm/trunk/unittests/Support/ErrorTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ErrorTest.cpp?rev=312066&r1=312065&r2=312066&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ErrorTest.cpp?rev=312066&r1=312065&r2=312066&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/Support/ErrorTest.cpp (original)<br>
+++ llvm/trunk/unittests/Support/ErrorTest.cpp Tue Aug 29 16:29:09 2017<br>
@@ -486,8 +486,9 @@ TEST(Error, CantFailSuccess) {<br>
#if LLVM_ENABLE_ABI_BREAKING_CHECKS && !defined(NDEBUG)<br>
TEST(Error, CantFailDeath) {<br>
EXPECT_DEATH(<br>
- cantFail(make_error<StringError>("foo", inconvertibleErrorCode())),<br>
- "Failure value returned from cantFail wrapped call")<br>
+ cantFail(make_error<StringError>("foo", inconvertibleErrorCode()),<br>
+ "Cantfail call failed"),<br>
+ "Cantfail call failed")<br>
<< "cantFail(Error) did not cause an abort for failure value";<br>
<br>
EXPECT_DEATH(<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div></div>