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) {}
38 CommandReturnObject::~CommandReturnObject() {}
40 void CommandReturnObject::AppendErrorWithFormat(const char *format, ...) {
44 va_start(args, format);
46 sstrm.PrintfVarArg(format, args);
49 const std::string &s = sstrm.GetString();
51 Stream &error_strm = GetErrorStream();
52 error_strm.PutCString("error: ");
53 DumpStringToStreamWithNewline(error_strm, s, false);
57 void CommandReturnObject::AppendMessageWithFormat(const char *format, ...) {
61 va_start(args, format);
63 sstrm.PrintfVarArg(format, args);
66 GetOutputStream() << sstrm.GetString();
69 void CommandReturnObject::AppendWarningWithFormat(const char *format, ...) {
73 va_start(args, format);
75 sstrm.PrintfVarArg(format, args);
78 GetErrorStream() << "warning: " << sstrm.GetString();
81 void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
82 if (in_string.empty())
84 GetOutputStream() << in_string << "\n";
87 void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
88 if (in_string.empty())
90 GetErrorStream() << "warning: " << in_string << "\n";
93 // Similar to AppendWarning, but do not prepend 'warning: ' to message, and
94 // don't append "\n" to the end of it.
96 void CommandReturnObject::AppendRawWarning(llvm::StringRef in_string) {
97 if (in_string.empty())
99 GetErrorStream() << in_string;
102 void CommandReturnObject::AppendError(llvm::StringRef in_string) {
103 if (in_string.empty())
105 GetErrorStream() << "error: " << in_string << "\n";
108 void CommandReturnObject::SetError(const Status &error,
109 const char *fallback_error_cstr) {
110 const char *error_cstr = error.AsCString();
111 if (error_cstr == nullptr)
112 error_cstr = fallback_error_cstr;
113 SetError(error_cstr);
116 void CommandReturnObject::SetError(llvm::StringRef error_str) {
117 if (error_str.empty())
120 AppendError(error_str);
121 SetStatus(eReturnStatusFailed);
124 // Similar to AppendError, but do not prepend 'Status: ' to message, and don't
125 // append "\n" to the end of it.
127 void CommandReturnObject::AppendRawError(llvm::StringRef in_string) {
128 if (in_string.empty())
130 GetErrorStream() << in_string;
133 void CommandReturnObject::SetStatus(ReturnStatus status) { m_status = status; }
135 ReturnStatus CommandReturnObject::GetStatus() { return m_status; }
137 bool CommandReturnObject::Succeeded() {
138 return m_status <= eReturnStatusSuccessContinuingResult;
141 bool CommandReturnObject::HasResult() {
142 return (m_status == eReturnStatusSuccessFinishResult ||
143 m_status == eReturnStatusSuccessContinuingResult);
146 void CommandReturnObject::Clear() {
147 lldb::StreamSP stream_sp;
148 stream_sp = m_out_stream.GetStreamAtIndex(eStreamStringIndex);
150 static_cast<StreamString *>(stream_sp.get())->Clear();
151 stream_sp = m_err_stream.GetStreamAtIndex(eStreamStringIndex);
153 static_cast<StreamString *>(stream_sp.get())->Clear();
154 m_status = eReturnStatusStarted;
155 m_did_change_process_state = false;
156 m_interactive = true;
159 bool CommandReturnObject::GetDidChangeProcessState() {
160 return m_did_change_process_state;
163 void CommandReturnObject::SetDidChangeProcessState(bool b) {
164 m_did_change_process_state = b;
167 bool CommandReturnObject::GetInteractive() const { return m_interactive; }
169 void CommandReturnObject::SetInteractive(bool b) { m_interactive = b; }