]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Utility/Status.cpp
Merge ^/head r318560 through r318657.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Utility / Status.cpp
1 //===-- Status.cpp -----------------------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #include "lldb/Utility/Status.h"
12
13 #include "lldb/Utility/VASPrintf.h"
14 #include "lldb/lldb-defines.h"            // for LLDB_GENERIC_ERROR
15 #include "lldb/lldb-enumerations.h"       // for ErrorType, ErrorType::eErr...
16 #include "llvm/ADT/SmallString.h"         // for SmallString
17 #include "llvm/ADT/StringRef.h"           // for StringRef
18 #include "llvm/Support/FormatProviders.h" // for format_provider
19
20 #include <cerrno>
21 #include <cstdarg>
22 #include <string> // for string
23 #include <system_error>
24
25 #ifdef __APPLE__
26 #include <mach/mach.h>
27 #endif
28
29 #include <stdint.h> // for uint32_t
30 #include <string.h> // for strerror
31
32 namespace llvm {
33 class raw_ostream;
34 }
35
36 using namespace lldb;
37 using namespace lldb_private;
38
39 Status::Status() : m_code(0), m_type(eErrorTypeInvalid), m_string() {}
40
41 Status::Status(ValueType err, ErrorType type)
42     : m_code(err), m_type(type), m_string() {}
43
44 Status::Status(std::error_code EC)
45     : m_code(EC.value()), m_type(ErrorType::eErrorTypeGeneric),
46       m_string(EC.message()) {}
47
48 Status::Status(const Status &rhs) = default;
49
50 Status::Status(const char *format, ...)
51     : m_code(0), m_type(eErrorTypeInvalid), m_string() {
52   va_list args;
53   va_start(args, format);
54   SetErrorToGenericError();
55   SetErrorStringWithVarArg(format, args);
56   va_end(args);
57 }
58
59 //----------------------------------------------------------------------
60 // Assignment operator
61 //----------------------------------------------------------------------
62 const Status &Status::operator=(const Status &rhs) {
63   if (this != &rhs) {
64     m_code = rhs.m_code;
65     m_type = rhs.m_type;
66     m_string = rhs.m_string;
67   }
68   return *this;
69 }
70
71 //----------------------------------------------------------------------
72 // Assignment operator
73 //----------------------------------------------------------------------
74 const Status &Status::operator=(uint32_t err) {
75   m_code = err;
76   m_type = eErrorTypeMachKernel;
77   m_string.clear();
78   return *this;
79 }
80
81 Status::~Status() = default;
82
83 //----------------------------------------------------------------------
84 // Get the error value as a NULL C string. The error string will be
85 // fetched and cached on demand. The cached error string value will
86 // remain until the error value is changed or cleared.
87 //----------------------------------------------------------------------
88 const char *Status::AsCString(const char *default_error_str) const {
89   if (Success())
90     return nullptr;
91
92   if (m_string.empty()) {
93     const char *s = nullptr;
94     switch (m_type) {
95     case eErrorTypeMachKernel:
96 #if defined(__APPLE__)
97       s = ::mach_error_string(m_code);
98 #endif
99       break;
100
101     case eErrorTypePOSIX:
102       s = ::strerror(m_code);
103       break;
104
105     default:
106       break;
107     }
108     if (s != nullptr)
109       m_string.assign(s);
110   }
111   if (m_string.empty()) {
112     if (default_error_str)
113       m_string.assign(default_error_str);
114     else
115       return nullptr; // User wanted a nullptr string back...
116   }
117   return m_string.c_str();
118 }
119
120 //----------------------------------------------------------------------
121 // Clear the error and any cached error string that it might contain.
122 //----------------------------------------------------------------------
123 void Status::Clear() {
124   m_code = 0;
125   m_type = eErrorTypeInvalid;
126   m_string.clear();
127 }
128
129 //----------------------------------------------------------------------
130 // Access the error value.
131 //----------------------------------------------------------------------
132 Status::ValueType Status::GetError() const { return m_code; }
133
134 //----------------------------------------------------------------------
135 // Access the error type.
136 //----------------------------------------------------------------------
137 ErrorType Status::GetType() const { return m_type; }
138
139 //----------------------------------------------------------------------
140 // Returns true if this object contains a value that describes an
141 // error or otherwise non-success result.
142 //----------------------------------------------------------------------
143 bool Status::Fail() const { return m_code != 0; }
144
145 //----------------------------------------------------------------------
146 // Set accesssor for the error value to "err" and the type to
147 // "eErrorTypeMachKernel"
148 //----------------------------------------------------------------------
149 void Status::SetMachError(uint32_t err) {
150   m_code = err;
151   m_type = eErrorTypeMachKernel;
152   m_string.clear();
153 }
154
155 void Status::SetExpressionError(lldb::ExpressionResults result,
156                                 const char *mssg) {
157   m_code = result;
158   m_type = eErrorTypeExpression;
159   m_string = mssg;
160 }
161
162 int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
163                                          const char *format, ...) {
164   int length = 0;
165
166   if (format != nullptr && format[0]) {
167     va_list args;
168     va_start(args, format);
169     length = SetErrorStringWithVarArg(format, args);
170     va_end(args);
171   } else {
172     m_string.clear();
173   }
174   m_code = result;
175   m_type = eErrorTypeExpression;
176   return length;
177 }
178
179 //----------------------------------------------------------------------
180 // Set accesssor for the error value and type.
181 //----------------------------------------------------------------------
182 void Status::SetError(ValueType err, ErrorType type) {
183   m_code = err;
184   m_type = type;
185   m_string.clear();
186 }
187
188 //----------------------------------------------------------------------
189 // Update the error value to be "errno" and update the type to
190 // be "POSIX".
191 //----------------------------------------------------------------------
192 void Status::SetErrorToErrno() {
193   m_code = errno;
194   m_type = eErrorTypePOSIX;
195   m_string.clear();
196 }
197
198 //----------------------------------------------------------------------
199 // Update the error value to be LLDB_GENERIC_ERROR and update the type
200 // to be "Generic".
201 //----------------------------------------------------------------------
202 void Status::SetErrorToGenericError() {
203   m_code = LLDB_GENERIC_ERROR;
204   m_type = eErrorTypeGeneric;
205   m_string.clear();
206 }
207
208 //----------------------------------------------------------------------
209 // Set accessor for the error string value for a specific error.
210 // This allows any string to be supplied as an error explanation.
211 // The error string value will remain until the error value is
212 // cleared or a new error value/type is assigned.
213 //----------------------------------------------------------------------
214 void Status::SetErrorString(llvm::StringRef err_str) {
215   if (!err_str.empty()) {
216     // If we have an error string, we should always at least have an error
217     // set to a generic value.
218     if (Success())
219       SetErrorToGenericError();
220   }
221   m_string = err_str;
222 }
223
224 //------------------------------------------------------------------
225 /// Set the current error string to a formatted error string.
226 ///
227 /// @param format
228 ///     A printf style format string
229 //------------------------------------------------------------------
230 int Status::SetErrorStringWithFormat(const char *format, ...) {
231   if (format != nullptr && format[0]) {
232     va_list args;
233     va_start(args, format);
234     int length = SetErrorStringWithVarArg(format, args);
235     va_end(args);
236     return length;
237   } else {
238     m_string.clear();
239   }
240   return 0;
241 }
242
243 int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
244   if (format != nullptr && format[0]) {
245     // If we have an error string, we should always at least have
246     // an error set to a generic value.
247     if (Success())
248       SetErrorToGenericError();
249
250     llvm::SmallString<1024> buf;
251     VASprintf(buf, format, args);
252     m_string = buf.str();
253     return buf.size();
254   } else {
255     m_string.clear();
256   }
257   return 0;
258 }
259
260 //----------------------------------------------------------------------
261 // Returns true if the error code in this object is considered a
262 // successful return value.
263 //----------------------------------------------------------------------
264 bool Status::Success() const { return m_code == 0; }
265
266 bool Status::WasInterrupted() const {
267   return (m_type == eErrorTypePOSIX && m_code == EINTR);
268 }
269
270 void llvm::format_provider<lldb_private::Status>::format(
271     const lldb_private::Status &error, llvm::raw_ostream &OS,
272     llvm::StringRef Options) {
273   llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
274                                                  Options);
275 }