1 //===-- CommandReturnObject.cpp ---------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Interpreter/CommandReturnObject.h"
11 #include "lldb/Utility/Status.h"
12 #include "lldb/Utility/StreamString.h"
15 using namespace lldb_private;
17 static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s,
18 bool add_newline_if_empty) {
19 bool add_newline = false;
21 add_newline = add_newline_if_empty;
23 // We already checked for empty above, now make sure there is a newline in
24 // the error, and if there isn't one, add one.
25 strm.Write(s.c_str(), s.size());
27 const char last_char = *s.rbegin();
28 add_newline = last_char != '\n' && last_char != '\r';
34 CommandReturnObject::CommandReturnObject()
35 : m_out_stream(), m_err_stream(), m_status(eReturnStatusStarted),
36 m_did_change_process_state(false), m_interactive(true),
37 m_abnormal_stop_was_expected(false) {}
39 CommandReturnObject::~CommandReturnObject() {}
41 void CommandReturnObject::AppendErrorWithFormat(const char *format, ...) {
45 va_start(args, format);
47 sstrm.PrintfVarArg(format, args);
50 const std::string &s = sstrm.GetString();
52 Stream &error_strm = GetErrorStream();
53 error_strm.PutCString("error: ");
54 DumpStringToStreamWithNewline(error_strm, s, false);
58 void CommandReturnObject::AppendMessageWithFormat(const char *format, ...) {
62 va_start(args, format);
64 sstrm.PrintfVarArg(format, args);
67 GetOutputStream() << sstrm.GetString();
70 void CommandReturnObject::AppendWarningWithFormat(const char *format, ...) {
74 va_start(args, format);
76 sstrm.PrintfVarArg(format, args);
79 GetErrorStream() << "warning: " << sstrm.GetString();
82 void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
83 if (in_string.empty())
85 GetOutputStream() << in_string << "\n";
88 void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
89 if (in_string.empty())
91 GetErrorStream() << "warning: " << in_string << "\n";
94 // Similar to AppendWarning, but do not prepend 'warning: ' to message, and
95 // don't append "\n" to the end of it.
97 void CommandReturnObject::AppendRawWarning(llvm::StringRef in_string) {
98 if (in_string.empty())
100 GetErrorStream() << in_string;
103 void CommandReturnObject::AppendError(llvm::StringRef in_string) {
104 if (in_string.empty())
106 GetErrorStream() << "error: " << in_string << "\n";
109 void CommandReturnObject::SetError(const Status &error,
110 const char *fallback_error_cstr) {
111 const char *error_cstr = error.AsCString();
112 if (error_cstr == nullptr)
113 error_cstr = fallback_error_cstr;
114 SetError(error_cstr);
117 void CommandReturnObject::SetError(llvm::StringRef error_str) {
118 if (error_str.empty())
121 AppendError(error_str);
122 SetStatus(eReturnStatusFailed);
125 // Similar to AppendError, but do not prepend 'Status: ' to message, and don't
126 // append "\n" to the end of it.
128 void CommandReturnObject::AppendRawError(llvm::StringRef in_string) {
129 if (in_string.empty())
131 GetErrorStream() << in_string;
134 void CommandReturnObject::SetStatus(ReturnStatus status) { m_status = status; }
136 ReturnStatus CommandReturnObject::GetStatus() { return m_status; }
138 bool CommandReturnObject::Succeeded() {
139 return m_status <= eReturnStatusSuccessContinuingResult;
142 bool CommandReturnObject::HasResult() {
143 return (m_status == eReturnStatusSuccessFinishResult ||
144 m_status == eReturnStatusSuccessContinuingResult);
147 void CommandReturnObject::Clear() {
148 lldb::StreamSP stream_sp;
149 stream_sp = m_out_stream.GetStreamAtIndex(eStreamStringIndex);
151 static_cast<StreamString *>(stream_sp.get())->Clear();
152 stream_sp = m_err_stream.GetStreamAtIndex(eStreamStringIndex);
154 static_cast<StreamString *>(stream_sp.get())->Clear();
155 m_status = eReturnStatusStarted;
156 m_did_change_process_state = false;
157 m_interactive = true;
160 bool CommandReturnObject::GetDidChangeProcessState() {
161 return m_did_change_process_state;
164 void CommandReturnObject::SetDidChangeProcessState(bool b) {
165 m_did_change_process_state = b;
168 bool CommandReturnObject::GetInteractive() const { return m_interactive; }
170 void CommandReturnObject::SetInteractive(bool b) { m_interactive = b; }