Skip to content

Commit 7b925c3

Browse files
[lldb] refactor highlighting function for image lookup command (#76112)
Follow-up to #69422. This PR puts all the highlighting settings into a single struct for easier handling Co-authored-by: Talha Tahir <[email protected]>
1 parent 11d1310 commit 7b925c3

File tree

9 files changed

+89
-100
lines changed

9 files changed

+89
-100
lines changed

lldb/include/lldb/Core/Address.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLDB_CORE_ADDRESS_H
1010
#define LLDB_CORE_ADDRESS_H
1111

12+
#include "lldb/Utility/Stream.h"
1213
#include "lldb/lldb-defines.h"
1314
#include "lldb/lldb-forward.h"
1415
#include "lldb/lldb-private-enumerations.h"
@@ -252,10 +253,11 @@ class Address {
252253
/// in such cases.
253254
///
254255
/// \see Address::DumpStyle
255-
bool Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
256-
DumpStyle fallback_style = DumpStyleInvalid,
257-
uint32_t addr_byte_size = UINT32_MAX, bool all_ranges = false,
258-
llvm::StringRef pattern = "") const;
256+
bool
257+
Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
258+
DumpStyle fallback_style = DumpStyleInvalid,
259+
uint32_t addr_byte_size = UINT32_MAX, bool all_ranges = false,
260+
std::optional<Stream::HighlightSettings> settings = std::nullopt) const;
259261

260262
AddressClass GetAddressClass() const;
261263

lldb/include/lldb/Symbol/Symbol.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "lldb/Core/Mangled.h"
1414
#include "lldb/Core/Section.h"
1515
#include "lldb/Symbol/SymbolContextScope.h"
16+
#include "lldb/Utility/Stream.h"
1617
#include "lldb/Utility/UserID.h"
1718
#include "lldb/lldb-private.h"
1819
#include "llvm/Support/JSON.h"
@@ -174,8 +175,9 @@ class Symbol : public SymbolContextScope {
174175

175176
void SetFlags(uint32_t flags) { m_flags = flags; }
176177

177-
void GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target,
178-
llvm::StringRef pattern = "") const;
178+
void GetDescription(
179+
Stream *s, lldb::DescriptionLevel level, Target *target,
180+
std::optional<Stream::HighlightSettings> settings = std::nullopt) const;
179181

180182
bool IsSynthetic() const { return m_is_synthetic; }
181183

lldb/include/lldb/Symbol/SymbolContext.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "lldb/Core/Mangled.h"
1818
#include "lldb/Symbol/LineEntry.h"
1919
#include "lldb/Utility/Iterable.h"
20+
#include "lldb/Utility/Stream.h"
2021
#include "lldb/lldb-private.h"
2122

