]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/API/SBCommandReturnObject.cpp
Upgrade our copies of clang, llvm, lld and libc++ to r311219 from the
[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/Interpreter/CommandReturnObject.h"
19 #include "lldb/Utility/ConstString.h"
20 #include "lldb/Utility/Log.h"
21 #include "lldb/Utility/Status.h"
22
23 using namespace lldb;
24 using namespace lldb_private;
25
26 SBCommandReturnObject::SBCommandReturnObject()
27     : m_opaque_ap(new CommandReturnObject()) {}
28
29 SBCommandReturnObject::SBCommandReturnObject(const SBCommandReturnObject &rhs)
30     : m_opaque_ap() {
31   if (rhs.m_opaque_ap)
32     m_opaque_ap.reset(new CommandReturnObject(*rhs.m_opaque_ap));
33 }
34
35 SBCommandReturnObject::SBCommandReturnObject(CommandReturnObject *ptr)
36     : m_opaque_ap(ptr) {}
37
38 SBCommandReturnObject::~SBCommandReturnObject() = default;
39
40 CommandReturnObject *SBCommandReturnObject::Release() {
41   return m_opaque_ap.release();
42 }
43
44 const SBCommandReturnObject &SBCommandReturnObject::
45 operator=(const SBCommandReturnObject &rhs) {
46   if (this != &rhs) {
47     if (rhs.m_opaque_ap)
48       m_opaque_ap.reset(new CommandReturnObject(*rhs.m_opaque_ap));
49     else
50       m_opaque_ap.reset();
51   }
52   return *this;
53 }
54
55 bool SBCommandReturnObject::IsValid() const {
56   return m_opaque_ap.get() != nullptr;
57 }
58
59 const char *SBCommandReturnObject::GetOutput() {
60   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
61
62   if (m_opaque_ap) {
63     llvm::StringRef output = m_opaque_ap->GetOutputData();
64     ConstString result(output.empty() ? llvm::StringRef("") : output);
65
66     if (log)
67       log->Printf("SBCommandReturnObject(%p)::GetOutput () => \"%s\"",
68                   static_cast<void *>(m_opaque_ap.get()), result.AsCString());
69
70     return result.AsCString();
71   }
72
73   if (log)
74     log->Printf("SBCommandReturnObject(%p)::GetOutput () => nullptr",
75                 static_cast<void *>(m_opaque_ap.get()));
76
77   return nullptr;
78 }
79
80 const char *SBCommandReturnObject::GetError() {
81   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
82
83   if (m_opaque_ap) {
84     llvm::StringRef output = m_opaque_ap->GetErrorData();
85     ConstString result(output.empty() ? llvm::StringRef("") : output);
86     if (log)
87       log->Printf("SBCommandReturnObject(%p)::GetError () => \"%s\"",
88                   static_cast<void *>(m_opaque_ap.get()), result.AsCString());
89
90     return result.AsCString();
91   }
92
93   if (log)
94     log->Printf("SBCommandReturnObject(%p)::GetError () => nullptr",
95                 static_cast<void *>(m_opaque_ap.get()));
96
97   return nullptr;
98 }
99
100 size_t SBCommandReturnObject::GetOutputSize() {
101   return (m_opaque_ap ? m_opaque_ap->GetOutputData().size() : 0);
102 }
103
104 size_t SBCommandReturnObject::GetErrorSize() {
105   return (m_opaque_ap ? m_opaque_ap->GetErrorData().size() : 0);
106 }
107
108 size_t SBCommandReturnObject::PutOutput(FILE *fh) {
109   if (fh) {
110     size_t num_bytes = GetOutputSize();
111     if (num_bytes)
112       return ::fprintf(fh, "%s", GetOutput());
113   }
114   return 0;
115 }
116
117 size_t SBCommandReturnObject::PutError(FILE *fh) {
118   if (fh) {
119     size_t num_bytes = GetErrorSize();
120     if (num_bytes)
121       return ::fprintf(fh, "%s", GetError());
122   }
123   return 0;
124 }
125
126 void SBCommandReturnObject::Clear() {
127   if (m_opaque_ap)
128     m_opaque_ap->Clear();
129 }
130
131 lldb::ReturnStatus SBCommandReturnObject::GetStatus() {
132   return (m_opaque_ap ? m_opaque_ap->GetStatus() : lldb::eReturnStatusInvalid);
133 }
134
135 void SBCommandReturnObject::SetStatus(lldb::ReturnStatus status) {
136   if (m_opaque_ap)
137     m_opaque_ap->SetStatus(status);
138 }
139
140 bool SBCommandReturnObject::Succeeded() {
141   return (m_opaque_ap ? m_opaque_ap->Succeeded() : false);
142 }
143
144 bool SBCommandReturnObject::HasResult() {
145   return (m_opaque_ap ? m_opaque_ap->HasResult() : false);
146 }
147
148 void SBCommandReturnObject::AppendMessage(const char *message) {
149   if (m_opaque_ap)
150     m_opaque_ap->AppendMessage(message);
151 }
152
153 void SBCommandReturnObject::AppendWarning(const char *message) {
154   if (m_opaque_ap)
155     m_opaque_ap->AppendWarning(message);
156 }
157
158 CommandReturnObject *SBCommandReturnObject::operator->() const {
159   return m_opaque_ap.get();
160 }
161
162 CommandReturnObject *SBCommandReturnObject::get() const {
163   return m_opaque_ap.get();
164 }
165
166 CommandReturnObject &SBCommandReturnObject::operator*() const {
167   assert(m_opaque_ap.get());
168   return *(m_opaque_ap.get());
169 }
170
171 CommandReturnObject &SBCommandReturnObject::ref() const {
172   assert(m_opaque_ap.get());
173   return *(m_opaque_ap.get());
174 }
175
176 void SBCommandReturnObject::SetLLDBObjectPtr(CommandReturnObject *ptr) {
177   if (m_opaque_ap)
178     m_opaque_ap.reset(ptr);
179 }
180
181 bool SBCommandReturnObject::GetDescription(SBStream &description) {
182   Stream &strm = description.ref();
183
184   if (m_opaque_ap) {
185     description.Printf("Error:  ");
186     lldb::ReturnStatus status = m_opaque_ap->GetStatus();
187     if (status == lldb::eReturnStatusStarted)
188       strm.PutCString("Started");
189     else if (status == lldb::eReturnStatusInvalid)
190       strm.PutCString("Invalid");
191     else if (m_opaque_ap->Succeeded())
192       strm.PutCString("Success");
193     else
194       strm.PutCString("Fail");
195
196     if (GetOutputSize() > 0)
197       strm.Printf("\nOutput Message:\n%s", GetOutput());
198
199     if (GetErrorSize() > 0)
200       strm.Printf("\nError Message:\n%s", GetError());
201   } else
202     strm.PutCString("No value");
203
204   return true;
205 }
206
207 void SBCommandReturnObject::SetImmediateOutputFile(FILE *fh) {
208   SetImmediateOutputFile(fh, false);
209 }
210
211 void SBCommandReturnObject::SetImmediateErrorFile(FILE *fh) {
212   SetImmediateErrorFile(fh, false);
213 }
214
215 void SBCommandReturnObject::SetImmediateOutputFile(FILE *fh,
216                                                    bool transfer_ownership) {
217   if (m_opaque_ap)
218     m_opaque_ap->SetImmediateOutputFile(fh, transfer_ownership);
219 }
220
221 void SBCommandReturnObject::SetImmediateErrorFile(FILE *fh,
222                                                   bool transfer_ownership) {
223   if (m_opaque_ap)
224     m_opaque_ap->SetImmediateErrorFile(fh, transfer_ownership);
225 }
226
227 void SBCommandReturnObject::PutCString(const char *string, int len) {
228   if (m_opaque_ap) {
229     if (len == 0 || string == nullptr || *string == 0) {
230       return;
231     } else if (len > 0) {
232       std::string buffer(string, len);
233       m_opaque_ap->AppendMessage(buffer.c_str());
234     } else
235       m_opaque_ap->AppendMessage(string);
236   }
237 }
238
239 const char *SBCommandReturnObject::GetOutput(bool only_if_no_immediate) {
240   if (!m_opaque_ap)
241     return nullptr;
242   if (!only_if_no_immediate ||
243       m_opaque_ap->GetImmediateOutputStream().get() == nullptr)
244     return GetOutput();
245   return nullptr;
246 }
247
248 const char *SBCommandReturnObject::GetError(bool only_if_no_immediate) {
249   if (!m_opaque_ap)
250     return nullptr;
251   if (!only_if_no_immediate ||
252       m_opaque_ap->GetImmediateErrorStream().get() == nullptr)
253     return GetError();
254   return nullptr;
255 }
256
257 size_t SBCommandReturnObject::Printf(const char *format, ...) {
258   if (m_opaque_ap) {
259     va_list args;
260     va_start(args, format);
261     size_t result = m_opaque_ap->GetOutputStream().PrintfVarArg(format, args);
262     va_end(args);
263     return result;
264   }
265   return 0;
266 }
267
268 void SBCommandReturnObject::SetError(lldb::SBError &error,
269                                      const char *fallback_error_cstr) {
270   if (m_opaque_ap) {
271     if (error.IsValid())
272       m_opaque_ap->SetError(error.ref(), fallback_error_cstr);
273     else if (fallback_error_cstr)
274       m_opaque_ap->SetError(Status(), fallback_error_cstr);
275   }
276 }
277
278 void SBCommandReturnObject::SetError(const char *error_cstr) {
279   if (m_opaque_ap && error_cstr)
280     m_opaque_ap->SetError(error_cstr);
281 }