On 11/15/19 6:22 PM, David Malcolm wrote:
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 and 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 124e8cf..3c024b3 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -1257,6 +1257,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 412b0d4..ec5fdc0 100644
--- a/gcc/diagnostic-show-locus.c
+++ b/gcc/diagnostic-show-locus.c
@@ -303,6 +303,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 <layout_range> m_layout_ranges;
auto_vec <const fixit_hint *> m_fixit_hints;
auto_vec <line_span> m_line_spans;
@@ -849,6 +850,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 ()),
@@ -884,7 +886,7 @@ layout::layout (diagnostic_context * context,
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);
@@ -1272,10 +1274,13 @@ layout::print_source_line (linenum_type row, const char
*line, int line_width,
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);
@@ -3778,18 +3783,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 d6604e6..81e0a10 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;
+
I assume you added the argument to the function to avoid having
to test m_use_nn_for_line_numbers_p at each call site, but, FWIW,
I don't really think it belongs in such a general purpose utility
function. I don't see it called too many times so it's not a big
deal either way, but it is something that jumped out at me as odd.
Martin
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 91e4c50..2fb75e6 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 384848a..fc92913 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
@@ -4018,6 +4019,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 bf69ce4..3207040 100644
--- a/gcc/dwarf2out.c
+++ b/gcc/dwarf2out.c
@@ -24490,6 +24490,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 9a7bbd0..aabfe6d 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 32869ca..9928d01 100644
--- a/gcc/opts.c
+++ b/gcc/opts.c
@@ -2470,6 +2470,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 1c7002f..2d0a390 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1100,6 +1100,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