]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/API/SBCommandReturnObject.cpp
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / source / API / SBCommandReturnObject.cpp
1 //===-- SBCommandReturnObject.cpp -------------------------------*- 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 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 // Project includes
14 #include "lldb/API/SBCommandReturnObject.h"
15 #include "lldb/API/SBError.h"
16 #include "lldb/API/SBStream.h"
17
18 #include "lldb/Core/Error.h"
19 #include "lldb/Core/Log.h"
20 #include "lldb/Interpreter/CommandReturnObject.h"
21
22 using namespace lldb;
23 using namespace lldb_private;
24
25 SBCommandReturnObject::SBCommandReturnObject () :
26     m_opaque_ap (new CommandReturnObject ())
27 {
28 }
29
30 SBCommandReturnObject::SBCommandReturnObject (const SBCommandReturnObject &rhs):
31     m_opaque_ap ()
32 {
33     if (rhs.m_opaque_ap)
34         m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
35 }
36
37 SBCommandReturnObject::SBCommandReturnObject (CommandReturnObject *ptr) :
38     m_opaque_ap (ptr)
39 {
40 }
41
42 SBCommandReturnObject::~SBCommandReturnObject() = default;
43
44 CommandReturnObject *
45 SBCommandReturnObject::Release ()
46 {
47     return m_opaque_ap.release();
48 }
49
50 const SBCommandReturnObject &
51 SBCommandReturnObject::operator = (const SBCommandReturnObject &rhs)
52 {
53     if (this != &rhs)
54     {
55         if (rhs.m_opaque_ap)
56             m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
57         else
58             m_opaque_ap.reset();
59     }
60     return *this;
61 }
62
63 bool
64 SBCommandReturnObject::IsValid() const
65 {
66     return m_opaque_ap.get() != nullptr;
67 }
68
69 const char *
70 SBCommandReturnObject::GetOutput ()
71 {
72     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
73
74     if (m_opaque_ap)
75     {
76         if (log)
77             log->Printf ("SBCommandReturnObject(%p)::GetOutput () => \"%s\"",
78                          static_cast<void*>(m_opaque_ap.get()),
79                          m_opaque_ap->GetOutputData());
80
81         return m_opaque_ap->GetOutputData();
82     }
83
84     if (log)
85         log->Printf ("SBCommandReturnObject(%p)::GetOutput () => nullptr",
86                      static_cast<void*>(m_opaque_ap.get()));
87
88     return nullptr;
89 }
90
91 const char *
92 SBCommandReturnObject::GetError ()
93 {
94     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
95
96     if (m_opaque_ap)
97     {
98         if (log)
99             log->Printf ("SBCommandReturnObject(%p)::GetError () => \"%s\"",
100                          static_cast<void*>(m_opaque_ap.get()),
101                          m_opaque_ap->GetErrorData());
102
103         return m_opaque_ap->GetErrorData();
104     }
105
106     if (log)
107         log->Printf ("SBCommandReturnObject(%p)::GetError () => nullptr",
108                      static_cast<void*>(m_opaque_ap.get()));
109
110     return nullptr;
111 }
112
113 size_t
114 SBCommandReturnObject::GetOutputSize()
115 {
116     return (m_opaque_ap ? strlen(m_opaque_ap->GetOutputData()) : 0);
117 }
118
119 size_t
120 SBCommandReturnObject::GetErrorSize()
121 {
122     return (m_opaque_ap ? strlen(m_opaque_ap->GetErrorData()) : 0);
123 }
124
125 size_t
126 SBCommandReturnObject::PutOutput (FILE *fh)
127 {
128     if (fh)
129     {
130         size_t num_bytes = GetOutputSize ();
131         if (num_bytes)
132             return ::fprintf (fh, "%s", GetOutput());
133     }
134     return 0;
135 }
136
137 size_t
138 SBCommandReturnObject::PutError (FILE *fh)
139 {
140     if (fh)
141     {
142         size_t num_bytes = GetErrorSize ();
143         if (num_bytes)
144             return ::fprintf (fh, "%s", GetError());
145     }
146     return 0;
147 }
148
149 void
150 SBCommandReturnObject::Clear()
151 {
152     if (m_opaque_ap)
153         m_opaque_ap->Clear();
154 }
155
156 lldb::ReturnStatus
157 SBCommandReturnObject::GetStatus()
158 {
159     return (m_opaque_ap ? m_opaque_ap->GetStatus() : lldb::eReturnStatusInvalid);
160 }
161
162 void
163 SBCommandReturnObject::SetStatus(lldb::ReturnStatus status)
164 {
165     if (m_opaque_ap)
166          m_opaque_ap->SetStatus(status);
167 }
168
169 bool
170 SBCommandReturnObject::Succeeded()
171 {
172     return (m_opaque_ap ? m_opaque_ap->Succeeded() : false);
173 }
174
175 bool
176 SBCommandReturnObject::HasResult()
177 {
178     return (m_opaque_ap ? m_opaque_ap->HasResult() : false);
179 }
180
181 void
182 SBCommandReturnObject::AppendMessage (const char *message)
183 {
184     if (m_opaque_ap)
185         m_opaque_ap->AppendMessage (message);
186 }
187
188 void
189 SBCommandReturnObject::AppendWarning (const char *message)
190 {
191     if (m_opaque_ap)
192         m_opaque_ap->AppendWarning (message);
193 }
194
195 CommandReturnObject *
196 SBCommandReturnObject::operator ->() const
197 {
198     return m_opaque_ap.get();
199 }
200
201 CommandReturnObject *
202 SBCommandReturnObject::get() const
203 {
204     return m_opaque_ap.get();
205 }
206
207 CommandReturnObject &
208 SBCommandReturnObject::operator *() const
209 {
210     assert(m_opaque_ap.get());
211     return *(m_opaque_ap.get());
212 }
213
214 CommandReturnObject &
215 SBCommandReturnObject::ref() const
216 {
217     assert(m_opaque_ap.get());
218     return *(m_opaque_ap.get());
219 }
220
221 void
222 SBCommandReturnObject::SetLLDBObjectPtr (CommandReturnObject *ptr)
223 {
224     if (m_opaque_ap)
225         m_opaque_ap.reset (ptr);
226 }
227
228 bool
229 SBCommandReturnObject::GetDescription (SBStream &description)
230 {
231     Stream &strm = description.ref();
232
233     if (m_opaque_ap)
234     {
235         description.Printf ("Status:  ");
236         lldb::ReturnStatus status = m_opaque_ap->GetStatus();
237         if (status == lldb::eReturnStatusStarted)
238             strm.PutCString ("Started");
239         else if (status == lldb::eReturnStatusInvalid)
240             strm.PutCString ("Invalid");
241         else if (m_opaque_ap->Succeeded())
242             strm.PutCString ("Success");
243         else
244             strm.PutCString ("Fail");
245
246         if (GetOutputSize() > 0)
247             strm.Printf ("\nOutput Message:\n%s", GetOutput());
248
249         if (GetErrorSize() > 0)
250             strm.Printf ("\nError Message:\n%s", GetError());
251     }
252     else
253         strm.PutCString ("No value");
254
255     return true;
256 }
257
258 void
259 SBCommandReturnObject::SetImmediateOutputFile(FILE *fh)
260 {
261     SetImmediateOutputFile(fh, false);
262 }
263
264 void
265 SBCommandReturnObject::SetImmediateErrorFile(FILE *fh)
266 {
267     SetImmediateErrorFile(fh, false);
268 }
269
270 void
271 SBCommandReturnObject::SetImmediateOutputFile(FILE *fh, bool transfer_ownership)
272 {
273     if (m_opaque_ap)
274         m_opaque_ap->SetImmediateOutputFile(fh, transfer_ownership);
275 }
276
277 void
278 SBCommandReturnObject::SetImmediateErrorFile(FILE *fh, bool transfer_ownership)
279 {
280     if (m_opaque_ap)
281         m_opaque_ap->SetImmediateErrorFile(fh, transfer_ownership);
282 }
283
284 void
285 SBCommandReturnObject::PutCString(const char* string, int len)
286 {
287     if (m_opaque_ap)
288     {
289         if (len == 0 || string == nullptr || *string == 0)
290         {
291             return;
292         }
293         else if (len > 0)
294         {
295             std::string buffer(string, len);
296             m_opaque_ap->AppendMessage(buffer.c_str());
297         }
298         else
299             m_opaque_ap->AppendMessage(string);
300     }
301 }
302
303 const char *
304 SBCommandReturnObject::GetOutput (bool only_if_no_immediate)
305 {
306     if (!m_opaque_ap)
307         return nullptr;
308     if (!only_if_no_immediate || m_opaque_ap->GetImmediateOutputStream().get() == nullptr)
309         return GetOutput();
310     return nullptr;
311 }
312
313 const char *
314 SBCommandReturnObject::GetError (bool only_if_no_immediate)
315 {
316     if (!m_opaque_ap)
317         return nullptr;
318     if (!only_if_no_immediate || m_opaque_ap->GetImmediateErrorStream().get() == nullptr)
319         return GetError();
320     return nullptr;
321 }
322
323 size_t
324 SBCommandReturnObject::Printf(const char* format, ...)
325 {
326     if (m_opaque_ap)
327     {
328         va_list args;
329         va_start (args, format);
330         size_t result = m_opaque_ap->GetOutputStream().PrintfVarArg(format, args);
331         va_end (args);
332         return result;
333     }
334     return 0;
335 }
336
337 void
338 SBCommandReturnObject::SetError (lldb::SBError &error, const char *fallback_error_cstr)
339 {
340     if (m_opaque_ap)
341     {
342         if (error.IsValid())
343             m_opaque_ap->SetError(error.ref(), fallback_error_cstr);
344         else if (fallback_error_cstr)
345             m_opaque_ap->SetError(Error(), fallback_error_cstr);
346     }
347 }
348
349 void
350 SBCommandReturnObject::SetError (const char *error_cstr)
351 {
352     if (m_opaque_ap && error_cstr)
353         m_opaque_ap->SetError(error_cstr);
354 }