blob: d4827175761aebea621a8630d2c5073fd3a94bc2 [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{
29 if (rhs.IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +000030 m_opaque_ap.reset (new Error(*rhs));
Chris Lattner24943d22010-06-08 16:52:24 +000031}
32
33
34SBError::~SBError()
35{
36}
37
38const SBError &
39SBError::operator = (const SBError &rhs)
40{
41 if (rhs.IsValid())
42 {
Greg Clayton63094e02010-06-23 01:19:29 +000043 if (m_opaque_ap.get())
44 *m_opaque_ap = *rhs;
Chris Lattner24943d22010-06-08 16:52:24 +000045 else
Greg Clayton63094e02010-06-23 01:19:29 +000046 m_opaque_ap.reset (new Error(*rhs));
Chris Lattner24943d22010-06-08 16:52:24 +000047 }
48 else
Greg Clayton63094e02010-06-23 01:19:29 +000049 m_opaque_ap.reset();
Caroline Tice7826c882010-10-26 03:11:13 +000050
Chris Lattner24943d22010-06-08 16:52:24 +000051 return *this;
52}
53
54
55const char *
56SBError::GetCString () const
57{
Greg Clayton63094e02010-06-23 01:19:29 +000058 if (m_opaque_ap.get())
59 return m_opaque_ap->AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +000060 return NULL;
61}
62
63void
64SBError::Clear ()
65{
Greg Clayton63094e02010-06-23 01:19:29 +000066 if (m_opaque_ap.get())
67 m_opaque_ap->Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000068}
69
70bool
71SBError::Fail () const
72{
Greg Claytone005f2c2010-11-06 01:53:30 +000073 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000074
Caroline Tice7826c882010-10-26 03:11:13 +000075 bool ret_value = false;
Greg Clayton63094e02010-06-23 01:19:29 +000076 if (m_opaque_ap.get())
Caroline Tice7826c882010-10-26 03:11:13 +000077 ret_value = m_opaque_ap->Fail();
78
79 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +000080 log->Printf ("SBError(%p)::Fail () => %i", m_opaque_ap.get(), ret_value);
Caroline Tice7826c882010-10-26 03:11:13 +000081
82 return ret_value;
Chris Lattner24943d22010-06-08 16:52:24 +000083}
84
85bool
86SBError::Success () const
87{
Greg Claytone005f2c2010-11-06 01:53:30 +000088 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham574c3d62011-08-12 23:34:31 +000089 bool ret_value = true;
Greg Clayton63094e02010-06-23 01:19:29 +000090 if (m_opaque_ap.get())
Greg Claytona66ba462010-10-30 04:51:46 +000091 ret_value = m_opaque_ap->Success();
92
93 if (log)
94 log->Printf ("SBError(%p)::Success () => %i", m_opaque_ap.get(), ret_value);
95
96 return ret_value;
Chris Lattner24943d22010-06-08 16:52:24 +000097}
98
99uint32_t
100SBError::GetError () const
101{
Greg Claytone005f2c2010-11-06 01:53:30 +0000102 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000103
104 uint32_t err = 0;
Greg Clayton63094e02010-06-23 01:19:29 +0000105 if (m_opaque_ap.get())
Greg Claytona66ba462010-10-30 04:51:46 +0000106 err = m_opaque_ap->GetError();
107
108 if (log)
109 log->Printf ("SBError(%p)::GetError () => 0x%8.8x", m_opaque_ap.get(), err);
110
111
112 return err;
Chris Lattner24943d22010-06-08 16:52:24 +0000113}
114
115ErrorType
116SBError::GetType () const
117{
Greg Claytone005f2c2010-11-06 01:53:30 +0000118 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000119 ErrorType err_type = eErrorTypeInvalid;
Greg Clayton63094e02010-06-23 01:19:29 +0000120 if (m_opaque_ap.get())
Greg Claytona66ba462010-10-30 04:51:46 +0000121 err_type = m_opaque_ap->GetType();
122
123 if (log)
124 log->Printf ("SBError(%p)::GetType () => %i", m_opaque_ap.get(), err_type);
125
126 return err_type;
Chris Lattner24943d22010-06-08 16:52:24 +0000127}
128
129void
130SBError::SetError (uint32_t err, ErrorType type)
131{
132 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000133 m_opaque_ap->SetError (err, type);
Chris Lattner24943d22010-06-08 16:52:24 +0000134}
135
136void
137SBError::SetError (const Error &lldb_error)
138{
139 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000140 *m_opaque_ap = lldb_error;
Chris Lattner24943d22010-06-08 16:52:24 +0000141}
142
143
144void
145SBError::SetErrorToErrno ()
146{
147 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000148 m_opaque_ap->SetErrorToErrno ();
Chris Lattner24943d22010-06-08 16:52:24 +0000149}
150
151void
152SBError::SetErrorToGenericError ()
153{
154 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000155 m_opaque_ap->SetErrorToErrno ();
Chris Lattner24943d22010-06-08 16:52:24 +0000156}
157
158void
159SBError::SetErrorString (const char *err_str)
160{
161 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000162 m_opaque_ap->SetErrorString (err_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000163}
164
165int
166SBError::SetErrorStringWithFormat (const char *format, ...)
167{
168 CreateIfNeeded ();
169 va_list args;
170 va_start (args, format);
Greg Clayton63094e02010-06-23 01:19:29 +0000171 int num_chars = m_opaque_ap->SetErrorStringWithVarArg (format, args);
Chris Lattner24943d22010-06-08 16:52:24 +0000172 va_end (args);
173 return num_chars;
174}
175
176bool
177SBError::IsValid () const
178{
Greg Clayton63094e02010-06-23 01:19:29 +0000179 return m_opaque_ap.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000180}
181
182void
183SBError::CreateIfNeeded ()
184{
Greg Clayton63094e02010-06-23 01:19:29 +0000185 if (m_opaque_ap.get() == NULL)
186 m_opaque_ap.reset(new Error ());
Chris Lattner24943d22010-06-08 16:52:24 +0000187}
188
189
190lldb_private::Error *
191SBError::operator->()
192{
Greg Clayton63094e02010-06-23 01:19:29 +0000193 return m_opaque_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000194}
195
196lldb_private::Error *
197SBError::get()
198{
Greg Clayton63094e02010-06-23 01:19:29 +0000199 return m_opaque_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000200}
201
Greg Clayton0baa3942010-11-04 01:54:29 +0000202lldb_private::Error &
203SBError::ref()
204{
205 CreateIfNeeded();
206 return *m_opaque_ap;
207}
Chris Lattner24943d22010-06-08 16:52:24 +0000208
209const lldb_private::Error &
210SBError::operator*() const
211{
212 // Be sure to call "IsValid()" before calling this function or it will crash
Greg Clayton63094e02010-06-23 01:19:29 +0000213 return *m_opaque_ap;
Chris Lattner24943d22010-06-08 16:52:24 +0000214}
215
Caroline Tice98f930f2010-09-20 05:20:02 +0000216bool
217SBError::GetDescription (SBStream &description)
218{
219 if (m_opaque_ap.get())
220 {
Greg Claytonbafc86e2011-07-06 16:49:27 +0000221 if (m_opaque_ap->Success())
222 description.Printf ("success");
Caroline Tice98f930f2010-09-20 05:20:02 +0000223 else
224 {
225 const char * err_string = GetCString();
Greg Claytonbafc86e2011-07-06 16:49:27 +0000226 description.Printf ("error: %s", (err_string != NULL ? err_string : ""));
Caroline Tice98f930f2010-09-20 05:20:02 +0000227 }
228 }
229 else
Greg Claytonbafc86e2011-07-06 16:49:27 +0000230 description.Printf ("error: <NULL>");
Caroline Tice7826c882010-10-26 03:11:13 +0000231
232 return true;
233}