xref: /llvm-project/lldb/source/Interpreter/CommandReturnObject.cpp (revision 54d03a4985bc9a0a84c4dff835ec6ed0f607582f)
1 //===-- CommandReturnObject.cpp -------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Interpreter/CommandReturnObject.h"
10 
11 #include "lldb/Utility/Status.h"
12 #include "lldb/Utility/StreamString.h"
13 
14 using namespace lldb;
15 using namespace lldb_private;
16 
17 static llvm::raw_ostream &error(Stream &strm) {
18   return llvm::WithColor(strm.AsRawOstream(), llvm::HighlightColor::Error,
19                          llvm::ColorMode::Enable)
20          << "error: ";
21 }
22 
23 static llvm::raw_ostream &warning(Stream &strm) {
24   return llvm::WithColor(strm.AsRawOstream(), llvm::HighlightColor::Warning,
25                          llvm::ColorMode::Enable)
26          << "warning: ";
27 }
28 
29 static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s) {
30   bool add_newline = false;
31   if (!s.empty()) {
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());
35 
36     const char last_char = *s.rbegin();
37     add_newline = last_char != '\n' && last_char != '\r';
38   }
39   if (add_newline)
40     strm.EOL();
41 }
42 
43 CommandReturnObject::CommandReturnObject(bool colors)
44     : m_out_stream(colors), m_err_stream(colors),
45       m_status(eReturnStatusStarted), m_did_change_process_state(false),
46       m_interactive(true) {}
47 
48 void CommandReturnObject::AppendErrorWithFormat(const char *format, ...) {
49   if (!format)
50     return;
51   va_list args;
52   va_start(args, format);
53   StreamString sstrm;
54   sstrm.PrintfVarArg(format, args);
55   va_end(args);
56 
57   const std::string &s = std::string(sstrm.GetString());
58   if (!s.empty()) {
59     error(GetErrorStream());
60     DumpStringToStreamWithNewline(GetErrorStream(), s);
61   }
62 }
63 
64 void CommandReturnObject::AppendMessageWithFormat(const char *format, ...) {
65   if (!format)
66     return;
67   va_list args;
68   va_start(args, format);
69   StreamString sstrm;
70   sstrm.PrintfVarArg(format, args);
71   va_end(args);
72 
73   GetOutputStream() << sstrm.GetString();
74 }
75 
76 void CommandReturnObject::AppendWarningWithFormat(const char *format, ...) {
77   if (!format)
78     return;
79   va_list args;
80   va_start(args, format);
81   StreamString sstrm;
82   sstrm.PrintfVarArg(format, args);
83   va_end(args);
84 
85   warning(GetErrorStream()) << sstrm.GetString();
86 }
87 
88 void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
89   if (in_string.empty())
90     return;
91   GetOutputStream() << in_string.rtrim() << '\n';
92 }
93 
94 void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
95   if (in_string.empty())
96     return;
97   warning(GetErrorStream()) << in_string.rtrim() << '\n';
98 }
99 
100 void CommandReturnObject::AppendError(llvm::StringRef in_string) {
101   if (in_string.empty())
102     return;
103   error(GetErrorStream()) << in_string.rtrim() << '\n';
104 }
105 
106 void CommandReturnObject::SetError(const Status &error,
107                                    const char *fallback_error_cstr) {
108   const char *error_cstr = error.AsCString();
109   if (error_cstr == nullptr)
110     error_cstr = fallback_error_cstr;
111   SetError(error_cstr);
112 }
113 
114 void CommandReturnObject::SetError(llvm::StringRef error_str) {
115   if (error_str.empty())
116     return;
117 
118   AppendError(error_str);
119   SetStatus(eReturnStatusFailed);
120 }
121 
122 // Similar to AppendError, but do not prepend 'Status: ' to message, and don't
123 // append "\n" to the end of it.
124 
125 void CommandReturnObject::AppendRawError(llvm::StringRef in_string) {
126   if (in_string.empty())
127     return;
128   GetErrorStream() << in_string;
129 }
130 
131 void CommandReturnObject::SetStatus(ReturnStatus status) { m_status = status; }
132 
133 ReturnStatus CommandReturnObject::GetStatus() { return m_status; }
134 
135 bool CommandReturnObject::Succeeded() {
136   return m_status <= eReturnStatusSuccessContinuingResult;
137 }
138 
139 bool CommandReturnObject::HasResult() {
140   return (m_status == eReturnStatusSuccessFinishResult ||
141           m_status == eReturnStatusSuccessContinuingResult);
142 }
143 
144 void CommandReturnObject::Clear() {
145   lldb::StreamSP stream_sp;
146   stream_sp = m_out_stream.GetStreamAtIndex(eStreamStringIndex);
147   if (stream_sp)
148     static_cast<StreamString *>(stream_sp.get())->Clear();
149   stream_sp = m_err_stream.GetStreamAtIndex(eStreamStringIndex);
150   if (stream_sp)
151     static_cast<StreamString *>(stream_sp.get())->Clear();
152   m_status = eReturnStatusStarted;
153   m_did_change_process_state = false;
154   m_interactive = true;
155 }
156 
157 bool CommandReturnObject::GetDidChangeProcessState() {
158   return m_did_change_process_state;
159 }
160 
161 void CommandReturnObject::SetDidChangeProcessState(bool b) {
162   m_did_change_process_state = b;
163 }
164 
165 bool CommandReturnObject::GetInteractive() const { return m_interactive; }
166 
167 void CommandReturnObject::SetInteractive(bool b) { m_interactive = b; }
168