[llvm] r264221 - [Support] Make all Errors convertible to std::error_code.
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 23 16:57:28 PDT 2016
Author: lhames
Date: Wed Mar 23 18:57:28 2016
New Revision: 264221
URL: http://llvm.org/viewvc/llvm-project?rev=264221&view=rev
Log:
[Support] Make all Errors convertible to std::error_code.
This is a temporary crutch to enable code that currently uses std::error_code
to be incrementally moved over to Error. Requiring all Error instances be
convertible enables clients to call errorToErrorCode on any error (not just
ECErrors created by conversion *from* an error_code).
This patch also moves code for Error from ErrorHandling.cpp into a new
Error.cpp file.
Added:
llvm/trunk/lib/Support/Error.cpp
Modified:
llvm/trunk/include/llvm/Support/Error.h
llvm/trunk/lib/Support/CMakeLists.txt
llvm/trunk/lib/Support/ErrorHandling.cpp
llvm/trunk/unittests/Support/ErrorTest.cpp
Modified: llvm/trunk/include/llvm/Support/Error.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Error.h?rev=264221&r1=264220&r2=264221&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Error.h (original)
+++ llvm/trunk/include/llvm/Support/Error.h Wed Mar 23 18:57:28 2016
@@ -35,6 +35,12 @@ public:
/// Print an error message to an output stream.
virtual void log(raw_ostream &OS) const = 0;
+ /// Convert this error to a std::error_code.
+ ///
+ /// This is a temporary crutch to enable interaction with code still
+ /// using std::error_code. It will be removed in the future.
+ virtual std::error_code convertToErrorCode() const = 0;
+
// Check whether this instance is a subclass of the class identified by
// ClassID.
virtual bool isA(const void *const ClassID) const {
@@ -309,6 +315,8 @@ public:
}
}
+ std::error_code convertToErrorCode() const override;
+
private:
ErrorList(std::unique_ptr<ErrorInfoBase> Payload1,
std::unique_ptr<ErrorInfoBase> Payload2) {
@@ -717,7 +725,8 @@ class ECError : public ErrorInfo<ECError
public:
ECError() = default;
ECError(std::error_code EC) : EC(EC) {}
- std::error_code getErrorCode() const { return EC; }
+ void setErrorCode(std::error_code EC) { this->EC = EC; }
+ std::error_code convertToErrorCode() const override { return EC; }
void log(raw_ostream &OS) const override { OS << EC.message(); }
protected:
@@ -738,7 +747,9 @@ inline Error errorCodeToError(std::error
inline std::error_code errorToErrorCode(Error Err) {
std::error_code EC;
handleAllErrors(std::move(Err),
- [&](const ECError &ECE) { EC = ECE.getErrorCode(); });
+ [&](const ErrorInfoBase &EI) {
+ EC = EI.convertToErrorCode();
+ });
return EC;
}
Modified: llvm/trunk/lib/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CMakeLists.txt?rev=264221&r1=264220&r2=264221&view=diff
==============================================================================
--- llvm/trunk/lib/Support/CMakeLists.txt (original)
+++ llvm/trunk/lib/Support/CMakeLists.txt Wed Mar 23 18:57:28 2016
@@ -48,6 +48,7 @@ add_llvm_library(LLVMSupport
DeltaAlgorithm.cpp
DAGDeltaAlgorithm.cpp
Dwarf.cpp
+ Error.cpp
ErrorHandling.cpp
FileUtilities.cpp
FileOutputBuffer.cpp
Added: llvm/trunk/lib/Support/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Error.cpp?rev=264221&view=auto
==============================================================================
--- llvm/trunk/lib/Support/Error.cpp (added)
+++ llvm/trunk/lib/Support/Error.cpp Wed Mar 23 18:57:28 2016
@@ -0,0 +1,45 @@
+//===----- lib/Support/Error.cpp - Error and associated utilities ---------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/Error.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/ManagedStatic.h"
+
+using namespace llvm;
+
+namespace {
+
+ enum class ErrorErrorCode {
+ MultipleErrors
+ };
+
+ class ErrorErrorCategory : public std::error_category {
+ public:
+ const char *name() const LLVM_NOEXCEPT override { return "Error"; }
+
+ std::string message(int condition) const override {
+ switch (static_cast<ErrorErrorCode>(condition)) {
+ case ErrorErrorCode::MultipleErrors:
+ return "Multiple errors";
+ };
+ llvm_unreachable("Unhandled error code");
+ }
+ };
+
+};
+
+void ErrorInfoBase::anchor() {}
+char ErrorInfoBase::ID = 0;
+
+static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
+
+std::error_code ErrorList::convertToErrorCode() const {
+ return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
+ *ErrorErrorCat);
+}
Modified: llvm/trunk/lib/Support/ErrorHandling.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/ErrorHandling.cpp?rev=264221&r1=264220&r2=264221&view=diff
==============================================================================
--- llvm/trunk/lib/Support/ErrorHandling.cpp (original)
+++ llvm/trunk/lib/Support/ErrorHandling.cpp Wed Mar 23 18:57:28 2016
@@ -139,9 +139,6 @@ void LLVMResetFatalErrorHandler() {
remove_fatal_error_handler();
}
-void ErrorInfoBase::anchor() {}
-char ErrorInfoBase::ID = 0;
-
#ifdef LLVM_ON_WIN32
#include <winerror.h>
Modified: llvm/trunk/unittests/Support/ErrorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ErrorTest.cpp?rev=264221&r1=264220&r2=264221&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ErrorTest.cpp (original)
+++ llvm/trunk/unittests/Support/ErrorTest.cpp Wed Mar 23 18:57:28 2016
@@ -9,6 +9,7 @@
#include "llvm/Support/Error.h"
#include "llvm/Support/Errc.h"
+#include "llvm/Support/ErrorHandling.h"
#include "gtest/gtest.h"
#include <memory>
@@ -30,6 +31,10 @@ public:
OS << "CustomError { " << getInfo() << "}";
}
+ std::error_code convertToErrorCode() const override {
+ llvm_unreachable("CustomError doesn't support ECError conversion");
+ }
+
protected:
// This error is subclassed below, but we can't use inheriting constructors
// yet, so we can't propagate the constructors through ErrorInfo. Instead
@@ -57,6 +62,10 @@ public:
OS << "CustomSubError { " << getInfo() << ", " << getExtraInfo() << "}";
}
+ std::error_code convertToErrorCode() const override {
+ llvm_unreachable("CustomSubError doesn't support ECError conversion");
+ }
+
protected:
int ExtraInfo;
};
More information about the llvm-commits
mailing list