[PowerPC] Collect some CallLowering arguments into a struct. [NFC]
[llvm-project.git] / lldb / source / API / SBError.cpp
blob7256e8e55de9445cb934092241f479e1a116d695
1 //===-- SBError.cpp ---------------------------------------------*- C++ -*-===//
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 "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/Utility/Status.h"
15 #include <stdarg.h>
17 using namespace lldb;
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);
34 if (this != &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);
42 if (m_opaque_up)
43 return m_opaque_up->AsCString();
44 return nullptr;
47 void SBError::Clear() {
48 LLDB_RECORD_METHOD_NO_ARGS(void, SBError, Clear);
50 if (m_opaque_up)
51 m_opaque_up->Clear();
54 bool SBError::Fail() const {
55 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBError, Fail);
57 bool ret_value = false;
58 if (m_opaque_up)
59 ret_value = m_opaque_up->Fail();
62 return ret_value;
65 bool SBError::Success() const {
66 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBError, Success);
68 bool ret_value = true;
69 if (m_opaque_up)
70 ret_value = m_opaque_up->Success();
72 return ret_value;
75 uint32_t SBError::GetError() const {
76 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBError, GetError);
79 uint32_t err = 0;
80 if (m_opaque_up)
81 err = m_opaque_up->GetError();
84 return err;
87 ErrorType SBError::GetType() const {
88 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::ErrorType, SBError, GetType);
90 ErrorType err_type = eErrorTypeInvalid;
91 if (m_opaque_up)
92 err_type = m_opaque_up->GetType();
94 return err_type;
97 void SBError::SetError(uint32_t err, ErrorType type) {
98 LLDB_RECORD_METHOD(void, SBError, SetError, (uint32_t, lldb::ErrorType), err,
99 type);
101 CreateIfNeeded();
102 m_opaque_up->SetError(err, type);
105 void SBError::SetError(const Status &lldb_error) {
106 CreateIfNeeded();
107 *m_opaque_up = lldb_error;
110 void SBError::SetErrorToErrno() {
111 LLDB_RECORD_METHOD_NO_ARGS(void, SBError, SetErrorToErrno);
113 CreateIfNeeded();
114 m_opaque_up->SetErrorToErrno();
117 void SBError::SetErrorToGenericError() {
118 LLDB_RECORD_METHOD_NO_ARGS(void, SBError, SetErrorToGenericError);
120 CreateIfNeeded();
121 m_opaque_up->SetErrorToErrno();
124 void SBError::SetErrorString(const char *err_str) {
125 LLDB_RECORD_METHOD(void, SBError, SetErrorString, (const char *), err_str);
127 CreateIfNeeded();
128 m_opaque_up->SetErrorString(err_str);
131 int SBError::SetErrorStringWithFormat(const char *format, ...) {
132 CreateIfNeeded();
133 va_list args;
134 va_start(args, format);
135 int num_chars = m_opaque_up->SetErrorStringWithVarArg(format, args);
136 va_end(args);
137 return num_chars;
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() {
160 CreateIfNeeded();
161 return *m_opaque_up;
164 const lldb_private::Status &SBError::operator*() const {
165 // Be sure to call "IsValid()" before calling this function or it will crash
166 return *m_opaque_up;
169 bool SBError::GetDescription(SBStream &description) {
170 LLDB_RECORD_METHOD(bool, SBError, GetDescription, (lldb::SBStream &),
171 description);
173 if (m_opaque_up) {
174 if (m_opaque_up->Success())
175 description.Printf("success");
176 else {
177 const char *err_string = GetCString();
178 description.Printf("error: %s",
179 (err_string != nullptr ? err_string : ""));
181 } else
182 description.Printf("error: <NULL>");
184 return true;
187 namespace lldb_private {
188 namespace repro {
190 template <>
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 &));