aboutsummaryrefslogtreecommitdiffstats
path: root/source/API/SBCommandReturnObject.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/API/SBCommandReturnObject.cpp')
-rw-r--r--source/API/SBCommandReturnObject.cpp304
1 files changed, 174 insertions, 130 deletions
diff --git a/source/API/SBCommandReturnObject.cpp b/source/API/SBCommandReturnObject.cpp
index 94e89916f7f6..eec1383df875 100644
--- a/source/API/SBCommandReturnObject.cpp
+++ b/source/API/SBCommandReturnObject.cpp
@@ -10,6 +10,7 @@
#include "SBReproducerPrivate.h"
#include "Utils.h"
#include "lldb/API/SBError.h"
+#include "lldb/API/SBFile.h"
#include "lldb/API/SBStream.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Utility/ConstString.h"
@@ -18,11 +19,43 @@
using namespace lldb;
using namespace lldb_private;
+class lldb_private::SBCommandReturnObjectImpl {
+public:
+ SBCommandReturnObjectImpl()
+ : m_ptr(new CommandReturnObject()), m_owned(true) {}
+ SBCommandReturnObjectImpl(CommandReturnObject &ref)
+ : m_ptr(&ref), m_owned(false) {}
+ SBCommandReturnObjectImpl(const SBCommandReturnObjectImpl &rhs)
+ : m_ptr(new CommandReturnObject(*rhs.m_ptr)), m_owned(rhs.m_owned) {}
+ SBCommandReturnObjectImpl &operator=(const SBCommandReturnObjectImpl &rhs) {
+ SBCommandReturnObjectImpl copy(rhs);
+ std::swap(*this, copy);
+ return *this;
+ }
+ // rvalue ctor+assignment are not used by SBCommandReturnObject.
+ ~SBCommandReturnObjectImpl() {
+ if (m_owned)
+ delete m_ptr;
+ }
+
+ CommandReturnObject &operator*() const { return *m_ptr; }
+
+private:
+ CommandReturnObject *m_ptr;
+ bool m_owned;
+};
+
SBCommandReturnObject::SBCommandReturnObject()
- : m_opaque_up(new CommandReturnObject()) {
+ : m_opaque_up(new SBCommandReturnObjectImpl()) {
LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBCommandReturnObject);
}
+SBCommandReturnObject::SBCommandReturnObject(CommandReturnObject &ref)
+ : m_opaque_up(new SBCommandReturnObjectImpl(ref)) {
+ LLDB_RECORD_CONSTRUCTOR(SBCommandReturnObject,
+ (lldb_private::CommandReturnObject &), ref);
+}
+
SBCommandReturnObject::SBCommandReturnObject(const SBCommandReturnObject &rhs)
: m_opaque_up() {
LLDB_RECORD_CONSTRUCTOR(SBCommandReturnObject,
@@ -31,25 +64,10 @@ SBCommandReturnObject::SBCommandReturnObject(const SBCommandReturnObject &rhs)
m_opaque_up = clone(rhs.m_opaque_up);
}
-SBCommandReturnObject::SBCommandReturnObject(CommandReturnObject *ptr)
- : m_opaque_up(ptr) {
- LLDB_RECORD_CONSTRUCTOR(SBCommandReturnObject,
- (lldb_private::CommandReturnObject *), ptr);
-}
-
-SBCommandReturnObject::~SBCommandReturnObject() = default;
-
-CommandReturnObject *SBCommandReturnObject::Release() {
- LLDB_RECORD_METHOD_NO_ARGS(lldb_private::CommandReturnObject *,
- SBCommandReturnObject, Release);
-
- return LLDB_RECORD_RESULT(m_opaque_up.release());
-}
-
-const SBCommandReturnObject &SBCommandReturnObject::
+SBCommandReturnObject &SBCommandReturnObject::
operator=(const SBCommandReturnObject &rhs) {
LLDB_RECORD_METHOD(
- const lldb::SBCommandReturnObject &,
+ lldb::SBCommandReturnObject &,
SBCommandReturnObject, operator=,(const lldb::SBCommandReturnObject &),
rhs);
@@ -58,6 +76,8 @@ operator=(const SBCommandReturnObject &rhs) {
return LLDB_RECORD_RESULT(*this);
}
+SBCommandReturnObject::~SBCommandReturnObject() = default;
+
bool SBCommandReturnObject::IsValid() const {
LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandReturnObject, IsValid);
return this->operator bool();
@@ -65,49 +85,38 @@ bool SBCommandReturnObject::IsValid() const {
SBCommandReturnObject::operator bool() const {
LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandReturnObject, operator bool);
- return m_opaque_up != nullptr;
+ // This method is not useful but it needs to stay to keep SB API stable.
+ return true;
}
const char *SBCommandReturnObject::GetOutput() {
LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommandReturnObject, GetOutput);
- if (m_opaque_up) {
- llvm::StringRef output = m_opaque_up->GetOutputData();
- ConstString result(output.empty() ? llvm::StringRef("") : output);
-
- return result.AsCString();
- }
-
- return nullptr;
+ ConstString output(ref().GetOutputData());
+ return output.AsCString(/*value_if_empty*/ "");
}
const char *SBCommandReturnObject::GetError() {
LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommandReturnObject, GetError);
- if (m_opaque_up) {
- llvm::StringRef output = m_opaque_up->GetErrorData();
- ConstString result(output.empty() ? llvm::StringRef("") : output);
- return result.AsCString();
- }
-
- return nullptr;
+ ConstString output(ref().GetErrorData());
+ return output.AsCString(/*value_if_empty*/ "");
}
size_t SBCommandReturnObject::GetOutputSize() {
LLDB_RECORD_METHOD_NO_ARGS(size_t, SBCommandReturnObject, GetOutputSize);
- return (m_opaque_up ? m_opaque_up->GetOutputData().size() : 0);
+ return ref().GetOutputData().size();
}
size_t SBCommandReturnObject::GetErrorSize() {
LLDB_RECORD_METHOD_NO_ARGS(size_t, SBCommandReturnObject, GetErrorSize);
- return (m_opaque_up ? m_opaque_up->GetErrorData().size() : 0);
+ return ref().GetErrorData().size();
}
size_t SBCommandReturnObject::PutOutput(FILE *fh) {
- LLDB_RECORD_METHOD(size_t, SBCommandReturnObject, PutOutput, (FILE *), fh);
-
+ LLDB_RECORD_DUMMY(size_t, SBCommandReturnObject, PutOutput, (FILE *), fh);
if (fh) {
size_t num_bytes = GetOutputSize();
if (num_bytes)
@@ -116,9 +125,23 @@ size_t SBCommandReturnObject::PutOutput(FILE *fh) {
return 0;
}
-size_t SBCommandReturnObject::PutError(FILE *fh) {
- LLDB_RECORD_METHOD(size_t, SBCommandReturnObject, PutError, (FILE *), fh);
+size_t SBCommandReturnObject::PutOutput(FileSP file_sp) {
+ LLDB_RECORD_METHOD(size_t, SBCommandReturnObject, PutOutput, (FileSP),
+ file_sp);
+ if (!file_sp)
+ return 0;
+ return file_sp->Printf("%s", GetOutput());
+}
+
+size_t SBCommandReturnObject::PutOutput(SBFile file) {
+ LLDB_RECORD_METHOD(size_t, SBCommandReturnObject, PutOutput, (SBFile), file);
+ if (!file.m_opaque_sp)
+ return 0;
+ return file.m_opaque_sp->Printf("%s", GetOutput());
+}
+size_t SBCommandReturnObject::PutError(FILE *fh) {
+ LLDB_RECORD_DUMMY(size_t, SBCommandReturnObject, PutError, (FILE *), fh);
if (fh) {
size_t num_bytes = GetErrorSize();
if (num_bytes)
@@ -127,77 +150,81 @@ size_t SBCommandReturnObject::PutError(FILE *fh) {
return 0;
}
+size_t SBCommandReturnObject::PutError(FileSP file_sp) {
+ LLDB_RECORD_METHOD(size_t, SBCommandReturnObject, PutError, (FileSP),
+ file_sp);
+ if (!file_sp)
+ return 0;
+ return file_sp->Printf("%s", GetError());
+}
+
+size_t SBCommandReturnObject::PutError(SBFile file) {
+ LLDB_RECORD_METHOD(size_t, SBCommandReturnObject, PutError, (SBFile), file);
+ if (!file.m_opaque_sp)
+ return 0;
+ return file.m_opaque_sp->Printf("%s", GetError());
+}
+
void SBCommandReturnObject::Clear() {
LLDB_RECORD_METHOD_NO_ARGS(void, SBCommandReturnObject, Clear);
- if (m_opaque_up)
- m_opaque_up->Clear();
+ ref().Clear();
}
lldb::ReturnStatus SBCommandReturnObject::GetStatus() {
LLDB_RECORD_METHOD_NO_ARGS(lldb::ReturnStatus, SBCommandReturnObject,
GetStatus);
- return (m_opaque_up ? m_opaque_up->GetStatus() : lldb::eReturnStatusInvalid);
+ return ref().GetStatus();
}
void SBCommandReturnObject::SetStatus(lldb::ReturnStatus status) {
LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetStatus,
(lldb::ReturnStatus), status);
- if (m_opaque_up)
- m_opaque_up->SetStatus(status);
+ ref().SetStatus(status);
}
bool SBCommandReturnObject::Succeeded() {
LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandReturnObject, Succeeded);
- return (m_opaque_up ? m_opaque_up->Succeeded() : false);
+ return ref().Succeeded();
}
bool SBCommandReturnObject::HasResult() {
LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandReturnObject, HasResult);
- return (m_opaque_up ? m_opaque_up->HasResult() : false);
+ return ref().HasResult();
}
void SBCommandReturnObject::AppendMessage(const char *message) {
LLDB_RECORD_METHOD(void, SBCommandReturnObject, AppendMessage, (const char *),
message);
- if (m_opaque_up)
- m_opaque_up->AppendMessage(message);
+ ref().AppendMessage(message);
}
void SBCommandReturnObject::AppendWarning(const char *message) {
LLDB_RECORD_METHOD(void, SBCommandReturnObject, AppendWarning, (const char *),
message);
- if (m_opaque_up)
- m_opaque_up->AppendWarning(message);
+ ref().AppendWarning(message);
}
CommandReturnObject *SBCommandReturnObject::operator->() const {
- return m_opaque_up.get();
+ return &**m_opaque_up;
}
CommandReturnObject *SBCommandReturnObject::get() const {
- return m_opaque_up.get();
+ return &**m_opaque_up;
}
CommandReturnObject &SBCommandReturnObject::operator*() const {
- assert(m_opaque_up.get());
- return *(m_opaque_up.get());
+ return **m_opaque_up;
}
CommandReturnObject &SBCommandReturnObject::ref() const {
- assert(m_opaque_up.get());
- return *(m_opaque_up.get());
-}
-
-void SBCommandReturnObject::SetLLDBObjectPtr(CommandReturnObject *ptr) {
- if (m_opaque_up)
- m_opaque_up.reset(ptr);
+ return **m_opaque_up;
}
bool SBCommandReturnObject::GetDescription(SBStream &description) {
@@ -206,84 +233,99 @@ bool SBCommandReturnObject::GetDescription(SBStream &description) {
Stream &strm = description.ref();
- if (m_opaque_up) {
- description.Printf("Error: ");
- lldb::ReturnStatus status = m_opaque_up->GetStatus();
- if (status == lldb::eReturnStatusStarted)
- strm.PutCString("Started");
- else if (status == lldb::eReturnStatusInvalid)
- strm.PutCString("Invalid");
- else if (m_opaque_up->Succeeded())
- strm.PutCString("Success");
- else
- strm.PutCString("Fail");
-
- if (GetOutputSize() > 0)
- strm.Printf("\nOutput Message:\n%s", GetOutput());
-
- if (GetErrorSize() > 0)
- strm.Printf("\nError Message:\n%s", GetError());
- } else
- strm.PutCString("No value");
+ description.Printf("Error: ");
+ lldb::ReturnStatus status = ref().GetStatus();
+ if (status == lldb::eReturnStatusStarted)
+ strm.PutCString("Started");
+ else if (status == lldb::eReturnStatusInvalid)
+ strm.PutCString("Invalid");
+ else if (ref().Succeeded())
+ strm.PutCString("Success");
+ else
+ strm.PutCString("Fail");
+
+ if (GetOutputSize() > 0)
+ strm.Printf("\nOutput Message:\n%s", GetOutput());
+
+ if (GetErrorSize() > 0)
+ strm.Printf("\nError Message:\n%s", GetError());
return true;
}
void SBCommandReturnObject::SetImmediateOutputFile(FILE *fh) {
- LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetImmediateOutputFile,
- (FILE *), fh);
+ LLDB_RECORD_DUMMY(void, SBCommandReturnObject, SetImmediateOutputFile,
+ (FILE *), fh);
SetImmediateOutputFile(fh, false);
}
void SBCommandReturnObject::SetImmediateErrorFile(FILE *fh) {
- LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetImmediateErrorFile,
- (FILE *), fh);
+ LLDB_RECORD_DUMMY(void, SBCommandReturnObject, SetImmediateErrorFile,
+ (FILE *), fh);
SetImmediateErrorFile(fh, false);
}
void SBCommandReturnObject::SetImmediateOutputFile(FILE *fh,
bool transfer_ownership) {
- LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetImmediateOutputFile,
- (FILE *, bool), fh, transfer_ownership);
-
- if (m_opaque_up)
- m_opaque_up->SetImmediateOutputFile(fh, transfer_ownership);
+ LLDB_RECORD_DUMMY(void, SBCommandReturnObject, SetImmediateOutputFile,
+ (FILE *, bool), fh, transfer_ownership);
+ FileSP file = std::make_shared<NativeFile>(fh, transfer_ownership);
+ ref().SetImmediateOutputFile(file);
}
void SBCommandReturnObject::SetImmediateErrorFile(FILE *fh,
bool transfer_ownership) {
+ LLDB_RECORD_DUMMY(void, SBCommandReturnObject, SetImmediateErrorFile,
+ (FILE *, bool), fh, transfer_ownership);
+ FileSP file = std::make_shared<NativeFile>(fh, transfer_ownership);
+ ref().SetImmediateErrorFile(file);
+}
+
+void SBCommandReturnObject::SetImmediateOutputFile(SBFile file) {
+ LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetImmediateOutputFile,
+ (SBFile), file);
+ ref().SetImmediateOutputFile(file.m_opaque_sp);
+}
+
+void SBCommandReturnObject::SetImmediateErrorFile(SBFile file) {
LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetImmediateErrorFile,
- (FILE *, bool), fh, transfer_ownership);
+ (SBFile), file);
+ ref().SetImmediateErrorFile(file.m_opaque_sp);
+}
- if (m_opaque_up)
- m_opaque_up->SetImmediateErrorFile(fh, transfer_ownership);
+void SBCommandReturnObject::SetImmediateOutputFile(FileSP file_sp) {
+ LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetImmediateOutputFile,
+ (FileSP), file_sp);
+ SetImmediateOutputFile(SBFile(file_sp));
+}
+
+void SBCommandReturnObject::SetImmediateErrorFile(FileSP file_sp) {
+ LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetImmediateErrorFile,
+ (FileSP), file_sp);
+ SetImmediateErrorFile(SBFile(file_sp));
}
void SBCommandReturnObject::PutCString(const char *string, int len) {
LLDB_RECORD_METHOD(void, SBCommandReturnObject, PutCString,
(const char *, int), string, len);
- if (m_opaque_up) {
- if (len == 0 || string == nullptr || *string == 0) {
- return;
- } else if (len > 0) {
- std::string buffer(string, len);
- m_opaque_up->AppendMessage(buffer.c_str());
- } else
- m_opaque_up->AppendMessage(string);
- }
+ if (len == 0 || string == nullptr || *string == 0) {
+ return;
+ } else if (len > 0) {
+ std::string buffer(string, len);
+ ref().AppendMessage(buffer.c_str());
+ } else
+ ref().AppendMessage(string);
}
const char *SBCommandReturnObject::GetOutput(bool only_if_no_immediate) {
LLDB_RECORD_METHOD(const char *, SBCommandReturnObject, GetOutput, (bool),
only_if_no_immediate);
- if (!m_opaque_up)
- return nullptr;
if (!only_if_no_immediate ||
- m_opaque_up->GetImmediateOutputStream().get() == nullptr)
+ ref().GetImmediateOutputStream().get() == nullptr)
return GetOutput();
return nullptr;
}
@@ -292,23 +334,17 @@ const char *SBCommandReturnObject::GetError(bool only_if_no_immediate) {
LLDB_RECORD_METHOD(const char *, SBCommandReturnObject, GetError, (bool),
only_if_no_immediate);
- if (!m_opaque_up)
- return nullptr;
- if (!only_if_no_immediate ||
- m_opaque_up->GetImmediateErrorStream().get() == nullptr)
+ if (!only_if_no_immediate || ref().GetImmediateErrorStream().get() == nullptr)
return GetError();
return nullptr;
}
size_t SBCommandReturnObject::Printf(const char *format, ...) {
- if (m_opaque_up) {
- va_list args;
- va_start(args, format);
- size_t result = m_opaque_up->GetOutputStream().PrintfVarArg(format, args);
- va_end(args);
- return result;
- }
- return 0;
+ va_list args;
+ va_start(args, format);
+ size_t result = ref().GetOutputStream().PrintfVarArg(format, args);
+ va_end(args);
+ return result;
}
void SBCommandReturnObject::SetError(lldb::SBError &error,
@@ -317,20 +353,18 @@ void SBCommandReturnObject::SetError(lldb::SBError &error,
(lldb::SBError &, const char *), error,
fallback_error_cstr);
- if (m_opaque_up) {
- if (error.IsValid())
- m_opaque_up->SetError(error.ref(), fallback_error_cstr);
- else if (fallback_error_cstr)
- m_opaque_up->SetError(Status(), fallback_error_cstr);
- }
+ if (error.IsValid())
+ ref().SetError(error.ref(), fallback_error_cstr);
+ else if (fallback_error_cstr)
+ ref().SetError(Status(), fallback_error_cstr);
}
void SBCommandReturnObject::SetError(const char *error_cstr) {
LLDB_RECORD_METHOD(void, SBCommandReturnObject, SetError, (const char *),
error_cstr);
- if (m_opaque_up && error_cstr)
- m_opaque_up->SetError(error_cstr);
+ if (error_cstr)
+ ref().SetError(error_cstr);
}
namespace lldb_private {
@@ -340,13 +374,11 @@ template <>
void RegisterMethods<SBCommandReturnObject>(Registry &R) {
LLDB_REGISTER_CONSTRUCTOR(SBCommandReturnObject, ());
LLDB_REGISTER_CONSTRUCTOR(SBCommandReturnObject,
- (const lldb::SBCommandReturnObject &));
+ (lldb_private::CommandReturnObject &));
LLDB_REGISTER_CONSTRUCTOR(SBCommandReturnObject,
- (lldb_private::CommandReturnObject *));
- LLDB_REGISTER_METHOD(lldb_private::CommandReturnObject *,
- SBCommandReturnObject, Release, ());
+ (const lldb::SBCommandReturnObject &));
LLDB_REGISTER_METHOD(
- const lldb::SBCommandReturnObject &,
+ lldb::SBCommandReturnObject &,
SBCommandReturnObject, operator=,(const lldb::SBCommandReturnObject &));
LLDB_REGISTER_METHOD_CONST(bool, SBCommandReturnObject, IsValid, ());
LLDB_REGISTER_METHOD_CONST(bool, SBCommandReturnObject, operator bool, ());
@@ -356,6 +388,10 @@ void RegisterMethods<SBCommandReturnObject>(Registry &R) {
LLDB_REGISTER_METHOD(size_t, SBCommandReturnObject, GetErrorSize, ());
LLDB_REGISTER_METHOD(size_t, SBCommandReturnObject, PutOutput, (FILE *));
LLDB_REGISTER_METHOD(size_t, SBCommandReturnObject, PutError, (FILE *));
+ LLDB_REGISTER_METHOD(size_t, SBCommandReturnObject, PutOutput, (SBFile));
+ LLDB_REGISTER_METHOD(size_t, SBCommandReturnObject, PutError, (SBFile));
+ LLDB_REGISTER_METHOD(size_t, SBCommandReturnObject, PutOutput, (FileSP));
+ LLDB_REGISTER_METHOD(size_t, SBCommandReturnObject, PutError, (FileSP));
LLDB_REGISTER_METHOD(void, SBCommandReturnObject, Clear, ());
LLDB_REGISTER_METHOD(lldb::ReturnStatus, SBCommandReturnObject, GetStatus,
());
@@ -374,6 +410,14 @@ void RegisterMethods<SBCommandReturnObject>(Registry &R) {
LLDB_REGISTER_METHOD(void, SBCommandReturnObject, SetImmediateErrorFile,
(FILE *));
LLDB_REGISTER_METHOD(void, SBCommandReturnObject, SetImmediateOutputFile,
+ (SBFile));
+ LLDB_REGISTER_METHOD(void, SBCommandReturnObject, SetImmediateErrorFile,
+ (SBFile));
+ LLDB_REGISTER_METHOD(void, SBCommandReturnObject, SetImmediateOutputFile,
+ (FileSP));
+ LLDB_REGISTER_METHOD(void, SBCommandReturnObject, SetImmediateErrorFile,
+ (FileSP));
+ LLDB_REGISTER_METHOD(void, SBCommandReturnObject, SetImmediateOutputFile,
(FILE *, bool));
LLDB_REGISTER_METHOD(void, SBCommandReturnObject, SetImmediateErrorFile,
(FILE *, bool));