Skip to content

Commit de019b8

Browse files
committed
[lldb/Interpreter] Support color in CommandReturnObject
Color the error: and warning: part of the CommandReturnObject output, similar to how an error is printed from the driver when colors are enabled. Differential revision: https://reviews.llvm.org/D81058
1 parent 1f48f8f commit de019b8

File tree

19 files changed

+68
-43
lines changed

19 files changed

+68
-43
lines changed

lldb/include/lldb/Interpreter/CommandReturnObject.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616

1717
#include "llvm/ADT/StringRef.h"
1818
#include "llvm/Support/FormatVariadic.h"
19+
#include "llvm/Support/WithColor.h"
1920

2021
#include <memory>
2122

2223
namespace lldb_private {
2324

2425
class CommandReturnObject {
2526
public:
26-
CommandReturnObject();
27+
CommandReturnObject(bool colors);
2728

2829
~CommandReturnObject();
2930

lldb/include/lldb/Utility/Stream.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ class Stream {
5656
///
5757
/// Construct with dump flags \a flags and the default address size. \a
5858
/// flags can be any of the above enumeration logical OR'ed together.
59-
Stream(uint32_t flags, uint32_t addr_size, lldb::ByteOrder byte_order);
59+
Stream(uint32_t flags, uint32_t addr_size, lldb::ByteOrder byte_order,
60+
bool colors = false);
6061

6162
/// Construct a default Stream, not binary, host byte order and host addr
6263
/// size.
6364
///
64-
Stream();
65+
Stream(bool colors = false);
6566

6667
// FIXME: Streams should not be copyable.
6768
Stream(const Stream &other) : m_forwarder(*this) { (*this) = other; }
@@ -403,8 +404,10 @@ class Stream {
403404
}
404405

405406
public:
406-
RawOstreamForward(Stream &target)
407-
: llvm::raw_ostream(/*unbuffered*/ true), m_target(target) {}
407+
RawOstreamForward(Stream &target, bool colors = false)
408+
: llvm::raw_ostream(/*unbuffered*/ true), m_target(target) {
409+
enable_colors(colors);
410+
}
408411
};
409412
RawOstreamForward m_forwarder;
410413
};

lldb/include/lldb/Utility/StreamTee.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ namespace lldb_private {
1919

2020
class StreamTee : public Stream {
2121
public:
22-
StreamTee() : Stream(), m_streams_mutex(), m_streams() {}
22+
StreamTee(bool colors = false)
23+
: Stream(colors), m_streams_mutex(), m_streams() {}
2324

2425
StreamTee(lldb::StreamSP &stream_sp)
2526
: Stream(), m_streams_mutex(), m_streams() {

lldb/source/API/SBCommandReturnObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using namespace lldb_private;
2222
class lldb_private::SBCommandReturnObjectImpl {
2323
public:
2424
SBCommandReturnObjectImpl()
25-
: m_ptr(new CommandReturnObject()), m_owned(true) {}
25+
: m_ptr(new CommandReturnObject(false)), m_owned(true) {}
2626
SBCommandReturnObjectImpl(CommandReturnObject &ref)
2727
: m_ptr(&ref), m_owned(false) {}
2828
SBCommandReturnObjectImpl(const SBCommandReturnObjectImpl &rhs)

lldb/source/Breakpoint/BreakpointOptions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,11 @@ bool BreakpointOptions::BreakpointOptionsCallbackFunction(
630630
ExecutionContext exe_ctx(context->exe_ctx_ref);
631631
Target *target = exe_ctx.GetTargetPtr();
632632
if (target) {
633-
CommandReturnObject result;
634633
Debugger &debugger = target->GetDebugger();
634+
CommandReturnObject result(debugger.GetUseColor());
635+
635636
// Rig up the results secondary output stream to the debugger's, so the
636637
// output will come out synchronously if the debugger is set up that way.
637-
638638
StreamSP output_stream(debugger.GetAsyncOutputStream());
639639
StreamSP error_stream(debugger.GetAsyncErrorStream());
640640
result.SetImmediateOutputStream(output_stream);

lldb/source/Commands/CommandObjectExpression.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,8 @@ void CommandObjectExpression::IOHandlerInputComplete(IOHandler &io_handler,
500500
StreamFileSP output_sp = io_handler.GetOutputStreamFileSP();
501501
StreamFileSP error_sp = io_handler.GetErrorStreamFileSP();
502502

503-
CommandReturnObject return_obj;
503+
CommandReturnObject return_obj(
504+
GetCommandInterpreter().GetDebugger().GetUseColor());
504505
EvaluateExpression(line.c_str(), *output_sp, *error_sp, return_obj);
505506
if (output_sp)
506507
output_sp->Flush();

lldb/source/Commands/CommandObjectWatchpointCommand.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,12 @@ are no syntax errors may indicate that a function was declared but never called.
282282
ExecutionContext exe_ctx(context->exe_ctx_ref);
283283
Target *target = exe_ctx.GetTargetPtr();
284284
if (target) {
285-
CommandReturnObject result;
286285
Debugger &debugger = target->GetDebugger();
286+
CommandReturnObject result(debugger.GetUseColor());
287+
287288
// Rig up the results secondary output stream to the debugger's, so the
288289
// output will come out synchronously if the debugger is set up that
289290
// way.
290-
291291
StreamSP output_stream(debugger.GetAsyncOutputStream());
292292
StreamSP error_stream(debugger.GetAsyncErrorStream());
293293
result.SetImmediateOutputStream(output_stream);

lldb/source/Expression/REPL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ void REPL::IOHandlerInputComplete(IOHandler &io_handler, std::string &code) {
216216
ci.SetPromptOnQuit(false);
217217

218218
// Execute the command
219-
CommandReturnObject result;
219+
CommandReturnObject result(debugger.GetUseColor());
220220
result.SetImmediateOutputStream(output_sp);
221221
result.SetImmediateErrorStream(error_sp);
222222
ci.HandleCommand(code.c_str(), eLazyBoolNo, result);

lldb/source/Interpreter/CommandAlias.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static bool ProcessAliasOptionsArgs(lldb::CommandObjectSP &cmd_obj_sp,
3232
std::string options_string(options_args);
3333
// TODO: Find a way to propagate errors in this CommandReturnObject up the
3434
// stack.
35-
CommandReturnObject result;
35+
CommandReturnObject result(false);
3636
// Check to see if the command being aliased can take any command options.
3737
Options *options = cmd_obj_sp->GetOptions();
3838
if (options) {

lldb/source/Interpreter/CommandInterpreter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ void CommandInterpreter::Initialize() {
209209
static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
210210
Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
211211

212-
CommandReturnObject result;
212+
CommandReturnObject result(m_debugger.GetUseColor());
213213

214214
LoadCommandDictionary();
215215

@@ -2264,7 +2264,7 @@ void CommandInterpreter::HandleCommands(const StringList &commands,
22642264
m_debugger.GetPrompt().str().c_str(), cmd);
22652265
}
22662266

2267-
CommandReturnObject tmp_result;
2267+
CommandReturnObject tmp_result(m_debugger.GetUseColor());
22682268
// If override_context is not NULL, pass no_context_switching = true for
22692269
// HandleCommand() since we updated our context already.
22702270

@@ -2792,7 +2792,7 @@ void CommandInterpreter::IOHandlerInputComplete(IOHandler &io_handler,
27922792

27932793
StartHandlingCommand();
27942794

2795-
lldb_private::CommandReturnObject result;
2795+
lldb_private::CommandReturnObject result(m_debugger.GetUseColor());
27962796
HandleCommand(line.c_str(), eLazyBoolCalculate, result);
27972797

27982798
// Now emit the command output text from the command we just executed

lldb/source/Interpreter/CommandObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ void CommandObject::HandleCompletion(CompletionRequest &request) {
282282
} else {
283283
// Can we do anything generic with the options?
284284
Options *cur_options = GetOptions();
285-
CommandReturnObject result;
285+
CommandReturnObject result(m_interpreter.GetDebugger().GetUseColor());
286286
OptionElementVector opt_element_vector;
287287

288288
if (cur_options != nullptr) {

lldb/source/Interpreter/CommandReturnObject.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
using namespace lldb;
1515
using namespace lldb_private;
1616

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+
1729
static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s) {
1830
bool add_newline = false;
1931
if (!s.empty()) {
@@ -28,9 +40,10 @@ static void DumpStringToStreamWithNewline(Stream &strm, const std::string &s) {
2840
strm.EOL();
2941
}
3042

31-
CommandReturnObject::CommandReturnObject()
32-
: m_out_stream(), m_err_stream(), m_status(eReturnStatusStarted),
33-
m_did_change_process_state(false), m_interactive(true) {}
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) {}
3447

3548
CommandReturnObject::~CommandReturnObject() {}
3649

@@ -45,9 +58,8 @@ void CommandReturnObject::AppendErrorWithFormat(const char *format, ...) {
4558

4659
const std::string &s = std::string(sstrm.GetString());
4760
if (!s.empty()) {
48-
Stream &error_strm = GetErrorStream();
49-
error_strm.PutCString("error: ");
50-
DumpStringToStreamWithNewline(error_strm, s);
61+
error(GetErrorStream());
62+
DumpStringToStreamWithNewline(GetErrorStream(), s);
5163
}
5264
}
5365

@@ -72,7 +84,7 @@ void CommandReturnObject::AppendWarningWithFormat(const char *format, ...) {
7284
sstrm.PrintfVarArg(format, args);
7385
va_end(args);
7486

75-
GetErrorStream() << "warning: " << sstrm.GetString();
87+
warning(GetErrorStream()) << sstrm.GetString();
7688
}
7789

7890
void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
@@ -84,7 +96,7 @@ void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
8496
void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
8597
if (in_string.empty())
8698
return;
87-
GetErrorStream() << "warning: " << in_string << "\n";
99+
warning(GetErrorStream()) << in_string << '\n';
88100
}
89101

90102
// Similar to AppendWarning, but do not prepend 'warning: ' to message, and
@@ -99,7 +111,7 @@ void CommandReturnObject::AppendRawWarning(llvm::StringRef in_string) {
99111
void CommandReturnObject::AppendError(llvm::StringRef in_string) {
100112
if (in_string.empty())
101113
return;
102-
GetErrorStream() << "error: " << in_string << "\n";
114+
error(GetErrorStream()) << in_string << '\n';
103115
}
104116

105117
void CommandReturnObject::SetError(const Status &error,

lldb/source/Plugins/StructuredData/DarwinLog/StructuredDataDarwinLog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ EnableOptionsSP ParseAutoEnableOptions(Status &error, Debugger &debugger) {
981981
EnableOptionsSP options_sp(new EnableOptions());
982982
options_sp->NotifyOptionParsingStarting(&exe_ctx);
983983

984-
CommandReturnObject result;
984+
CommandReturnObject result(debugger.GetUseColor());
985985

986986
// Parse the arguments.
987987
auto options_property_sp =
@@ -1036,7 +1036,7 @@ bool RunEnableCommand(CommandInterpreter &interpreter) {
10361036
}
10371037

10381038
// Run the command.
1039-
CommandReturnObject return_object;
1039+
CommandReturnObject return_object(interpreter.GetDebugger().GetUseColor());
10401040
interpreter.HandleCommand(command_stream.GetData(), eLazyBoolNo,
10411041
return_object);
10421042
return return_object.Succeeded();

lldb/source/Target/Target.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2556,7 +2556,7 @@ void Target::RunStopHooks() {
25562556
if (!any_active_hooks)
25572557
return;
25582558

2559-
CommandReturnObject result;
2559+
CommandReturnObject result(m_debugger.GetUseColor());
25602560

25612561
std::vector<ExecutionContext> exc_ctx_with_reasons;
25622562
std::vector<SymbolContext> sym_ctx_with_reasons;

lldb/source/Utility/Stream.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
using namespace lldb;
2323
using namespace lldb_private;
2424

25-
Stream::Stream(uint32_t flags, uint32_t addr_size, ByteOrder byte_order)
25+
Stream::Stream(uint32_t flags, uint32_t addr_size, ByteOrder byte_order,
26+
bool colors)
2627
: m_flags(flags), m_addr_size(addr_size), m_byte_order(byte_order),
27-
m_indent_level(0), m_forwarder(*this) {}
28+
m_indent_level(0), m_forwarder(*this, colors) {}
2829

29-
Stream::Stream()
30+
Stream::Stream(bool colors)
3031
: m_flags(0), m_addr_size(4), m_byte_order(endian::InlHostByteOrder()),
31-
m_indent_level(0), m_forwarder(*this) {}
32+
m_indent_level(0), m_forwarder(*this, colors) {}
3233

3334
// Destructor
3435
Stream::~Stream() {}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# RUN: %lldb --no-use-colors -s %s | FileCheck %s
2-
settings show use-color
3-
# CHECK: use-color (boolean) = false
4-
q
1+
RUN: not %lldb -b --no-use-colors -o 'settings show use-color' -o 'bogus' 2>&1 | FileCheck %s --check-prefix NOCOLOR
2+
NOCOLOR: use-color (boolean) = false
3+
NOCOLOR: error: 'bogus' is not a valid command
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
UNSUPPORTED: system-windows
2+
3+
RUN: not %lldb -b -o 'settings set use-color true' -o 'settings show use-color' -o 'bogus' > %t 2>&1
4+
RUN: cat -e %t | FileCheck %s --check-prefix COLOR
5+
COLOR: use-color (boolean) = true
6+
# The [[ confuses FileCheck so regex match it.
7+
COLOR: {{.+}}0;1;31merror: {{.+}}0m'bogus' is not a valid command

lldb/tools/lldb-test/lldb-test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ int opts::breakpoint::evaluateBreakpoints(Debugger &Dbg) {
380380

381381
std::string Command = substitute(Line);
382382
P.formatLine("Command: {0}", Command);
383-
CommandReturnObject Result;
383+
CommandReturnObject Result(/*colors*/ false);
384384
if (!Dbg.GetCommandInterpreter().HandleCommand(
385385
Command.c_str(), /*add_to_history*/ eLazyBoolNo, Result)) {
386386
P.formatLine("Failed: {0}", Result.GetErrorData());
@@ -530,7 +530,7 @@ Error opts::symbols::findTypes(lldb_private::Module &Module) {
530530
LanguageSet languages;
531531
if (!Language.empty())
532532
languages.Insert(Language::GetLanguageTypeFromString(Language));
533-
533+
534534
DenseSet<SymbolFile *> SearchedFiles;
535535
TypeMap Map;
536536
if (!Name.empty())
@@ -1034,7 +1034,7 @@ int opts::irmemorymap::evaluateMemoryMapCommands(Debugger &Dbg) {
10341034

10351035
// Set up a Process. In order to allocate memory within a target, this
10361036
// process must be alive and must support JIT'ing.
1037-
CommandReturnObject Result;
1037+
CommandReturnObject Result(/*colors*/ false);
10381038
Dbg.SetAsyncExecution(false);
10391039
CommandInterpreter &CI = Dbg.GetCommandInterpreter();
10401040
auto IssueCmd = [&](const char *Cmd) -> bool {
@@ -1098,7 +1098,7 @@ int main(int argc, const char *argv[]) {
10981098

10991099
auto Dbg = lldb_private::Debugger::CreateInstance();
11001100
ModuleList::GetGlobalModuleListProperties().SetEnableExternalLookup(false);
1101-
CommandReturnObject Result;
1101+
CommandReturnObject Result(/*colors*/ false);
11021102
Dbg->GetCommandInterpreter().HandleCommand(
11031103
"settings set plugin.process.gdb-remote.packet-timeout 60",
11041104
/*add_to_history*/ eLazyBoolNo, Result);

lldb/unittests/ScriptInterpreter/Lua/ScriptInterpreterTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ TEST_F(ScriptInterpreterTest, ExecuteOneLine) {
5454
ASSERT_TRUE(debugger_sp);
5555

5656
ScriptInterpreterLua script_interpreter(*debugger_sp);
57-
CommandReturnObject result;
57+
CommandReturnObject result(/*colors*/ false);
5858
EXPECT_TRUE(script_interpreter.ExecuteOneLine("foo = 1", &result));
5959
EXPECT_FALSE(script_interpreter.ExecuteOneLine("nil = foo", &result));
6060
EXPECT_TRUE(result.GetErrorData().startswith(

0 commit comments

Comments
 (0)