[Lldb-commits] [lldb] f512b97 - [lldb/Utility] Improve error_code->Status conversion

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Thu Apr 23 07:12:52 PDT 2020


Author: Pavel Labath
Date: 2020-04-23T16:12:41+02:00
New Revision: f512b978b0ebd09be08d879acb4feb7846370cd5

URL: https://github.com/llvm/llvm-project/commit/f512b978b0ebd09be08d879acb4feb7846370cd5
DIFF: https://github.com/llvm/llvm-project/commit/f512b978b0ebd09be08d879acb4feb7846370cd5.diff

LOG: [lldb/Utility] Improve error_code->Status conversion

Both entities have the notion of error "namespaces". Map the errno
namespace correctly.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lldb/source/Utility/Status.cpp b/lldb/source/Utility/Status.cpp
index c8ca485cdd3b..e3c4284a8e8a 100644
--- a/lldb/source/Utility/Status.cpp
+++ b/lldb/source/Utility/Status.cpp
@@ -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, ...)

diff  --git a/lldb/unittests/Utility/StatusTest.cpp b/lldb/unittests/Utility/StatusTest.cpp
index 516f10b5b439..862c063b2e06 100644
--- a/lldb/unittests/Utility/StatusTest.cpp
+++ b/lldb/unittests/Utility/StatusTest.cpp
@@ -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()));
 


        


More information about the lldb-commits mailing list