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

0 commit comments

Comments
 (0)