[Lldb-commits] [lldb] [lldb] Change the implementation of Status to store an llvm::Error (NFC) (PR #106774)
Pavel Labath via lldb-commits
lldb-commits at lists.llvm.org
Fri Sep 6 00:32:21 PDT 2024
================
@@ -37,48 +39,75 @@ class raw_ostream;
using namespace lldb;
using namespace lldb_private;
-Status::Status() {}
+char CloneableError::ID;
+char MachKernelError::ID;
+char Win32Error::ID;
+char ExpressionError::ID;
+
+namespace {
+/// A std::error_code category for eErrorTypeGeneric.
+class GenericCategory : public std::error_category {
+ const char *name() const override { return "LLDBGenericCategory"; }
+ std::string message(int __ev) const override { return "generic LLDB error"; };
+};
+GenericCategory &generic_category() {
+ static GenericCategory g_generic_category;
+ return g_generic_category;
+}
+
+/// A std::error_code category for eErrorTypeExpression.
+class ExpressionCategory : public std::error_category {
+ const char *name() const override { return "LLDBExpressionCategory"; }
+ std::string message(int __ev) const override {
+ return ExecutionResultAsCString(static_cast<lldb::ExpressionResults>(__ev));
+ };
+};
+ExpressionCategory &expression_category() {
+ static ExpressionCategory g_expression_category;
+ return g_expression_category;
+}
+} // namespace
+
+Status::Status() : m_error(llvm::Error::success()) {}
+
+static llvm::Error ErrorFromEnums(Status::ValueType err, ErrorType type,
+ std::string msg) {
+ switch (type) {
+ case eErrorTypeMachKernel:
+ return llvm::make_error<MachKernelError>(
+ std::error_code(err, std::system_category()), msg);
+ case eErrorTypePOSIX:
+ return llvm::errorCodeToError(
+ std::error_code(err, std::generic_category()));
+ case eErrorTypeWin32:
+ return llvm::make_error<Win32Error>(
+ std::error_code(err, std::system_category()), msg);
+ default:
+ return llvm::createStringError(std::move(msg),
+ std::error_code(err, generic_category()));
+ }
+}
Status::Status(ValueType err, ErrorType type, std::string msg)
- : m_code(err), m_type(type), m_string(std::move(msg)) {}
+ : m_error(ErrorFromEnums(err, type, msg)) {}
-// This logic is confusing because c++ calls the traditional (posix) errno codes
+// This logic is confusing because C++ calls the traditional (posix) errno codes
// "generic errors", while we use the term "generic" to mean completely
// arbitrary (text-based) errors.
Status::Status(std::error_code EC)
- : m_code(EC.value()),
- m_type(EC.category() == std::generic_category() ? eErrorTypePOSIX
- : eErrorTypeGeneric),
- m_string(EC.message()) {}
+ : m_error(!EC ? llvm::Error::success()
+ : (EC.category() == std::generic_category()
+ ? llvm::errorCodeToError(EC)
+ : llvm::createStringError(EC, "generic error"))) {}
----------------
labath wrote:
I'm not sure I follow. I agree that we ought to return eErrorTypePosix for errors codes with `std::generic_category()`. However, I don't see how that would be lost by using ECError for representing error codes from *other* categories. We should be able check that at the point where we compute the error type by looking at whether the Error is an ECError *and* whether it stores a error code of the `std::generic_category()` category.
(And I might argue that we don't even need the first part of the condition, because if someone creates a random Error subclass whose `convertToErrorCode()` method returns a `std::generic_category()` error code, then it might be fine to return `eErrorTypePosix` because the associated error number should really be a posix errno number.)
https://github.com/llvm/llvm-project/pull/106774
More information about the lldb-commits
mailing list