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