Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / source / API / SBError.cpp
blob2eb9e927514ac3591c143825272cb56217c0931e
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"
15 #include <cstdarg>
17 using namespace lldb;
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);
44 if (this != &rhs)
45 m_opaque_up = clone(rhs.m_opaque_up);
46 return *this;
49 const char *SBError::GetCString() const {
50 LLDB_INSTRUMENT_VA(this);
52 if (m_opaque_up)
53 return m_opaque_up->AsCString();
54 return nullptr;
57 void SBError::Clear() {
58 LLDB_INSTRUMENT_VA(this);
60 if (m_opaque_up)
61 m_opaque_up->Clear();
64 bool SBError::Fail() const {
65 LLDB_INSTRUMENT_VA(this);
67 bool ret_value = false;
68 if (m_opaque_up)
69 ret_value = m_opaque_up->Fail();
72 return ret_value;
75 bool SBError::Success() const {
76 LLDB_INSTRUMENT_VA(this);
78 bool ret_value = true;
79 if (m_opaque_up)
80 ret_value = m_opaque_up->Success();
82 return ret_value;
85 uint32_t SBError::GetError() const {
86 LLDB_INSTRUMENT_VA(this);
88 uint32_t err = 0;
89 if (m_opaque_up)
90 err = m_opaque_up->GetError();
93 return err;
96 ErrorType SBError::GetType() const {
97 LLDB_INSTRUMENT_VA(this);
99 ErrorType err_type = eErrorTypeInvalid;
100 if (m_opaque_up)
101 err_type = m_opaque_up->GetType();
103 return err_type;
106 void SBError::SetError(uint32_t err, ErrorType type) {
107 LLDB_INSTRUMENT_VA(this, err, type);
109 CreateIfNeeded();
110 m_opaque_up->SetError(err, type);
113 void SBError::SetError(const Status &lldb_error) {
114 CreateIfNeeded();
115 *m_opaque_up = lldb_error;
118 void SBError::SetErrorToErrno() {
119 LLDB_INSTRUMENT_VA(this);
121 CreateIfNeeded();
122 m_opaque_up->SetErrorToErrno();
125 void SBError::SetErrorToGenericError() {
126 LLDB_INSTRUMENT_VA(this);
128 CreateIfNeeded();
129 m_opaque_up->SetErrorToGenericError();
132 void SBError::SetErrorString(const char *err_str) {
133 LLDB_INSTRUMENT_VA(this, err_str);
135 CreateIfNeeded();
136 m_opaque_up->SetErrorString(err_str);
139 int SBError::SetErrorStringWithFormat(const char *format, ...) {
140 CreateIfNeeded();
141 va_list args;
142 va_start(args, format);
143 int num_chars = m_opaque_up->SetErrorStringWithVarArg(format, args);
144 va_end(args);
145 return num_chars;
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() {
168 CreateIfNeeded();
169 return *m_opaque_up;
172 const lldb_private::Status &SBError::operator*() const {
173 // Be sure to call "IsValid()" before calling this function or it will crash
174 return *m_opaque_up;
177 bool SBError::GetDescription(SBStream &description) {
178 LLDB_INSTRUMENT_VA(this, description);
180 if (m_opaque_up) {
181 if (m_opaque_up->Success())
182 description.Printf("success");
183 else {
184 const char *err_string = GetCString();
185 description.Printf("error: %s",
186 (err_string != nullptr ? err_string : ""));
188 } else
189 description.Printf("error: <NULL>");
191 return true;