Handle very long lines when printing fix-it hints
authorDavid Malcolm <dmalcolm@redhat.com>
Tue, 28 Nov 2017 21:45:56 +0000 (21:45 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Tue, 28 Nov 2017 21:45:56 +0000 (21:45 +0000)
When fixing PR c/82050 I noticed a bug in how we print fix-it hints
for very long lines: we weren't taking into account the x-offset for
the line when printing the fix-it hint.

This could lead to output where instead of printing:

foo.c:14:3944: error: etc
  = foo.field
        ^~~~~
        replacement

where the lines have been offset to start printing at about column 3900,
the "replacement" line was erroneously *not* offset, and was thus
prefixed by thousands of spaces, leading to large whitespace gaps in
the output, and the replacement failing to line up with the source to be
replaced.

Fixed thusly.

gcc/ChangeLog:
* diagnostic-show-locus.c (layout::print_trailing_fixits): Handle
m_x_offset.
(layout::move_to_column): Likewise.

gcc/testsuite/ChangeLog:
* gcc.dg/plugin/diagnostic-test-show-locus-bw.c
(test_very_wide_line): Update expected output to include a
fix-it hint.
* gcc.dg/plugin/diagnostic-test-show-locus-color.c
(test_very_wide_line): Likewise.
* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
(test_show_locus): Add a fix-it hint to "test_very_wide_line".

From-SVN: r255219

gcc/ChangeLog
gcc/diagnostic-show-locus.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c
gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color.c
gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c

index 02f9a9e5789595ace18b01f9bb7ccc3ac86442a5..bfc721c48506c169fc182ca945aa34e5dd9d19d2 100644 (file)
@@ -1,3 +1,9 @@
+2017-11-28  David Malcolm  <dmalcolm@redhat.com>
+
+       * diagnostic-show-locus.c (layout::print_trailing_fixits): Handle
+       m_x_offset.
+       (layout::move_to_column): Likewise.
+
 2017-11-28  Jakub Jelinek  <jakub@redhat.com>
 
        PR sanitizer/81275
index a1ce682403b1df1593ef3bf25a3808d8b0d507ce..387c24555fc756f044abea40389fad42f8397d0f 100644 (file)
@@ -1691,7 +1691,7 @@ layout::print_trailing_fixits (int row)
   /* Now print the corrections.  */
   unsigned i;
   correction *c;
-  int column = 0;
+  int column = m_x_offset;
 
   FOR_EACH_VEC_ELT (corrections.m_corrections, i, c)
     {
@@ -1845,7 +1845,7 @@ layout::move_to_column (int *column, int dest_column)
   if (*column > dest_column)
     {
       print_newline ();
-      *column = 0;
+      *column = m_x_offset;
     }
 
   while (*column < dest_column)
index c2488b6412deae3155e22fc7659a4466c8a40dd3..efee461a10729cf5cd9acdfc8f6cde3cf87c0306 100644 (file)
@@ -1,3 +1,13 @@
+2017-11-28  David Malcolm  <dmalcolm@redhat.com>
+
+       * gcc.dg/plugin/diagnostic-test-show-locus-bw.c
+       (test_very_wide_line): Update expected output to include a
+       fix-it hint.
+       * gcc.dg/plugin/diagnostic-test-show-locus-color.c
+       (test_very_wide_line): Likewise.
+       * gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
+       (test_show_locus): Add a fix-it hint to "test_very_wide_line".
+
 2017-11-28  Jakub Jelinek  <jakub@redhat.com>
 
        PR sanitizer/81275
index 100fa380cd70df22ae4e9d5a715f4102f5758e22..513c0af772fa2c143d2c052c3b584516eb2f9caf 100644 (file)
@@ -122,6 +122,7 @@ void test_very_wide_line (void)
  6789012345678901234567890123456789012345678901234567890123456789012345
                                               float f = foo * bar;
                                                         ~~~~^~~~~
+                                                        bar * foo
    { dg-end-multiline-output "" } */
 #endif
 }
index 3fb1c240238e50f29fd3a4a4a0dce023fba2c392..4cc406d76dedd5b1dfcf5a810cbb0d6d30d1e248 100644 (file)
@@ -122,6 +122,7 @@ void test_very_wide_line (void)
  6789012345678901234567890123456789012345678901234567890123456789012345
                                               float f = \e[01;35m\e[Kfoo * bar\e[m\e[K;
                                                         \e[01;35m\e[K~~~~^~~~~\e[m\e[K
+                                                        \e[32m\e[Kbar * foo\e[m\e[K
    { dg-end-multiline-output "" } */
 #endif
 }
index 9751e1cd25e9fa52447d4a0cadbc1971ffcf60d3..3908b922b26b8c722b0b865364c8b9a05d814651 100644 (file)
@@ -235,9 +235,12 @@ test_show_locus (function *fun)
     {
       const int line = fnstart_line + 2;
       global_dc->show_ruler_p = true;
-      warning_at (make_location (get_loc (line, 94), get_loc (line, 90),
-                                get_loc (line, 98)),
-                 0, "test");
+      rich_location richloc (line_table,
+                            make_location (get_loc (line, 94),
+                                           get_loc (line, 90),
+                                           get_loc (line, 98)));
+      richloc.add_fixit_replace ("bar * foo");
+      warning_at (&richloc, 0, "test");
       global_dc->show_ruler_p = false;
     }