blob: 229b894e799842a9d6f7d231dabd546d4aba6519 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- SBError.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#include "lldb/API/SBError.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000011#include "lldb/API/SBStream.h"
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "lldb/Core/Error.h"
Caroline Tice7826c882010-10-26 03:11:13 +000013#include "lldb/Core/Log.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000014
Eli Friedman1501a962010-06-09 08:46:23 +000015#include <stdarg.h>
Chris Lattner24943d22010-06-08 16:52:24 +000016
17using namespace lldb;
18using namespace lldb_private;
19
20
21SBError::SBError () :
Greg Clayton63094e02010-06-23 01:19:29 +000022 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000023{
24}
25
26SBError::SBError (const SBError &rhs) :
Greg Clayton63094e02010-06-23 01:19:29 +000027 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000028{
Caroline Tice61ba7ec2010-10-26 23:49:36 +000029 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Tice7826c882010-10-26 03:11:13 +000030
Chris Lattner24943d22010-06-08 16:52:24 +000031 if (rhs.IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +000032 m_opaque_ap.reset (new Error(*rhs));
Caroline Tice7826c882010-10-26 03:11:13 +000033
34 if (log)
35 {
36 SBStream sstr;
37 GetDescription (sstr);
Greg Claytona66ba462010-10-30 04:51:46 +000038 log->Printf ("SBError::SBError (const SBError rhs.ap=%p) => SBError(%p): %s",
39 rhs.m_opaque_ap.get(), m_opaque_ap.get(), sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +000040 }
Chris Lattner24943d22010-06-08 16:52:24 +000041}
42
43
44SBError::~SBError()
45{
46}
47
48const SBError &
49SBError::operator = (const SBError &rhs)
50{
Caroline Tice7826c882010-10-26 03:11:13 +000051 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Greg Claytona66ba462010-10-30 04:51:46 +000052 void *old_error = m_opaque_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +000053 if (rhs.IsValid())
54 {
Greg Clayton63094e02010-06-23 01:19:29 +000055 if (m_opaque_ap.get())
56 *m_opaque_ap = *rhs;
Chris Lattner24943d22010-06-08 16:52:24 +000057 else
Greg Clayton63094e02010-06-23 01:19:29 +000058 m_opaque_ap.reset (new Error(*rhs));
Chris Lattner24943d22010-06-08 16:52:24 +000059 }
60 else
61 {
Greg Clayton63094e02010-06-23 01:19:29 +000062 m_opaque_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +000063 }
Caroline Tice7826c882010-10-26 03:11:13 +000064
65 if (log)
Caroline Tice61ba7ec2010-10-26 23:49:36 +000066 {
67 SBStream sstr;
68 GetDescription (sstr);
Greg Claytona66ba462010-10-30 04:51:46 +000069 log->Printf ("SBError(%p)::operator= (SBError(%p)) => SBError(%s)",
70 old_error, rhs.m_opaque_ap.get(), sstr.GetData());
Caroline Tice61ba7ec2010-10-26 23:49:36 +000071 }
Caroline Tice7826c882010-10-26 03:11:13 +000072
Chris Lattner24943d22010-06-08 16:52:24 +000073 return *this;
74}
75
76
77const char *
78SBError::GetCString () const
79{
Greg Clayton63094e02010-06-23 01:19:29 +000080 if (m_opaque_ap.get())
81 return m_opaque_ap->AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +000082 return NULL;
83}
84
85void
86SBError::Clear ()
87{
Greg Clayton63094e02010-06-23 01:19:29 +000088 if (m_opaque_ap.get())
89 m_opaque_ap->Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000090}
91
92bool
93SBError::Fail () const
94{
Caroline Tice7826c882010-10-26 03:11:13 +000095 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
96
Caroline Tice7826c882010-10-26 03:11:13 +000097 bool ret_value = false;
Greg Clayton63094e02010-06-23 01:19:29 +000098 if (m_opaque_ap.get())
Caroline Tice7826c882010-10-26 03:11:13 +000099 ret_value = m_opaque_ap->Fail();
100
101 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +0000102 log->Printf ("SBError(%p)::Fail () => %i", m_opaque_ap.get(), ret_value);
Caroline Tice7826c882010-10-26 03:11:13 +0000103
104 return ret_value;
Chris Lattner24943d22010-06-08 16:52:24 +0000105}
106
107bool
108SBError::Success () const
109{
Greg Claytona66ba462010-10-30 04:51:46 +0000110 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
111 bool ret_value = false;
Greg Clayton63094e02010-06-23 01:19:29 +0000112 if (m_opaque_ap.get())
Greg Claytona66ba462010-10-30 04:51:46 +0000113 ret_value = m_opaque_ap->Success();
114
115 if (log)
116 log->Printf ("SBError(%p)::Success () => %i", m_opaque_ap.get(), ret_value);
117
118 return ret_value;
Chris Lattner24943d22010-06-08 16:52:24 +0000119}
120
121uint32_t
122SBError::GetError () const
123{
Greg Claytona66ba462010-10-30 04:51:46 +0000124 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
125
126 uint32_t err = 0;
Greg Clayton63094e02010-06-23 01:19:29 +0000127 if (m_opaque_ap.get())
Greg Claytona66ba462010-10-30 04:51:46 +0000128 err = m_opaque_ap->GetError();
129
130 if (log)
131 log->Printf ("SBError(%p)::GetError () => 0x%8.8x", m_opaque_ap.get(), err);
132
133
134 return err;
Chris Lattner24943d22010-06-08 16:52:24 +0000135}
136
137ErrorType
138SBError::GetType () const
139{
Greg Claytona66ba462010-10-30 04:51:46 +0000140 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
141 ErrorType err_type = eErrorTypeInvalid;
Greg Clayton63094e02010-06-23 01:19:29 +0000142 if (m_opaque_ap.get())
Greg Claytona66ba462010-10-30 04:51:46 +0000143 err_type = m_opaque_ap->GetType();
144
145 if (log)
146 log->Printf ("SBError(%p)::GetType () => %i", m_opaque_ap.get(), err_type);
147
148 return err_type;
Chris Lattner24943d22010-06-08 16:52:24 +0000149}
150
151void
152SBError::SetError (uint32_t err, ErrorType type)
153{
154 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000155 m_opaque_ap->SetError (err, type);
Chris Lattner24943d22010-06-08 16:52:24 +0000156}
157
158void
159SBError::SetError (const Error &lldb_error)
160{
161 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000162 *m_opaque_ap = lldb_error;
Chris Lattner24943d22010-06-08 16:52:24 +0000163}
164
165
166void
167SBError::SetErrorToErrno ()
168{
169 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000170 m_opaque_ap->SetErrorToErrno ();
Chris Lattner24943d22010-06-08 16:52:24 +0000171}
172
173void
174SBError::SetErrorToGenericError ()
175{
176 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000177 m_opaque_ap->SetErrorToErrno ();
Chris Lattner24943d22010-06-08 16:52:24 +0000178}
179
180void
181SBError::SetErrorString (const char *err_str)
182{
183 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000184 m_opaque_ap->SetErrorString (err_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000185}
186
187int
188SBError::SetErrorStringWithFormat (const char *format, ...)
189{
190 CreateIfNeeded ();
191 va_list args;
192 va_start (args, format);
Greg Clayton63094e02010-06-23 01:19:29 +0000193 int num_chars = m_opaque_ap->SetErrorStringWithVarArg (format, args);
Chris Lattner24943d22010-06-08 16:52:24 +0000194 va_end (args);
195 return num_chars;
196}
197
198bool
199SBError::IsValid () const
200{
Greg Clayton63094e02010-06-23 01:19:29 +0000201 return m_opaque_ap.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000202}
203
204void
205SBError::CreateIfNeeded ()
206{
Greg Clayton63094e02010-06-23 01:19:29 +0000207 if (m_opaque_ap.get() == NULL)
208 m_opaque_ap.reset(new Error ());
Chris Lattner24943d22010-06-08 16:52:24 +0000209}
210
211
212lldb_private::Error *
213SBError::operator->()
214{
Greg Clayton63094e02010-06-23 01:19:29 +0000215 return m_opaque_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000216}
217
218lldb_private::Error *
219SBError::get()
220{
Greg Clayton63094e02010-06-23 01:19:29 +0000221 return m_opaque_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000222}
223
224
225const lldb_private::Error &
226SBError::operator*() const
227{
228 // Be sure to call "IsValid()" before calling this function or it will crash
Greg Clayton63094e02010-06-23 01:19:29 +0000229 return *m_opaque_ap;
Chris Lattner24943d22010-06-08 16:52:24 +0000230}
231
Caroline Tice98f930f2010-09-20 05:20:02 +0000232bool
233SBError::GetDescription (SBStream &description)
234{
235 if (m_opaque_ap.get())
236 {
237 if (Success())
238 description.Printf ("Status: Success");
239 else
240 {
241 const char * err_string = GetCString();
242 description.Printf ("Status: Error: %s", (err_string != NULL ? err_string : ""));
243 }
244 }
245 else
246 description.Printf ("No value");
247
248 return true;
249}
Caroline Tice7826c882010-10-26 03:11:13 +0000250
251bool
252SBError::GetDescription (SBStream &description) const
253{
254 if (m_opaque_ap.get())
255 {
256 if (Success())
257 description.Printf ("Status: Success");
258 else
259 {
260 const char * err_string = GetCString();
261 description.Printf ("Status: Error: %s", (err_string != NULL ? err_string : ""));
262 }
263 }
264 else
265 description.Printf ("No value");
266
267 return true;
268}