1 //===-- SBError.cpp -------------------------------------------------------===//
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"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Utility/Instrumentation.h"
13 #include "lldb/Utility/Status.h"
18 using namespace lldb_private;
20 SBError::SBError() { LLDB_INSTRUMENT_VA(this); }
22 SBError::SBError(const SBError &rhs) {
23 LLDB_INSTRUMENT_VA(this, rhs);
25 m_opaque_up = clone(rhs.m_opaque_up);
28 SBError::SBError(const char *message) {
29 LLDB_INSTRUMENT_VA(this, message);
31 SetErrorString(message);
34 SBError::SBError(const lldb_private::Status &status)
35 : m_opaque_up(new Status(status)) {
36 LLDB_INSTRUMENT_VA(this, status);
39 SBError::~SBError() = default;
41 const SBError &SBError::operator=(const SBError &rhs) {
42 LLDB_INSTRUMENT_VA(this, rhs);
45 m_opaque_up = clone(rhs.m_opaque_up);
49 const char *SBError::GetCString() const {
50 LLDB_INSTRUMENT_VA(this);
53 return m_opaque_up->AsCString();
57 void SBError::Clear() {
58 LLDB_INSTRUMENT_VA(this);
64 bool SBError::Fail() const {
65 LLDB_INSTRUMENT_VA(this);
67 bool ret_value = false;
69 ret_value = m_opaque_up->Fail();
75 bool SBError::Success() const {
76 LLDB_INSTRUMENT_VA(this);
78 bool ret_value = true;
80 ret_value = m_opaque_up->Success();
85 uint32_t SBError::GetError() const {
86 LLDB_INSTRUMENT_VA(this);
90 err = m_opaque_up->GetError();
96 ErrorType SBError::GetType() const {
97 LLDB_INSTRUMENT_VA(this);
99 ErrorType err_type = eErrorTypeInvalid;
101 err_type = m_opaque_up->GetType();
106 void SBError::SetError(uint32_t err, ErrorType type) {
107 LLDB_INSTRUMENT_VA(this, err, type);
110 m_opaque_up->SetError(err, type);
113 void SBError::SetError(const Status &lldb_error) {
115 *m_opaque_up = lldb_error;
118 void SBError::SetErrorToErrno() {
119 LLDB_INSTRUMENT_VA(this);
122 m_opaque_up->SetErrorToErrno();
125 void SBError::SetErrorToGenericError() {
126 LLDB_INSTRUMENT_VA(this);
129 m_opaque_up->SetErrorToGenericError();
132 void SBError::SetErrorString(const char *err_str) {
133 LLDB_INSTRUMENT_VA(this, err_str);
136 m_opaque_up->SetErrorString(err_str);
139 int SBError::SetErrorStringWithFormat(const char *format, ...) {
142 va_start(args, format);
143 int num_chars = m_opaque_up->SetErrorStringWithVarArg(format, args);
148 bool SBError::IsValid() const {
149 LLDB_INSTRUMENT_VA(this);
150 return this->operator bool();
152 SBError::operator bool() const {
153 LLDB_INSTRUMENT_VA(this);
155 return m_opaque_up != nullptr;
158 void SBError::CreateIfNeeded() {
159 if (m_opaque_up == nullptr)
160 m_opaque_up = std::make_unique<Status>();
163 lldb_private::Status *SBError::operator->() { return m_opaque_up.get(); }
165 lldb_private::Status *SBError::get() { return m_opaque_up.get(); }
167 lldb_private::Status &SBError::ref() {
172 const lldb_private::Status &SBError::operator*() const {
173 // Be sure to call "IsValid()" before calling this function or it will crash
177 bool SBError::GetDescription(SBStream &description) {
178 LLDB_INSTRUMENT_VA(this, description);
181 if (m_opaque_up->Success())
182 description.Printf("success");
184 const char *err_string = GetCString();
185 description.Printf("error: %s",
186 (err_string != nullptr ? err_string : ""));
189 description.Printf("error: <NULL>");