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"
32 /// @brief An error handling class.
34 /// This class is designed to be able to hold any error code that can be
35 /// encountered on a given platform. The errors are stored as a value
36 /// of type Status::ValueType. This value should be large enough to hold
37 /// any and all errors that the class supports. Each error has an
38 /// associated type that is of type lldb::ErrorType. New types
39 /// can be added to support new error types, and architecture specific
40 /// types can be enabled. In the future we may wish to switch to a
41 /// registration mechanism where new error types can be registered at
42 /// runtime instead of a hard coded scheme.
44 /// All errors in this class also know how to generate a string
45 /// representation of themselves for printing results and error codes.
46 /// The string value will be fetched on demand and its string value will
47 /// be cached until the error is cleared of the value of the error
49 //----------------------------------------------------------------------
52 //------------------------------------------------------------------
53 /// Every error value that this object can contain needs to be able
54 /// to fit into ValueType.
55 //------------------------------------------------------------------
56 typedef uint32_t ValueType;
58 //------------------------------------------------------------------
59 /// Default constructor.
61 /// Initialize the error object with a generic success value.
67 /// The type for \a err.
68 //------------------------------------------------------------------
71 explicit Status(ValueType err,
72 lldb::ErrorType type = lldb::eErrorTypeGeneric);
74 /* implicit */ Status(std::error_code EC);
76 explicit Status(const char *format, ...)
77 __attribute__((format(printf, 2, 3)));
79 Status(const Status &rhs);
80 //------------------------------------------------------------------
81 /// Assignment operator.
87 /// A const reference to this object.
88 //------------------------------------------------------------------
89 const Status &operator=(const Status &rhs);
93 // llvm::Error support
94 explicit Status(llvm::Error error) { *this = std::move(error); }
95 const Status &operator=(llvm::Error error);
96 llvm::Error ToError() const;
98 //------------------------------------------------------------------
99 /// Get the error string associated with the current error.
101 /// Gets the error value as a NULL terminated C string. The error
102 /// string will be fetched and cached on demand. The error string
103 /// will be retrieved from a callback that is appropriate for the
104 /// type of the error and will be cached until the error value is
105 /// changed or cleared.
108 /// The error as a NULL terminated C string value if the error
109 /// is valid and is able to be converted to a string value,
111 //------------------------------------------------------------------
112 const char *AsCString(const char *default_error_str = "unknown error") const;
114 //------------------------------------------------------------------
115 /// Clear the object state.
117 /// Reverts the state of this object to contain a generic success
118 /// value and frees any cached error string value.
119 //------------------------------------------------------------------
122 //------------------------------------------------------------------
123 /// Test for error condition.
126 /// \b true if this object contains an error, \b false
128 //------------------------------------------------------------------
131 //------------------------------------------------------------------
132 /// Access the error value.
136 //------------------------------------------------------------------
137 ValueType GetError() const;
139 //------------------------------------------------------------------
140 /// Access the error type.
143 /// The error type enumeration value.
144 //------------------------------------------------------------------
145 lldb::ErrorType GetType() const;
147 //------------------------------------------------------------------
148 /// Set accessor from a kern_return_t.
150 /// Set accesssor for the error value to \a err and the error type
151 /// to \c MachKernel.
154 /// A mach error code.
155 //------------------------------------------------------------------
156 void SetMachError(uint32_t err);
158 void SetExpressionError(lldb::ExpressionResults, const char *mssg);
160 int SetExpressionErrorWithFormat(lldb::ExpressionResults, const char *format,
161 ...) __attribute__((format(printf, 3, 4)));
163 //------------------------------------------------------------------
164 /// Set accesssor with an error value and type.
166 /// Set accesssor for the error value to \a err and the error type
170 /// A mach error code.
173 /// The type for \a err.
174 //------------------------------------------------------------------
175 void SetError(ValueType err, lldb::ErrorType type);
177 //------------------------------------------------------------------
178 /// Set the current error to errno.
180 /// Update the error value to be \c errno and update the type to
181 /// be \c Status::POSIX.
182 //------------------------------------------------------------------
183 void SetErrorToErrno();
185 //------------------------------------------------------------------
186 /// Set the current error to a generic error.
188 /// Update the error value to be \c LLDB_GENERIC_ERROR and update the
189 /// type to be \c Status::Generic.
190 //------------------------------------------------------------------
191 void SetErrorToGenericError();
193 //------------------------------------------------------------------
194 /// Set the current error string to \a err_str.
196 /// Set accessor for the error string value for a generic errors,
197 /// or to supply additional details above and beyond the standard
198 /// error strings that the standard type callbacks typically
199 /// provide. This allows custom strings to be supplied as an
200 /// error explanation. The error string value will remain until the
201 /// error value is cleared or a new error value/type is assigned.
204 /// The new custom error string to copy and cache.
205 //------------------------------------------------------------------
206 void SetErrorString(llvm::StringRef err_str);
208 //------------------------------------------------------------------
209 /// Set the current error string to a formatted error string.
212 /// A printf style format string
213 //------------------------------------------------------------------
214 int SetErrorStringWithFormat(const char *format, ...)
215 __attribute__((format(printf, 2, 3)));
217 int SetErrorStringWithVarArg(const char *format, va_list args);
219 template <typename... Args>
220 void SetErrorStringWithFormatv(const char *format, Args &&... args) {
221 SetErrorString(llvm::formatv(format, std::forward<Args>(args)...).str());
224 //------------------------------------------------------------------
225 /// Test for success condition.
227 /// Returns true if the error code in this object is considered a
228 /// successful return value.
231 /// \b true if this object contains an value that describes
232 /// success (non-erro), \b false otherwise.
233 //------------------------------------------------------------------
234 bool Success() const;
236 //------------------------------------------------------------------
237 /// Test for a failure due to a generic interrupt.
239 /// Returns true if the error code in this object was caused by an interrupt.
240 /// At present only supports Posix EINTR.
243 /// \b true if this object contains an value that describes
244 /// failure due to interrupt, \b false otherwise.
245 //------------------------------------------------------------------
246 bool WasInterrupted() const;
249 //------------------------------------------------------------------
251 //------------------------------------------------------------------
252 ValueType m_code; ///< Status code as an integer value.
253 lldb::ErrorType m_type; ///< The type of the above error code.
254 mutable std::string m_string; ///< A string representation of the error code.
257 } // namespace lldb_private
260 template <> struct format_provider<lldb_private::Status> {
261 static void format(const lldb_private::Status &error, llvm::raw_ostream &OS,
262 llvm::StringRef Options);
266 #endif // #ifndef LLDB_UTILITY_STATUS_H