Index: include/llvm/Support/Error.h =================================================================== --- include/llvm/Support/Error.h +++ include/llvm/Support/Error.h @@ -207,6 +207,20 @@ return getPtr() && getPtr()->isA(ErrT::classID()); } + /// Get the error message. Uses the log method on all contained instances of + /// ErrorInfoBase if this Error is in a failure state, and the empty string + /// if the error is in a Success state. This has the same effect on the + /// underlying Error as a bool conversion. + std::string message() { + this->operator bool(); + std::string Msg; + if (getPtr()) { + raw_string_ostream OS(Msg); + getPtr()->log(OS); + } + return Msg; + } + private: void assertIsChecked() { #ifndef NDEBUG Index: unittests/Support/ErrorTest.cpp =================================================================== --- unittests/Support/ErrorTest.cpp +++ unittests/Support/ErrorTest.cpp @@ -544,4 +544,13 @@ } } +// Test that error messages work. +TEST(Error, ErrorMessage) { + EXPECT_EQ(Error::success().message().compare(""), 0); + + auto E = make_error(0); + EXPECT_EQ(E.message().compare("CustomError { 0}"), 0); + consumeError(std::move(E)); +} + } // end anon namespace