[lldb][dwarf] Compute fully qualified names on simplified template names with DWARFT...
[llvm-project.git] / lldb / source / API / SBError.cpp
blob31964931649db33cda998578e7897b641e75929e
1 //===-- SBError.cpp -------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBError.h"
10 #include "Utils.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Utility/Instrumentation.h"
13 #include "lldb/Utility/Status.h"
14 #include "lldb/Utility/VASPrintf.h"
16 #include <cstdarg>
18 using namespace lldb;
19 using namespace lldb_private;
21 SBError::SBError() { LLDB_INSTRUMENT_VA(this); }
23 SBError::SBError(const SBError &rhs) {
24 LLDB_INSTRUMENT_VA(this, rhs);
26 if (rhs.m_opaque_up)
27 m_opaque_up = std::make_unique<Status>(rhs.m_opaque_up->Clone());
30 SBError::SBError(const char *message) {
31 LLDB_INSTRUMENT_VA(this, message);
33 SetErrorString(message);
36 SBError::SBError(lldb_private::Status &&status)
37 : m_opaque_up(new Status(std::move(status))) {
38 LLDB_INSTRUMENT_VA(this, status);
41 SBError::~SBError() = default;
43 const SBError &SBError::operator=(const SBError &rhs) {
44 LLDB_INSTRUMENT_VA(this, rhs);
46 if (this != &rhs)
47 if (rhs.m_opaque_up)
48 m_opaque_up = std::make_unique<Status>(rhs.m_opaque_up->Clone());
50 return *this;
53 const char *SBError::GetCString() const {
54 LLDB_INSTRUMENT_VA(this);
56 if (m_opaque_up)
57 return m_opaque_up->AsCString();
58 return nullptr;
61 void SBError::Clear() {
62 LLDB_INSTRUMENT_VA(this);
64 if (m_opaque_up)
65 m_opaque_up->Clear();
68 bool SBError::Fail() const {
69 LLDB_INSTRUMENT_VA(this);
71 bool ret_value = false;
72 if (m_opaque_up)
73 ret_value = m_opaque_up->Fail();
76 return ret_value;
79 bool SBError::Success() const {
80 LLDB_INSTRUMENT_VA(this);
82 bool ret_value = true;
83 if (m_opaque_up)
84 ret_value = m_opaque_up->Success();
86 return ret_value;
89 uint32_t SBError::GetError() const {
90 LLDB_INSTRUMENT_VA(this);
92 uint32_t err = 0;
93 if (m_opaque_up)
94 err = m_opaque_up->GetError();
97 return err;
100 ErrorType SBError::GetType() const {
101 LLDB_INSTRUMENT_VA(this);
103 ErrorType err_type = eErrorTypeInvalid;
104 if (m_opaque_up)
105 err_type = m_opaque_up->GetType();
107 return err_type;
110 void SBError::SetError(uint32_t err, ErrorType type) {
111 LLDB_INSTRUMENT_VA(this, err, type);
113 CreateIfNeeded();
114 *m_opaque_up = Status(err, type);
117 void SBError::SetError(Status &&lldb_error) {
118 CreateIfNeeded();
119 *m_opaque_up = std::move(lldb_error);
122 void SBError::SetErrorToErrno() {
123 LLDB_INSTRUMENT_VA(this);
125 CreateIfNeeded();
126 *m_opaque_up = Status::FromErrno();
129 void SBError::SetErrorToGenericError() {
130 LLDB_INSTRUMENT_VA(this);
132 CreateIfNeeded();
133 *m_opaque_up = Status(std::string("generic error"));
136 void SBError::SetErrorString(const char *err_str) {
137 LLDB_INSTRUMENT_VA(this, err_str);
139 CreateIfNeeded();
140 *m_opaque_up = Status::FromErrorString(err_str);
143 int SBError::SetErrorStringWithFormat(const char *format, ...) {
144 CreateIfNeeded();
145 std::string string;
146 va_list args;
147 va_start(args, format);
148 if (format != nullptr && format[0]) {
149 llvm::SmallString<1024> buf;
150 VASprintf(buf, format, args);
151 string = std::string(buf.str());
152 *m_opaque_up = Status(std::move(string));
154 va_end(args);
155 return string.size();
158 bool SBError::IsValid() const {
159 LLDB_INSTRUMENT_VA(this);
160 return this->operator bool();
162 SBError::operator bool() const {
163 LLDB_INSTRUMENT_VA(this);
165 return m_opaque_up != nullptr;
168 void SBError::CreateIfNeeded() {
169 if (m_opaque_up == nullptr)
170 m_opaque_up = std::make_unique<Status>();
173 lldb_private::Status *SBError::operator->() { return m_opaque_up.get(); }
175 lldb_private::Status *SBError::get() { return m_opaque_up.get(); }
177 lldb_private::Status &SBError::ref() {
178 CreateIfNeeded();
179 return *m_opaque_up;
182 const lldb_private::Status &SBError::operator*() const {
183 // Be sure to call "IsValid()" before calling this function or it will crash
184 return *m_opaque_up;
187 bool SBError::GetDescription(SBStream &description) {
188 LLDB_INSTRUMENT_VA(this, description);
190 if (m_opaque_up) {
191 if (m_opaque_up->Success())
192 description.Printf("success");
193 else {
194 const char *err_string = GetCString();
195 description.Printf("error: %s",
196 (err_string != nullptr ? err_string : ""));
198 } else
199 description.Printf("error: <NULL>");
201 return true;