From 42ae705a85c887ad4127554e1d774bdc62ebdf6a Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 11 Oct 2019 13:02:52 -0400
Subject: [PATCH 03/74] diagnostic_show_locus: move initial newline to callers

diagnostic_show_locus adds a newline before doing anything (including
the do-nothing-else case).

This patch removes this initial newline, adding it to all callers
of diagnostic_show_locus instead.

Doing so makes diagnostic_show_locus more flexible, allowing it to be
used later in this patch kit when printing diagnostic paths.

gcc/c-family/ChangeLog:
	* c-format.c (test_type_mismatch_range_labels): Remove initial
	newline from expected outputs.
	* c-opts.c (c_diagnostic_finalizer): Add pp_newline call before
	call to diagnostic_show_locus.

gcc/ChangeLog:
	* diagnostic-show-locus.c (diagnostic_show_locus): Remove initial
	newline.
	(selftest::test_diagnostic_show_locus_unknown_location): Remove
	initial newline from expected output.
	(selftest::test_one_liner_simple_caret): Likewise.
	(selftest::test_one_liner_caret_and_range): Likewise.
	(selftest::test_one_liner_multiple_carets_and_ranges): Likewise.
	(selftest::test_one_liner_fixit_insert_before): Likewise.
	(selftest::test_one_liner_fixit_insert_after): Likewise.
	(selftest::test_one_liner_fixit_remove): Likewise.
	(selftest::test_one_liner_fixit_replace): Likewise.
	(selftest::test_one_liner_fixit_replace_non_equal_range):
	Likewise.
	(selftest::test_one_liner_fixit_replace_equal_secondary_range):
	Likewise.
	(selftest::test_one_liner_fixit_validation_adhoc_locations):
	Likewise.
	(selftest::test_one_liner_many_fixits_1): Likewise.
	(selftest::test_one_liner_many_fixits_2): Likewise.
	(selftest::test_one_liner_labels): Likewise.
	(selftest::test_add_location_if_nearby): Likewise.
	(selftest::test_diagnostic_show_locus_fixit_lines): Likewise.
	(selftest::test_overlapped_fixit_printing): Likewise.
	(selftest::test_overlapped_fixit_printing_2): Likewise.
	(selftest::test_fixit_insert_containing_newline): Likewise.
	(selftest::test_fixit_insert_containing_newline_2): Likewise.
	(selftest::test_fixit_replace_containing_newline): Likewise.
	(selftest::test_fixit_deletion_affecting_newline): Likewise.
	(selftest::test_line_numbers_multiline_range): Likewise.
	* diagnostic.c (default_diagnostic_finalizer): Add pp_newline call
	before call to diagnostic_show_locus.
	(diagnostic_append_note): Likewise.

gcc/fortran/ChangeLog:
	* error.c (gfc_diagnostic_starter): Add pp_newline call
	before call to diagnostic_show_locus.

gcc/testsuite/ChangeLog:
	* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
	(custom_diagnostic_finalizer): Add pp_newline call before call to
	diagnostic_show_locus.
---
 gcc/c-family/c-format.c                            |   6 +-
 gcc/c-family/c-opts.c                              |   1 +
 gcc/diagnostic-show-locus.c                        | 142 +++++++--------------
 gcc/diagnostic.c                                   |   2 +
 gcc/fortran/error.c                                |   1 +
 .../plugin/diagnostic_plugin_test_show_locus.c     |   1 +
 6 files changed, 54 insertions(+), 99 deletions(-)

diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c
index 3c291ca..7ddf064 100644
--- a/gcc/c-family/c-format.c
+++ b/gcc/c-family/c-format.c
@@ -5390,16 +5390,14 @@ test_type_mismatch_range_labels ()
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
   if (c_dialect_cxx ())
     /* "char*", without a space.  */
