From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id 9D4E9385B835 for ; Fri, 10 Apr 2020 07:31:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 9D4E9385B835 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tdevries@suse.de X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 1D5DCAF13; Fri, 10 Apr 2020 07:31:28 +0000 (UTC) Date: Fri, 10 Apr 2020 09:31:26 +0200 From: Tom de Vries To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [committed][gdb/cli] Don't let python colorize strip leading newlines Message-ID: <20200410073125.GA27408@delia> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-31.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 10 Apr 2020 07:31:31 -0000 Hi, 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. Committed to trunk. Thanks, - Tom [gdb/cli] Don't let python colorize strip leading newlines gdb/ChangeLog: 2020-04-10 Tom de Vries PR cli/25808 * python/lib/gdb/__init__.py: Initialize lexer with stripnl=False. gdb/testsuite/ChangeLog: 2020-04-10 Tom de Vries 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/python/lib/gdb/__init__.py | 2 +- gdb/testsuite/gdb.base/style.c | 4 ++++ gdb/testsuite/gdb.base/style.exp | 8 +++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py index a1aac00792..3dfb51b2af 100644 --- a/gdb/python/lib/gdb/__init__.py +++ b/gdb/python/lib/gdb/__init__.py @@ -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: diff --git a/gdb/testsuite/gdb.base/style.c b/gdb/testsuite/gdb.base/style.c index cb75b3b915..4e0e0dfcd7 100644 --- a/gdb/testsuite/gdb.base/style.c +++ b/gdb/testsuite/gdb.base/style.c @@ -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 diff --git a/gdb/testsuite/gdb.base/style.exp b/gdb/testsuite/gdb.base/style.exp index 47ef8c93c7..1071b023aa 100644 --- a/gdb/testsuite/gdb.base/style.exp +++ b/gdb/testsuite/gdb.base/style.exp @@ -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]