Add Status -- llvm::Error glue

Summary:
This adds functions to convert between llvm::Error and Status classes.
Posix errors in Status are represented as llvm::ECError, and the rest as
llvm::StringError.

For the conversion from Error to Status, ECError is again represented as
a posix error in Status, while other errors are stored as generic errors
and only the string value is preserved.

Reviewers: zturner, jingham

Subscribers: lldb-commits

Differential Revision: https://reviews.llvm.org/D33241

llvm-svn: 303348
This commit is contained in:
Pavel Labath 2017-05-18 12:46:50 +00:00
parent ebfdaf7394
commit a24a3a30d0
3 changed files with 72 additions and 10 deletions

View File

@ -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 <cstdarg>
#include <stdint.h> // for uint32_t
#include <string>
#include <system_error> // for error_code
#include <type_traits> // for forward
#include <stdint.h> // for uint32_t
namespace llvm {
class raw_ostream;
}
@ -106,6 +103,10 @@ public:
~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 @@ template <> struct format_provider<lldb_private::Status> {
};
}
#endif // #if defined(__cplusplus)
#endif // #ifndef __DCError_h__
#endif // #ifndef LLDB_UTILITY_STATUS_H

View File

@ -56,6 +56,37 @@ Status::Status(const char *format, ...)
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<llvm::ECError> 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<llvm::StringError>(AsCString(),
llvm::inconvertibleErrorCode());
}
//----------------------------------------------------------------------
// Assignment operator
//----------------------------------------------------------------------

View File

@ -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<llvm::StringError>(
"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)));
}