Commit 56b61d7f by David Malcolm Committed by David Malcolm

diagnostics: add line numbers to source (PR other/84889)

This patch adds a left margin to the lines of source (and annotations)
printed by diagnostic_show_locus, so that e.g. rather than:

test.c: In function 'test':
test.c:12:15: error: 'struct foo' has no member named 'm_bar'; did you mean 'bar'?
   return ptr->m_bar;
               ^~~~~
               bar

we print:

test.c: In function 'test':
test.c:12:15: error: 'struct foo' has no member named 'm_bar'; did you mean 'bar'?
12 |   return ptr->m_bar;
   |               ^~~~~
   |               bar

Similarly, for a multiline case (in C++ this time), this:

bad-binary-ops.C: In function 'int test_2()':
bad-binary-ops.C:26:4: error: no match for 'operator+' (operand types are 's' and 't')
   return (some_function ()
           ~~~~~~~~~~~~~~~~
    + some_other_function ());
    ^~~~~~~~~~~~~~~~~~~~~~~~

becomes:

bad-binary-ops.C: In function 'int test_2()':
bad-binary-ops.C:26:4: error: no match for 'operator+' (operand types are 's' and 't')
25 |   return (some_function ()
   |           ~~~~~~~~~~~~~~~~
26 |    + some_other_function ());
   |    ^~~~~~~~~~~~~~~~~~~~~~~~

I believe this slightly improves the readability of the output, in that it:
- distinguishes between the user's source code vs the annotation lines
  that we're adding (the underlinings and fix-it hints here)
- shows the line numbers in another place (potentially helpful for
  multiline diagnostics, where the user can see the line numbers directly,
  rather than have to figure them out relative to the caret: in the 2nd
  example, note how the diagnostic is reported at line 26, but the first
  line printed is actually line 25)

I'm not sure that this is the precise format we want to go with [1], but
I think it's an improvement over the status quo, and we're in stage 1
of gcc 9, so there's plenty of time to shake out issues.

I've turned it on by default; it can be disabled via
-fno-diagnostics-show-line-numbers (it's also turned off in the testsuite, to
avoid breaking numerous existing test cases).

[1] Some possible variants:
  - maybe just "LL|" rather than "LL | "
  - maybe ':' rather than '|'
  - maybe we should have some leading indentation, to better split up
    the diagnostics visually via the left-hand column
  - etc

gcc/ChangeLog:
	PR other/84889
	* common.opt (fdiagnostics-show-line-numbers): New option.
	* diagnostic-show-locus.c (class layout): Add fields
	"m_show_line_numbers_p" and "m_linenum_width";
	(num_digits): New function.
	(test_num_digits): New function.
	(layout::layout): Initialize new fields.  Update m_x_offset
	logic to handle any left margin.
	(layout::print_source_line): Print line number when requested.
	(layout::start_annotation_line): New member function.
	(layout::print_annotation_line): Call it.
	(layout::print_leading_fixits): Likewise.
	(layout::print_trailing_fixits): Likewise.  Update calls to
	move_to_column for new parameter.
	(layout::get_x_bound_for_row): Add "add_left_margin" param and use
	it to potentially call start_annotation_line.
	(layout::show_ruler): Call start_annotation_line.
	(selftest::test_line_numbers_multiline_range): New selftest.
	(selftest::diagnostic_show_locus_c_tests): Call test_num_digits
	and selftest::test_line_numbers_multiline_range.
	* diagnostic.c (diagnostic_initialize): Initialize
	show_line_numbers_p.
	* diagnostic.h (struct diagnostic_context): Add field
	"show_line_numbers_p".
	* doc/invoke.texi (Diagnostic Message Formatting Options): Add
	-fno-diagnostics-show-line-numbers.
	* dwarf2out.c (gen_producer_string): Add
	OPT_fdiagnostics_show_line_numbers to the ignored options.
	* lto-wrapper.c (merge_and_complain): Likewise to the "pick
	one setting" options.
	(append_compiler_options): Likewise to the dropped options.
	(append_diag_options): Likewise to the passed-on options.
	* opts.c (common_handle_option): Handle the new option.
	* toplev.c (general_init): Set up global_dc->show_line_numbers_p.

