[gdb/cli] Don't let python colorize strip leading newlines
authorTom de Vries <tdevries@suse.de>
Fri, 10 Apr 2020 07:29:52 +0000 (09:29 +0200)
committerTom de Vries <tdevries@suse.de>
Fri, 10 Apr 2020 07:29:52 +0000 (09:29 +0200)
Consider the test-case gdb.base/async.exp.  Using the executable, I run to
main, and land on a line advertised as line 26:
...
$ gdb outputs/gdb.base/async/async -ex start
Reading symbols from outputs/gdb.base/async/async...
Temporary breakpoint 1 at 0x4004e4: file gdb.base/async.c, line 26.
Starting program: outputs/gdb.base/async/async

Temporary breakpoint 1, main () at gdb.base/async.c:26
26       y = foo ();
...

But actually, the line turns out to be line 28:
...
$ cat -n gdb.base/async.c
    ...
    26   y = 2;
    27   z = 9;
    28   y = foo ();
...

This is caused by the following: the python colorizer initializes the lexer
with default options (no second argument to get_lexer_for_filename):
...
    def colorize(filename, contents):
        # Don't want any errors.
        try:
            lexer = lexers.get_lexer_for_filename(filename)
            formatter = formatters.TerminalFormatter()
            return highlight(contents, lexer, formatter)
...
which include option stripnl=True, which strips leading and trailing newlines.

This causes the python colorizer to strip the two leading newlines of async.c.

Fix this by initializing the lexer with stripnl=False.

Build and reg-tested on x86_64-linux.

gdb/ChangeLog:

2020-04-10  Tom de Vries  <tdevries@suse.de>

PR cli/25808
* python/lib/gdb/__init__.py: Initialize lexer with stripnl=False.

gdb/testsuite/ChangeLog:

2020-04-10  Tom de Vries  <tdevries@suse.de>

PR cli/25808
* gdb.base/style.c: Add leading newlines.
* gdb.base/style.exp: Use gdb_get_line_number to get specific lines.
Check listing of main's one-line body.

gdb/ChangeLog
gdb/python/lib/gdb/__init__.py
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/style.c
gdb/testsuite/gdb.base/style.exp

index 050c9b264e4466b2708775028d4c484c3c2aa009..2c347fc473d723a4de0b2d015ba6dd7b15c6f0ed 100644 (file)
@@ -1,3 +1,8 @@
+2020-04-10  Tom de Vries  <tdevries@suse.de>
+
+       PR cli/25808
+       * python/lib/gdb/__init__.py: Initialize lexer with stripnl=False.
+
 2020-04-09  Simon Marchi  <simon.marchi@polymtl.ca>
 
        * MAINTAINERS (Global Maintainers): Add Tom de Vries.
index a1aac0079238949cd0470212b210747cb1115158..3dfb51b2af937381778c89b59d64447db3c65a4f 100644 (file)
@@ -216,7 +216,7 @@ try:
     def colorize(filename, contents):
         # Don't want any errors.
         try:
-            lexer = lexers.get_lexer_for_filename(filename)
+            lexer = lexers.get_lexer_for_filename(filename, stripnl=False)
             formatter = formatters.TerminalFormatter()
             return highlight(contents, lexer, formatter)
         except:
index 16b982bfc9c4d1507d5d9314516d98be66ea97c9..023f5a815c612f166698d285186cfd23554bf11f 100644 (file)
@@ -1,3 +1,10 @@
+2020-04-10  Tom de Vries  <tdevries@suse.de>
+
+       PR cli/25808
+       * gdb.base/style.c: Add leading newlines.
+       * gdb.base/style.exp: Use gdb_get_line_number to get specific lines.
+       Check listing of main's one-line body.
+
 2020-04-08  Tom de Vries  <tdevries@suse.de>
 
        * lib/gdb.exp (psymtabs_p): New proc.
index cb75b3b915ea8afa56139beb6766eff52a61177a..4e0e0dfcd7739de9134f06fbf4d788845d39cc69 100644 (file)
@@ -1,3 +1,7 @@
+
+
+/* The leading newlines here are intentional, do not remove.  They are used to
+   test that the source highlighter doesn't strip them.  */
 /* Copyright 2018-2020 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
index 47ef8c93c7c09dc7acb1f664038eafe99c7fae78..1071b023aa600348193aa7c882e381b3a71c117c 100644 (file)
@@ -39,6 +39,11 @@ save_vars { env(TERM) } {
        return
     }
 
+    # Check that the source highlighter has not stripped away the leading
+    # newlines.
+    set main_line [gdb_get_line_number "break here"]
+    gdb_test "list $main_line,$main_line" "return.*some_called_function.*"
+
     gdb_test_no_output "set style enabled on"
 
     set main_expr [style main function]
@@ -79,8 +84,9 @@ save_vars { env(TERM) } {
     }
 
     if {$test_macros} {
+       set macro_line [gdb_get_line_number "\#define SOME_MACRO"]
        gdb_test "info macro SOME_MACRO" \
-           "Defined at $base_file_expr:16\r\n#define SOME_MACRO 23"
+           "Defined at $base_file_expr:$macro_line\r\n#define SOME_MACRO 23"
     }
 
     set func [style some_called_function function]
This page took 0.036299 seconds and 4 git commands to generate.