]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Utility/Status.h
MFV: r334448
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Utility / Status.h
1 //===-- Status.h ------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLDB_UTILITY_STATUS_H
11 #define LLDB_UTILITY_STATUS_H
12
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"
18 #include <cstdarg>
19 #include <stdint.h> // for uint32_t
20 #include <string>
21 #include <system_error> // for error_code
22 #include <type_traits>  // for forward
23
24 namespace llvm {
25 class raw_ostream;
26 }
27
28 namespace lldb_private {
29
30 //----------------------------------------------------------------------
31 /// @class Status Status.h "lldb/Utility/Status.h"
32 /// @brief An error handling class.
33 ///
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.
43 ///
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
48 /// changes.
49 //----------------------------------------------------------------------
50 class Status {
51 public:
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;
57
58   //------------------------------------------------------------------
59   /// Default constructor.
60   ///
61   /// Initialize the error object with a generic success value.
62   ///
63   /// @param[in] err
64   ///     An error code.
65   ///
66   /// @param[in] type
67   ///     The type for \a err.
68   //------------------------------------------------------------------
69   Status();
70
71   explicit Status(ValueType err,
72                   lldb::ErrorType type = lldb::eErrorTypeGeneric);
73
74   /* implicit */ Status(std::error_code EC);
75
76   explicit Status(const char *format, ...)
77       __attribute__((format(printf, 2, 3)));
78
79   Status(const Status &rhs);
80   //------------------------------------------------------------------
81   /// Assignment operator.
82   ///
83   /// @param[in] err
84   ///     An error code.
85   ///
86   /// @return
87   ///     A const reference to this object.
88   //------------------------------------------------------------------
89   const Status &operator=(const Status &rhs);
90
91   ~Status();
92
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;
97
98   //------------------------------------------------------------------
99   /// Get the error string associated with the current error.
100   //
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.
106   ///
107   /// @return
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,
110   ///     NULL otherwise.
111   //------------------------------------------------------------------
112   const char *AsCString(const char *default_error_str = "unknown error") const;
113
114   //------------------------------------------------------------------
115   /// Clear the object state.
116   ///
117   /// Reverts the state of this object to contain a generic success
118   /// value and frees any cached error string value.
119   //------------------------------------------------------------------
120   void Clear();
121
122   //------------------------------------------------------------------
123   /// Test for error condition.
124   ///
125   /// @return
126   ///     \b true if this object contains an error, \b false
127   ///     otherwise.
128   //------------------------------------------------------------------
129   bool Fail() const;
130
131   //------------------------------------------------------------------
132   /// Access the error value.
133   ///
134   /// @return
135   ///     The error value.
136   //------------------------------------------------------------------
137   ValueType GetError() const;
138
139   //------------------------------------------------------------------
140   /// Access the error type.
141   ///
142   /// @return
143   ///     The error type enumeration value.
144   //------------------------------------------------------------------
145   lldb::ErrorType GetType() const;
146
147   //------------------------------------------------------------------
148   /// Set accessor from a kern_return_t.
149   ///
150   /// Set accesssor for the error value to \a err and the error type
151   /// to \c MachKernel.
152   ///
153   /// @param[in] err
154   ///     A mach error code.
155   //------------------------------------------------------------------
156   void SetMachError(uint32_t err);
157
158   void SetExpressionError(lldb::ExpressionResults, const char *mssg);
159
160   int SetExpressionErrorWithFormat(lldb::ExpressionResults, const char *format,
161                                    ...) __attribute__((format(printf, 3, 4)));
162
163   //------------------------------------------------------------------
164   /// Set accesssor with an error value and type.
165   ///
166   /// Set accesssor for the error value to \a err and the error type
167   /// to \a type.
168   ///
169   /// @param[in] err
170   ///     A mach error code.
171   ///
172   /// @param[in] type
173   ///     The type for \a err.
174   //------------------------------------------------------------------
175   void SetError(ValueType err, lldb::ErrorType type);
176
177   //------------------------------------------------------------------
178   /// Set the current error to errno.
179   ///
180   /// Update the error value to be \c errno and update the type to
181   /// be \c Status::POSIX.
182   //------------------------------------------------------------------
183   void SetErrorToErrno();
184
185   //------------------------------------------------------------------
186   /// Set the current error to a generic error.
187   ///
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();
192
193   //------------------------------------------------------------------
194   /// Set the current error string to \a err_str.
195   ///
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.
202   ///
203   /// @param err_str
204   ///     The new custom error string to copy and cache.
205   //------------------------------------------------------------------
206   void SetErrorString(llvm::StringRef err_str);
207
208   //------------------------------------------------------------------
209   /// Set the current error string to a formatted error string.
210   ///
211   /// @param format
212   ///     A printf style format string
213   //------------------------------------------------------------------
214   int SetErrorStringWithFormat(const char *format, ...)
215       __attribute__((format(printf, 2, 3)));
216
217   int SetErrorStringWithVarArg(const char *format, va_list args);
218
219   template <typename... Args>
220   void SetErrorStringWithFormatv(const char *format, Args &&... args) {
221     SetErrorString(llvm::formatv(format, std::forward<Args>(args)...).str());
222   }
223
224   //------------------------------------------------------------------
225   /// Test for success condition.
226   ///
227   /// Returns true if the error code in this object is considered a
228   /// successful return value.
229   ///
230   /// @return
231   ///     \b true if this object contains an value that describes
232   ///     success (non-erro), \b false otherwise.
233   //------------------------------------------------------------------
234   bool Success() const;
235
236   //------------------------------------------------------------------
237   /// Test for a failure due to a generic interrupt.
238   ///
239   /// Returns true if the error code in this object was caused by an interrupt.
240   /// At present only supports Posix EINTR.
241   ///
242   /// @return
243   ///     \b true if this object contains an value that describes
244   ///     failure due to interrupt, \b false otherwise.
245   //------------------------------------------------------------------
246   bool WasInterrupted() const;
247
248 protected:
249   //------------------------------------------------------------------
250   /// Member variables
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.
255 };
256
257 } // namespace lldb_private
258
259 namespace llvm {
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);
263 };
264 }
265
266 #endif // #ifndef LLDB_UTILITY_STATUS_H