Files
binutils-gdb/gdb/testsuite/gdb.python/py-source-styling-2.exp
Tom de Vries 93bb1ebf7f [gdb/cli] Use debug info language to pick pygments lexer
Consider the following scenario:
...
$ cat hello

int
main (void)
{
  printf ("hello\n");
  return 0;
}
$ gcc -x c hello -g
$ gdb -q -iex "maint set gnu-source-highlight enabled off" a.out
Reading symbols from a.out...
(gdb) start
Temporary breakpoint 1 at 0x4005db: file hello, line 6.
Starting program: /data/vries/gdb/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Temporary breakpoint 1, main () at hello:6
6	  printf ("hello\n");
...

This doesn't produce highlighting for line 6, because:
- pygments is used for highlighting instead of source-highlight, and
- pygments guesses the language for highlighting only based on the filename,
  which in this case doesn't give a clue.

Fix this by:
- adding a language parameter to the extension_language_ops.colorize interface,
- passing the language as found in the debug info, and
- using it in gdb.styling.colorize to pick the pygments lexer.

The new test-case gdb.python/py-source-styling-2.exp excercises a slightly
different scenario: it compiles a c++ file with a .c extension, and checks
that c++ highlighting is done instead of c highlighting.

Tested on x86_64-linux.

Approved-By: Tom Tromey <tom@tromey.com>

PR cli/30966
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30966
2025-04-07 22:40:04 +02:00

56 lines
1.5 KiB
Plaintext

# Copyright (C) 2025 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Compile a c++ file using a .c extension, and check that pygments uses c++
# highlighting instead of c highlighting.
require allow_python_tests
load_lib gdb-python.exp
standard_testfile py-source-styling-2.c
set line_number [gdb_get_line_number "List this line."]
set opts {}
lappend opts debug
lappend opts c++
if { [build_executable "failed to build" $testfile $srcfile $opts] == -1 } {
return
}
clean_restart
gdb_test_no_output "maint set gnu-source-highlight enabled off"
gdb_load $binfile
require {gdb_py_module_available pygments}
with_ansi_styling_terminal {
gdb_test_no_output "set style enabled on"
gdb_test_multiple "list $line_number" "Styling of c++ keyword try" {
-re -wrap " try\r\n.*" {
# Unstyled.
fail $gdb_test_name
}
-re -wrap "" {
pass $gdb_test_name
}
}
}