Skip to content

Commit cb45a87

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 0cbb7f1 commit cb45a87

File tree

2 files changed

+217
-86
lines changed

2 files changed

+217
-86
lines changed

lldb/include/lldb/Utility/Status.h

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,52 @@ class raw_ostream;
2626

2727
namespace lldb_private {
2828

29+
/// Going a bit against the spirit of llvm::Error,
30+
/// lldb_private::Status need to store errors long-term and sometimes
31+
/// copy them. This base class defines an interface for this
32+
/// operation.
33+
class CloneableError
34+
: public llvm::ErrorInfo<CloneableError, llvm::StringError> {
35+
public:
36+
using llvm::ErrorInfo<CloneableError, llvm::StringError>::ErrorInfo;
37+
CloneableError(std::error_code ec, const llvm::Twine &msg = {})
38+
: ErrorInfo(msg, ec) {}
39+
virtual std::unique_ptr<CloneableError> Clone() const = 0;
40+
static char ID;
41+
};
42+
43+
/// FIXME: Move these declarations closer to where they're used.
44+
class MachKernelError
45+
: public llvm::ErrorInfo<MachKernelError, CloneableError> {
46+
public:
47+
using llvm::ErrorInfo<MachKernelError, CloneableError>::ErrorInfo;
48+
MachKernelError(std::error_code ec, const llvm::Twine &msg = {})
49+
: ErrorInfo(msg, ec) {}
50+
std::string message() const override;
51+
std::unique_ptr<CloneableError> Clone() const override;
52+
static char ID;
53+
};
54+
55+
class Win32Error : public llvm::ErrorInfo<Win32Error, CloneableError> {
56+
public:
57+
using llvm::ErrorInfo<Win32Error, CloneableError>::ErrorInfo;
58+
Win32Error(std::error_code ec, const llvm::Twine &msg = {})
59+
: ErrorInfo(msg, ec) {}
60+
std::string message() const override;
61+
std::unique_ptr<CloneableError> Clone() const override;
62+
static char ID;
63+
};
64+
65+
class ExpressionError
66+
: public llvm::ErrorInfo<ExpressionError, CloneableError> {
67+
public:
68+
using llvm::ErrorInfo<ExpressionError, CloneableError>::ErrorInfo;
69+
ExpressionError(std::error_code ec, std::string msg = {})
70+
: ErrorInfo(msg, ec) {}
71+
std::unique_ptr<CloneableError> Clone() const override;
72+
static char ID;
73+
};
74+
2975
const char *ExecutionResultAsCString(lldb::ExpressionResults result);
3076

3177
/// \class Status Status.h "lldb/Utility/Status.h" An error handling class.
@@ -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)