1 //===-- CommandReturnObject.cpp -------------------------------------------===//
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 llvm::raw_ostream
&error(Stream
&strm
) {
18 return llvm::WithColor(strm
.AsRawOstream(), llvm::HighlightColor::Error
,
19 llvm::ColorMode::Enable
)
23 static llvm::raw_ostream
&warning(Stream
&strm
) {
24 return llvm::WithColor(strm
.AsRawOstream(), llvm::HighlightColor::Warning
,
25 llvm::ColorMode::Enable
)
29 static void DumpStringToStreamWithNewline(Stream
&strm
, const std::string
&s
) {
30 bool add_newline
= false;
32 // We already checked for empty above, now make sure there is a newline in
33 // the error, and if there isn't one, add one.
34 strm
.Write(s
.c_str(), s
.size());
36 const char last_char
= *s
.rbegin();
37 add_newline
= last_char
!= '\n' && last_char
!= '\r';
43 CommandReturnObject::CommandReturnObject(bool colors
)
44 : m_out_stream(colors
), m_err_stream(colors
) {}
46 void CommandReturnObject::AppendErrorWithFormat(const char *format
, ...) {
47 SetStatus(eReturnStatusFailed
);
52 va_start(args
, format
);
54 sstrm
.PrintfVarArg(format
, args
);
57 const std::string
&s
= std::string(sstrm
.GetString());
59 error(GetErrorStream());
60 DumpStringToStreamWithNewline(GetErrorStream(), s
);
64 void CommandReturnObject::AppendMessageWithFormat(const char *format
, ...) {
68 va_start(args
, format
);
70 sstrm
.PrintfVarArg(format
, args
);
73 GetOutputStream() << sstrm
.GetString();
76 void CommandReturnObject::AppendWarningWithFormat(const char *format
, ...) {
80 va_start(args
, format
);
82 sstrm
.PrintfVarArg(format
, args
);
85 warning(GetErrorStream()) << sstrm
.GetString();
88 void CommandReturnObject::AppendMessage(llvm::StringRef in_string
) {
89 if (in_string
.empty())
91 GetOutputStream() << in_string
.rtrim() << '\n';
94 void CommandReturnObject::AppendWarning(llvm::StringRef in_string
) {
95 if (in_string
.empty())
97 warning(GetErrorStream()) << in_string
.rtrim() << '\n';
100 void CommandReturnObject::AppendError(llvm::StringRef in_string
) {
101 SetStatus(eReturnStatusFailed
);
102 if (in_string
.empty())
104 // Workaround to deal with already fully formatted compiler diagnostics.
105 llvm::StringRef
msg(in_string
.rtrim());
106 msg
.consume_front("error: ");
107 error(GetErrorStream()) << msg
<< '\n';
110 void CommandReturnObject::SetError(const Status
&error
,
111 const char *fallback_error_cstr
) {
113 AppendError(error
.AsCString(fallback_error_cstr
));
116 void CommandReturnObject::SetError(llvm::Error error
) {
118 AppendError(llvm::toString(std::move(error
)));
121 // Similar to AppendError, but do not prepend 'Status: ' to message, and don't
122 // append "\n" to the end of it.
124 void CommandReturnObject::AppendRawError(llvm::StringRef in_string
) {
125 SetStatus(eReturnStatusFailed
);
126 assert(!in_string
.empty() && "Expected a non-empty error message");
127 GetErrorStream() << in_string
;
130 void CommandReturnObject::SetStatus(ReturnStatus status
) { m_status
= status
; }
132 ReturnStatus
CommandReturnObject::GetStatus() const { return m_status
; }
134 bool CommandReturnObject::Succeeded() const {
135 return m_status
<= eReturnStatusSuccessContinuingResult
;
138 bool CommandReturnObject::HasResult() const {
139 return (m_status
== eReturnStatusSuccessFinishResult
||
140 m_status
== eReturnStatusSuccessContinuingResult
);
143 void CommandReturnObject::Clear() {
144 lldb::StreamSP stream_sp
;
145 stream_sp
= m_out_stream
.GetStreamAtIndex(eStreamStringIndex
);
147 static_cast<StreamString
*>(stream_sp
.get())->Clear();
148 stream_sp
= m_err_stream
.GetStreamAtIndex(eStreamStringIndex
);
150 static_cast<StreamString
*>(stream_sp
.get())->Clear();
151 m_status
= eReturnStatusStarted
;
152 m_did_change_process_state
= false;
153 m_suppress_immediate_output
= false;
154 m_interactive
= true;
157 bool CommandReturnObject::GetDidChangeProcessState() const {
158 return m_did_change_process_state
;
161 void CommandReturnObject::SetDidChangeProcessState(bool b
) {
162 m_did_change_process_state
= b
;
165 bool CommandReturnObject::GetInteractive() const { return m_interactive
; }
167 void CommandReturnObject::SetInteractive(bool b
) { m_interactive
= b
; }
169 bool CommandReturnObject::GetSuppressImmediateOutput() const {
170 return m_suppress_immediate_output
;
173 void CommandReturnObject::SetSuppressImmediateOutput(bool b
) {
174 m_suppress_immediate_output
= b
;