2223
namespace lldb_private {
@@ -153,11 +154,11 @@ class SymbolContext {
153154
///
154155
/// \return
155156
/// \b true if some text was dumped, \b false otherwise.
156-
bool DumpStopContext(Stream *s, ExecutionContextScope *exe_scope,
157-
const Address &so_addr, bool show_fullpaths,
158-
bool show_module, bool show_inlined_frames,
159-
bool show_function_arguments, bool show_function_name,
160-
llvm::StringRef pattern = "") const;
157+
bool DumpStopContext(
158+
Stream *s, ExecutionContextScope *exe_scope, const Address &so_addr,
159+
bool show_fullpaths, bool show_module, bool show_inlined_frames,
160+
bool show_function_arguments, bool show_function_name,
161+
std::optional<Stream::HighlightSettings> settings = std::nullopt) const;
161162

162163
/// Get the address range contained within a symbol context.
163164
///
@@ -223,8 +224,9 @@ class SymbolContext {
223224
/// The symbol that was found, or \b nullptr if none was found.
224225
const Symbol *FindBestGlobalDataSymbol(ConstString name, Status &error);
225226

226-
void GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target,
227-
llvm::StringRef pattern = "") const;
227+
void GetDescription(
228+
Stream *s, lldb::DescriptionLevel level, Target *target,
229+
std::optional<Stream::HighlightSettings> settings = std::nullopt) const;
228230

229231
uint32_t GetResolvedMask() const;
230232

lldb/include/lldb/Utility/Stream.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ class Stream {
3333
/// string mode.
3434
};
3535

36+
/// Struct to store information for color highlighting in the stream.
37+
struct HighlightSettings {
38+
llvm::StringRef pattern; ///< Regex pattern for highlighting.
39+
llvm::StringRef prefix; ///< ANSI color code to start colorization.
40+
llvm::StringRef suffix; ///< ANSI color code to end colorization.
41+
42+
HighlightSettings(llvm::StringRef p, llvm::StringRef pre,
43+
llvm::StringRef suf)
44+
: pattern(p), prefix(pre), suffix(suf) {}
45+
};
46+
3647
/// Utility class for counting the bytes that were written to a stream in a
3748
/// certain time span.
3849
///
@@ -260,10 +271,9 @@ class Stream {
260271
/// The ANSI color code to end colorization. This is
261272
/// environment-dependent.
262273

263-
void PutCStringColorHighlighted(llvm::StringRef text,
264-
llvm::StringRef pattern = "",
265-
llvm::StringRef prefix = "",
266-
llvm::StringRef suffix = "");
274+
void PutCStringColorHighlighted(
275+
llvm::StringRef text,
276+
std::optional<HighlightSettings> settings = std::nullopt);
267277

268278
/// Output and End of Line character to the stream.
269279
size_t EOL();

lldb/source/Commands/CommandObjectTarget.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "lldb/Utility/FileSpec.h"
5454
#include "lldb/Utility/LLDBLog.h"
5555
#include "lldb/Utility/State.h"
56+
#include "lldb/Utility/Stream.h"
5657
#include "lldb/Utility/StructuredData.h"
5758
#include "lldb/Utility/Timer.h"
5859
#include "lldb/lldb-enumerations.h"
@@ -1531,9 +1532,10 @@ static void DumpOsoFilesTable(Stream &strm,
15311532
});
15321533
}
15331534

1534-
static void DumpAddress(ExecutionContextScope *exe_scope,
1535-
const Address &so_addr, bool verbose, bool all_ranges,
1536-
Stream &strm, llvm::StringRef pattern = "") {
1535+
static void
1536+
DumpAddress(ExecutionContextScope *exe_scope, const Address &so_addr,
1537+
bool verbose, bool all_ranges, Stream &strm,
1538+
std::optional<Stream::HighlightSettings> settings = std::nullopt) {
15371539
strm.IndentMore();
15381540
strm.Indent(" Address: ");
15391541
so_addr.Dump(&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
@@ -1544,13 +1546,13 @@ static void DumpAddress(ExecutionContextScope *exe_scope,
15441546
const uint32_t save_indent = strm.GetIndentLevel();
15451547
strm.SetIndentLevel(save_indent + 13);
15461548
so_addr.Dump(&strm, exe_scope, Address::DumpStyleResolvedDescription,
1547-
Address::DumpStyleInvalid, UINT32_MAX, false, pattern);
1549+
Address::DumpStyleInvalid, UINT32_MAX, false, settings);
15481550
strm.SetIndentLevel(save_indent);
15491551
// Print out detailed address information when verbose is enabled
15501552
if (verbose) {
15511553
strm.EOL();
15521554
so_addr.Dump(&strm, exe_scope, Address::DumpStyleDetailedSymbolContext,
1553-
Address::DumpStyleInvalid, UINT32_MAX, all_ranges, pattern);
1555+
Address::DumpStyleInvalid, UINT32_MAX, all_ranges, settings);
15541556
}
15551557
strm.IndentLess();
15561558
}
@@ -1615,25 +1617,28 @@ static uint32_t LookupSymbolInModule(CommandInterpreter &interpreter,
16151617
DumpFullpath(strm, &module->GetFileSpec(), 0);
16161618
strm.PutCString(":\n");
16171619
strm.IndentMore();
1620+
Stream::HighlightSettings settings(
1621+
name, interpreter.GetDebugger().GetRegexMatchAnsiPrefix(),
1622+
interpreter.GetDebugger().GetRegexMatchAnsiSuffix());
16181623
for (uint32_t i = 0; i < num_matches; ++i) {
16191624
Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
16201625
if (symbol) {
16211626
if (symbol->ValueIsAddress()) {
16221627
DumpAddress(
16231628
interpreter.GetExecutionContext().GetBestExecutionContextScope(),
16241629
symbol->GetAddressRef(), verbose, all_ranges, strm,
1625-
use_color && name_is_regex ? name : nullptr);
1630+
use_color && name_is_regex
1631+
? std::optional<Stream::HighlightSettings>{settings}
1632+
: std::nullopt);
16261633
strm.EOL();
16271634
} else {
16281635
strm.IndentMore();
16291636
strm.Indent(" Name: ");
1630-
llvm::StringRef ansi_prefix =
1631-
interpreter.GetDebugger().GetRegexMatchAnsiPrefix();
1632-
llvm::StringRef ansi_suffix =
1633-
interpreter.GetDebugger().GetRegexMatchAnsiSuffix();
16341637
strm.PutCStringColorHighlighted(
16351638
symbol->GetDisplayName().GetStringRef(),
1636-
use_color ? name : nullptr, ansi_prefix, ansi_suffix);
1639+
use_color && name_is_regex
1640+
? std::optional<Stream::HighlightSettings>{settings}
1641+
: std::nullopt);
16371642
strm.EOL();
16381643
strm.Indent(" Value: ");
16391644
strm.Printf("0x%16.16" PRIx64 "\n", symbol->GetRawValue());
@@ -1650,10 +1655,10 @@ static uint32_t LookupSymbolInModule(CommandInterpreter &interpreter,
16501655
return num_matches;
16511656
}
16521657

1653-
static void DumpSymbolContextList(ExecutionContextScope *exe_scope,
1654-
Stream &strm,
1655-
const SymbolContextList &sc_list,
1656-
bool verbose, bool all_ranges) {
1658+
static void DumpSymbolContextList(
1659+
ExecutionContextScope *exe_scope, Stream &strm,
1660+
const SymbolContextList &sc_list, bool verbose, bool all_ranges,
1661+
std::optional<Stream::HighlightSettings> settings = std::nullopt) {
16571662
strm.IndentMore();
16581663
bool first_module = true;
16591664
for (const SymbolContext &sc : sc_list) {
@@ -1664,7 +1669,8 @@ static void DumpSymbolContextList(ExecutionContextScope *exe_scope,
16641669

16651670
sc.GetAddressRange(eSymbolContextEverything, 0, true, range);
16661671

1667-
DumpAddress(exe_scope, range.GetBaseAddress(), verbose, all_ranges, strm);
1672+
DumpAddress(exe_scope, range.GetBaseAddress(), verbose, all_ranges, strm,
1673+
settings);
16681674
first_module = false;
16691675
}
16701676
strm.IndentLess();

lldb/source/Core/Address.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ bool Address::GetDescription(Stream &s, Target &target,
407407

408408
bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
409409
DumpStyle fallback_style, uint32_t addr_size,
410-
bool all_ranges, llvm::StringRef pattern) const {
410+
bool all_ranges,
411+
std::optional<Stream::HighlightSettings> settings) const {
411412
// If the section was nullptr, only load address is going to work unless we
412413
// are trying to deref a pointer
413414
SectionSP section_sp(GetSection());
@@ -516,16 +517,7 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
516517
if (symbol) {
517518
const char *symbol_name = symbol->GetName().AsCString();
518519
if (symbol_name) {
519-
llvm::StringRef ansi_prefix;
520-
llvm::StringRef ansi_suffix;
521-
if (target) {
522-
ansi_prefix =
523-
target->GetDebugger().GetRegexMatchAnsiPrefix();
524-
ansi_suffix =
525-
target->GetDebugger().GetRegexMatchAnsiSuffix();
526-
}
527-
s->PutCStringColorHighlighted(symbol_name, pattern,
528-
ansi_prefix, ansi_suffix);
520+
s->PutCStringColorHighlighted(symbol_name, settings);
529521
addr_t delta =
530522
file_Addr - symbol->GetAddressRef().GetFileAddress();
531523
if (delta)
@@ -653,7 +645,7 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
653645
pointer_sc.symbol != nullptr) {
654646
s->PutCString(": ");
655647
pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false,
656-
false, true, true, pattern);
648+
false, true, true, settings);
657649
}
658650
}
659651
}
@@ -693,21 +685,21 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
693685
sc.DumpStopContext(s, exe_scope, *this, show_fullpaths,
694686
show_module, show_inlined_frames,
695687
show_function_arguments, show_function_name,
696-
pattern);
688+
settings);
697689
} else {
698690
// We found a symbol but it was in a different section so it
699691
// isn't the symbol we should be showing, just show the section
700692
// name + offset
701693
Dump(s, exe_scope, DumpStyleSectionNameOffset, DumpStyleInvalid,
702-
UINT32_MAX, false, pattern);
694+
UINT32_MAX, false, settings);
703695
}
704696
}
705697
}
706698
}
707699
} else {
708700
if (fallback_style != DumpStyleInvalid)
709701
return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size,
710-
false, pattern);
702+
false, settings);
711703
return false;
712704
}
713705
break;
@@ -728,7 +720,7 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
728720
sc.symbol->GetAddressRef().GetSection() != GetSection())
729721
sc.symbol = nullptr;
730722
}
731-
sc.GetDescription(s, eDescriptionLevelBrief, target, pattern);
723+
sc.GetDescription(s, eDescriptionLevelBrief, target, settings);
732724

