]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/API/SBCommandReturnObject.cpp
MFV r304057:
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / 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     if (m_opaque_ap)
262         m_opaque_ap->SetImmediateOutputFile(fh);
263 }
264
265 void
266 SBCommandReturnObject::SetImmediateErrorFile(FILE *fh)
267 {
268     if (m_opaque_ap)
269         m_opaque_ap->SetImmediateErrorFile(fh);
270 }
271
272 void
273 SBCommandReturnObject::PutCString(const char* string, int len)
274 {
275     if (m_opaque_ap)
276     {
277         if (len == 0 || string == nullptr || *string == 0)
278         {
279             return;
280         }
281         else if (len > 0)
282         {
283             std::string buffer(string, len);
284             m_opaque_ap->AppendMessage(buffer.c_str());
285         }
286         else
287             m_opaque_ap->AppendMessage(string);
288     }
289 }
290
291 const char *
292 SBCommandReturnObject::GetOutput (bool only_if_no_immediate)
293 {
294     if (!m_opaque_ap)
295         return nullptr;
296     if (!only_if_no_immediate || m_opaque_ap->GetImmediateOutputStream().get() == nullptr)
297         return GetOutput();
298     return nullptr;
299 }
300
301 const char *
302 SBCommandReturnObject::GetError (bool only_if_no_immediate)
303 {
304     if (!m_opaque_ap)
305         return nullptr;
306     if (!only_if_no_immediate || m_opaque_ap->GetImmediateErrorStream().get() == nullptr)
307         return GetError();
308     return nullptr;
309 }
310
311 size_t
312 SBCommandReturnObject::Printf(const char* format, ...)
313 {
314     if (m_opaque_ap)
315     {
316         va_list args;
317         va_start (args, format);
318         size_t result = m_opaque_ap->GetOutputStream().PrintfVarArg(format, args);
319         va_end (args);
320         return result;
321     }
322     return 0;
323 }
324
325 void
326 SBCommandReturnObject::SetError (lldb::SBError &error, const char *fallback_error_cstr)
327 {
328     if (m_opaque_ap)
329     {
330         if (error.IsValid())
331             m_opaque_ap->SetError(error.ref(), fallback_error_cstr);
332         else if (fallback_error_cstr)
333             m_opaque_ap->SetError(Error(), fallback_error_cstr);
334     }
335 }
336
337 void
338 SBCommandReturnObject::SetError (const char *error_cstr)
339 {
340     if (m_opaque_ap && error_cstr)
341         m_opaque_ap->SetError(error_cstr);
342 }