1 //===-- SBError.cpp ---------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBError.h"
10 #include "SBReproducerPrivate.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/Utility/Status.h"
18 using namespace lldb_private
;
20 SBError::SBError() : m_opaque_up() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBError
); }
22 SBError::SBError(const SBError
&rhs
) : m_opaque_up() {
23 LLDB_RECORD_CONSTRUCTOR(SBError
, (const lldb::SBError
&), rhs
);
25 m_opaque_up
= clone(rhs
.m_opaque_up
);
28 SBError::~SBError() {}
30 const SBError
&SBError::operator=(const SBError
&rhs
) {
31 LLDB_RECORD_METHOD(const lldb::SBError
&,
32 SBError
, operator=,(const lldb::SBError
&), rhs
);
35 m_opaque_up
= clone(rhs
.m_opaque_up
);
36 return LLDB_RECORD_RESULT(*this);
39 const char *SBError::GetCString() const {
40 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBError
, GetCString
);
43 return m_opaque_up
->AsCString();
47 void SBError::Clear() {
48 LLDB_RECORD_METHOD_NO_ARGS(void, SBError
, Clear
);
54 bool SBError::Fail() const {
55 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBError
, Fail
);
57 bool ret_value
= false;
59 ret_value
= m_opaque_up
->Fail();
65 bool SBError::Success() const {
66 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBError
, Success
);
68 bool ret_value
= true;
70 ret_value
= m_opaque_up
->Success();
75 uint32_t SBError::GetError() const {
76 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBError
, GetError
);
81 err
= m_opaque_up
->GetError();
87 ErrorType
SBError::GetType() const {
88 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::ErrorType
, SBError
, GetType
);
90 ErrorType err_type
= eErrorTypeInvalid
;
92 err_type
= m_opaque_up
->GetType();
97 void SBError::SetError(uint32_t err
, ErrorType type
) {
98 LLDB_RECORD_METHOD(void, SBError
, SetError
, (uint32_t, lldb::ErrorType
), err
,
102 m_opaque_up
->SetError(err
, type
);
105 void SBError::SetError(const Status
&lldb_error
) {
107 *m_opaque_up
= lldb_error
;
110 void SBError::SetErrorToErrno() {
111 LLDB_RECORD_METHOD_NO_ARGS(void, SBError
, SetErrorToErrno
);
114 m_opaque_up
->SetErrorToErrno();
117 void SBError::SetErrorToGenericError() {
118 LLDB_RECORD_METHOD_NO_ARGS(void, SBError
, SetErrorToGenericError
);
121 m_opaque_up
->SetErrorToErrno();
124 void SBError::SetErrorString(const char *err_str
) {
125 LLDB_RECORD_METHOD(void, SBError
, SetErrorString
, (const char *), err_str
);
128 m_opaque_up
->SetErrorString(err_str
);
131 int SBError::SetErrorStringWithFormat(const char *format
, ...) {
134 va_start(args
, format
);
135 int num_chars
= m_opaque_up
->SetErrorStringWithVarArg(format
, args
);
140 bool SBError::IsValid() const {
141 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBError
, IsValid
);
142 return this->operator bool();
144 SBError::operator bool() const {
145 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBError
, operator bool);
147 return m_opaque_up
!= nullptr;
150 void SBError::CreateIfNeeded() {
151 if (m_opaque_up
== nullptr)
152 m_opaque_up
.reset(new Status());
155 lldb_private::Status
*SBError::operator->() { return m_opaque_up
.get(); }
157 lldb_private::Status
*SBError::get() { return m_opaque_up
.get(); }
159 lldb_private::Status
&SBError::ref() {
164 const lldb_private::Status
&SBError::operator*() const {
165 // Be sure to call "IsValid()" before calling this function or it will crash
169 bool SBError::GetDescription(SBStream
&description
) {
170 LLDB_RECORD_METHOD(bool, SBError
, GetDescription
, (lldb::SBStream
&),
174 if (m_opaque_up
->Success())
175 description
.Printf("success");
177 const char *err_string
= GetCString();
178 description
.Printf("error: %s",
179 (err_string
!= nullptr ? err_string
: ""));
182 description
.Printf("error: <NULL>");
187 namespace lldb_private
{
191 void RegisterMethods
<SBError
>(Registry
&R
) {
192 LLDB_REGISTER_CONSTRUCTOR(SBError
, ());
193 LLDB_REGISTER_CONSTRUCTOR(SBError
, (const lldb::SBError
&));
194 LLDB_REGISTER_METHOD(const lldb::SBError
&,
195 SBError
, operator=,(const lldb::SBError
&));
196 LLDB_REGISTER_METHOD_CONST(const char *, SBError
, GetCString
, ());
197 LLDB_REGISTER_METHOD(void, SBError
, Clear
, ());
198 LLDB_REGISTER_METHOD_CONST(bool, SBError
, Fail
, ());
199 LLDB_REGISTER_METHOD_CONST(bool, SBError
, Success
, ());
200 LLDB_REGISTER_METHOD_CONST(uint32_t, SBError
, GetError
, ());
201 LLDB_REGISTER_METHOD_CONST(lldb::ErrorType
, SBError
, GetType
, ());
202 LLDB_REGISTER_METHOD(void, SBError
, SetError
, (uint32_t, lldb::ErrorType
));
203 LLDB_REGISTER_METHOD(void, SBError
, SetErrorToErrno
, ());
204 LLDB_REGISTER_METHOD(void, SBError
, SetErrorToGenericError
, ());
205 LLDB_REGISTER_METHOD(void, SBError
, SetErrorString
, (const char *));
206 LLDB_REGISTER_METHOD_CONST(bool, SBError
, IsValid
, ());
207 LLDB_REGISTER_METHOD_CONST(bool, SBError
, operator bool, ());
208 LLDB_REGISTER_METHOD(bool, SBError
, GetDescription
, (lldb::SBStream
&));