Index: lldb/trunk/include/lldb/Utility/Status.h =================================================================== --- lldb/trunk/include/lldb/Utility/Status.h +++ lldb/trunk/include/lldb/Utility/Status.h @@ -1,5 +1,4 @@ -//===-- Status.h -------------------------------------------------*- C++ -//-*-===// +//===-- Status.h ------------------------------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -8,22 +7,20 @@ // //===----------------------------------------------------------------------===// -#ifndef __DCError_h__ -#define __DCError_h__ -#if defined(__cplusplus) +#ifndef LLDB_UTILITY_STATUS_H +#define LLDB_UTILITY_STATUS_H #include "lldb/lldb-defines.h" #include "lldb/lldb-enumerations.h" // for ErrorType, ErrorType... #include "llvm/ADT/StringRef.h" // for StringRef +#include "llvm/Support/Error.h" #include "llvm/Support/FormatVariadic.h" - #include +#include // for uint32_t #include #include // for error_code #include // for forward -#include // for uint32_t - namespace llvm { class raw_ostream; } @@ -106,6 +103,10 @@ ~Status(); + // llvm::Error support + explicit Status(llvm::Error error); + llvm::Error ToError() const; + //------------------------------------------------------------------ /// Get the error string associated with the current error. // @@ -274,5 +275,4 @@ }; } -#endif // #if defined(__cplusplus) -#endif // #ifndef __DCError_h__ +#endif // #ifndef LLDB_UTILITY_STATUS_H Index: lldb/trunk/source/Utility/Status.cpp =================================================================== --- lldb/trunk/source/Utility/Status.cpp +++ lldb/trunk/source/Utility/Status.cpp @@ -56,6 +56,37 @@ va_end(args); } +Status::Status(llvm::Error error) + : m_code(0), m_type(ErrorType::eErrorTypeGeneric) { + if (!error) + return; + + // if the error happens to be a errno error, preserve the error code + error = llvm::handleErrors( + std::move(error), [&](std::unique_ptr e) -> llvm::Error { + std::error_code ec = e->convertToErrorCode(); + if (ec.category() == std::generic_category()) { + m_code = ec.value(); + m_type = ErrorType::eErrorTypePOSIX; + return llvm::Error::success(); + } + return llvm::Error(std::move(e)); + }); + + // Otherwise, just preserve the message + if (error) + SetErrorString(llvm::toString(std::move(error))); +} + +llvm::Error Status::ToError() const { + if (Success()) + return llvm::Error::success(); + if (m_type == ErrorType::eErrorTypePOSIX) + return llvm::errorCodeToError(std::error_code(m_code, std::generic_category())); + return llvm::make_error(AsCString(), + llvm::inconvertibleErrorCode()); +} + //---------------------------------------------------------------------- // Assignment operator //---------------------------------------------------------------------- Index: lldb/trunk/unittests/Utility/StatusTest.cpp =================================================================== --- lldb/trunk/unittests/Utility/StatusTest.cpp +++ lldb/trunk/unittests/Utility/StatusTest.cpp @@ -11,9 +11,40 @@ #include "gtest/gtest.h" using namespace lldb_private; +using namespace lldb; TEST(StatusTest, Formatv) { EXPECT_EQ("", llvm::formatv("{0}", Status()).str()); EXPECT_EQ("Hello Status", llvm::formatv("{0}", Status("Hello Status")).str()); EXPECT_EQ("Hello", llvm::formatv("{0:5}", Status("Hello Error")).str()); } + +TEST(StatusTest, ErrorConstructor) { + EXPECT_TRUE(Status(llvm::Error::success()).Success()); + + Status eagain( + llvm::errorCodeToError(std::error_code(EAGAIN, std::generic_category()))); + EXPECT_TRUE(eagain.Fail()); + EXPECT_EQ(eErrorTypePOSIX, eagain.GetType()); + EXPECT_EQ(Status::ValueType(EAGAIN), eagain.GetError()); + + Status foo(llvm::make_error( + "foo", llvm::inconvertibleErrorCode())); + EXPECT_TRUE(foo.Fail()); + EXPECT_EQ(eErrorTypeGeneric, foo.GetType()); + EXPECT_STREQ("foo", foo.AsCString()); +} + +TEST(StatusTest, ErrorConversion) { + EXPECT_FALSE(bool(Status().ToError())); + + llvm::Error eagain = Status(EAGAIN, ErrorType::eErrorTypePOSIX).ToError(); + EXPECT_TRUE(bool(eagain)); + std::error_code ec = llvm::errorToErrorCode(std::move(eagain)); + EXPECT_EQ(EAGAIN, ec.value()); + EXPECT_EQ(std::generic_category(), ec.category()); + + llvm::Error foo = Status("foo").ToError(); + EXPECT_TRUE(bool(foo)); + EXPECT_EQ("foo", llvm::toString(std::move(foo))); +}