1 //===-- CommandReturnObject.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Interpreter/CommandReturnObject.h"
12 #include "lldb/Utility/Status.h"
13 #include "lldb/Utility/StreamString.h"
16 using namespace lldb_private;
18 static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s,
19 bool add_newline_if_empty) {
20 bool add_newline = false;
22 add_newline = add_newline_if_empty;
24 // We already checked for empty above, now make sure there is a newline in
25 // the error, and if there isn't one, add one.
26 strm.Write(s.c_str(), s.size());
28 const char last_char = *s.rbegin();
29 add_newline = last_char != '\n' && last_char != '\r';
35 CommandReturnObject::CommandReturnObject()
36 : m_out_stream(), m_err_stream(), m_status(eReturnStatusStarted),
37 m_did_change_process_state(false), m_interactive(true),
38 m_abnormal_stop_was_expected(false) {}
40 CommandReturnObject::~CommandReturnObject() {}
42 void CommandReturnObject::AppendErrorWithFormat(const char *format, ...) {
46 va_start(args, format);
48 sstrm.PrintfVarArg(format, args);
51 const std::string &s = sstrm.GetString();
53 Stream &error_strm = GetErrorStream();
54 error_strm.PutCString("error: ");
55 DumpStringToStreamWithNewline(error_strm, s, false);
59 void CommandReturnObject::AppendMessageWithFormat(const char *format, ...) {
63 va_start(args, format);
65 sstrm.PrintfVarArg(format, args);
68 GetOutputStream() << sstrm.GetString();
71 void CommandReturnObject::AppendWarningWithFormat(const char *format, ...) {
75 va_start(args, format);
77 sstrm.PrintfVarArg(format, args);
80 GetErrorStream() << "warning: " << sstrm.GetString();
83 void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
84 if (in_string.empty())
86 GetOutputStream() << in_string << "\n";
89 void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
90 if (in_string.empty())
92 GetErrorStream() << "warning: " << in_string << "\n";
95 // Similar to AppendWarning, but do not prepend 'warning: ' to message, and
96 // don't append "\n" to the end of it.
98 void CommandReturnObject::AppendRawWarning(llvm::StringRef in_string) {
99 if (in_string.empty())
101 GetErrorStream() << in_string;
104 void CommandReturnObject::AppendError(llvm::StringRef in_string) {
105 if (in_string.empty())
107 GetErrorStream() << "error: " << in_string << "\n";
110 void CommandReturnObject::SetError(const Status &error,
111 const char *fallback_error_cstr) {
112 const char *error_cstr = error.AsCString();
113 if (error_cstr == nullptr)
114 error_cstr = fallback_error_cstr;
115 SetError(error_cstr);
118 void CommandReturnObject::SetError(llvm::StringRef error_str) {
119 if (error_str.empty())
122 AppendError(error_str);
123 SetStatus(eReturnStatusFailed);
126 // Similar to AppendError, but do not prepend 'Status: ' to message, and don't
127 // append "\n" to the end of it.
129 void CommandReturnObject::AppendRawError(llvm::StringRef in_string) {
130 if (in_string.empty())
132 GetErrorStream() << in_string;
135 void CommandReturnObject::SetStatus(ReturnStatus status) { m_status = status; }
137 ReturnStatus CommandReturnObject::GetStatus() { return m_status; }
139 bool CommandReturnObject::Succeeded() {
140 return m_status <= eReturnStatusSuccessContinuingResult;
143 bool CommandReturnObject::HasResult() {
144 return (m_status == eReturnStatusSuccessFinishResult ||
145 m_status == eReturnStatusSuccessContinuingResult);
148 void CommandReturnObject::Clear() {
149 lldb::StreamSP stream_sp;
150 stream_sp = m_out_stream.GetStreamAtIndex(eStreamStringIndex);
152 static_cast<StreamString *>(stream_sp.get())->Clear();
153 stream_sp = m_err_stream.GetStreamAtIndex(eStreamStringIndex);
155 static_cast<StreamString *>(stream_sp.get())->Clear();
156 m_status = eReturnStatusStarted;
157 m_did_change_process_state = false;
158 m_interactive = true;
161 bool CommandReturnObject::GetDidChangeProcessState() {
162 return m_did_change_process_state;
165 void CommandReturnObject::SetDidChangeProcessState(bool b) {
166 m_did_change_process_state = b;
169 bool CommandReturnObject::GetInteractive() const { return m_interactive; }
171 void CommandReturnObject::SetInteractive(bool b) { m_interactive = b; }