LLDB mainline
CommandReturnObject.cpp
Go to the documentation of this file.
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
10
12#include "lldb/Utility/Status.h"
14
15using namespace lldb;
16using namespace lldb_private;
17
18static llvm::raw_ostream &error(Stream &strm) {
19 return llvm::WithColor(strm.AsRawOstream(), llvm::HighlightColor::Error,
20 llvm::ColorMode::Enable)
21 << "error: ";
22}
23
24static llvm::raw_ostream &warning(Stream &strm) {
25 return llvm::WithColor(strm.AsRawOstream(), llvm::HighlightColor::Warning,
26 llvm::ColorMode::Enable)
27 << "warning: ";
28}
29
30static llvm::raw_ostream &note(Stream &strm) {
31 return llvm::WithColor(strm.AsRawOstream(), llvm::HighlightColor::Note,
32 llvm::ColorMode::Enable)
33 << "note: ";
34}
35
36static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s) {
37 bool add_newline = false;
38 if (!s.empty()) {
39 // We already checked for empty above, now make sure there is a newline in
40 // the error, and if there isn't one, add one.
41 strm.Write(s.c_str(), s.size());
42
43 const char last_char = *s.rbegin();
44 add_newline = last_char != '\n' && last_char != '\r';
45 }
46 if (add_newline)
47 strm.EOL();
48}
49
51 : m_out_stream(colors), m_err_stream(colors), m_colors(colors) {}
52
53void CommandReturnObject::AppendErrorWithFormat(const char *format, ...) {
55
56 if (!format)
57 return;
58 va_list args;
59 va_start(args, format);
60 StreamString sstrm;
61 sstrm.PrintfVarArg(format, args);
62 va_end(args);
63
64 const std::string &s = std::string(sstrm.GetString());
65 if (!s.empty()) {
68 }
69}
70
71void CommandReturnObject::AppendMessageWithFormat(const char *format, ...) {
72 if (!format)
73 return;
74 va_list args;
75 va_start(args, format);
76 StreamString sstrm;
77 sstrm.PrintfVarArg(format, args);
78 va_end(args);
79
80 GetOutputStream() << sstrm.GetString();
81}
82
83void CommandReturnObject::AppendNoteWithFormat(const char *format, ...) {
84 if (!format)
85 return;
86 va_list args;
87 va_start(args, format);
88 StreamString sstrm;
89 sstrm.PrintfVarArg(format, args);
90 va_end(args);
91
92 note(GetOutputStream()) << sstrm.GetString();
93}
94
95void CommandReturnObject::AppendWarningWithFormat(const char *format, ...) {
96 if (!format)
97 return;
98 va_list args;
99 va_start(args, format);
100 StreamString sstrm;
101 sstrm.PrintfVarArg(format, args);
102 va_end(args);
103
104 warning(GetErrorStream()) << sstrm.GetString();
105}
106
107void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
108 if (in_string.empty())
109 return;
110 GetOutputStream() << in_string.rtrim() << '\n';
111}
112
113void CommandReturnObject::AppendNote(llvm::StringRef in_string) {
114 if (in_string.empty())
115 return;
116 note(GetOutputStream()) << in_string.rtrim() << '\n';
117}
118
119void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
120 if (in_string.empty())
121 return;
122 warning(GetErrorStream()) << in_string.rtrim() << '\n';
123}
124
125void CommandReturnObject::AppendError(llvm::StringRef in_string) {
127 if (in_string.empty())
128 return;
129 // Workaround to deal with already fully formatted compiler diagnostics.
130 llvm::StringRef msg(in_string.rtrim());
131 msg.consume_front("error: ");
132 error(GetErrorStream()) << msg << '\n';
133}
134
136 SetError(error.takeError());
137}
138
140 // Retrieve any diagnostics.
141 error = llvm::handleErrors(std::move(error), [&](DiagnosticError &error) {
143 m_diagnostics = error.GetDetails();
144 });
145 if (error) {
146 AppendError(llvm::toString(std::move(error)));
147 }
148}
149
151 StreamString diag_stream(m_colors);
152 RenderDiagnosticDetails(diag_stream, indent, true, m_diagnostics);
153 // Duplex the diagnostics to the secondary stream (but not inlined).
155 RenderDiagnosticDetails(*stream_sp, std::nullopt, false, m_diagnostics);
156
157 return diag_stream.GetString().str();
158}
159
160std::string CommandReturnObject::GetErrorString(bool with_diagnostics) {
161 StreamString stream(m_colors);
162 if (with_diagnostics)
163 RenderDiagnosticDetails(stream, std::nullopt, false, m_diagnostics);
164
166 if (stream_sp)
167 stream << std::static_pointer_cast<StreamString>(stream_sp)->GetString();
168 return stream.GetString().str();
169}
170
172 auto make_array = []() { return std::make_unique<StructuredData::Array>(); };
173 auto make_bool = [](bool b) {
174 return std::make_unique<StructuredData::Boolean>(b);
175 };
176 auto make_dict = []() {
177 return std::make_unique<StructuredData::Dictionary>();
178 };
179 auto make_int = [](unsigned i) {
180 return std::make_unique<StructuredData::UnsignedInteger>(i);
181 };
182 auto make_string = [](llvm::StringRef s) {
183 return std::make_unique<StructuredData::String>(s);
184 };
185 auto dict_up = make_dict();
186 dict_up->AddItem("version", make_int(1));
187 auto array_up = make_array();
188 for (const DiagnosticDetail &diag : m_diagnostics) {
189 auto detail_up = make_dict();
190 if (auto &sloc = diag.source_location) {
191 auto sloc_up = make_dict();
192 sloc_up->AddItem("file", make_string(sloc->file.GetPath()));
193 sloc_up->AddItem("line", make_int(sloc->line));
194 sloc_up->AddItem("length", make_int(sloc->length));
195 sloc_up->AddItem("hidden", make_bool(sloc->hidden));
196 sloc_up->AddItem("in_user_input", make_bool(sloc->in_user_input));
197 detail_up->AddItem("source_location", std::move(sloc_up));
198 }
199 llvm::StringRef severity = "unknown";
200 switch (diag.severity) {
202 severity = "error";
203 break;
205 severity = "warning";
206 break;
208 severity = "note";
209 break;
210 }
211 detail_up->AddItem("severity", make_string(severity));
212 detail_up->AddItem("message", make_string(diag.message));
213 detail_up->AddItem("rendered", make_string(diag.rendered));
214 array_up->AddItem(std::move(detail_up));
215 }
216 dict_up->AddItem("details", std::move(array_up));
217 if (auto stream_sp = m_err_stream.GetStreamAtIndex(eStreamStringIndex)) {
218 auto text = std::static_pointer_cast<StreamString>(stream_sp)->GetString();
219 if (!text.empty())
220 dict_up->AddItem("text", make_string(text));
221 }
222 return dict_up;
223}
224
225// Similar to AppendError, but do not prepend 'Status: ' to message, and don't
226// append "\n" to the end of it.
227
228void CommandReturnObject::AppendRawError(llvm::StringRef in_string) {
230 assert(!in_string.empty() && "Expected a non-empty error message");
231 GetErrorStream() << in_string;
232}
233
235
237
240}
241
245}
246
248 lldb::StreamSP stream_sp;
250 if (stream_sp)
251 static_cast<StreamString *>(stream_sp.get())->Clear();
253 if (stream_sp)
254 static_cast<StreamString *>(stream_sp.get())->Clear();
255 m_diagnostics.clear();
259 m_interactive = true;
260}
261
264}
265
268}
269
271
273
276}
277
280}
static llvm::raw_ostream & error(Stream &strm)
static llvm::raw_ostream & warning(Stream &strm)
static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s)
static llvm::raw_ostream & note(Stream &strm)
void AppendMessage(llvm::StringRef in_string)
void void AppendError(llvm::StringRef in_string)
void void AppendNote(llvm::StringRef in_string)
void AppendWarningWithFormat(const char *format,...) __attribute__((format(printf
bool m_interactive
If true, then the input handle from the debugger will be hooked up.
void AppendRawError(llvm::StringRef in_string)
void SetStatus(lldb::ReturnStatus status)
std::string GetErrorString(bool with_diagnostics=true)
Return the errors as a string.
std::string GetInlineDiagnosticString(unsigned indent)
Format any inline diagnostics with an indentation of indent.
void AppendErrorWithFormat(const char *format,...) __attribute__((format(printf
void AppendMessageWithFormat(const char *format,...) __attribute__((format(printf
lldb::ReturnStatus GetStatus() const
StructuredData::ObjectSP GetErrorData()
std::vector< DiagnosticDetail > m_diagnostics
void void AppendWarning(llvm::StringRef in_string)
void AppendNoteWithFormat(const char *format,...) __attribute__((format(printf
An error handling class.
Definition: Status.h:115
llvm::StringRef GetString() const
lldb::StreamSP GetStreamAtIndex(uint32_t idx)
Definition: StreamTee.h:88
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
Definition: Stream.h:112
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:401
size_t size_t PrintfVarArg(const char *format, va_list args)
Definition: Stream.cpp:143
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:155
std::shared_ptr< Object > ObjectSP
A class that represents a running process on the host machine.
void RenderDiagnosticDetails(Stream &stream, std::optional< uint16_t > offset_in_command, bool show_inline, llvm::ArrayRef< DiagnosticDetail > details)
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Stream > StreamSP
Definition: lldb-forward.h:432
ReturnStatus
Command Return Status Types.
@ eReturnStatusStarted
@ eReturnStatusSuccessContinuingResult
@ eReturnStatusFailed
@ eReturnStatusSuccessFinishResult
A compiler-independent representation of an lldb_private::Diagnostic.