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 llvm::StringRef validate_diagnostic(llvm::StringRef diagnostic) {
37 // This class is already adding the prefix.
38 assert(!diagnostic.starts_with("warning:") &&
39 !diagnostic.starts_with("error:") &&
40 !diagnostic.starts_with("note:") &&
41 "diagnostics shouldn't duplicate error:/warning:/note:");
42
43 // https://llvm.org/docs/CodingStandards.html#error-and-warning-messages
44 assert(!diagnostic.ends_with('\n') && !diagnostic.ends_with('.') &&
45 "diagnostics should end without a period/newline");
46
47 // Handing the case where the assert doesn't hold goes against the idea of
48 // them being pre-conditions. However this isn't really a matter of internal
49 // consistency and therefore we prioritize a consistent user experience over
50 // purity.
51 return diagnostic.trim("\n.");
52}
53
54static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s) {
55 bool add_newline = false;
56 if (!s.empty()) {
57 // We already checked for empty above, now make sure there is a newline in
58 // the error, and if there isn't one, add one.
59 strm.Write(s.c_str(), s.size());
60
61 const char last_char = *s.rbegin();
62 add_newline = last_char != '\n' && last_char != '\r';
63 }
64 if (add_newline)
65 strm.EOL();
66}
67
69 : m_out_stream(colors), m_err_stream(colors), m_colors(colors) {}
70
71void CommandReturnObject::AppendErrorWithFormat(const char *format, ...) {
73
74 if (!format)
75 return;
76 va_list args;
77 va_start(args, format);
78 StreamString sstrm;
79 sstrm.PrintfVarArg(format, args);
80 va_end(args);
81
82 const std::string &s = std::string(sstrm.GetString());
83 if (!s.empty()) {
86 }
87}
88
89void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
90 if (in_string.empty())
91 return;
92 GetOutputStream() << in_string.rtrim() << '\n';
93}
94
95void CommandReturnObject::AppendNote(llvm::StringRef in_string) {
96 in_string = validate_diagnostic(in_string);
97 if (in_string.empty())
98 return;
99 note(GetOutputStream()) << in_string.rtrim() << '\n';
100}
101
102void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
103 in_string = validate_diagnostic(in_string);
104 if (in_string.empty())
105 return;
106 warning(GetErrorStream()) << in_string.rtrim() << '\n';
107}
108
109void CommandReturnObject::AppendError(llvm::StringRef in_string) {
111 if (in_string.empty())
112 return;
113 // Workaround to deal with already fully formatted compiler diagnostics.
114 llvm::StringRef msg(in_string.rtrim());
115 msg.consume_front("error: ");
116
117 // FIXME: We should call validate_diagnostic here.
118 error(GetErrorStream()) << msg << '\n';
119}
120
124
126 // Retrieve any diagnostics.
127 error = llvm::handleErrors(std::move(error), [&](DiagnosticError &error) {
129 m_diagnostics = error.GetDetails();
130 });
131 if (error) {
132 AppendError(llvm::toString(std::move(error)));
133 }
134}
135
136std::string
138 StreamString diag_stream(m_colors);
139 RenderDiagnosticDetails(diag_stream, indent, true, m_diagnostics);
140 // Duplex the diagnostics to the secondary stream (but not inlined).
141 if (auto stream_sp = m_err_stream.GetStreamAtIndex(eImmediateStreamIndex))
142 RenderDiagnosticDetails(*stream_sp, std::nullopt, false, m_diagnostics);
143
144 return diag_stream.GetString().str();
145}
146
147std::string CommandReturnObject::GetErrorString(bool with_diagnostics) const {
148 StreamString stream(m_colors);
149 if (with_diagnostics)
150 RenderDiagnosticDetails(stream, std::nullopt, false, m_diagnostics);
151
152 lldb::StreamSP stream_sp(m_err_stream.GetStreamAtIndex(eStreamStringIndex));
153 if (stream_sp)
154 stream << std::static_pointer_cast<StreamString>(stream_sp)->GetString();
155 return stream.GetString().str();
156}
157
161
163
165
169
174
176 lldb::StreamSP stream_sp;
177 stream_sp = m_out_stream.GetStreamAtIndex(eStreamStringIndex);
178 if (stream_sp)
179 static_cast<StreamString *>(stream_sp.get())->Clear();
180 stream_sp = m_err_stream.GetStreamAtIndex(eStreamStringIndex);
181 if (stream_sp)
182 static_cast<StreamString *>(stream_sp.get())->Clear();
183 m_diagnostics.clear();
187 m_interactive = true;
188}
189
193
197
199
201
205
static llvm::raw_ostream & error(Stream &strm)
static llvm::StringRef validate_diagnostic(llvm::StringRef diagnostic)
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 AppendError(llvm::StringRef in_string)
std::string GetErrorString(bool with_diagnostics=true) const
Return the errors as a string.
void AppendNote(llvm::StringRef in_string)
bool m_interactive
If true, then the input handle from the debugger will be hooked up.
void SetStatus(lldb::ReturnStatus status)
void AppendErrorWithFormat(const char *format,...) __attribute__((format(printf
StructuredData::ObjectSP GetErrorData()
std::vector< DiagnosticDetail > m_diagnostics
void AppendWarning(llvm::StringRef in_string)
std::string GetInlineDiagnosticString(unsigned indent) const
Format any inline diagnostics with an indentation of indent.
An error handling class.
Definition Status.h:118
llvm::StringRef GetString() const
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:110
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:402
size_t size_t PrintfVarArg(const char *format, va_list args)
Definition Stream.cpp:141
size_t EOL()
Output and End of Line character to the stream.
Definition Stream.cpp:153
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, bool force_ascii=false)
Renders an array of DiagnosticDetail instances.
StructuredData::ObjectSP Serialize(llvm::ArrayRef< DiagnosticDetail > details)
std::shared_ptr< lldb_private::Stream > StreamSP
ReturnStatus
Command Return Status Types.
@ eReturnStatusStarted
@ eReturnStatusSuccessContinuingResult
@ eReturnStatusFailed
@ eReturnStatusSuccessFinishResult