-    ASSERT_STREQ ("\n"
-		  "   printf (\"msg: %i\\n\", msg);\n"
+    ASSERT_STREQ ("   printf (\"msg: %i\\n\", msg);\n"
 		  "                 ~^     ~~~\n"
 		  "                  |     |\n"
 		  "                  char* int\n",
 		  pp_formatted_text (dc.printer));
   else
     /* "char *", with a space.  */
-    ASSERT_STREQ ("\n"
-		  "   printf (\"msg: %i\\n\", msg);\n"
+    ASSERT_STREQ ("   printf (\"msg: %i\\n\", msg);\n"
 		  "                 ~^     ~~~\n"
 		  "                  |     |\n"
 		  "                  |     int\n"
diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index 0fffe60..d752328 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -170,6 +170,7 @@ c_diagnostic_finalizer (diagnostic_context *context,
 {
   char *saved_prefix = pp_take_prefix (context->printer);
   pp_set_prefix (context->printer, NULL);
+  pp_newline (context->printer);
   diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
   /* By default print macro expansion contexts in the diagnostic
      finalizer -- for tokens resulting from macro expansion.  */
diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c
index cb920f6..412b0d4 100644
--- a/gcc/diagnostic-show-locus.c
+++ b/gcc/diagnostic-show-locus.c
@@ -2283,8 +2283,6 @@ diagnostic_show_locus (diagnostic_context * context,
 		       rich_location *richloc,
 		       diagnostic_t diagnostic_kind)
 {
-  pp_newline (context->printer);
-
   location_t loc = richloc->get_loc ();
   /* Do nothing if source-printing has been disabled.  */
   if (!context->show_caret)
@@ -2347,7 +2345,7 @@ test_diagnostic_show_locus_unknown_location ()
   test_diagnostic_context dc;
   rich_location richloc (line_table, UNKNOWN_LOCATION);
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-  ASSERT_STREQ ("\n", pp_formatted_text (dc.printer));
+  ASSERT_STREQ ("", pp_formatted_text (dc.printer));
 }
 
 /* Verify that diagnostic_show_locus works sanely for various
@@ -2369,8 +2367,7 @@ test_one_liner_simple_caret ()
   location_t caret = linemap_position_for_column (line_table, 10);
   rich_location richloc (line_table, caret);
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-  ASSERT_STREQ ("\n"
-		" foo = bar.field;\n"
+  ASSERT_STREQ (" foo = bar.field;\n"
 		"          ^\n",
 		pp_formatted_text (dc.printer));
 }
@@ -2387,8 +2384,7 @@ test_one_liner_caret_and_range ()
   location_t loc = make_location (caret, start, finish);
   rich_location richloc (line_table, loc);
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-  ASSERT_STREQ ("\n"
-		" foo = bar.field;\n"
+  ASSERT_STREQ (" foo = bar.field;\n"
 		"       ~~~^~~~~~\n",
 		pp_formatted_text (dc.printer));
 }
@@ -2421,8 +2417,7 @@ test_one_liner_multiple_carets_and_ranges ()
   richloc.add_range (bar, SHOW_RANGE_WITH_CARET);
   richloc.add_range (field, SHOW_RANGE_WITH_CARET);
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-  ASSERT_STREQ ("\n"
-		" foo = bar.field;\n"
+  ASSERT_STREQ (" foo = bar.field;\n"
 		" ~A~   ~B~ ~~C~~\n",
 		pp_formatted_text (dc.printer));
 }
@@ -2437,8 +2432,7 @@ test_one_liner_fixit_insert_before ()
   rich_location richloc (line_table, caret);
   richloc.add_fixit_insert_before ("&");
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-  ASSERT_STREQ ("\n"
-		" foo = bar.field;\n"
+  ASSERT_STREQ (" foo = bar.field;\n"
 		"       ^\n"
 		"       &\n",
 		pp_formatted_text (dc.printer));
@@ -2456,8 +2450,7 @@ test_one_liner_fixit_insert_after ()
   rich_location richloc (line_table, foo);
   richloc.add_fixit_insert_after ("[0]");
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-  ASSERT_STREQ ("\n"
-		" foo = bar.field;\n"
+  ASSERT_STREQ (" foo = bar.field;\n"
 		" ^~~\n"
 		"    [0]\n",
 		pp_formatted_text (dc.printer));
@@ -2480,8 +2473,7 @@ test_one_liner_fixit_remove ()
   {
     test_diagnostic_context dc;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  " foo = bar.field;\n"
+    ASSERT_STREQ (" foo = bar.field;\n"
 		  "          ^~~~~~\n"
 		  "          ------\n",
 		  pp_formatted_text (dc.printer));
@@ -2493,8 +2485,7 @@ test_one_liner_fixit_remove ()
     pp_prefixing_rule (dc.printer) = DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE;
     pp_set_prefix (dc.printer, xstrdup ("TEST PREFIX:"));
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "TEST PREFIX: foo = bar.field;\n"
+    ASSERT_STREQ ("TEST PREFIX: foo = bar.field;\n"
 		  "TEST PREFIX:          ^~~~~~\n"
 		  "TEST PREFIX:          ------\n",
 		  pp_formatted_text (dc.printer));
@@ -2506,8 +2497,7 @@ test_one_liner_fixit_remove ()
     dc.show_ruler_p = true;
     dc.caret_max_width = 104;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "          0         0         0         0         0         0         0         0         0         1    \n"
+    ASSERT_STREQ ("          0         0         0         0         0         0         0         0         0         1    \n"
 		  "          1         2         3         4         5         6         7         8         9         0    \n"
 		  " 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234\n"
 		  " foo = bar.field;\n"
@@ -2524,8 +2514,7 @@ test_one_liner_fixit_remove ()
     pp_prefixing_rule (dc.printer) = DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE;
     pp_set_prefix (dc.printer, xstrdup ("TEST PREFIX:"));
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "TEST PREFIX:          1         2         3         4         5\n"
+    ASSERT_STREQ ("TEST PREFIX:          1         2         3         4         5\n"
 		  "TEST PREFIX: 12345678901234567890123456789012345678901234567890\n"
 		  "TEST PREFIX: foo = bar.field;\n"
 		  "TEST PREFIX:          ^~~~~~\n"
@@ -2542,8 +2531,7 @@ test_one_liner_fixit_remove ()
     pp_prefixing_rule (dc.printer) = DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE;
     pp_set_prefix (dc.printer, xstrdup ("TEST PREFIX:"));
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "TEST PREFIX:      |          1         2         3         4         5\n"
+    ASSERT_STREQ ("TEST PREFIX:      |          1         2         3         4         5\n"
 		  "TEST PREFIX:      | 12345678901234567890123456789012345678901234567890\n"
 		  "TEST PREFIX:    1 | foo = bar.field;\n"
 		  "TEST PREFIX:      |          ^~~~~~\n"
@@ -2564,8 +2552,7 @@ test_one_liner_fixit_replace ()
   rich_location richloc (line_table, field);
   richloc.add_fixit_replace ("m_field");
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-  ASSERT_STREQ ("\n"
-		" foo = bar.field;\n"
+  ASSERT_STREQ (" foo = bar.field;\n"
 		"           ^~~~~\n"
 		"           m_field\n",
 		pp_formatted_text (dc.printer));
@@ -2589,8 +2576,7 @@ test_one_liner_fixit_replace_non_equal_range ()
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
   /* The replacement range is not indicated in the annotation line, so
      it should be indicated via an additional underline.  */
-  ASSERT_STREQ ("\n"
-		" foo = bar.field;\n"
+  ASSERT_STREQ (" foo = bar.field;\n"
 		"     ^\n"
 		"           -----\n"
 		"           m_field\n",
@@ -2615,8 +2601,7 @@ test_one_liner_fixit_replace_equal_secondary_range ()
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
   /* The replacement range is indicated in the annotation line,
      so it shouldn't be indicated via an additional underline.  */
-  ASSERT_STREQ ("\n"
-		" foo = bar.field;\n"
+  ASSERT_STREQ (" foo = bar.field;\n"
 		"     ^     ~~~~~\n"
 		"           m_field\n",
 		pp_formatted_text (dc.printer));
@@ -2649,8 +2634,7 @@ test_one_liner_fixit_validation_adhoc_locations ()
 
     test_diagnostic_context dc;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  " foo = bar.field;\n"
+    ASSERT_STREQ (" foo = bar.field;\n"
 		  "       ^~~~~~~~~~                               \n"
 		  "       test\n",
 		  pp_formatted_text (dc.printer));
@@ -2666,8 +2650,7 @@ test_one_liner_fixit_validation_adhoc_locations ()
 
     test_diagnostic_context dc;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  " foo = bar.field;\n"
+    ASSERT_STREQ (" foo = bar.field;\n"
 		  "       ^~~~~~~~~~                               \n"
 		  "       -----------------------------------------\n",
 		  pp_formatted_text (dc.printer));
@@ -2683,8 +2666,7 @@ test_one_liner_fixit_validation_adhoc_locations ()
 
     test_diagnostic_context dc;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  " foo = bar.field;\n"
+    ASSERT_STREQ (" foo = bar.field;\n"
 		  "       ^~~~~~~~~~                               \n"
 		  "       test\n",
 		  pp_formatted_text (dc.printer));
@@ -2703,8 +2685,7 @@ test_one_liner_many_fixits_1 ()
     richloc.add_fixit_insert_before ("a");
   ASSERT_EQ (1, richloc.get_num_fixit_hints ());
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-  ASSERT_STREQ ("\n"
-		" foo = bar.field;\n"
+  ASSERT_STREQ (" foo = bar.field;\n"
 		"     ^\n"
 		"     aaaaaaaaaaaaaaaaaaa\n",
 		pp_formatted_text (dc.printer));
@@ -2726,8 +2707,7 @@ test_one_liner_many_fixits_2 ()
     }
   ASSERT_EQ (19, richloc.get_num_fixit_hints ());
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-  ASSERT_STREQ ("\n"
-		" foo = bar.field;\n"
+  ASSERT_STREQ (" foo = bar.field;\n"
 		"     ^\n"
 		"a a a a a a a a a a a a a a a a a a a\n",
 		pp_formatted_text (dc.printer));
@@ -2763,8 +2743,7 @@ test_one_liner_labels ()
     {
       test_diagnostic_context dc;
       diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-      ASSERT_STREQ ("\n"
-		    " foo = bar.field;\n"
+      ASSERT_STREQ (" foo = bar.field;\n"
 		    " ^~~   ~~~ ~~~~~\n"
 		    " |     |   |\n"
 		    " 0     1   2\n",
@@ -2776,8 +2755,7 @@ test_one_liner_labels ()
       test_diagnostic_context dc;
       dc.show_labels_p = false;
       diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-      ASSERT_STREQ ("\n"
-		    " foo = bar.field;\n"
+      ASSERT_STREQ (" foo = bar.field;\n"
 		    " ^~~   ~~~ ~~~~~\n",
 		    pp_formatted_text (dc.printer));
     }
@@ -2794,8 +2772,7 @@ test_one_liner_labels ()
 
     test_diagnostic_context dc;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  " foo = bar.field;\n"
+    ASSERT_STREQ (" foo = bar.field;\n"
 		  " ^~~   ~~~ ~~~~~\n"
 		  " |     |   |\n"
 		  " |     |   label 2\n"
@@ -2816,8 +2793,7 @@ test_one_liner_labels ()
 
     test_diagnostic_context dc;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  " foo = bar.field;\n"
+    ASSERT_STREQ (" foo = bar.field;\n"
 		  " ^~~   ~~~ ~~~~~\n"
 		  " |     |   |\n"
 		  " |     |   c\n"
@@ -2836,8 +2812,7 @@ test_one_liner_labels ()
 
     test_diagnostic_context dc;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  " foo = bar.field;\n"
+    ASSERT_STREQ (" foo = bar.field;\n"
 		  " ~~~   ~~~ ^~~~~\n"
 		  " |     |   |\n"
 		  " 2     1   0\n",
@@ -2856,8 +2831,7 @@ test_one_liner_labels ()
 
     test_diagnostic_context dc;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  " foo = bar.field;\n"
+    ASSERT_STREQ (" foo = bar.field;\n"
 		  "       ^~~\n"
 		  "       |\n"
 		  "       label 0\n"
@@ -2892,8 +2866,7 @@ test_one_liner_labels ()
 
     test_diagnostic_context dc;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  " foo = bar.field;\n"
+    ASSERT_STREQ (" foo = bar.field;\n"
 		  " ~~~   ~~~ ^~~~~\n"
 		  " |     |   |\n"
 		  " |     |   label 0a\n"
@@ -2916,8 +2889,7 @@ test_one_liner_labels ()
 
     test_diagnostic_context dc;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  " foo = bar.field;\n"
+    ASSERT_STREQ (" foo = bar.field;\n"
 		  "       ^~~\n",
 		  pp_formatted_text (dc.printer));
    }
@@ -3009,8 +2981,7 @@ test_add_location_if_nearby (const line_table_case &case_)
     ASSERT_EQ (2, richloc.get_num_locations ());
     test_diagnostic_context dc;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  " struct same_line { double x; double y; ;\n"
+    ASSERT_STREQ (" struct same_line { double x; double y; ;\n"
 		  "                  ~                    ^\n",
 		  pp_formatted_text (dc.printer));
   }
@@ -3074,8 +3045,7 @@ test_diagnostic_show_locus_fixit_lines (const line_table_case &case_)
     richloc.add_fixit_insert_before (x, ".");
     richloc.add_fixit_replace (colon, "=");
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  " struct point origin = {x: 0.0,\n"
+    ASSERT_STREQ (" struct point origin = {x: 0.0,\n"
 		  "                         ^\n"
 		  "                        .=\n",
 		  pp_formatted_text (dc.printer));
@@ -3095,8 +3065,7 @@ test_diagnostic_show_locus_fixit_lines (const line_table_case &case_)
     richloc.add_fixit_insert_before (y, ".");
     richloc.add_fixit_replace (colon, "=");
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "FILENAME:3:24:\n"
+    ASSERT_STREQ ("FILENAME:3:24:\n"
 		  "                        y\n"
 		  "                        .\n"
 		  "FILENAME:6:25:\n"
@@ -3119,8 +3088,7 @@ test_diagnostic_show_locus_fixit_lines (const line_table_case &case_)
     test_diagnostic_context dc;
     dc.show_line_numbers_p = true;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "    3 |                        y\n"
+    ASSERT_STREQ ("    3 |                        y\n"
 		  "      |                        .\n"
 		  "......\n"
 		  "    6 |                         : 0.0};\n"
@@ -3330,8 +3298,7 @@ test_overlapped_fixit_printing (const line_table_case &case_)
     richloc.add_fixit_insert_after (")");
 
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "   foo *f = (foo *)ptr->field;\n"
+    ASSERT_STREQ ("   foo *f = (foo *)ptr->field;\n"
 		  "                   ^~~~~~~~~~\n"
 		  "            -----------------\n"
 		  "            const_cast<foo *> (ptr->field)\n",
@@ -3388,8 +3355,7 @@ test_overlapped_fixit_printing (const line_table_case &case_)
     richloc.add_fixit_insert_after (")");
 
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "   foo *f = (foo *)ptr->field;\n"
+    ASSERT_STREQ ("   foo *f = (foo *)ptr->field;\n"
 		  "                   ^~~~~~~~~~\n"
 		  "            -\n"
 		  "            CAST (-\n"
@@ -3406,8 +3372,7 @@ test_overlapped_fixit_printing (const line_table_case &case_)
     richloc.add_fixit_insert_after (")");
 
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "   foo *f = (foo *)ptr->field;\n"
+    ASSERT_STREQ ("   foo *f = (foo *)ptr->field;\n"
 		  "                   ^~~~~~~~~~\n"
 		  "            -\n"
 		  "            CST ( -\n"
@@ -3428,8 +3393,7 @@ test_overlapped_fixit_printing (const line_table_case &case_)
     ASSERT_EQ (1, richloc.get_num_fixit_hints ());
 
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "   foo *f = (foo *)ptr->field;\n"
+    ASSERT_STREQ ("   foo *f = (foo *)ptr->field;\n"
 		  "                   ^~~~~~~~~~\n"
 		  "            -------\n"
 		  "            (bar *)\n",
@@ -3449,8 +3413,7 @@ test_overlapped_fixit_printing (const line_table_case &case_)
 
     /* But the corrections are.  */
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "   foo *f = (foo *)ptr->field;\n"
+    ASSERT_STREQ ("   foo *f = (foo *)ptr->field;\n"
 		  "                   ^~~~~~~~~~\n"
 		  "            -----------------\n"
 		  "            (longer *)(foo *)\n",
@@ -3468,8 +3431,7 @@ test_overlapped_fixit_printing (const line_table_case &case_)
        it would overlap with the second.
        Verify that they are printed as a single replacement.  */
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "   foo *f = (foo *)ptr->field;\n"
+    ASSERT_STREQ ("   foo *f = (foo *)ptr->field;\n"
 		  "                   ^~~~~~~~~~\n"
 		  "            -------\n"
 		  "            LONGER THAN THE CAST(foo *)TEST\n",
@@ -3535,8 +3497,7 @@ test_overlapped_fixit_printing_2 (const line_table_case &case_)
     /* Verify that they're printed correctly.  */
     test_diagnostic_context dc;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  " int a5[][0][0] = { 1, 2 };\n"
+    ASSERT_STREQ (" int a5[][0][0] = { 1, 2 };\n"
 		  "                    ^\n"
 		  "                     } {\n",
 		  pp_formatted_text (dc.printer));
@@ -3558,8 +3519,7 @@ test_overlapped_fixit_printing_2 (const line_table_case &case_)
     richloc.add_fixit_insert_before (col_1, "{");
     richloc.add_fixit_insert_before (col_25, "}");
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  " int a5[][0][0] = { 1, 2 };\n"
+    ASSERT_STREQ (" int a5[][0][0] = { 1, 2 };\n"
 		  "                    ^\n"
 		  " {                  -----\n"
 		  "                    {{1}}}}, {{{2 }}\n",
@@ -3602,8 +3562,7 @@ test_fixit_insert_containing_newline (const line_table_case &case_)
     {
       test_diagnostic_context dc;
       diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-      ASSERT_STREQ ("\n"
-		    "       x = a;\n"
+      ASSERT_STREQ ("       x = a;\n"
 		    "+      break;\n"
 		    "     case 'b':\n"
 		    "     ^~~~~~~~~\n",
@@ -3615,8 +3574,7 @@ test_fixit_insert_containing_newline (const line_table_case &case_)
       test_diagnostic_context dc;
       dc.show_line_numbers_p = true;
       diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-      ASSERT_STREQ ("\n"
-		    "    2 |       x = a;\n"
+      ASSERT_STREQ ("    2 |       x = a;\n"
 		    "  +++ |+      break;\n"
 		    "    3 |     case 'b':\n"
 		    "      |     ^~~~~~~~~\n",
@@ -3632,8 +3590,7 @@ test_fixit_insert_containing_newline (const line_table_case &case_)
     ASSERT_TRUE (richloc.seen_impossible_fixit_p ());
     test_diagnostic_context dc;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "     case 'b':\n"
+    ASSERT_STREQ ("     case 'b':\n"
 		  "     ^~~~~~~~~\n",
 		  pp_formatted_text (dc.printer));
   }
@@ -3681,8 +3638,7 @@ test_fixit_insert_containing_newline_2 (const line_table_case &case_)
   {
     test_diagnostic_context dc;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "FILENAME:1:1:\n"
+    ASSERT_STREQ ("FILENAME:1:1:\n"
 		  "+#include <stdio.h>\n"
 		  " test (int ch)\n"
 		  "FILENAME:3:2:\n"
@@ -3697,8 +3653,7 @@ test_fixit_insert_containing_newline_2 (const line_table_case &case_)
     test_diagnostic_context dc;
     dc.show_line_numbers_p = true;
     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-    ASSERT_STREQ ("\n"
-		  "  +++ |+#include <stdio.h>\n"
+    ASSERT_STREQ ("  +++ |+#include <stdio.h>\n"
 		  "    1 | test (int ch)\n"
 		  "    2 | {\n"
 		  "    3 |  putchar (ch);\n"
@@ -3741,8 +3696,7 @@ test_fixit_replace_containing_newline (const line_table_case &case_)
 
   test_diagnostic_context dc;
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-  ASSERT_STREQ ("\n"
-		" foo = bar ();\n"
+  ASSERT_STREQ (" foo = bar ();\n"
 		"             ^\n",
 		pp_formatted_text (dc.printer));
 }
@@ -3786,8 +3740,7 @@ test_fixit_deletion_affecting_newline (const line_table_case &case_)
 
   test_diagnostic_context dc;
   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
-  ASSERT_STREQ ("\n"
-		" foo = bar (\n"
+  ASSERT_STREQ (" foo = bar (\n"
 		"          ~^\n"
 		"       );\n"
 		"       ~    \n",
@@ -3830,8 +3783,7 @@ test_line_numbers_multiline_range ()
   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"
+  ASSERT_STREQ (" 9 | this is line 9\n"
 		"   |         ~~~~~~\n"
 		"10 | this is line 10\n"
 		"   | ~~~~~^~~~~~~~~~\n"
diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c
index a29bcf1..d6604e6 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -679,6 +679,7 @@ default_diagnostic_finalizer (diagnostic_context *context,
 {
   char *saved_prefix = pp_take_prefix (context->printer);
   pp_set_prefix (context->printer, NULL);
+  pp_newline (context->printer);
   diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
   pp_set_prefix (context->printer, saved_prefix);
   pp_flush (context->printer);
@@ -1173,6 +1174,7 @@ diagnostic_append_note (diagnostic_context *context,
   pp_output_formatted_text (context->printer);
   pp_destroy_prefix (context->printer);
   pp_set_prefix (context->printer, saved_prefix);
+  pp_newline (context->printer);
   diagnostic_show_locus (context, &richloc, DK_NOTE);
   va_end (ap);
 }
diff --git a/gcc/fortran/error.c b/gcc/fortran/error.c
index b20aa86..03de73e 100644
--- a/gcc/fortran/error.c
+++ b/gcc/fortran/error.c
@@ -1147,6 +1147,7 @@ gfc_diagnostic_starter (diagnostic_context *context,
       /* Fortran uses an empty line between locus and caret line.  */
       pp_newline (context->printer);
       pp_set_prefix (context->printer, NULL);
+      pp_newline (context->printer);
       diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
       /* If the caret line was shown, the prefix does not contain the
 	 locus.  */
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
index 3f62edd..fc52de4 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
@@ -137,6 +137,7 @@ custom_diagnostic_finalizer (diagnostic_context *context,
     pp_show_color (context->printer) = true;
   char *saved_prefix = pp_take_prefix (context->printer);
   pp_set_prefix (context->printer, NULL);
+  pp_newline (context->printer);
   diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
   pp_show_color (context->printer) = old_show_color;
   pp_set_prefix (context->printer, saved_prefix);
-- 
1.8.5.3

