[Lldb-commits] [lldb] r303348 - Add Status -- llvm::Error glue

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Thu May 18 05:46:51 PDT 2017


Author: labath
Date: Thu May 18 07:46:50 2017
New Revision: 303348

URL: http://llvm.org/viewvc/llvm-project?rev=303348&view=rev
Log:
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

Modified:
    lldb/trunk/include/lldb/Utility/Status.h
    lldb/trunk/source/Utility/Status.cpp
    lldb/trunk/unittests/Utility/StatusTest.cpp

Modified: lldb/trunk/include/lldb/Utility/Status.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/Status.h?rev=303348&r1=303347&r2=303348&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/Status.h (original)
+++ lldb/trunk/include/lldb/Utility/Status.h Thu May 18 07:46:50 2017
@@ -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_
 };
 }
 
-#endif // #if defined(__cplusplus)
-#endif // #ifndef __DCError_h__
+#endif // #ifndef LLDB_UTILITY_STATUS_H

Modified: lldb/trunk/source/Utility/Status.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/Status.cpp?rev=303348&r1=303347&r2=303348&view=diff
==============================================================================
--- lldb/trunk/source/Utility/Status.cpp (original)
+++ lldb/trunk/source/Utility/Status.cpp Thu May 18 07:46:50 2017
@@ -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
 //----------------------------------------------------------------------

Modified: lldb/trunk/unittests/Utility/StatusTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/StatusTest.cpp?rev=303348&r1=303347&r2=303348&view=diff
==============================================================================
--- lldb/trunk/unittests/Utility/StatusTest.cpp (original)
+++ lldb/trunk/unittests/Utility/StatusTest.cpp Thu May 18 07:46:50 2017
@@ -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)));
+}




More information about the lldb-commits mailing list