diff mbox series

[05/41] Add -fdiagnostics-nn-line-numbers

Message ID 20200108090302.2425-6-dmalcolm@redhat.com
State New
Headers show
Series v5 of analyzer patch kit | expand

Commit Message

David Malcolm Jan. 8, 2020, 9:02 a.m. UTC
I may be able to self-approve this.  It's used by the diagnostic_path
patch, and by the analyzer test suite.  Perhaps better to make
undocumeted, or do it via a DejaGnu pruning directive, but I wanted
to get v5 of the kit posted.

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(-)

Comments

Jeff Law Jan. 9, 2020, 4:17 a.m. UTC | #1
On Wed, 2020-01-08 at 04:02 -0500, David Malcolm wrote:
> I may be able to self-approve this.  It's used by the diagnostic_path
> patch, and by the analyzer test suite.  Perhaps better to make
> undocumeted, or do it via a DejaGnu pruning directive, but I wanted
> to get v5 of the kit posted.
> 
> 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.
Reminds me a lot of the option to not print insn numbers and certain
addresses in RTL dumps -- which makes comparing them easier.

OK
jeff
>
David Malcolm Jan. 9, 2020, 4:35 a.m. UTC | #2
On Wed, 2020-01-08 at 21:17 -0700, Jeff Law wrote:
> On Wed, 2020-01-08 at 04:02 -0500, David Malcolm wrote:
> > I may be able to self-approve this.  It's used by the
> > diagnostic_path
> > patch, and by the analyzer test suite.  Perhaps better to make
> > undocumeted, or do it via a DejaGnu pruning directive, but I wanted
> > to get v5 of the kit posted.
> > 
> > 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.
> Reminds me a lot of the option to not print insn numbers and certain
> addresses in RTL dumps -- which makes comparing them easier.
> 
> OK
> jeff

Thanks.  I've actually reworked my working copy to use a DejaGnu-based
postprocessing approach instead:
  https://gcc.gnu.org/ml/gcc-patches/2020-01/msg00398.html
which avoids adding an option.

Does this latter approach look OK?  (and is the other patch OK?)

Thanks
Dave
Jeff Law Jan. 9, 2020, 4:49 a.m. UTC | #3
On Wed, 2020-01-08 at 23:35 -0500, David Malcolm wrote:
> On Wed, 2020-01-08 at 21:17 -0700, Jeff Law wrote:
> > On Wed, 2020-01-08 at 04:02 -0500, David Malcolm wrote:
> > > I may be able to self-approve this.  It's used by the
> > > diagnostic_path
> > > patch, and by the analyzer test suite.  Perhaps better to make
> > > undocumeted, or do it via a DejaGnu pruning directive, but I wanted
> > > to get v5 of the kit posted.
> > > 
> > > 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.
> > Reminds me a lot of the option to not print insn numbers and certain
> > addresses in RTL dumps -- which makes comparing them easier.
> > 
> > OK
> > jeff
> 
> Thanks.  I've actually reworked my working copy to use a DejaGnu-based
> postprocessing approach instead:
>   https://gcc.gnu.org/ml/gcc-patches/2020-01/msg00398.html
> which avoids adding an option.
> 
> Does this latter approach look OK?  (and is the other patch OK?)
I don't have a strong opinion here.  I guess it's marginally better to
not have the option in gcc.  I'll take a looksie at the updated patch.

Jeff
Jeff Law Jan. 10, 2020, 3:35 p.m. UTC | #4
On Wed, 2020-01-08 at 23:35 -0500, David Malcolm wrote:
> On Wed, 2020-01-08 at 21:17 -0700, Jeff Law wrote:
> > On Wed, 2020-01-08 at 04:02 -0500, David Malcolm wrote:
> > > I may be able to self-approve this.  It's used by the
> > > diagnostic_path
> > > patch, and by the analyzer test suite.  Perhaps better to make
> > > undocumeted, or do it via a DejaGnu pruning directive, but I wanted
> > > to get v5 of the kit posted.
> > > 
> > > 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.
> > Reminds me a lot of the option to not print insn numbers and certain
> > addresses in RTL dumps -- which makes comparing them easier.
> > 
> > OK
> > jeff
> 
> Thanks.  I've actually reworked my working copy to use a DejaGnu-based
> postprocessing approach instead:
>   https://gcc.gnu.org/ml/gcc-patches/2020-01/msg00398.html
> which avoids adding an option.
> 
> Does this latter approach look OK?  (and is the other patch OK?)
After looking at it, I prefer the dejagnu approach.  Not because I like
expect/tcl implementations of *anything* but because it doesn't pollute
the actual compiler with a feature that's really just to make life
easier for the dejagnu testsuite.

jeff
>
diff mbox series

Patch

diff --git a/gcc/common.opt b/gcc/common.opt
index 9fc921109caa..f0f1328b70df 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 4ca8efe7847a..ed0ce7abe5eb 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 <layout_range> m_layout_ranges;
   auto_vec <const fixit_hint *> m_fixit_hints;
   auto_vec <line_span> 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 1ab420cbf870..4a9e9df9c2cb 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -213,6 +213,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;
@@ -1126,15 +1127,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;
 
@@ -1965,6 +1972,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 a670e0ca7439..376d181e1a5b 100644
--- a/gcc/diagnostic.h
+++ b/gcc/diagnostic.h
@@ -241,6 +241,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;
@@ -440,6 +444,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 f15ddd0c80b3..730a23eeba2f 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -279,6 +279,7 @@  Objective-C and Objective-C++ Dialects}.
 -fno-diagnostics-show-option  -fno-diagnostics-show-caret @gol
 -fno-diagnostics-show-labels  -fno-diagnostics-show-line-numbers @gol
 -fno-diagnostics-show-cwe  @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
@@ -4079,6 +4080,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 d0dee485cdb5..c95e52b29e52 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 fe8f292f8778..d6a8a2b8bf91 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:
@@ -606,6 +607,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:
@@ -654,6 +656,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 d5efadb43346..e441c0cb5c0b 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 0d70f6c07a03..1ad69562958b 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1181,6 +1181,8 @@  general_init (const char *argv0, bool init_signals)
     = global_options_init.x_flag_diagnostics_show_line_numbers;
   global_dc->show_cwe
     = global_options_init.x_flag_diagnostics_show_cwe;
+  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