1 //===-- Status.h ------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLDB_UTILITY_STATUS_H
11 #define LLDB_UTILITY_STATUS_H
13 #include "lldb/lldb-defines.h"
14 #include "lldb/lldb-enumerations.h" // for ErrorType, ErrorType...
15 #include "llvm/ADT/StringRef.h" // for StringRef
16 #include "llvm/Support/Error.h"
17 #include "llvm/Support/FormatVariadic.h"
19 #include <stdint.h> // for uint32_t
21 #include <system_error> // for error_code
22 #include <type_traits> // for forward
28 namespace lldb_private {
30 //----------------------------------------------------------------------
31 /// @class Status Status.h "lldb/Utility/Status.h" An error handling class.
33 /// This class is designed to be able to hold any error code that can be
34 /// encountered on a given platform. The errors are stored as a value of type
35 /// Status::ValueType. This value should be large enough to hold any and all
36 /// errors that the class supports. Each error has an associated type that is
37 /// of type lldb::ErrorType. New types can be added to support new error
38 /// types, and architecture specific types can be enabled. In the future we
39 /// may wish to switch to a registration mechanism where new error types can
40 /// be registered at runtime instead of a hard coded scheme.
42 /// All errors in this class also know how to generate a string representation
43 /// of themselves for printing results and error codes. The string value will
44 /// be fetched on demand and its string value will be cached until the error
45 /// is cleared of the value of the error changes.
46 //----------------------------------------------------------------------
49 //------------------------------------------------------------------
50 /// Every error value that this object can contain needs to be able to fit
52 //------------------------------------------------------------------
53 typedef uint32_t ValueType;
55 //------------------------------------------------------------------
56 /// Default constructor.
58 /// Initialize the error object with a generic success value.
64 /// The type for \a err.
65 //------------------------------------------------------------------
68 explicit Status(ValueType err,
69 lldb::ErrorType type = lldb::eErrorTypeGeneric);
71 /* implicit */ Status(std::error_code EC);
73 explicit Status(const char *format, ...)
74 __attribute__((format(printf, 2, 3)));
76 Status(const Status &rhs);
77 //------------------------------------------------------------------
78 /// Assignment operator.
84 /// A const reference to this object.
85 //------------------------------------------------------------------
86 const Status &operator=(const Status &rhs);
90 // llvm::Error support
91 explicit Status(llvm::Error error) { *this = std::move(error); }
92 const Status &operator=(llvm::Error error);
93 llvm::Error ToError() const;
95 //------------------------------------------------------------------
96 /// Get the error string associated with the current error.
98 /// Gets the error value as a NULL terminated C string. The error string
99 /// will be fetched and cached on demand. The error string will be retrieved
100 /// from a callback that is appropriate for the type of the error and will
101 /// be cached until the error value is changed or cleared.
104 /// The error as a NULL terminated C string value if the error
105 /// is valid and is able to be converted to a string value,
107 //------------------------------------------------------------------
108 const char *AsCString(const char *default_error_str = "unknown error") const;
110 //------------------------------------------------------------------
111 /// Clear the object state.
113 /// Reverts the state of this object to contain a generic success value and
114 /// frees any cached error string value.
115 //------------------------------------------------------------------
118 //------------------------------------------------------------------
119 /// Test for error condition.
122 /// \b true if this object contains an error, \b false
124 //------------------------------------------------------------------
127 //------------------------------------------------------------------
128 /// Access the error value.
132 //------------------------------------------------------------------
133 ValueType GetError() const;
135 //------------------------------------------------------------------
136 /// Access the error type.
139 /// The error type enumeration value.
140 //------------------------------------------------------------------
141 lldb::ErrorType GetType() const;
143 //------------------------------------------------------------------
144 /// Set accessor from a kern_return_t.
146 /// Set accesssor for the error value to \a err and the error type to \c
150 /// A mach error code.
151 //------------------------------------------------------------------
152 void SetMachError(uint32_t err);
154 void SetExpressionError(lldb::ExpressionResults, const char *mssg);
156 int SetExpressionErrorWithFormat(lldb::ExpressionResults, const char *format,
157 ...) __attribute__((format(printf, 3, 4)));
159 //------------------------------------------------------------------
160 /// Set accesssor with an error value and type.
162 /// Set accesssor for the error value to \a err and the error type to \a
166 /// A mach error code.
169 /// The type for \a err.
170 //------------------------------------------------------------------
171 void SetError(ValueType err, lldb::ErrorType type);
173 //------------------------------------------------------------------
174 /// Set the current error to errno.
176 /// Update the error value to be \c errno and update the type to be \c
178 //------------------------------------------------------------------
179 void SetErrorToErrno();
181 //------------------------------------------------------------------
182 /// Set the current error to a generic error.
184 /// Update the error value to be \c LLDB_GENERIC_ERROR and update the type
185 /// to be \c Status::Generic.
186 //------------------------------------------------------------------
187 void SetErrorToGenericError();
189 //------------------------------------------------------------------
190 /// Set the current error string to \a err_str.
192 /// Set accessor for the error string value for a generic errors, or to
193 /// supply additional details above and beyond the standard error strings
194 /// that the standard type callbacks typically provide. This allows custom
195 /// strings to be supplied as an error explanation. The error string value
196 /// will remain until the error value is cleared or a new error value/type
200 /// The new custom error string to copy and cache.
201 //------------------------------------------------------------------
202 void SetErrorString(llvm::StringRef err_str);
204 //------------------------------------------------------------------
205 /// Set the current error string to a formatted error string.
208 /// A printf style format string
209 //------------------------------------------------------------------
210 int SetErrorStringWithFormat(const char *format, ...)
211 __attribute__((format(printf, 2, 3)));
213 int SetErrorStringWithVarArg(const char *format, va_list args);
215 template <typename... Args>
216 void SetErrorStringWithFormatv(const char *format, Args &&... args) {
217 SetErrorString(llvm::formatv(format, std::forward<Args>(args)...).str());
220 //------------------------------------------------------------------
221 /// Test for success condition.
223 /// Returns true if the error code in this object is considered a successful
227 /// \b true if this object contains an value that describes
228 /// success (non-erro), \b false otherwise.
229 //------------------------------------------------------------------
230 bool Success() const;
232 //------------------------------------------------------------------
233 /// Test for a failure due to a generic interrupt.
235 /// Returns true if the error code in this object was caused by an
236 /// interrupt. At present only supports Posix EINTR.
239 /// \b true if this object contains an value that describes
240 /// failure due to interrupt, \b false otherwise.
241 //------------------------------------------------------------------
242 bool WasInterrupted() const;
245 //------------------------------------------------------------------
247 //------------------------------------------------------------------
248 ValueType m_code; ///< Status code as an integer value.
249 lldb::ErrorType m_type; ///< The type of the above error code.
250 mutable std::string m_string; ///< A string representation of the error code.
253 } // namespace lldb_private
256 template <> struct format_provider<lldb_private::Status> {
257 static void format(const lldb_private::Status &error, llvm::raw_ostream &OS,
258 llvm::StringRef Options);
262 #endif // #ifndef LLDB_UTILITY_STATUS_H