Skip to content

Commit e7aad35

Browse files
committed
[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. llvm-svn: 264221
1 parent ea00b49 commit e7aad35

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

llvm/include/llvm/Support/Error.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class ErrorInfoBase {
3535
/// Print an error message to an output stream.
3636
virtual void log(raw_ostream &OS) const = 0;
3737

38+
/// Convert this error to a std::error_code.
39+
///
40+
/// This is a temporary crutch to enable interaction with code still
41+
/// using std::error_code. It will be removed in the future.
42+
virtual std::error_code convertToErrorCode() const = 0;
43+
3844
// Check whether this instance is a subclass of the class identified by
3945
// ClassID.
4046
virtual bool isA(const void *const ClassID) const {
@@ -309,6 +315,8 @@ class ErrorList final : public ErrorInfo<ErrorList> {
309315
}
310316
}
311317

318+
std::error_code convertToErrorCode() const override;
319+
312320
private:
313321
ErrorList(std::unique_ptr<ErrorInfoBase> Payload1,
314322
std::unique_ptr<ErrorInfoBase> Payload2) {
@@ -717,7 +725,8 @@ class ECError : public ErrorInfo<ECError> {
717725
public:
718726
ECError() = default;
719727
ECError(std::error_code EC) : EC(EC) {}
720-
std::error_code getErrorCode() const { return EC; }
728+
void setErrorCode(std::error_code EC) { this->EC = EC; }
729+
std::error_code convertToErrorCode() const override { return EC; }
721730
void log(raw_ostream &OS) const override { OS << EC.message(); }
722731

723732
protected:
@@ -738,7 +747,9 @@ inline Error errorCodeToError(std::error_code EC) {
738747
inline std::error_code errorToErrorCode(Error Err) {
739748
std::error_code EC;
740749
handleAllErrors(std::move(Err),
741-
[&](const ECError &ECE) { EC = ECE.getErrorCode(); });
750+
[&](const ErrorInfoBase &EI) {
751+
EC = EI.convertToErrorCode();
752+
});
742753
return EC;
743754
}
744755

llvm/lib/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ add_llvm_library(LLVMSupport
4848
DeltaAlgorithm.cpp
4949
DAGDeltaAlgorithm.cpp
5050
Dwarf.cpp
51+
Error.cpp
5152
ErrorHandling.cpp
5253
FileUtilities.cpp
5354
FileOutputBuffer.cpp

llvm/lib/Support/Error.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===----- lib/Support/Error.cpp - Error and associated utilities ---------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "llvm/Support/Error.h"
11+
#include "llvm/Support/ErrorHandling.h"
12+
#include "llvm/Support/ManagedStatic.h"
13+
14+
using namespace llvm;
15+
16+
namespace {
17+
18+
enum class ErrorErrorCode {
19+
MultipleErrors
20+
};
21+
22+
class ErrorErrorCategory : public std::error_category {
23+
public:
24+
const char *name() const LLVM_NOEXCEPT override { return "Error"; }
25+
26+
std::string message(int condition) const override {
27+
switch (static_cast<ErrorErrorCode>(condition)) {
28+
case ErrorErrorCode::MultipleErrors:
29+
return "Multiple errors";
30+
};
31+
llvm_unreachable("Unhandled error code");
32+
}
33+
};
34+
35+
};
36+
37+
void ErrorInfoBase::anchor() {}
38+
char ErrorInfoBase::ID = 0;
39+
40+
static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
41+
42+
std::error_code ErrorList::convertToErrorCode() const {
43+
return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
44+
*ErrorErrorCat);
45+
}

llvm/lib/Support/ErrorHandling.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,6 @@ void LLVMResetFatalErrorHandler() {
139139
remove_fatal_error_handler();
140140
}
141141

142-
void ErrorInfoBase::anchor() {}
143-
char ErrorInfoBase::ID = 0;
144-
145142
#ifdef LLVM_ON_WIN32
146143

147144
#include <winerror.h>

llvm/unittests/Support/ErrorTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "llvm/Support/Error.h"
1111
#include "llvm/Support/Errc.h"
12+
#include "llvm/Support/ErrorHandling.h"
1213
#include "gtest/gtest.h"
1314
#include <memory>
1415

@@ -30,6 +31,10 @@ class CustomError : public ErrorInfo<CustomError> {
3031
OS << "CustomError { " << getInfo() << "}";
3132
}
3233

34+
std::error_code convertToErrorCode() const override {
35+
llvm_unreachable("CustomError doesn't support ECError conversion");
36+
}
37+
3338
protected:
3439
// This error is subclassed below, but we can't use inheriting constructors
3540
// yet, so we can't propagate the constructors through ErrorInfo. Instead
@@ -57,6 +62,10 @@ class CustomSubError : public ErrorInfo<CustomSubError, CustomError> {
5762
OS << "CustomSubError { " << getInfo() << ", " << getExtraInfo() << "}";
5863
}
5964

65+
std::error_code convertToErrorCode() const override {
66+
llvm_unreachable("CustomSubError doesn't support ECError conversion");
67+
}
68+
6069
protected:
6170
int ExtraInfo;
6271
};

0 commit comments

Comments
 (0)