Skip to content

Commit

Permalink
colorize SymbolContext only when target available | adapt test
Browse files Browse the repository at this point in the history
  • Loading branch information
junior-jl committed Dec 6, 2023
1 parent 9d910d3 commit 4c0c2d2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
6 changes: 6 additions & 0 deletions lldb/include/lldb/Core/Address.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ class Address {
/// contains the address, otherwise dumping the range that contains the
/// address.
///
/// \param[in] pattern
/// An optional regex pattern to match against the description. If
/// specified, parts of the description matching this pattern may be
/// highlighted or processed differently. If this parameter is an empty
/// string or not provided, no highlighting is applied.
///
/// \return
/// Returns \b true if the address was able to be displayed.
/// File and load addresses may be unresolved and it may not be
Expand Down
6 changes: 6 additions & 0 deletions lldb/include/lldb/Symbol/SymbolContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ class SymbolContext {
/// is dumped if this flag is \b true, otherwise the line info
/// of the actual inlined function is dumped.
///
/// \param[in] pattern
/// An optional regex pattern to match against the stop context
/// description. If specified, parts of the description matching this
/// pattern may be highlighted or processed differently. If this parameter
/// is an empty string or not provided, no highlighting is applied.
///
/// \return
/// \b true if some text was dumped, \b false otherwise.
bool DumpStopContext(Stream *s, ExecutionContextScope *exe_scope,
Expand Down
17 changes: 16 additions & 1 deletion lldb/include/lldb/Utility/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,33 @@ class Stream {

/// Output a C string to the stream with color highlighting.
///
/// Print a C string \a text to the stream, applying red color highlighting to
/// Print a C string \a text to the stream, applying color highlighting to
/// the portions of the string that match the regex pattern \a pattern. The
/// pattern is matched as many times as possible throughout the string. If \a
/// pattern is nullptr, then no highlighting is applied.
///
/// The highlighting is applied by enclosing the matching text in ANSI color
/// codes. The \a prefix parameter specifies the ANSI code to start the color
/// (the standard value is assumed to be 'ansi.fg.red', representing red
/// foreground), and the \a suffix parameter specifies the ANSI code to end
/// the color (the standard value is assumed to be 'ansi.normal', resetting to
/// default text style). These constants should be defined appropriately in
/// your environment.
///
/// \param[in] text
/// The string to be output to the stream.
///
/// \param[in] pattern
/// The regex pattern to match against the \a text string. Portions of \a
/// text matching this pattern will be colorized. If this parameter is
/// nullptr, highlighting is not performed.
/// \param[in] prefix
/// The ANSI color code to start colorization. This is
/// environment-dependent.
/// \param[in] suffix
/// The ANSI color code to end colorization. This is
/// environment-dependent.

void PutCStringColorHighlighted(llvm::StringRef text,
llvm::StringRef pattern = "",
llvm::StringRef prefix = "",
Expand Down
8 changes: 6 additions & 2 deletions lldb/source/Symbol/SymbolContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ bool SymbolContext::DumpStopContext(Stream *s, ExecutionContextScope *exe_scope,
if (!name)
name = function->GetName();
if (name) {
llvm::StringRef ansi_prefix = "${ansi.fg.red}";
llvm::StringRef ansi_suffix = "${ansi.normal}";
llvm::StringRef ansi_prefix;
llvm::StringRef ansi_suffix;
if (target_sp) {
ansi_prefix = target_sp->GetDebugger().GetRegexMatchAnsiPrefix();
ansi_suffix = target_sp->GetDebugger().GetRegexMatchAnsiSuffix();
}
s->PutCStringColorHighlighted(name.GetStringRef(), pattern, ansi_prefix,
ansi_suffix);
}
Expand Down
36 changes: 16 additions & 20 deletions lldb/test/Shell/Commands/command-image-lookup-color.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,37 @@

# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s ma' | FileCheck %s --check-prefix CHECK1
# CHECK1: Name: {{.+}}31mma{{.+}}0min.c
# CHECK1: Summary: {{.+}}`{{.+}}31mma{{.+}}0min at main.c:2

# Checking complex regex searches

# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s main.c|foo' | FileCheck %s --check-prefix CHECK2
# CHECK2: Name: {{.+}}31mmain.c{{.+}}0m
# CHECK2: Summary: {{.+}}`{{.+}}31mfoo{{.+}}0m at main.c:1

# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s m[abc]' | FileCheck %s --check-prefix CHECK3
# CHECK3: Name: {{.+}}31mma{{.+}}0min.c
# CHECK3: Summary: {{.+}}`{{.+}}31mma{{.+}}0min at main.c:2

# Checking tail match
# Checking to ensure that no attempt is made to color anything when there are no matching symbols found

# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s .*o$' | FileCheck %s --check-prefix CHECK4
# CHECK4: Summary: {{.+}}`{{.+}}31mfoo{{.+}}0m at main.c:1
# RUN: %lldb %t -o 'settings set use-color true' -o 'image lookup -r -s IMPPATTERN123456' | FileCheck %s --check-prefix CHECK4
# CHECK4-NOT: {{[0-9]+}} symbols match the regular expression

# Checking to ensure that no attempt is made to color anything when there are no matching symbols found
# Checking multiple matches on same symbol

# RUN: %lldb %t -o 'settings set use-color true' -o 'image lookup -r -s IMPPATTERN123456' | FileCheck %s --check-prefix CHECK5
# CHECK5-NOT: {{[0-9]+}} symbols match the regular expression
# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s (ma|n)' | FileCheck %s --check-prefix CHECK5
# CHECK5: Name: {{.+}}31mma{{.+}}0mi{{.+}}31mn{{.+}}0m.c

# Checking search which find more than 1 symbol
# Checking no colorization without regex search

# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s foo|main' | FileCheck %s --check-prefix CHECK6
# CHECK6: Name: {{.+}}31mmain{{.+}}0m.c
# CHECK6: Summary: {{.+}}`{{.+}}31mfoo{{.+}}0m at main.c:1
# CHECK6: Summary: {{.+}}`{{.+}}31mmain{{.+}}0m at main.c:2
# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -s main' | FileCheck %s --check-prefix CHECK6
# CHECK6: Summary: {{.+}}`main at main.c:2

# Checking multiple matches on same symbol
# Checking no colorization when use-color is false

# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s (ma|n$)' | FileCheck %s --check-prefix CHECK7
# CHECK7: Summary: {{.+}}`{{.+}}31mma{{.+}}0mi{{.+}}31mn{{.+}}0m at main.c:2
# RUN: %lldb %t -b -o 'settings set use-color false' -o 'image lookup -r -s ma' | FileCheck %s --check-prefix CHECK7
# CHECK7: Name: main.c

# Checking no colorization without regex search
# Checking for custom colors

# RUN: %lldb %t -b -o 'settings set use-color true' -o 'settings set show-regex-match-ansi-prefix ${ansi.fg.green}' -o 'image lookup -r -s ma' | FileCheck %s --check-prefix CHECK8
# CHECK8: Name: {{.+}}32mma{{.+}}0min.c

# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -s main' | FileCheck %s --check-prefix CHECK8
# CHECK8: Summary: {{.+}}`main at main.c:2

0 comments on commit 4c0c2d2

Please sign in to comment.