From patchwork Fri Dec 13 18:10:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1209338 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-515924-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="KXa5H+wb"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="I6BNNmC3"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47ZJgv3xwqz9sPh for ; Sat, 14 Dec 2019 05:13:31 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; q=dns; s= default; b=jCWNfIiW3NbNq8W68v61vNSHnveOlIYWY79K9eU8GCt1UdvCfT04y 0F8MExodKlU4CdByRqgSyk+KVxExr7uQuwC6puoec08jR5skRA1BvLpA3Lm5S0fA Xz12ovHvsClmgEbhDd6sB8wyeY/aq9zppbn5nriO4MgDJLNc/azENQ= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; s=default; bh=3Yt4+FFOhdv8A1nrV723Usx5Um8=; b=KXa5H+wbYnBdHerq90maTGMF9uZt cI3IgqXIxqg6w5ZKICvpentZQOZ39osju8x4PflfmhCVxV0mYHL7ymtklI6HSjdg wH/8iDeS7ShGfw7C41NoYSKmPuZ/NqvZZQPqdc9bs1GyRbP54EuAKjXhXrZ4zWp7 ktxY3C1mNgyj/eE= Received: (qmail 100014 invoked by alias); 13 Dec 2019 18:11:50 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 99879 invoked by uid 89); 13 Dec 2019 18:11:49 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_PASS autolearn=ham version=3.3.1 spammy=dwarf2out.c, dwarf2outc, UD:dwarf2out.c X-HELO: us-smtp-delivery-1.mimecast.com Received: from us-smtp-2.mimecast.com (HELO us-smtp-delivery-1.mimecast.com) (207.211.31.81) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 13 Dec 2019 18:11:46 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1576260704; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ai5+c3Wnkyyr8y3QFo0nG2XttxOuo1bjTKjNH5pnbiY=; b=I6BNNmC3H9pJIndr1Wf7ZmD778Lv8APnCGXTSkCybaYp9ldYq4L7eXZfTL2GG/sjfSG72e Tanira4Bi1nzZtJk3iUidRN58HLV0XdDjxG0MXawC6EgYg6hVMVo0paoKaOdYF9pyKOSGE LlvlJTRsbFOJy+bqflswGtUpV/fhMrU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-237-giElk0XgOP60Jk78itSV8Q-1; Fri, 13 Dec 2019 13:11:43 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 48F84DBE5 for ; Fri, 13 Dec 2019 18:11:42 +0000 (UTC) Received: from t470.redhat.com (ovpn-117-164.phx2.redhat.com [10.3.117.164]) by smtp.corp.redhat.com (Postfix) with ESMTP id D9A525C241; Fri, 13 Dec 2019 18:11:41 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 08/45] Add -fdiagnostics-nn-line-numbers Date: Fri, 13 Dec 2019 13:10:57 -0500 Message-Id: <20191213181134.1830-9-dmalcolm@redhat.com> In-Reply-To: <20191213181134.1830-1-dmalcolm@redhat.com> References: <20191213181134.1830-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-IsSubscribed: yes This patch implements -fdiagnostics-nn-line-numbers, a new option which makes diagnostic_show_locus print "NN" rather than specific line numbers when printing the left margin. This is intended purely to make it easier to write certain kinds of DejaGnu test; various integration tests for diagnostic paths later in the patch kit make use of it. gcc/ChangeLog: * common.opt (fdiagnostics-nn-line-numbers): New option. * diagnostic-show-locus.c (layout::m_use_nn_for_line_numbers_p): New field. (layout::layout): Initialize it. (layout::calculate_linenum_width): Use it when computing m_linenum_width. (layout::print_source_line): Implement printing "NN" rather than the line number. (selftest::test_line_numbers_multiline_range): Add a test of "NN" printing. * diagnostic.c (diagnostic_initialize): Initialize use_nn_for_line_numbers_p. (num_digits): Add "use_nn_p" param. (selftest::test_num_digits): Add a test for use_nn_p==true. * diagnostic.h (diagnostic_context::use_nn_for_line_numbers_p): New field. (num_digits): Add optional "use_nn_p" param. * doc/invoke.texi (-fdiagnostics-nn-line-numbers): New option. * dwarf2out.c (gen_producer_string): Ignore OPT_fdiagnostics_nn_line_numbers. * lto-wrapper.c (merge_and_complain): Handle OPT_fdiagnostics_nn_line_numbers. (append_compiler_options): Likewise. (append_diag_options): Likewise. * opts.c (common_handle_option): Likewise. * toplev.c (general_init): Initialize global_dc->use_nn_for_line_numbers_p. --- gcc/common.opt | 4 +++ gcc/diagnostic-show-locus.c | 51 ++++++++++++++++++++++++++----------- gcc/diagnostic.c | 13 ++++++++-- gcc/diagnostic.h | 6 ++++- gcc/doc/invoke.texi | 7 +++++ gcc/dwarf2out.c | 1 + gcc/lto-wrapper.c | 3 +++ gcc/opts.c | 4 +++ gcc/toplev.c | 2 ++ 9 files changed, 73 insertions(+), 18 deletions(-) diff --git a/gcc/common.opt b/gcc/common.opt index b4dc31c7490d..d5727c300c07 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -1261,6 +1261,10 @@ fdiagnostics-show-line-numbers Common Var(flag_diagnostics_show_line_numbers) Init(1) Show line numbers in the left margin when showing source. +fdiagnostics-nn-line-numbers +Common Var(flag_diagnostics_nn_line_numbers) Init(0) +Replace line numbers with 'NN' when showing source. + fdiagnostics-color Common Alias(fdiagnostics-color=,always,never) ; diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c index c87603caf419..49832fd0ea03 100644 --- a/gcc/diagnostic-show-locus.c +++ b/gcc/diagnostic-show-locus.c @@ -385,6 +385,7 @@ class layout bool m_colorize_source_p; bool m_show_labels_p; bool m_show_line_numbers_p; + bool m_use_nn_for_line_numbers_p; auto_vec m_layout_ranges; auto_vec m_fixit_hints; auto_vec m_line_spans; @@ -958,6 +959,7 @@ layout::layout (diagnostic_context * context, m_colorize_source_p (context->colorize_source_p), m_show_labels_p (context->show_labels_p), m_show_line_numbers_p (context->show_line_numbers_p), + m_use_nn_for_line_numbers_p (context->use_nn_for_line_numbers_p), m_layout_ranges (richloc->get_num_locations ()), m_fixit_hints (richloc->get_num_fixit_hints ()), m_line_spans (1 + richloc->get_num_locations ()), @@ -1343,7 +1345,7 @@ layout::calculate_linenum_width () int highest_line = last_span->m_last_line; if (highest_line < 0) highest_line = 0; - m_linenum_width = num_digits (highest_line); + m_linenum_width = num_digits (highest_line, m_use_nn_for_line_numbers_p); /* If we're showing jumps in the line-numbering, allow at least 3 chars. */ if (m_line_spans.length () > 1) m_linenum_width = MAX (m_linenum_width, 3); @@ -1449,10 +1451,13 @@ layout::print_source_line (linenum_type row, const char *line, int line_bytes, pp_emit_prefix (m_pp); if (m_show_line_numbers_p) { - int width = num_digits (row); + int width = num_digits (row, m_use_nn_for_line_numbers_p); for (int i = 0; i < m_linenum_width - width; i++) pp_space (m_pp); - pp_printf (m_pp, "%i | ", row); + if (m_use_nn_for_line_numbers_p) + pp_printf (m_pp, "%s | ", "NN"); + else + pp_printf (m_pp, "%i | ", row); } else pp_space (m_pp); @@ -4968,18 +4973,34 @@ test_line_numbers_multiline_range () = linemap_position_for_line_and_column (line_table, ord_map, 11, 4); location_t loc = make_location (caret, start, finish); - test_diagnostic_context dc; - dc.show_line_numbers_p = true; - dc.min_margin_width = 0; - gcc_rich_location richloc (loc); - diagnostic_show_locus (&dc, &richloc, DK_ERROR); - ASSERT_STREQ (" 9 | this is line 9\n" - " | ~~~~~~\n" - "10 | this is line 10\n" - " | ~~~~~^~~~~~~~~~\n" - "11 | this is line 11\n" - " | ~~~~ \n", - pp_formatted_text (dc.printer)); + { + test_diagnostic_context dc; + dc.show_line_numbers_p = true; + dc.min_margin_width = 0; + gcc_rich_location richloc (loc); + diagnostic_show_locus (&dc, &richloc, DK_ERROR); + ASSERT_STREQ (" 9 | this is line 9\n" + " | ~~~~~~\n" + "10 | this is line 10\n" + " | ~~~~~^~~~~~~~~~\n" + "11 | this is line 11\n" + " | ~~~~ \n", + pp_formatted_text (dc.printer)); + } + + /* Verify that obscuring line numbers via "NN" works (and always uses + at least two columns). */ + { + test_diagnostic_context dc; + dc.show_line_numbers_p = true; + dc.use_nn_for_line_numbers_p = true; + dc.min_margin_width = 0; + gcc_rich_location richloc (start); + diagnostic_show_locus (&dc, &richloc, DK_ERROR); + ASSERT_STREQ ("NN | this is line 9\n" + " | ^\n", + pp_formatted_text (dc.printer)); + } } /* Run all of the selftests within this file. */ diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c index d6604e6fd41e..81e0a100169b 100644 --- a/gcc/diagnostic.c +++ b/gcc/diagnostic.c @@ -209,6 +209,7 @@ diagnostic_initialize (diagnostic_context *context, int n_opts) context->colorize_source_p = false; context->show_labels_p = false; context->show_line_numbers_p = false; + context->use_nn_for_line_numbers_p = false; context->min_margin_width = 0; context->show_ruler_p = false; context->parseable_fixits_p = false; @@ -1078,15 +1079,21 @@ diagnostic_report_diagnostic (diagnostic_context *context, return true; } -/* Get the number of digits in the decimal representation of VALUE. */ +/* Get the number of digits in the decimal representation of VALUE. + + If USE_NN_P is true, return 2 (for the case where all numbers are to + be printed as just "NN"). */ int -num_digits (int value) +num_digits (int value, bool use_nn_p) { /* Perhaps simpler to use log10 for this, but doing it this way avoids using floating point. */ gcc_assert (value >= 0); + if (use_nn_p) + return 2; + if (value == 0) return 1; @@ -1885,6 +1892,8 @@ test_num_digits () ASSERT_EQ (7, num_digits (9999999)); ASSERT_EQ (8, num_digits (10000000)); ASSERT_EQ (8, num_digits (99999999)); + + ASSERT_EQ (2, num_digits (1000, true)); } /* Run all of the selftests within this file. */ diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h index 91e4c5096051..2fb75e6ffca6 100644 --- a/gcc/diagnostic.h +++ b/gcc/diagnostic.h @@ -233,6 +233,10 @@ struct diagnostic_context showing line numbers? */ bool show_line_numbers_p; + /* When printing line numbers, should the actual numbers be replaced with + "NN"? (for ease of DejaGnu testing) */ + bool use_nn_for_line_numbers_p; + /* If printing source code, what should the minimum width of the margin be? Line numbers will be right-aligned, and padded to this width. */ int min_margin_width; @@ -432,6 +436,6 @@ extern void diagnostic_output_format_init (diagnostic_context *, enum diagnostics_output_format); /* Compute the number of digits in the decimal representation of an integer. */ -extern int num_digits (int); +extern int num_digits (int, bool use_nn_p = false); #endif /* ! GCC_DIAGNOSTIC_H */ diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 19e0631f6ea5..6e8552de73f2 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -278,6 +278,7 @@ Objective-C and Objective-C++ Dialects}. -fdiagnostics-format=@r{[}text@r{|}json@r{]} @gol -fno-diagnostics-show-option -fno-diagnostics-show-caret @gol -fno-diagnostics-show-labels -fno-diagnostics-show-line-numbers @gol +-fdiagnostics-nn-line-numbers @gol -fdiagnostics-minimum-margin-width=@var{width} @gol -fdiagnostics-parseable-fixits -fdiagnostics-generate-patch @gol -fdiagnostics-show-template-tree -fno-elide-type @gol @@ -4028,6 +4029,12 @@ By default, when printing source code (via @option{-fdiagnostics-show-caret}), a left margin is printed, showing line numbers. This option suppresses this left margin. +@item -fdiagnostics-nn-line-numbers +@opindex fdiagnostics-nn-line-numbers +When printing source code in diagnostics, replace line numbers ``NN''. +This option is intended for GCC developers, to make it easier to write +certain kinds of automated test. + @item -fdiagnostics-minimum-margin-width=@var{width} @opindex fdiagnostics-minimum-margin-width This option controls the minimum width of the left margin printed by diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c index ff55e3969b17..9e5ceb8a50ed 100644 --- a/gcc/dwarf2out.c +++ b/gcc/dwarf2out.c @@ -24500,6 +24500,7 @@ gen_producer_string (void) case OPT_fdiagnostics_show_caret: case OPT_fdiagnostics_show_labels: case OPT_fdiagnostics_show_line_numbers: + case OPT_fdiagnostics_nn_line_numbers: case OPT_fdiagnostics_color_: case OPT_fdiagnostics_format_: case OPT_fverbose_asm: diff --git a/gcc/lto-wrapper.c b/gcc/lto-wrapper.c index 9a7bbd0c0222..aabfe6d4e91a 100644 --- a/gcc/lto-wrapper.c +++ b/gcc/lto-wrapper.c @@ -260,6 +260,7 @@ merge_and_complain (struct cl_decoded_option **decoded_options, case OPT_fdiagnostics_show_caret: case OPT_fdiagnostics_show_labels: case OPT_fdiagnostics_show_line_numbers: + case OPT_fdiagnostics_nn_line_numbers: case OPT_fdiagnostics_show_option: case OPT_fdiagnostics_show_location_: case OPT_fshow_column: @@ -605,6 +606,7 @@ append_compiler_options (obstack *argv_obstack, struct cl_decoded_option *opts, case OPT_fdiagnostics_show_caret: case OPT_fdiagnostics_show_labels: case OPT_fdiagnostics_show_line_numbers: + case OPT_fdiagnostics_nn_line_numbers: case OPT_fdiagnostics_show_option: case OPT_fdiagnostics_show_location_: case OPT_fshow_column: @@ -653,6 +655,7 @@ append_diag_options (obstack *argv_obstack, struct cl_decoded_option *opts, case OPT_fdiagnostics_format_: case OPT_fdiagnostics_show_caret: case OPT_fdiagnostics_show_labels: + case OPT_fdiagnostics_nn_line_numbers: case OPT_fdiagnostics_show_line_numbers: case OPT_fdiagnostics_show_option: case OPT_fdiagnostics_show_location_: diff --git a/gcc/opts.c b/gcc/opts.c index df6cc6495db1..de7887274938 100644 --- a/gcc/opts.c +++ b/gcc/opts.c @@ -2390,6 +2390,10 @@ common_handle_option (struct gcc_options *opts, dc->show_line_numbers_p = value; break; + case OPT_fdiagnostics_nn_line_numbers: + dc->use_nn_for_line_numbers_p = value; + break; + case OPT_fdiagnostics_color_: diagnostic_color_init (dc, value); break; diff --git a/gcc/toplev.c b/gcc/toplev.c index 059046f40f38..b5fba7762e34 100644 --- a/gcc/toplev.c +++ b/gcc/toplev.c @@ -1179,6 +1179,8 @@ general_init (const char *argv0, bool init_signals) = global_options_init.x_flag_diagnostics_show_labels; global_dc->show_line_numbers_p = global_options_init.x_flag_diagnostics_show_line_numbers; + global_dc->use_nn_for_line_numbers_p + = global_options_init.x_flag_diagnostics_nn_line_numbers; global_dc->show_option_requested = global_options_init.x_flag_diagnostics_show_option; global_dc->min_margin_width