733725
if (sc.block) {
734726
bool can_create = true;
@@ -777,7 +769,7 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
777769
} else {
778770
if (fallback_style != DumpStyleInvalid)
779771
return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size,
780-
false, pattern);
772+
false, settings);
781773
return false;
782774
}
783775
break;

lldb/source/Symbol/Symbol.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,9 @@ bool Symbol::IsTrampoline() const { return m_type == eSymbolTypeTrampoline; }
226226

227227
bool Symbol::IsIndirect() const { return m_type == eSymbolTypeResolver; }
228228

229-
void Symbol::GetDescription(Stream *s, lldb::DescriptionLevel level,
230-
Target *target, llvm::StringRef pattern) const {
229+
void Symbol::GetDescription(
230+
Stream *s, lldb::DescriptionLevel level, Target *target,
231+
std::optional<Stream::HighlightSettings> settings) const {
231232
s->Printf("id = {0x%8.8x}", m_uid);
232233

233234
if (m_addr_range.GetBaseAddress().GetSection()) {
@@ -254,22 +255,14 @@ void Symbol::GetDescription(Stream *s, lldb::DescriptionLevel level,
254255
s->Printf(", value = 0x%16.16" PRIx64,
255256
m_addr_range.GetBaseAddress().GetOffset());
256257
}
257-
llvm::StringRef ansi_prefix;
258-
llvm::StringRef ansi_suffix;
259-
if (target) {
260-
ansi_prefix = target->GetDebugger().GetRegexMatchAnsiPrefix();
261-
ansi_suffix = target->GetDebugger().GetRegexMatchAnsiSuffix();
262-
}
263258
if (ConstString demangled = m_mangled.GetDemangledName()) {
264259
s->PutCString(", name=\"");
265-
s->PutCStringColorHighlighted(demangled.GetStringRef(), pattern,
266-
ansi_prefix, ansi_suffix);
260+
s->PutCStringColorHighlighted(demangled.GetStringRef(), settings);
267261
s->PutCString("\"");
268262
}
269263
if (ConstString mangled_name = m_mangled.GetMangledName()) {
270264
s->PutCString(", mangled=\"");
271-
s->PutCStringColorHighlighted(mangled_name.GetStringRef(), pattern,
272-
ansi_prefix, ansi_suffix);
265+
s->PutCStringColorHighlighted(mangled_name.GetStringRef(), settings);
273266
s->PutCString("\"");
274267
}
275268
}

0 commit comments

Comments
 (0)