least one caller. */
if (i != size - 1)
std::swap (m_source_map[i], m_source_map[size - 1]);
- return true;
+
+ /* If the styling status of the cached entry matches our desired
+ styling status, or we know this file cannot be styled, in
+ which case, this (unstyled) content, is the best we can do. */
+ if (((source_styling && gdb_stdout->can_emit_style_escape ())
+ == m_source_map[size - 1].styled)
+ || m_no_styling_files.count (fullname) > 0)
+ return true;
+
+ /* We found a match, but styling status doesn't match the desired
+ styling status. We already moved the matching item to the
+ back of M_SOURCE_MAP, so drop the entry now, and then
+ recompute with the desired styling. */
+ m_source_map.pop_back ();
+ break;
}
}
std::string contents;
+ bool styled_p = false;
try
{
contents = get_plain_source_lines (s, fullname);
if (source_styling && gdb_stdout->can_emit_style_escape ()
&& m_no_styling_files.count (fullname) == 0)
{
- bool already_styled
+ styled_p
= try_source_highlight (contents, s->language (), fullname);
- if (!already_styled)
+ if (!styled_p)
{
std::optional<std::string> ext_contents;
ext_contents = ext_lang_colorize (fullname, contents);
if (ext_contents.has_value ())
{
contents = std::move (*ext_contents);
- already_styled = true;
+ styled_p = true;
}
}
- if (!already_styled)
+ if (!styled_p)
{
/* Styling failed. Styling can fail for instance for these
reasons:
}
}
- source_text result = { std::move (fullname), std::move (contents) };
+ source_text result
+ = { std::move (fullname), std::move (contents), styled_p };
m_source_map.push_back (std::move (result));
if (m_source_map.size () > MAX_ENTRIES)
set line_number [gdb_get_line_number "List this line."]
+# Helper proc. Run CMD, which should produce a source listing, and
+# check if the source code is styled or not. EXPECT_STYLED indicates
+# if we expect the source listing to be styled or not.
+proc check_source_listing_styling { cmd expect_styled { testname "" } } {
+ if { $testname eq "" } {
+ set testname $cmd
+ }
+
+ set seen_style_escape false
+ gdb_test_multiple $cmd $testname {
+ -re -wrap "Python Exception.*" {
+ fail $gdb_test_name
+ return
+ }
+ -re "\033" {
+ set seen_style_escape true
+ exp_continue
+ }
+ -re "$::gdb_prompt $" {
+ gdb_assert { $seen_style_escape == $expect_styled } \
+ $gdb_test_name
+ }
+ }
+}
+
# Check that the Python pygments module can be used for source
# highlighting when GNU source highlight is not available (or is
# disabled, as is done in this test).
proc test_pygments_styling {} {
clean_restart $::binfile
+ # Remote host boards disable styling via GDB's command line. Turn
+ # it back on now.
+ if {[is_remote host]} {
+ gdb_test "set style enabled on"
+ }
+
if { ![gdb_py_module_available "pygments"] } {
unsupported "pygments module not available"
return
gdb_test "maint flush source-cache" "Source cache flushed\\."
- set seen_style_escape false
- gdb_test_multiple "list $::line_number" "" {
- -re "Python Exception.*" {
- fail $gdb_test_name
- }
- -re "\033" {
- set seen_style_escape true
- exp_continue
- }
- -re "$::gdb_prompt $" {
- gdb_assert { $seen_style_escape } $gdb_test_name
- }
- }
+ check_source_listing_styling "list $::line_number" true
}
# Use gdb.execute to list source code containing non-utf-8 character.
gdb_test "python print(source)" ".*List this line.*"
}
+# Use gdb.execute() to list source code. Alternate between asking for
+# styled, and unstyled source code. In some cases we ask for the
+# output to be returned via a string, and in other cases we ask for
+# the output to be sent straight to stdout.
+proc_with_prefix test_source_cache_style_tracking {} {
+ clean_restart $::binfile
+
+ # Remote host boards disable styling via GDB's command line. Turn
+ # it back on now.
+ if {[is_remote host]} {
+ gdb_test "set style enabled on"
+ }
+
+ gdb_test_no_output "set host-charset ISO-8859-1"
+
+ # Commands which return styled, and non-styled source code mixed
+ # together. This ensures that the source cache will need to keep
+ # discarding the entry with the wrong styling mode. All of these
+ # gdb.execute calls send their output via a string.
+ check_source_listing_styling \
+ "python print(gdb.execute('list $::line_number', to_string=True), end='')" \
+ false
+ check_source_listing_styling \
+ "python print(gdb.execute('list $::line_number', to_string=True, styling=True), end='')" \
+ true
+ foreach from_tty { True False } {
+ check_source_listing_styling \
+ "python print(gdb.execute('list $::line_number', $from_tty, True), end='')" \
+ false
+ check_source_listing_styling \
+ "python print(gdb.execute('list $::line_number', $from_tty, True, True), end='')" \
+ true
+ check_source_listing_styling \
+ "python print(gdb.execute('list $::line_number', $from_tty, True, False), end='')" \
+ false
+ }
+
+ # The same again, but this time the output is sent directly to
+ # stdout.
+ check_source_listing_styling \
+ "python gdb.execute('list $::line_number')" \
+ true
+ check_source_listing_styling \
+ "python gdb.execute('list $::line_number', to_string=False, styling=False)" \
+ false
+ check_source_listing_styling \
+ "python gdb.execute('list $::line_number', to_string=False, styling=True)" \
+ true
+ foreach from_tty { True False } {
+ check_source_listing_styling \
+ "python gdb.execute('list $::line_number', $from_tty, False, False)" \
+ false
+ check_source_listing_styling \
+ "python gdb.execute('list $::line_number', $from_tty, False, True)" \
+ true
+ check_source_listing_styling \
+ "python gdb.execute('list $::line_number', $from_tty, False)" \
+ true
+ }
+}
+
# We need an ANSI-capable terminal to get the output, additionally we
# need to set LC_ALL so GDB knows the terminal is UTF-8 capable,
# otherwise we'll get a UnicodeEncodeError trying to encode the
with_ansi_styling_terminal {
test_pygments_styling
test_gdb_execute_non_utf8_source
+ test_source_cache_style_tracking
}