Path: blob/main/contrib/llvm-project/lldb/source/API/SBError.cpp
39587 views
//===-- SBError.cpp -------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/API/SBError.h"9#include "Utils.h"10#include "lldb/API/SBStream.h"11#include "lldb/Utility/Instrumentation.h"12#include "lldb/Utility/Status.h"1314#include <cstdarg>1516using namespace lldb;17using namespace lldb_private;1819SBError::SBError() { LLDB_INSTRUMENT_VA(this); }2021SBError::SBError(const SBError &rhs) {22LLDB_INSTRUMENT_VA(this, rhs);2324m_opaque_up = clone(rhs.m_opaque_up);25}2627SBError::SBError(const char *message) {28LLDB_INSTRUMENT_VA(this, message);2930SetErrorString(message);31}3233SBError::SBError(const lldb_private::Status &status)34: m_opaque_up(new Status(status)) {35LLDB_INSTRUMENT_VA(this, status);36}3738SBError::~SBError() = default;3940const SBError &SBError::operator=(const SBError &rhs) {41LLDB_INSTRUMENT_VA(this, rhs);4243if (this != &rhs)44m_opaque_up = clone(rhs.m_opaque_up);45return *this;46}4748const char *SBError::GetCString() const {49LLDB_INSTRUMENT_VA(this);5051if (m_opaque_up)52return m_opaque_up->AsCString();53return nullptr;54}5556void SBError::Clear() {57LLDB_INSTRUMENT_VA(this);5859if (m_opaque_up)60m_opaque_up->Clear();61}6263bool SBError::Fail() const {64LLDB_INSTRUMENT_VA(this);6566bool ret_value = false;67if (m_opaque_up)68ret_value = m_opaque_up->Fail();697071return ret_value;72}7374bool SBError::Success() const {75LLDB_INSTRUMENT_VA(this);7677bool ret_value = true;78if (m_opaque_up)79ret_value = m_opaque_up->Success();8081return ret_value;82}8384uint32_t SBError::GetError() const {85LLDB_INSTRUMENT_VA(this);8687uint32_t err = 0;88if (m_opaque_up)89err = m_opaque_up->GetError();909192return err;93}9495ErrorType SBError::GetType() const {96LLDB_INSTRUMENT_VA(this);9798ErrorType err_type = eErrorTypeInvalid;99if (m_opaque_up)100err_type = m_opaque_up->GetType();101102return err_type;103}104105void SBError::SetError(uint32_t err, ErrorType type) {106LLDB_INSTRUMENT_VA(this, err, type);107108CreateIfNeeded();109m_opaque_up->SetError(err, type);110}111112void SBError::SetError(const Status &lldb_error) {113CreateIfNeeded();114*m_opaque_up = lldb_error;115}116117void SBError::SetErrorToErrno() {118LLDB_INSTRUMENT_VA(this);119120CreateIfNeeded();121m_opaque_up->SetErrorToErrno();122}123124void SBError::SetErrorToGenericError() {125LLDB_INSTRUMENT_VA(this);126127CreateIfNeeded();128m_opaque_up->SetErrorToGenericError();129}130131void SBError::SetErrorString(const char *err_str) {132LLDB_INSTRUMENT_VA(this, err_str);133134CreateIfNeeded();135m_opaque_up->SetErrorString(err_str);136}137138int SBError::SetErrorStringWithFormat(const char *format, ...) {139CreateIfNeeded();140va_list args;141va_start(args, format);142int num_chars = m_opaque_up->SetErrorStringWithVarArg(format, args);143va_end(args);144return num_chars;145}146147bool SBError::IsValid() const {148LLDB_INSTRUMENT_VA(this);149return this->operator bool();150}151SBError::operator bool() const {152LLDB_INSTRUMENT_VA(this);153154return m_opaque_up != nullptr;155}156157void SBError::CreateIfNeeded() {158if (m_opaque_up == nullptr)159m_opaque_up = std::make_unique<Status>();160}161162lldb_private::Status *SBError::operator->() { return m_opaque_up.get(); }163164lldb_private::Status *SBError::get() { return m_opaque_up.get(); }165166lldb_private::Status &SBError::ref() {167CreateIfNeeded();168return *m_opaque_up;169}170171const lldb_private::Status &SBError::operator*() const {172// Be sure to call "IsValid()" before calling this function or it will crash173return *m_opaque_up;174}175176bool SBError::GetDescription(SBStream &description) {177LLDB_INSTRUMENT_VA(this, description);178179if (m_opaque_up) {180if (m_opaque_up->Success())181description.Printf("success");182else {183const char *err_string = GetCString();184description.Printf("error: %s",185(err_string != nullptr ? err_string : ""));186}187} else188description.Printf("error: <NULL>");189190return true;191}192193194