llvm.org GIT mirror llvm / cfcd4a3
[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 Lang Hames 2 years ago
2 changed file(s) with 23 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
675675 ///
676676 /// cantFail(foo(false));
677677 /// @endcode
678 inline void cantFail(Error Err) {
679 if (Err)
680 llvm_unreachable("Failure value returned from cantFail wrapped call");
678 inline void cantFail(Error Err, const char *Msg = nullptr) {
679 if (Err) {
680 if (!Msg)
681 Msg = "Failure value returned from cantFail wrapped call";
682 llvm_unreachable(Msg);
683 }
681684 }
682685
683686 /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and
694697 /// int X = cantFail(foo(false));
695698 /// @endcode
696699 template
697 T cantFail(Expected ValOrErr) {
700 T cantFail(Expected ValOrErr, const char *Msg = nullptr) {
698701 if (ValOrErr)
699702 return std::move(*ValOrErr);
700 else
701 llvm_unreachable("Failure value returned from cantFail wrapped call");
703 else {
704 if (!Msg)
705 Msg = "Failure value returned from cantFail wrapped call";
706 llvm_unreachable(Msg);
707 }
702708 }
703709
704710 /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and
715721 /// Bar &X = cantFail(foo(false));
716722 /// @endcode
717723 template
718 T& cantFail(Expected ValOrErr) {
724 T& cantFail(Expected ValOrErr, const char *Msg = nullptr) {
719725 if (ValOrErr)
720726 return *ValOrErr;
721 else
722 llvm_unreachable("Failure value returned from cantFail wrapped call");
727 else {
728 if (!Msg)
729 Msg = "Failure value returned from cantFail wrapped call";
730 llvm_unreachable(Msg);
731 }
723732 }
724733
725734 /// Helper for testing applicability of, and applying, handlers for
774783 }
775784 };
776785
777 /// Specialization for functions of the form 'Error (std::unique_ptr)'.
786 /// Specialization for functions of the form 'void (std::unique_ptr)'.
778787 template
779788 class ErrorHandlerTraits)> {
780789 public:
812821 : public ErrorHandlerTraits {};
813822
814823 /// Specialization for member functions of the form
815 /// 'RetT (std::unique_ptr) const'.
824 /// 'RetT (std::unique_ptr)'.
816825 template
817826 class ErrorHandlerTraits)>
818827 : public ErrorHandlerTraits)> {};
485485 #if LLVM_ENABLE_ABI_BREAKING_CHECKS && !defined(NDEBUG)
486486 TEST(Error, CantFailDeath) {
487487 EXPECT_DEATH(
488 cantFail(make_error("foo", inconvertibleErrorCode())),
489 "Failure value returned from cantFail wrapped call")
488 cantFail(make_error("foo", inconvertibleErrorCode()),
489 "Cantfail call failed"),
490 "Cantfail call failed")
490491 << "cantFail(Error) did not cause an abort for failure value";
491492
492493 EXPECT_DEATH(