From 49a07d55579915f19c84afabd69d3d76914ec141 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 16 Aug 2018 16:36:40 -0400
Subject: [PATCH 06/19] {NEEDS possible option, B&R, ChangeLog} diagnostics:
 line number indent/min margin size

TODO: do we need an option or a param for this?

This patch adds a minimum width to line numbers in the left margin.

I set the default to 5.  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 hope that this makes it easier to see where each
diagnostic starts, by visually breaking things up a bit.
---
 gcc/diagnostic-show-locus.c                        |  2 +
 gcc/diagnostic.c                                   |  1 +
 gcc/diagnostic.h                                   |  4 ++
 gcc/testsuite/gcc.dg/missing-header-fixit-3.c      | 12 ++---
 .../diagnostic-test-show-locus-bw-line-numbers.c   | 56 +++++++++++-----------
 ...diagnostic-test-show-locus-color-line-numbers.c | 12 ++---
 6 files changed, 47 insertions(+), 40 deletions(-)

diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c
index a759826..a2b3bfe 100644
--- a/gcc/diagnostic-show-locus.c
+++ b/gcc/diagnostic-show-locus.c
@@ -923,6 +923,7 @@ layout::layout (diagnostic_context * context,
   if (highest_line < 0)
     highest_line = 0;
   m_linenum_width = num_digits (highest_line);
+  m_linenum_width = MAX (m_linenum_width, context->min_linenum_width);
 
   /* Adjust m_x_offset.
      Center the primary caret to fit in max_width; all columns
@@ -3607,6 +3608,7 @@ test_line_numbers_multiline_range ()
 
   test_diagnostic_context dc;
   dc.show_line_numbers_p = true;
+  dc.min_linenum_width = 1;
   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 aae0934..e331705 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_linenum_width = 5;
   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 34ea03b..f421ac7 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 line numbers, what should the minimum width be?
+     Line numbers will be right-aligned, and padded to this width.  */
+  int min_linenum_width;
+
   /* Usable by plugins; if true, print a debugging ruler above the
      source output.  */
   bool show_ruler_p;
diff --git a/gcc/testsuite/gcc.dg/missing-header-fixit-3.c b/gcc/testsuite/gcc.dg/missing-header-fixit-3.c
index 8f2fb5b..1c7c7654 100644
--- a/gcc/testsuite/gcc.dg/missing-header-fixit-3.c
+++ b/gcc/testsuite/gcc.dg/missing-header-fixit-3.c
@@ -10,18 +10,18 @@ void test (int i, int j)
   /* { dg-message "include '<stdio.h>' 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-regexp ".*missing-header-fixit-3.c:1:1:" } */
 /* { dg-begin-multiline-output "" }
-+ |+#include <stdio.h>
-1 | /* Example of a fix-it hint that adds a #include directive,
++++++ |+#include <stdio.h>
+    1 | /* Example of a fix-it hint that adds a #include directive,
    { dg-end-multiline-output "" } */
 /* { dg-regexp ".*missing-header-fixit-3.c:9:3:" } */
 /* { dg-begin-multiline-output "" }
-9 |   printf ("%i of %i\n", i, j);
-  |   ^~~~~~
+    9 |   printf ("%i of %i\n", i, j);
+      |   ^~~~~~
    { 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 f2bbc58..7802da1 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,9 +111,9 @@ void test_fixit_insert_newline (void)
       x = b;
     }
 /* { dg-begin-multiline-output "" }
-+++ |+      break;
-110 |     case 'b':
-    |     ^~~~~~~~
++++++ |+      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 0453c52..bb83862 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 = ([32m[Kfirst_function ()[m[K
-   |        [32m[K~~~~~~~~~~~~~~~~~[m[K
-15 |        [01;35m[K+[m[K [34m[Ksecond_function ()[m[K);
-   |        [01;35m[K^[m[K [34m[K~~~~~~~~~~~~~~~~~~[m[K
-   |        [01;35m[K|[m[K
-   |        [01;35m[Klabel[m[K
+   14 |   x = ([32m[Kfirst_function ()[m[K
+      |        [32m[K~~~~~~~~~~~~~~~~~[m[K
+   15 |        [01;35m[K+[m[K [34m[Ksecond_function ()[m[K);
+      |        [01;35m[K^[m[K [34m[K~~~~~~~~~~~~~~~~~~[m[K
+      |        [01;35m[K|[m[K
+      |        [01;35m[Klabel[m[K
    { dg-end-multiline-output "" } */
 #endif
 }
-- 
1.8.5.3

