blob: 157997b2502f667bdb017c3eff642bd0099a3dd5 [file] [log] [blame]
Chris Lattner30fdc8d2010-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 Ticedde9cff2010-09-20 05:20:02 +000011#include "lldb/API/SBStream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/Error.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000013#include "lldb/Core/Log.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000014
Eli Friedman9c68da92010-06-09 08:46:23 +000015#include <stdarg.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016
17using namespace lldb;
18using namespace lldb_private;
19
20
21SBError::SBError () :
Greg Clayton66111032010-06-23 01:19:29 +000022 m_opaque_ap ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023{
24}
25
26SBError::SBError (const SBError &rhs) :
Greg Clayton66111032010-06-23 01:19:29 +000027 m_opaque_ap ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028{
29 if (rhs.IsValid())
Greg Clayton66111032010-06-23 01:19:29 +000030 m_opaque_ap.reset (new Error(*rhs));
Chris Lattner30fdc8d2010-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 Clayton66111032010-06-23 01:19:29 +000043 if (m_opaque_ap.get())
44 *m_opaque_ap = *rhs;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045 else
Greg Clayton66111032010-06-23 01:19:29 +000046 m_opaque_ap.reset (new Error(*rhs));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047 }
48 else
Greg Clayton66111032010-06-23 01:19:29 +000049 m_opaque_ap.reset();
Caroline Ticeceb6b132010-10-26 03:11:13 +000050
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 return *this;
52}
53
54
55const char *
56SBError::GetCString () const
57{
Greg Clayton66111032010-06-23 01:19:29 +000058 if (m_opaque_ap.get())
59 return m_opaque_ap->AsCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060 return NULL;
61}
62
63void
64SBError::Clear ()
65{
Greg Clayton66111032010-06-23 01:19:29 +000066 if (m_opaque_ap.get())
67 m_opaque_ap->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068}
69
70bool
71SBError::Fail () const
72{
Greg Clayton5160ce52013-03-27 23:08:40 +000073 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +000074
Caroline Ticeceb6b132010-10-26 03:11:13 +000075 bool ret_value = false;
Greg Clayton66111032010-06-23 01:19:29 +000076 if (m_opaque_ap.get())
Caroline Ticeceb6b132010-10-26 03:11:13 +000077 ret_value = m_opaque_ap->Fail();
78
79 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000080 log->Printf ("SBError(%p)::Fail () => %i",
81 static_cast<void*>(m_opaque_ap.get()), ret_value);
Caroline Ticeceb6b132010-10-26 03:11:13 +000082
83 return ret_value;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084}
85
86bool
87SBError::Success () const
88{
Greg Clayton5160ce52013-03-27 23:08:40 +000089 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Jim Ingham16e0c682011-08-12 23:34:31 +000090 bool ret_value = true;
Greg Clayton66111032010-06-23 01:19:29 +000091 if (m_opaque_ap.get())
Greg Clayton48381312010-10-30 04:51:46 +000092 ret_value = m_opaque_ap->Success();
93
94 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000095 log->Printf ("SBError(%p)::Success () => %i",
96 static_cast<void*>(m_opaque_ap.get()), ret_value);
Greg Clayton48381312010-10-30 04:51:46 +000097
98 return ret_value;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099}
100
101uint32_t
102SBError::GetError () const
103{
Greg Clayton5160ce52013-03-27 23:08:40 +0000104 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000105
106 uint32_t err = 0;
Greg Clayton66111032010-06-23 01:19:29 +0000107 if (m_opaque_ap.get())
Greg Clayton48381312010-10-30 04:51:46 +0000108 err = m_opaque_ap->GetError();
109
110 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000111 log->Printf ("SBError(%p)::GetError () => 0x%8.8x",
112 static_cast<void*>(m_opaque_ap.get()), err);
Greg Clayton48381312010-10-30 04:51:46 +0000113
114
115 return err;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116}
117
118ErrorType
119SBError::GetType () const
120{
Greg Clayton5160ce52013-03-27 23:08:40 +0000121 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton48381312010-10-30 04:51:46 +0000122 ErrorType err_type = eErrorTypeInvalid;
Greg Clayton66111032010-06-23 01:19:29 +0000123 if (m_opaque_ap.get())
Greg Clayton48381312010-10-30 04:51:46 +0000124 err_type = m_opaque_ap->GetType();
125
126 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000127 log->Printf ("SBError(%p)::GetType () => %i",
128 static_cast<void*>(m_opaque_ap.get()), err_type);
Greg Clayton48381312010-10-30 04:51:46 +0000129
130 return err_type;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131}
132
133void
134SBError::SetError (uint32_t err, ErrorType type)
135{
136 CreateIfNeeded ();
Greg Clayton66111032010-06-23 01:19:29 +0000137 m_opaque_ap->SetError (err, type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138}
139
140void
141SBError::SetError (const Error &lldb_error)
142{
143 CreateIfNeeded ();
Greg Clayton66111032010-06-23 01:19:29 +0000144 *m_opaque_ap = lldb_error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145}
146
147
148void
149SBError::SetErrorToErrno ()
150{
151 CreateIfNeeded ();
Greg Clayton66111032010-06-23 01:19:29 +0000152 m_opaque_ap->SetErrorToErrno ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153}
154
155void
156SBError::SetErrorToGenericError ()
157{
158 CreateIfNeeded ();
Greg Clayton66111032010-06-23 01:19:29 +0000159 m_opaque_ap->SetErrorToErrno ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160}
161
162void
163SBError::SetErrorString (const char *err_str)
164{
165 CreateIfNeeded ();
Greg Clayton66111032010-06-23 01:19:29 +0000166 m_opaque_ap->SetErrorString (err_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167}
168
169int
170SBError::SetErrorStringWithFormat (const char *format, ...)
171{
172 CreateIfNeeded ();
173 va_list args;
174 va_start (args, format);
Greg Clayton66111032010-06-23 01:19:29 +0000175 int num_chars = m_opaque_ap->SetErrorStringWithVarArg (format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176 va_end (args);
177 return num_chars;
178}
179
180bool
181SBError::IsValid () const
182{
Greg Clayton66111032010-06-23 01:19:29 +0000183 return m_opaque_ap.get() != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184}
185
186void
187SBError::CreateIfNeeded ()
188{
Greg Clayton66111032010-06-23 01:19:29 +0000189 if (m_opaque_ap.get() == NULL)
190 m_opaque_ap.reset(new Error ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191}
192
193
194lldb_private::Error *
195SBError::operator->()
196{
Greg Clayton66111032010-06-23 01:19:29 +0000197 return m_opaque_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198}
199
200lldb_private::Error *
201SBError::get()
202{
Greg Clayton66111032010-06-23 01:19:29 +0000203 return m_opaque_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204}
205
Greg Clayton8f343b02010-11-04 01:54:29 +0000206lldb_private::Error &
207SBError::ref()
208{
209 CreateIfNeeded();
210 return *m_opaque_ap;
211}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212
213const lldb_private::Error &
214SBError::operator*() const
215{
216 // Be sure to call "IsValid()" before calling this function or it will crash
Greg Clayton66111032010-06-23 01:19:29 +0000217 return *m_opaque_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218}
219
Caroline Ticedde9cff2010-09-20 05:20:02 +0000220bool
221SBError::GetDescription (SBStream &description)
222{
223 if (m_opaque_ap.get())
224 {
Greg Clayton262f80d2011-07-06 16:49:27 +0000225 if (m_opaque_ap->Success())
226 description.Printf ("success");
Caroline Ticedde9cff2010-09-20 05:20:02 +0000227 else
228 {
229 const char * err_string = GetCString();
Greg Clayton262f80d2011-07-06 16:49:27 +0000230 description.Printf ("error: %s", (err_string != NULL ? err_string : ""));
Caroline Ticedde9cff2010-09-20 05:20:02 +0000231 }
232 }
233 else
Greg Clayton262f80d2011-07-06 16:49:27 +0000234 description.Printf ("error: <NULL>");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000235
236 return true;
237}