gcc/testsuite/ChangeLog:
	PR other/84889
	* gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c: New
	test.
	* gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c:
	New test.
	* gcc.dg/plugin/plugin.exp (plugin_test_list): Add the new tests.
	* lib/prune.exp: Add -fno-diagnostics-show-line-numbers to
	TEST_ALWAYS_FLAGS.

From-SVN: r263450
parent f4b905f0
2018-08-09 David Malcolm <dmalcolm@redhat.com>
PR other/84889
* common.opt (fdiagnostics-show-line-numbers): New option.
* diagnostic-show-locus.c (class layout): Add fields
"m_show_line_numbers_p" and "m_linenum_width";
(num_digits): New function.
(test_num_digits): New function.
(layout::layout): Initialize new fields. Update m_x_offset
logic to handle any left margin.
(layout::print_source_line): Print line number when requested.
(layout::start_annotation_line): New member function.
(layout::print_annotation_line): Call it.
(layout::print_leading_fixits): Likewise.
(layout::print_trailing_fixits): Likewise. Update calls to
move_to_column for new parameter.
(layout::get_x_bound_for_row): Add "add_left_margin" param and use
it to potentially call start_annotation_line.
(layout::show_ruler): Call start_annotation_line.
(selftest::test_line_numbers_multiline_range): New selftest.
(selftest::diagnostic_show_locus_c_tests): Call test_num_digits
and selftest::test_line_numbers_multiline_range.
* diagnostic.c (diagnostic_initialize): Initialize
show_line_numbers_p.
* diagnostic.h (struct diagnostic_context): Add field
"show_line_numbers_p".
* doc/invoke.texi (Diagnostic Message Formatting Options): Add
-fno-diagnostics-show-line-numbers.
* dwarf2out.c (gen_producer_string): Add
OPT_fdiagnostics_show_line_numbers to the ignored options.
* lto-wrapper.c (merge_and_complain): Likewise to the "pick
one setting" options.
(append_compiler_options): Likewise to the dropped options.
(append_diag_options): Likewise to the passed-on options.
* opts.c (common_handle_option): Handle the new option.
* toplev.c (general_init): Set up global_dc->show_line_numbers_p.
2018-08-09 Kelvin Nilsen <kelvin@gcc.gnu.org>
* doc/extend.texi (PowerPC AltiVec Built-in Functions Available on
......
......@@ -1233,6 +1233,10 @@ fdiagnostics-show-caret
Common Var(flag_diagnostics_show_caret) Init(1)
Show the source line with a caret indicating the column.
fdiagnostics-show-line-numbers
Common Var(flag_diagnostics_show_line_numbers) Init(1)
Show line numbers in the left margin when showing source
fdiagnostics-color
Common Alias(fdiagnostics-color=,always,never)
;
......
......@@ -175,6 +175,7 @@ diagnostic_initialize (diagnostic_context *context, int n_opts)
context->lock = 0;
context->inhibit_notes_p = false;
context->colorize_source_p = false;
context->show_line_numbers_p = false;
context->show_ruler_p = false;
context->parseable_fixits_p = false;
context->edit_context_ptr = NULL;
......
......@@ -204,6 +204,10 @@ struct diagnostic_context
a token, which would look strange). */
bool colorize_source_p;
/* When printing source code, should there be a left-hand margin
showing line numbers? */
bool show_line_numbers_p;
/* Usable by plugins; if true, print a debugging ruler above the
source output. */
bool show_ruler_p;
......
......@@ -267,6 +267,7 @@ Objective-C and Objective-C++ Dialects}.
-fdiagnostics-show-location=@r{[}once@r{|}every-line@r{]} @gol
-fdiagnostics-color=@r{[}auto@r{|}never@r{|}always@r{]} @gol
-fno-diagnostics-show-option -fno-diagnostics-show-caret @gol
-fno-diagnostics-show-line-numbers @gol
-fdiagnostics-parseable-fixits -fdiagnostics-generate-patch @gol
-fdiagnostics-show-template-tree -fno-elide-type @gol
-fno-show-column}
......@@ -3710,6 +3711,13 @@ the @option{-fmessage-length=n} option is given. When the output is done
to the terminal, the width is limited to the width given by the
@env{COLUMNS} environment variable or, if not set, to the terminal width.
@item -fno-diagnostics-show-line-numbers
@opindex fno-diagnostics-show-line-numbers
@opindex fdiagnostics-show-line-numbers
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-parseable-fixits
@opindex fdiagnostics-parseable-fixits
Emit fix-it hints in a machine-parseable format, suitable for consumption
......@@ -24247,6 +24247,7 @@ gen_producer_string (void)
case OPT_fdiagnostics_show_location_:
case OPT_fdiagnostics_show_option:
case OPT_fdiagnostics_show_caret:
case OPT_fdiagnostics_show_line_numbers:
case OPT_fdiagnostics_color_:
case OPT_fverbose_asm:
case OPT____:
......
......@@ -255,6 +255,7 @@ merge_and_complain (struct cl_decoded_option **decoded_options,
/* Fallthru. */
case OPT_fdiagnostics_show_caret:
case OPT_fdiagnostics_show_line_numbers:
case OPT_fdiagnostics_show_option:
case OPT_fdiagnostics_show_location_:
case OPT_fshow_column:
......@@ -536,6 +537,7 @@ append_compiler_options (obstack *argv_obstack, struct cl_decoded_option *opts,
switch (option->opt_index)
{
case OPT_fdiagnostics_show_caret:
case OPT_fdiagnostics_show_line_numbers:
case OPT_fdiagnostics_show_option:
case OPT_fdiagnostics_show_location_:
case OPT_fshow_column:
......@@ -582,6 +584,7 @@ append_diag_options (obstack *argv_obstack, struct cl_decoded_option *opts,
{
case OPT_fdiagnostics_color_:
case OPT_fdiagnostics_show_caret:
case OPT_fdiagnostics_show_line_numbers:
case OPT_fdiagnostics_show_option:
case OPT_fdiagnostics_show_location_:
case OPT_fshow_column:
......
......@@ -2175,6 +2175,10 @@ common_handle_option (struct gcc_options *opts,
dc->show_caret = value;
break;
case OPT_fdiagnostics_show_line_numbers:
dc->show_line_numbers_p = value;
break;
case OPT_fdiagnostics_color_:
diagnostic_color_init (dc, value);
break;
......
2018-08-09 David Malcolm <dmalcolm@redhat.com>
PR other/84889
* gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c: New
test.
* gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c:
New test.
* gcc.dg/plugin/plugin.exp (plugin_test_list): Add the new tests.
* lib/prune.exp: Add -fno-diagnostics-show-line-numbers to
TEST_ALWAYS_FLAGS.
2018-08-09 Richard Sandiford <richard.sandiford@arm.com>
PR tree-optimization/86858
......
/* { dg-do compile } */
/* { dg-options "-O -fdiagnostics-show-caret -fdiagnostics-show-line-numbers" } */
/* 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 "" }
14 | myvar = myvar.x;
| ~~~~~^~
{ dg-end-multiline-output "" } */
#endif
}
void test_multiline (void)
{
#if 0
x = (first_function ()
+ second_function ()); /* { dg-warning "test" } */
/* { dg-begin-multiline-output "" }
26 | x = (first_function ()
| ~~~~~~~~~~~~~~~~~
27 | + second_function ());
| ^ ~~~~~~~~~~~~~~~~~~
{ dg-end-multiline-output "" } */
#endif
}
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
41 | float f = foo * bar;
| ~~~~^~~~~
| bar * foo
{ dg-end-multiline-output "" } */
#endif
}
/* Unit test for rendering of insertion fixit hints
(example taken from PR 62316). */
void test_fixit_insert (void)
{
#if 0
int a[2][2] = { 0, 1 , 2, 3 }; /* { dg-warning "insertion hints" } */
/* { dg-begin-multiline-output "" }
59 | int a[2][2] = { 0, 1 , 2, 3 };
| ^~~~
| { }
{ dg-end-multiline-output "" } */
#endif
}
/* Unit test for rendering of "remove" fixit hints. */
void test_fixit_remove (void)
{
#if 0
int a;; /* { dg-warning "example of a removal hint" } */
/* { dg-begin-multiline-output "" }
73 | int a;;
| ^
| -
{ dg-end-multiline-output "" } */
#endif
}
/* Unit test for rendering of "replace" fixit hints. */
void test_fixit_replace (void)
{
#if 0
gtk_widget_showall (dlg); /* { dg-warning "example of a replacement hint" } */
/* { dg-begin-multiline-output "" }
87 | gtk_widget_showall (dlg);
| ^~~~~~~~~~~~~~~~~~
| gtk_widget_show_all
{ dg-end-multiline-output "" } */
#endif
}
/* Unit test for rendering of fix-it hints that add new lines. */
void test_fixit_insert_newline (void)
{
#if 0
switch (op)
{
case 'a':
x = a;
case 'b': /* { dg-warning "newline insertion" } */
x = b;
}
/* { dg-begin-multiline-output "" }
|+ break;
106 | case 'b':
| ^~~~~~~~
{ dg-end-multiline-output "" } */
#endif
}
/* { dg-do compile } */
/* { dg-options "-O -fdiagnostics-show-caret -fplugin-arg-diagnostic_plugin_test_show_locus-color -fdiagnostics-show-line-numbers" } */
/* 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_multiline (void)
{
#if 0
x = (first_function ()
+ second_function ()); /* { dg-warning "test" } */
/* { dg-begin-multiline-output "" }
14 | x = (first_function ()
| ~~~~~~~~~~~~~~~~~
15 | + second_function ());
| ^ ~~~~~~~~~~~~~~~~~~
{ dg-end-multiline-output "" } */
#endif
}
......@@ -72,8 +72,10 @@ set plugin_test_list [list \
{ diagnostic_plugin_test_show_locus.c \
diagnostic-test-show-locus-bw.c \
diagnostic-test-show-locus-color.c \
diagnostic-test-show-locus-bw-line-numbers.c \
diagnostic-test-show-locus-color-line-numbers.c \
diagnostic-test-show-locus-parseable-fixits.c \
diagnostic-test-show-locus-generate-patch.c } \
diagnostic-test-show-locus-generate-patch.c }\
{ diagnostic_plugin_test_tree_expression_range.c \
diagnostic-test-expressions-1.c } \
{ diagnostic_plugin_show_trees.c \
......
......@@ -21,7 +21,7 @@ load_lib multiline.exp
if ![info exists TEST_ALWAYS_FLAGS] {
set TEST_ALWAYS_FLAGS ""
}
set TEST_ALWAYS_FLAGS "-fno-diagnostics-show-caret -fdiagnostics-color=never $TEST_ALWAYS_FLAGS"
set TEST_ALWAYS_FLAGS "-fno-diagnostics-show-caret -fno-diagnostics-show-line-numbers -fdiagnostics-color=never $TEST_ALWAYS_FLAGS"
proc prune_gcc_output { text } {
global srcdir
......
......@@ -1112,6 +1112,8 @@ general_init (const char *argv0, bool init_signals)
global_dc->show_caret
= global_options_init.x_flag_diagnostics_show_caret;
global_dc->show_line_numbers_p
= global_options_init.x_flag_diagnostics_show_line_numbers;
global_dc->show_option_requested
= global_options_init.x_flag_diagnostics_show_option;
global_dc->show_column
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment