This revision was automatically updated to reflect the committed changes.
Closed by commit rL303348: Add Status -- llvm::Error glue (authored by labath).

Changed prior to commit:
  https://reviews.llvm.org/D33241?vs=99283&id=99429#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D33241

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

Index: lldb/trunk/include/lldb/Utility/Status.h
===================================================================
--- lldb/trunk/include/lldb/Utility/Status.h
+++ lldb/trunk/include/lldb/Utility/Status.h
@@ -1,29 +1,26 @@
-//===-- Status.h -------------------------------------------------*- C++
-//-*-===//
+//===-- Status.h ------------------------------------------------*- C++ -*-===//
 //
 //                     The LLVM Compiler Infrastructure
 //
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 
-#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 @@
 
   ~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 @@
 };
 }
 
-#endif // #if defined(__cplusplus)
-#endif // #ifndef __DCError_h__
+#endif // #ifndef LLDB_UTILITY_STATUS_H
Index: lldb/trunk/unittests/Utility/StatusTest.cpp
===================================================================
--- lldb/trunk/unittests/Utility/StatusTest.cpp
+++ lldb/trunk/unittests/Utility/StatusTest.cpp
@@ -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)));
+}
Index: lldb/trunk/source/Utility/Status.cpp
===================================================================
--- lldb/trunk/source/Utility/Status.cpp
+++ lldb/trunk/source/Utility/Status.cpp
@@ -56,6 +56,37 @@
   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
 //----------------------------------------------------------------------
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to