From be932c938dab44c7635d4b01f8add7b9c6aab91c Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 12 May 2017 21:17:27 -0400
Subject: [PATCH 12/14] FIXME: integrate diagnostic_show_locus into AST dump,
 via a prefix

---
 gcc/ast.c                   | 26 ++++++++++++++++++++------
 gcc/c-family/c-opts.c       |  2 +-
 gcc/diagnostic-show-locus.c | 12 +++++++++---
 gcc/diagnostic.c            |  5 ++---
 4 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/gcc/ast.c b/gcc/ast.c
index 40e949f..7869898 100644
--- a/gcc/ast.c
+++ b/gcc/ast.c
@@ -23,6 +23,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree.h"
 #include "pretty-print.h"
 #include "diagnostic.h"
+#include "diagnostic-color.h"
 #include "ast.h"
 
 typedef hash_map <tree, ast_node *> tree_to_ast_map_t;
@@ -212,6 +213,12 @@ ast_node::dump (pretty_printer *pp, const char *prefix,
 	       get_tree_code_name (TREE_CODE (m_tree)), m_tree);
 #endif
 
+  const char *new_prefix;
+  if (parent)
+    new_prefix = ACONCAT ((prefix, is_last_child ? "  " : "|  ", NULL));
+  else
+    new_prefix = prefix;
+
   /* Show source code.  */
   if (m_start != parent_start_loc || m_finish != parent_finish_loc)
     {
@@ -223,15 +230,22 @@ ast_node::dump (pretty_printer *pp, const char *prefix,
       dc.show_caret = true;
       dc.caret_chars[0] = '^';
       diagnostic_set_caret_max_width (&dc, pp_line_cutoff (pp));
+      const char *saved_prefix = pp->prefix;
+      const char *source_prefix;
+      const char *begin_color = colorize_start (pp_show_color (pp), "note");
+      const char *end_color = colorize_stop (pp_show_color (pp));
+      source_prefix = ACONCAT ((begin_color, new_prefix, "|:", end_color, NULL));
+      pp_set_prefix (pp, source_prefix);
+      pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE;
       diagnostic_show_locus (&dc, &richloc, DK_NOTE);
+      pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
+      pp_set_prefix (pp, saved_prefix);
+      /* diagnostic_show_locus expects to add a newline at the start
+	 and ends with a newline.  */
     }
-
-  pp_newline (pp);
-  const char *new_prefix;
-  if (parent)
-    new_prefix = ACONCAT ((prefix, is_last_child ? "  " : "|  ", NULL));
   else
-    new_prefix = prefix;
+    pp_newline (pp);
+
   for (ast_node *child = m_first_child; child; child = child->m_next_sibling)
     {
       bool is_last_child = child == m_last_child;
diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index ea0e01b..c2802ed 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -165,11 +165,11 @@ static void
 c_diagnostic_finalizer (diagnostic_context *context,
 			diagnostic_info *diagnostic)
 {
+  pp_destroy_prefix (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.  */
   virt_loc_aware_diagnostic_finalizer (context, diagnostic);
-  pp_destroy_prefix (context->printer);
   pp_flush (context->printer);
 }
 
diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c
index f410a32..89552af 100644
--- a/gcc/diagnostic-show-locus.c
+++ b/gcc/diagnostic-show-locus.c
@@ -1076,6 +1076,8 @@ layout::print_source_line (int row, const char *line, int line_width,
 							   line_width);
   line += m_x_offset;
 
+  pp_emit_prefix (m_pp);
+
   pp_space (m_pp);
   int first_non_ws = INT_MAX;
   int last_non_ws = 0;
@@ -1144,6 +1146,8 @@ layout::should_print_annotation_line_p (int row) const
 void
 layout::print_annotation_line (int row, const line_bounds lbounds)
 {
+  pp_emit_prefix (m_pp);
+
   int x_bound = get_x_bound_for_row (row, m_exploc.column,
 				     lbounds.m_last_non_ws);
 
@@ -1713,6 +1717,7 @@ layout::move_to_column (int *column, int dest_column)
     {
       print_newline ();
       *column = 0;
+      pp_emit_prefix (m_pp);
     }
 
   while (*column < dest_column)
@@ -1806,8 +1811,8 @@ diagnostic_show_locus (diagnostic_context * context,
 
   context->last_location = loc;
 
-  const char *saved_prefix = pp_get_prefix (context->printer);
-  pp_set_prefix (context->printer, NULL);
+  //const char *saved_prefix = pp_get_prefix (context->printer);
+  //pp_set_prefix (context->printer, NULL);
 
   layout layout (context, richloc, diagnostic_kind);
   for (int line_span_idx = 0; line_span_idx < layout.get_num_line_spans ();
@@ -1824,7 +1829,7 @@ diagnostic_show_locus (diagnostic_context * context,
 	layout.print_line (row);
     }
 
-  pp_set_prefix (context->printer, saved_prefix);
+  //pp_set_prefix (context->printer, saved_prefix);
 }
 
 #if CHECKING_P
@@ -1842,6 +1847,7 @@ class test_diagnostic_context : public diagnostic_context
   test_diagnostic_context ()
   {
     diagnostic_initialize (this, 0);
+    pp_prefixing_rule (printer) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
     show_caret = true;
     show_column = true;
     start_span = start_span_cb;
diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c
index dc81755..d90f84f 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -593,9 +593,8 @@ void
 default_diagnostic_start_span_fn (diagnostic_context *context,
 				  expanded_location exploc)
 {
-  pp_set_prefix (context->printer,
-		 diagnostic_get_location_text (context, exploc));
-  pp_string (context->printer, "");
+  pp_string (context->printer,
+	     diagnostic_get_location_text (context, exploc));
   pp_newline (context->printer);
 }
 
-- 
1.8.5.3

