From 0d53810b125007ae6ed0b4a8a2bd4f2aa1d4ae5f Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 27 Mar 2019 16:09:23 -0400
Subject: [PATCH 25/51] FIXME: add -fdiagnostics-nn-line-numbers

---
 gcc/common.opt              |  4 ++++
 gcc/diagnostic-show-locus.c | 54 +++++++++++++++++++++++++++++++--------------
 gcc/diagnostic.c            |  8 ++++++-
 gcc/diagnostic.h            |  6 ++++-
 gcc/dwarf2out.c             |  1 +
 gcc/input.c                 |  4 ++--
 gcc/lto-wrapper.c           |  3 +++
 gcc/opts.c                  |  4 ++++
 gcc/toplev.c                |  2 ++
 gcc/tree-diagnostic.c       |  1 +
 10 files changed, 67 insertions(+), 20 deletions(-)

diff --git a/gcc/common.opt b/gcc/common.opt
index 3782c24..2ef36bf 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -1253,6 +1253,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)
+FIXME
+
 fdiagnostics-color
 Common Alias(fdiagnostics-color=,always,never)
 ;
diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c
index bd7f3af..3538115 100644
--- a/gcc/diagnostic-show-locus.c
+++ b/gcc/diagnostic-show-locus.c
@@ -295,6 +295,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;
@@ -841,6 +842,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 ()),
@@ -876,7 +878,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);
@@ -1263,10 +1265,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);
@@ -3816,19 +3821,36 @@ 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 ("\n"
-		" 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 ("\n"
+		  " 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 ("\n"
+		  "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 8fc0efb..0468325 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -207,6 +207,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;
@@ -1143,12 +1144,15 @@ diagnostic_report_diagnostic (diagnostic_context *context,
 /* Get the number of digits in the decimal representation of VALUE.  */
 
 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;
 
@@ -2092,6 +2096,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 2951177..3734ba2 100644
--- a/gcc/diagnostic.h
+++ b/gcc/diagnostic.h
@@ -262,6 +262,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;
@@ -462,7 +466,7 @@ 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);
 
 extern json::value *json_from_expanded_location (location_t loc);
 
diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c
index 94e36b1..531ab8a 100644
--- a/gcc/dwarf2out.c
+++ b/gcc/dwarf2out.c
@@ -24275,6 +24275,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/input.c b/gcc/input.c
index bf1ca66..c34a8ee 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -1132,10 +1132,10 @@ dump_location_info (FILE *stream)
 	      if (max_col > line_text.length ())
 		max_col = line_text.length () + 1;
 
-	      int len_lnum = num_digits (exploc.line);
+	      int len_lnum = num_digits (exploc.line, false);
 	      if (len_lnum < 3)
 		len_lnum = 3;
-	      int len_loc = num_digits (loc);
+	      int len_loc = num_digits (loc, false);
 	      if (len_loc < 5)
 		len_loc = 5;
 
diff --git a/gcc/lto-wrapper.c b/gcc/lto-wrapper.c
index c079c7b..befe364 100644
--- a/gcc/lto-wrapper.c
+++ b/gcc/lto-wrapper.c
@@ -261,6 +261,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:
@@ -652,6 +654,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 cb962e4..ec63e17 100644
--- a/gcc/opts.c
+++ b/gcc/opts.c
@@ -2383,6 +2383,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 55d4bdb..e996829 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1119,6 +1119,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_metadata
     = global_options_init.x_flag_diagnostics_show_metadata;
   global_dc->path_summary_format
diff --git a/gcc/tree-diagnostic.c b/gcc/tree-diagnostic.c
index 91a3881..1f50cef 100644
--- a/gcc/tree-diagnostic.c
+++ b/gcc/tree-diagnostic.c
@@ -749,6 +749,7 @@ print_event_range (pretty_printer *pp,
 	  dc.show_caret = true;
 	  dc.show_labels_p = global_dc->show_labels_p;
 	  dc.show_line_numbers_p = global_dc->show_line_numbers_p;
+	  dc.use_nn_for_line_numbers_p = global_dc->use_nn_for_line_numbers_p;
 	  dc.start_span = global_dc->start_span;
 	  dc.min_margin_width = global_dc->min_margin_width;
 	  dc.caret_chars[0] = '^';
-- 
1.8.5.3

