Skip to content

Commit 6e4559f

Browse files
committed
[lldb] Change the implementation of Status to store an llvm::Error (NFC)
Most APIs that currently vend a Status would be better served by returning llvm::Expected<> instead. Where possible, APIs should be refactored to avoid Status. The only legitimate long-term uses of Status are objects that need to store an error for a long time (which should be questioned as a design decision, too). This patch makes the transition to llvm::Error easier by making the places that cannot switch to llvm::Error explicit: They are marked with a call to Status::clone(). Every other API can and should be refactored to use llvm::Expected. In the end Status should only be used in very few places. Whenever an unchecked Error is dropped by Status it logs this to the verbose API channel. Implementation notes: This patch introduces two new kinds of error_category as well as new llvm::Error types. Here is the mapping of lldb::ErrorType to llvm::Errors: (eErrorTypeInvalid) eErrorTypeGeneric llvm::StringError eErrorTypePOSIX llvm::ECError eErrorTypeMachKernel MachKernelError eErrorTypeExpression llvm::ErrorList<ExpressionError> eErrorTypeWin32 Win32Error
1 parent 3b426a8 commit 6e4559f

File tree

2 files changed

+218
-86
lines changed

2 files changed

+218
-86
lines changed

lldb/include/lldb/Utility/Status.h

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,52 @@ namespace lldb_private {
2828

2929
const char *ExpressionResultAsCString(lldb::ExpressionResults result);
3030

31+
/// Going a bit against the spirit of llvm::Error,
32+
/// lldb_private::Status need to store errors long-term and sometimes
33+
/// copy them. This base class defines an interface for this
34+
/// operation.
35+
class CloneableError
36+
: public llvm::ErrorInfo<CloneableError, llvm::StringError> {
37+
public:
38+
using llvm::ErrorInfo<CloneableError, llvm::StringError>::ErrorInfo;
39+
CloneableError(std::error_code ec, const llvm::Twine &msg = {})
40+
: ErrorInfo(msg, ec) {}
41+
virtual std::unique_ptr<CloneableError> Clone() const = 0;
42+
static char ID;
43+
};
44+
45+
/// FIXME: Move these declarations closer to where they're used.
46+
class MachKernelError
47+
: public llvm::ErrorInfo<MachKernelError, CloneableError> {
48+
public:
49+
using llvm::ErrorInfo<MachKernelError, CloneableError>::ErrorInfo;
50+
MachKernelError(std::error_code ec, const llvm::Twine &msg = {})
51+
: ErrorInfo(msg, ec) {}
52+
std::string message() const override;
53+
std::unique_ptr<CloneableError> Clone() const override;
54+
static char ID;
55+
};
56+
57+
class Win32Error : public llvm::ErrorInfo<Win32Error, CloneableError> {
58+
public:
59+
using llvm::ErrorInfo<Win32Error, CloneableError>::ErrorInfo;
60+
Win32Error(std::error_code ec, const llvm::Twine &msg = {})
61+
: ErrorInfo(msg, ec) {}
62+
std::string message() const override;
63+
std::unique_ptr<CloneableError> Clone() const override;
64+
static char ID;
65+
};
66+
67+
class ExpressionError
68+
: public llvm::ErrorInfo<ExpressionError, CloneableError> {
69+
public:
70+
using llvm::ErrorInfo<ExpressionError, CloneableError>::ErrorInfo;
71+
ExpressionError(std::error_code ec, std::string msg = {})
72+
: ErrorInfo(msg, ec) {}
73+
std::unique_ptr<CloneableError> Clone() const override;
74+
static char ID;
75+
};
76+
3177
/// \class Status Status.h "lldb/Utility/Status.h" An error handling class.
3278
///
3379
/// This class is designed to be able to hold any error code that can be
@@ -100,9 +146,7 @@ class Status {
100146
}
101147

102148
static Status FromExpressionError(lldb::ExpressionResults result,
103-
std::string msg) {
104-
return Status(result, lldb::eErrorTypeExpression, msg);
105-
}
149+
std::string msg);
106150

107151
/// Set the current error to errno.
108152
///
@@ -115,6 +159,7 @@ class Status {
115159
const Status &operator=(Status &&);
116160
/// Avoid using this in new code. Migrate APIs to llvm::Expected instead.
117161
static Status FromError(llvm::Error error);
162+
118163
/// FIXME: Replace this with a takeError() method.
119164
llvm::Error ToError() const;
120165
/// Don't call this function in new code. Instead, redesign the API
@@ -170,12 +215,8 @@ class Status {
170215
bool Success() const;
171216

172217
protected:
173-
Status(llvm::Error error);
174-
/// Status code as an integer value.
175-
ValueType m_code = 0;
176-
/// The type of the above error code.
177-
lldb::ErrorType m_type = lldb::eErrorTypeInvalid;
178-
/// A string representation of the error code.
218+
Status(llvm::Error error) : m_error(std::move(error)) {}
219+
mutable llvm::Error m_error;
179220
mutable std::string m_string;
180221
};
181222

0 commit comments

Comments
 (0)