Skip to content

Commit

Permalink
[lldb/Utility] Improve error_code->Status conversion
Browse files Browse the repository at this point in the history
Both entities have the notion of error "namespaces". Map the errno
namespace correctly.
  • Loading branch information
labath committed Apr 23, 2020
1 parent d8a4a99 commit f512b97
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lldb/source/Utility/Status.cpp
Expand Up @@ -42,8 +42,13 @@ Status::Status() : m_code(0), m_type(eErrorTypeInvalid), m_string() {}
Status::Status(ValueType err, ErrorType type)
: m_code(err), m_type(type), m_string() {}

// This logic is confusing because c++ calls the traditional (posix) errno codes
// "generic errors", while we use the term "generic" to mean completely
// arbitrary (text-based) errors.
Status::Status(std::error_code EC)
: m_code(EC.value()), m_type(ErrorType::eErrorTypeGeneric),
: m_code(EC.value()),
m_type(EC.category() == std::generic_category() ? eErrorTypePOSIX
: eErrorTypeGeneric),
m_string(EC.message()) {}

Status::Status(const char *format, ...)
Expand Down
9 changes: 9 additions & 0 deletions lldb/unittests/Utility/StatusTest.cpp
Expand Up @@ -41,6 +41,15 @@ TEST(StatusTest, ErrorConstructor) {
EXPECT_TRUE(foo.Success());
}

TEST(StatusTest, ErrorCodeConstructor) {
EXPECT_TRUE(Status(std::error_code()).Success());

Status eagain = std::error_code(EAGAIN, std::generic_category());
EXPECT_TRUE(eagain.Fail());
EXPECT_EQ(eErrorTypePOSIX, eagain.GetType());
EXPECT_EQ(Status::ValueType(EAGAIN), eagain.GetError());
}

TEST(StatusTest, ErrorConversion) {
EXPECT_FALSE(bool(Status().ToError()));

Expand Down

0 comments on commit f512b97

Please sign in to comment.