[lldb/Utility] Improve error_code->Status conversion
authorPavel Labath <pavel@labath.sk>
Thu, 23 Apr 2020 13:55:39 +0000 (15:55 +0200)
committerPavel Labath <pavel@labath.sk>
Thu, 23 Apr 2020 14:12:41 +0000 (16:12 +0200)
Both entities have the notion of error "namespaces". Map the errno
namespace correctly.

lldb/source/Utility/Status.cpp
lldb/unittests/Utility/StatusTest.cpp

index c8ca485..e3c4284 100644 (file)
@@ -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, ...)
index 516f10b..862c063 100644 (file)
@@ -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()));