From 0141ab44c5591752b2cd235fb9644ed289a7e671 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Mon, 15 Oct 2018 22:16:59 +0000 Subject: [PATCH] diagnostics: add minimum width to left margin for line numbers This patch adds a minimum width to the left margin used for printing line numbers. I set the default to 6. Hence rather than: some-filename:9:1: some message 9 | some source text | ^~~~~~~~~~~~~~~~ some-filename:10:1: another message 10 | more source text | ^~~~~~~~~~~~~~~~ we now print: some-filename:9:42: some message 9 | some source text | ^~~~~~~~~~~~~~~~ some-filename:10:42: another message 10 | more source text | ^~~~~~~~~~~~~~~~ This implicitly fixes issues with margins failing to line up due to different lengths of the number when we haven't read the full file yet and so don't know the highest possible line number, for line numbers up to 99999. Doing so adds some whitespace on the left-hand side, for non-huge files, at least. I believe that this makes it easier to see where each diagnostic starts, by visually breaking things up at the leftmost column; my hope is to make it easier for the eye to see the different diagnostics as if they were different "paragraphs". gcc/ChangeLog: * common.opt (fdiagnostics-minimum-margin-width=): New option. * diagnostic-show-locus.c (layout::layout): Apply the minimum margin width. (layout::start_annotation_line): Only print up to 3 of the margin character, to avoid touching the left-hand side. (selftest::test_diagnostic_show_locus_fixit_lines): Update for minimum margin width, as set by test_diagnostic_context's ctor. (selftest::test_fixit_insert_containing_newline): Likewise. (selftest::test_fixit_insert_containing_newline_2): Likewise. (selftest::test_line_numbers_multiline_range): Clear dc.min_margin_width. * diagnostic.c (diagnostic_initialize): Initialize min_margin_width. * diagnostic.h (struct diagnostic_context): Add field "min_margin_width". * doc/invoke.texi: Add -fdiagnostics-minimum-margin-width=. * opts.c (common_handle_option): Handle OPT_fdiagnostics_minimum_margin_width_. * selftest-diagnostic.c (selftest::test_diagnostic_context::test_diagnostic_context): Initialize min_margin_width to 6. * toplev.c (general_init): Initialize global_dc->min_margin_width. gcc/testsuite/ChangeLog: * gcc.dg/missing-header-fixit-3.c: Update expected indentation to reflect minimum margin width. * gcc.dg/missing-header-fixit-4.c: Likewise. * gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c: Likewise. * gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c: Likewise. * gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers-2.c: New test. * gcc.dg/plugin/plugin.exp (plugin_test_list): Add it. From-SVN: r265178 --- gcc/ChangeLog | 25 ++++++++ gcc/common.opt | 4 ++ gcc/diagnostic-show-locus.c | 41 ++++++++----- gcc/diagnostic.c | 1 + gcc/diagnostic.h | 4 ++ gcc/doc/invoke.texi | 6 ++ gcc/opts.c | 4 ++ gcc/selftest-diagnostic.c | 1 + gcc/testsuite/ChangeLog | 13 +++++ gcc/testsuite/gcc.dg/missing-header-fixit-3.c | 8 +-- gcc/testsuite/gcc.dg/missing-header-fixit-4.c | 10 ++-- ...nostic-test-show-locus-bw-line-numbers-2.c | 22 +++++++ ...agnostic-test-show-locus-bw-line-numbers.c | 58 +++++++++---------- ...ostic-test-show-locus-color-line-numbers.c | 12 ++-- gcc/testsuite/gcc.dg/plugin/plugin.exp | 1 + gcc/toplev.c | 2 + 16 files changed, 152 insertions(+), 60 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers-2.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 82f3f02dabc..789e43b2388 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,28 @@ +2018-10-15 David Malcolm + + * common.opt (fdiagnostics-minimum-margin-width=): New option. + * diagnostic-show-locus.c (layout::layout): Apply the minimum + margin width. + (layout::start_annotation_line): Only print up to 3 of the + margin character, to avoid touching the left-hand side. + (selftest::test_diagnostic_show_locus_fixit_lines): Update for + minimum margin width, as set by test_diagnostic_context's ctor. + (selftest::test_fixit_insert_containing_newline): Likewise. + (selftest::test_fixit_insert_containing_newline_2): Likewise. + (selftest::test_line_numbers_multiline_range): Clear + dc.min_margin_width. + * diagnostic.c (diagnostic_initialize): Initialize + min_margin_width. + * diagnostic.h (struct diagnostic_context): Add field + "min_margin_width". + * doc/invoke.texi: Add -fdiagnostics-minimum-margin-width=. + * opts.c (common_handle_option): Handle + OPT_fdiagnostics_minimum_margin_width_. + * selftest-diagnostic.c + (selftest::test_diagnostic_context::test_diagnostic_context): + Initialize min_margin_width to 6. + * toplev.c (general_init): Initialize global_dc->min_margin_width. + 2018-10-15 David Malcolm * gcc-rich-location.h (gcc_rich_location::add_location_if_nearby): diff --git a/gcc/common.opt b/gcc/common.opt index 53aac194899..2971dc21b1f 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -1281,6 +1281,10 @@ fdiagnostics-show-option Common Var(flag_diagnostics_show_option) Init(1) Amend appropriate diagnostic messages with the command line option that controls them. +fdiagnostics-minimum-margin-width= +Common Joined UInteger Var(diagnostics_minimum_margin_width) Init(6) +Set minimum width of left margin of source code when showing source + fdisable- Common Joined RejectNegative Var(common_deferred_options) Defer -fdisable-[tree|rtl|ipa]-=range1+range2 disables an optimization pass. diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c index 43a49ea8913..a42ff819512 100644 --- a/gcc/diagnostic-show-locus.c +++ b/gcc/diagnostic-show-locus.c @@ -930,6 +930,9 @@ layout::layout (diagnostic_context * context, /* 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); + /* If there's a minimum margin width, apply it (subtracting 1 for the space + after the line number. */ + m_linenum_width = MAX (m_linenum_width, context->min_margin_width - 1); /* Adjust m_x_offset. Center the primary caret to fit in max_width; all columns @@ -1386,7 +1389,12 @@ layout::start_annotation_line (char margin_char) const { if (m_show_line_numbers_p) { - for (int i = 0; i < m_linenum_width; i++) + /* Print the margin. If MARGIN_CHAR != ' ', then print up to 3 + of it, right-aligned, padded with spaces. */ + int i; + for (i = 0; i < m_linenum_width - 3; i++) + pp_space (m_pp); + for (; i < m_linenum_width; i++) pp_character (m_pp, margin_char); pp_string (m_pp, " |"); } @@ -3027,12 +3035,12 @@ test_diagnostic_show_locus_fixit_lines (const line_table_case &case_) dc.show_line_numbers_p = true; diagnostic_show_locus (&dc, &richloc, DK_ERROR); ASSERT_STREQ ("\n" - " 3 | y\n" - " | .\n" - "....\n" - " 6 | : 0.0};\n" - " | ^\n" - " | =\n", + " 3 | y\n" + " | .\n" + "......\n" + " 6 | : 0.0};\n" + " | ^\n" + " | =\n", pp_formatted_text (dc.printer)); } } @@ -3523,10 +3531,10 @@ test_fixit_insert_containing_newline (const line_table_case &case_) dc.show_line_numbers_p = true; diagnostic_show_locus (&dc, &richloc, DK_ERROR); ASSERT_STREQ ("\n" - "2 | x = a;\n" - "+ |+ break;\n" - "3 | case 'b':\n" - " | ^~~~~~~~~\n", + " 2 | x = a;\n" + " +++ |+ break;\n" + " 3 | case 'b':\n" + " | ^~~~~~~~~\n", pp_formatted_text (dc.printer)); } } @@ -3605,11 +3613,11 @@ test_fixit_insert_containing_newline_2 (const line_table_case &case_) dc.show_line_numbers_p = true; diagnostic_show_locus (&dc, &richloc, DK_ERROR); ASSERT_STREQ ("\n" - "+ |+#include \n" - "1 | test (int ch)\n" - "2 | {\n" - "3 | putchar (ch);\n" - " | ^~~~~~~\n", + " +++ |+#include \n" + " 1 | test (int ch)\n" + " 2 | {\n" + " 3 | putchar (ch);\n" + " | ^~~~~~~\n", pp_formatted_text (dc.printer)); } } @@ -3734,6 +3742,7 @@ test_line_numbers_multiline_range () 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 ("\n" diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c index 8575065069b..a572c084aac 100644 --- a/gcc/diagnostic.c +++ b/gcc/diagnostic.c @@ -177,6 +177,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->min_margin_width = 0; context->show_ruler_p = false; context->parseable_fixits_p = false; context->edit_context_ptr = NULL; diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h index d4c605ea8f3..3498a9ba7bb 100644 --- a/gcc/diagnostic.h +++ b/gcc/diagnostic.h @@ -211,6 +211,10 @@ struct diagnostic_context showing line numbers? */ bool show_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; + /* Usable by plugins; if true, print a debugging ruler above the source output. */ bool show_ruler_p; diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 802cc642453..06a00a29de7 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -270,6 +270,7 @@ Objective-C and Objective-C++ Dialects}. -fdiagnostics-color=@r{[}auto@r{|}never@r{|}always@r{]} @gol -fno-diagnostics-show-option -fno-diagnostics-show-caret @gol -fno-diagnostics-show-labels -fno-diagnostics-show-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 -fno-show-column} @@ -3819,6 +3820,11 @@ 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-minimum-margin-width=@var{width} +@opindex -fdiagnostics-minimum-margin-width +This option controls the minimum width of the left margin printed by +@option{-fdiagnostics-show-line-numbers}. It defaults to 6. + @item -fdiagnostics-parseable-fixits @opindex fdiagnostics-parseable-fixits Emit fix-it hints in a machine-parseable format, suitable for consumption diff --git a/gcc/opts.c b/gcc/opts.c index dc12c2ecefd..3b61e17a7ab 100644 --- a/gcc/opts.c +++ b/gcc/opts.c @@ -2228,6 +2228,10 @@ common_handle_option (struct gcc_options *opts, dc->show_option_requested = value; break; + case OPT_fdiagnostics_minimum_margin_width_: + dc->min_margin_width = value; + break; + case OPT_fdump_: /* Deferred. */ break; diff --git a/gcc/selftest-diagnostic.c b/gcc/selftest-diagnostic.c index f3c255e3346..4a7f0dec4c9 100644 --- a/gcc/selftest-diagnostic.c +++ b/gcc/selftest-diagnostic.c @@ -40,6 +40,7 @@ test_diagnostic_context::test_diagnostic_context () show_labels_p = true; show_column = true; start_span = start_span_cb; + min_margin_width = 6; } test_diagnostic_context::~test_diagnostic_context () diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 46085c0cecf..37a9ed3f5b7 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,16 @@ +2018-10-15 David Malcolm + + * gcc.dg/missing-header-fixit-3.c: Update expected indentation + to reflect minimum margin width. + * gcc.dg/missing-header-fixit-4.c: Likewise. + * gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c: + Likewise. + * gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c: + Likewise. + * gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers-2.c: + New test. + * gcc.dg/plugin/plugin.exp (plugin_test_list): Add it. + 2018-10-15 Tobias Burnus PR fortran/87597 diff --git a/gcc/testsuite/gcc.dg/missing-header-fixit-3.c b/gcc/testsuite/gcc.dg/missing-header-fixit-3.c index a692b4d21b3..dd53bf65d3c 100644 --- a/gcc/testsuite/gcc.dg/missing-header-fixit-3.c +++ b/gcc/testsuite/gcc.dg/missing-header-fixit-3.c @@ -10,12 +10,12 @@ void test (int i, int j) /* { dg-message "include '' or provide a declaration of 'printf'" "" { target *-*-* } 1 } */ #if 0 /* { dg-begin-multiline-output "" } -9 | printf ("%i of %i\n", i, j); - | ^~~~~~ + 9 | printf ("%i of %i\n", i, j); + | ^~~~~~ { dg-end-multiline-output "" } */ /* { dg-begin-multiline-output "" } -+ |+#include -1 | /* Example of a fix-it hint that adds a #include directive, + +++ |+#include + 1 | /* Example of a fix-it hint that adds a #include directive, { dg-end-multiline-output "" } */ #endif } diff --git a/gcc/testsuite/gcc.dg/missing-header-fixit-4.c b/gcc/testsuite/gcc.dg/missing-header-fixit-4.c index 0ed3e2c2922..942897d8c79 100644 --- a/gcc/testsuite/gcc.dg/missing-header-fixit-4.c +++ b/gcc/testsuite/gcc.dg/missing-header-fixit-4.c @@ -11,13 +11,13 @@ void test (int i, int j) /* { dg-warning "implicit declaration of function" "" { target *-*-* } printf } */ /* { dg-warning "incompatible implicit declaration" "" { target *-*-* } printf } */ /* { dg-begin-multiline-output "" } -10 | printf ("%i of %i\n", i, j); - | ^~~~~~ + 10 | printf ("%i of %i\n", i, j); + | ^~~~~~ { dg-end-multiline-output "" } */ /* { dg-message "include '' or provide a declaration of 'printf'" "" { target *-*-* } 4 } */ /* { dg-begin-multiline-output "" } -3 | #include "empty.h" -+ |+#include -4 | int the_next_line; + 3 | #include "empty.h" + +++ |+#include + 4 | int the_next_line; { dg-end-multiline-output "" } */ } diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers-2.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers-2.c new file mode 100644 index 00000000000..a9d022a250a --- /dev/null +++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers-2.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* Verify "-fdiagnostics-minimum-margin-width=0". */ +/* { dg-options "-O -fdiagnostics-show-caret -fdiagnostics-show-line-numbers -fdiagnostics-minimum-margin-width=0" } */ + +/* This is a collection of unittests for diagnostic_show_locus; + see the overview in diagnostic_plugin_test_show_locus.c. + + In particular, note the discussion of why we need a very long line here: +01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 + and that we can't use macros in this file. */ + +void test_simple (void) +{ +#if 0 + myvar = myvar.x; /* { dg-warning "test" } */ + +/* { dg-begin-multiline-output "" } +15 | myvar = myvar.x; + | ~~~~~^~ + { dg-end-multiline-output "" } */ +#endif +} diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c index 63e585528fa..1e8f73bdb5d 100644 --- a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c +++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c @@ -14,8 +14,8 @@ void test_simple (void) myvar = myvar.x; /* { dg-warning "test" } */ /* { dg-begin-multiline-output "" } -14 | myvar = myvar.x; - | ~~~~~^~ + 14 | myvar = myvar.x; + | ~~~~~^~ { dg-end-multiline-output "" } */ #endif } @@ -27,12 +27,12 @@ void test_multiline (void) + second_function ()); /* { dg-warning "test" } */ /* { dg-begin-multiline-output "" } -26 | x = (first_function () - | ~~~~~~~~~~~~~~~~~ -27 | + second_function ()); - | ^ ~~~~~~~~~~~~~~~~~~ - | | - | label + 26 | x = (first_function () + | ~~~~~~~~~~~~~~~~~ + 27 | + second_function ()); + | ^ ~~~~~~~~~~~~~~~~~~ + | | + | label { dg-end-multiline-output "" } */ #endif } @@ -42,14 +42,14 @@ void test_very_wide_line (void) #if 0 float f = foo * bar; /* { dg-warning "95: test" } */ /* { dg-begin-multiline-output "" } - | 0 0 0 0 0 0 1 - | 4 5 6 7 8 9 0 - | 0123456789012345678901234567890123456789012345678901234567890123456789 -43 | float f = foo * bar; - | ~~~~^~~~~ - | | - | label - | bar * foo + | 0 0 0 0 0 1 1 + | 5 6 7 8 9 0 1 + | 3456789012345678901234567890123456789012345678901234567890123456789012 + 43 | float f = foo * bar; + | ~~~~^~~~~ + | | + | label 0 + | bar * foo { dg-end-multiline-output "" } */ #endif } @@ -62,9 +62,9 @@ void test_fixit_insert (void) #if 0 int a[2][2] = { 0, 1 , 2, 3 }; /* { dg-warning "insertion hints" } */ /* { dg-begin-multiline-output "" } -63 | int a[2][2] = { 0, 1 , 2, 3 }; - | ^~~~ - | { } + 63 | int a[2][2] = { 0, 1 , 2, 3 }; + | ^~~~ + | { } { dg-end-multiline-output "" } */ #endif } @@ -76,9 +76,9 @@ void test_fixit_remove (void) #if 0 int a;; /* { dg-warning "example of a removal hint" } */ /* { dg-begin-multiline-output "" } -77 | int a;; - | ^ - | - + 77 | int a;; + | ^ + | - { dg-end-multiline-output "" } */ #endif } @@ -90,9 +90,9 @@ void test_fixit_replace (void) #if 0 gtk_widget_showall (dlg); /* { dg-warning "example of a replacement hint" } */ /* { dg-begin-multiline-output "" } -91 | gtk_widget_showall (dlg); - | ^~~~~~~~~~~~~~~~~~ - | gtk_widget_show_all + 91 | gtk_widget_showall (dlg); + | ^~~~~~~~~~~~~~~~~~ + | gtk_widget_show_all { dg-end-multiline-output "" } */ #endif } @@ -111,10 +111,10 @@ void test_fixit_insert_newline (void) x = b; } /* { dg-begin-multiline-output "" } -109 | x = a; -+++ |+ break; -110 | case 'b': - | ^~~~~~~~ + 109 | x = a; + +++ |+ break; + 110 | case 'b': + | ^~~~~~~~ { dg-end-multiline-output "" } */ #endif } diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c index 0453c52b2cc..bb83862d550 100644 --- a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c +++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c @@ -15,12 +15,12 @@ void test_multiline (void) + second_function ()); /* { dg-warning "test" } */ /* { dg-begin-multiline-output "" } -14 | x = (first_function () - | ~~~~~~~~~~~~~~~~~ -15 | + second_function ()); - | ^ ~~~~~~~~~~~~~~~~~~ - | | - | label + 14 | x = (first_function () + | ~~~~~~~~~~~~~~~~~ + 15 | + second_function ()); + | ^ ~~~~~~~~~~~~~~~~~~ + | | + | label { dg-end-multiline-output "" } */ #endif } diff --git a/gcc/testsuite/gcc.dg/plugin/plugin.exp b/gcc/testsuite/gcc.dg/plugin/plugin.exp index 1d06c04562e..d92ede79c0d 100644 --- a/gcc/testsuite/gcc.dg/plugin/plugin.exp +++ b/gcc/testsuite/gcc.dg/plugin/plugin.exp @@ -76,6 +76,7 @@ set plugin_test_list [list \ diagnostic-test-show-locus-color.c \ diagnostic-test-show-locus-no-labels.c \ diagnostic-test-show-locus-bw-line-numbers.c \ + diagnostic-test-show-locus-bw-line-numbers-2.c \ diagnostic-test-show-locus-color-line-numbers.c \ diagnostic-test-show-locus-parseable-fixits.c \ diagnostic-test-show-locus-generate-patch.c }\ diff --git a/gcc/toplev.c b/gcc/toplev.c index 9fb83d4e43f..d7ea11abf53 100644 --- a/gcc/toplev.c +++ b/gcc/toplev.c @@ -1120,6 +1120,8 @@ general_init (const char *argv0, bool init_signals) = global_options_init.x_flag_diagnostics_show_line_numbers; global_dc->show_option_requested = global_options_init.x_flag_diagnostics_show_option; + global_dc->min_margin_width + = global_options_init.x_diagnostics_minimum_margin_width; global_dc->show_column = global_options_init.x_flag_show_column; global_dc->internal_error = internal_error_function; -- 2.30.2