From 0bdabc798807731b526c17796415e5cb4b73456a Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Mon, 22 May 2017 10:42:09 -0400
Subject: [PATCH 08/28] FIXME: fixes for blt

---
 gcc/blt.c                   |  1 +
 gcc/blt.h                   |  8 ++++---
 gcc/c/c-parser.c            | 55 ++++++++++++++++++++++++---------------------
 gcc/cp/parser.c             | 52 +++++++++++++++++++++++++++++++++++-------
 gcc/diagnostic-show-locus.c |  3 +++
 5 files changed, 82 insertions(+), 37 deletions(-)

diff --git a/gcc/blt.c b/gcc/blt.c
index d92149b..34e4591 100644
--- a/gcc/blt.c
+++ b/gcc/blt.c
@@ -238,6 +238,7 @@ blt_node::dump (pretty_printer *pp, const char *prefix,
       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;
+      dc.show_ruler_p = true;
       diagnostic_show_locus (&dc, &richloc, DK_NOTE);
       pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
       pp_set_prefix (pp, saved_prefix);
diff --git a/gcc/blt.h b/gcc/blt.h
index b253938..0fe874c 100644
--- a/gcc/blt.h
+++ b/gcc/blt.h
@@ -35,13 +35,15 @@ along with GCC; see the file COPYING3.  If not see
 
    There is a partial mapping between "tree" and blt_node: a blt_node
    can reference a tree, and a tree can reference a blt_node (though
-   typically the mapping is very sparse; most don't).  This allows up
+   typically the mapping is very sparse; most don't).  This allows us
    to go from e.g. a function_decl in the tree world and navigate
    pertinent parts of the syntax that was used to declare it.
 
    Working title = "BLT" as a backronym for "bonus location tree" (but
    actually a reference to a sandwich) - doesn't clash with anything else
-   in the source tree.  */
+   in the source tree ("Parse Tree" would be "PT" which clashes with
+   "points-to", and "Concrete Syntax Tree" would be "CST" which clashes
+   with our abbreviation for "constant").  */
 
 #include "cpplib.h"
 
@@ -79,7 +81,7 @@ public:
 
   /* Modification.  */
 
-  void set_finish (location_t loc) { m_finish = loc; }
+  void set_finish (location_t loc) { m_finish = ::get_finish (loc); }
   void set_tree (tree t);
   void set_kind (enum blt_kind kind) { m_kind = kind; }
 
diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c
index c9571c3..c8e8a95 100644
--- a/gcc/c/c-parser.c
+++ b/gcc/c/c-parser.c
@@ -213,10 +213,10 @@ struct GTY(()) c_parser {
   blt_node * GTY((skip)) blt_current_node;
 };
 
-/* FIXME.  */
-// RAII-style highlighting of the AST.
+/* A RAII-style class for optionally building a concrete parse tree (or
+   at least, something close to it) as the recursive descent parser runs.
 
-/* FIXME.  */
+   Close to a no-op if -fblt is not selected.  */
 
 class auto_blt_node
 {
@@ -230,43 +230,37 @@ private:
   c_parser *m_parser;
 };
 
-/* FIXME.  */
+/* RAII-style construction of the blt_node tree: push a blt_node of KIND
+   onto the current stack of blt_nodes, popping it when it goes out
+   of scope.  */
 
 #define AUTO_BLT_NODE(PARSER, KIND) \
   auto_blt_node tmp_blt_node ((PARSER), (KIND))
 
+/* The blt_node currently being constructed (the macro assumes that "parser"
+   exists in the current scope).  */
+
 #define CURRENT_BLT_NODE (parser->blt_current_node)
 
-#define BLT_ADD_NEXT_TOKEN(PARSER) \
-  add_next_token (PARSER)
+/* auto_blt_node's constructor.
 
-#define BLT_SET_TREE(TREE_NODE) \
-  tmp_blt_node.set_tree (TREE_NODE);
+   If -fblt was not enabled, return immediately.
 
-#if 0
-static void
-add_next_token (c_parser *parser)
-{
-  if (!flag_blt)
-    return;
+   Otherwise push a new blt_node of KIND as a child of the previous top
+   of the blt stack, effectively constructing a tree.
 
-  c_token *token = c_parser_peek_token (parser);
-  blt_node *parent = parser->blt_current_node;
-  blt_node *node = new blt_node (BLT_TOKEN, token->location);
-  parent->add_child (node);
-  // FIXME: get a meaningful string for the token
-  node->set_finish (token->location);
-}
-#endif
-
-/* FIXME.  */
+   Set the new blt_node's start location to that of the next token
+   within PARSER.  */
 
 auto_blt_node::auto_blt_node (c_parser *parser, enum blt_kind kind)
-: m_parser (parser)
 {
   if (!flag_blt)
     return;
 
+  /* Do this here rather than as an initializer
+     to avoid doing work when -fblt is not set.  */
+  m_parser = parser;
+
   c_token *first_token = c_parser_peek_token (parser);
   blt_node *parent = parser->blt_current_node;
   blt_node *node = new blt_node (kind, first_token->location);
@@ -277,7 +271,13 @@ auto_blt_node::auto_blt_node (c_parser *parser, enum blt_kind kind)
     parser->blt_root_node = node;
 }
 
-/* FIXME.  */
+/* auto_blt_node's destructor.
+
+   If -fblt was not enabled, return immediately.
+
+   Otherwise, pop the current blt_node from the stack,
+   and set its finish location to that of the last
+   token that was consumed.  */
 
 auto_blt_node::~auto_blt_node ()
 {
@@ -303,6 +303,9 @@ auto_blt_node::~auto_blt_node ()
   m_parser->blt_current_node = node->get_parent ();
 }
 
+/* Set the current blt_node's tree to be TREE_NODE.
+   Do nothing if -fblt is not set.  */
+
 void
 auto_blt_node::set_tree (tree tree_node)
 {
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index cfdbe3e..f7eadb8 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -1351,10 +1351,10 @@ cp_token_cache_new (cp_token *first, cp_token *last)
 
 
 
-/* FIXME.  */
-// RAII-style highlighting of the AST.
+/* A RAII-style class for optionally building a concrete parse tree (or
+   at least, something close to it) as the recursive descent parser runs.
 
-/* FIXME.  */
+   Close to a no-op if -fblt is not selected.  */
 
 class auto_blt_node
 {
@@ -1371,22 +1371,35 @@ private:
   blt_node *m_parent;
 };
 
-/* FIXME.  */
+/* RAII-style construction of the blt_node tree: push a blt_node of KIND
+   onto the current stack of blt_nodes, popping it when it goes out
+   of scope.  */
 
 #define AUTO_BLT_NODE(PARSER, KIND) \
   auto_blt_node tmp_blt_node ((PARSER), (KIND))
 
+/* FIXME.  */
+
 #define AUTO_BLT_NODE_WITH_RETURN(PARSER, KIND, ADDR_OF_CP_EXPR) \
   auto_blt_node tmp_blt_node ((PARSER), (KIND), (ADDR_OF_CP_EXPR))
 
+/* The blt_node currently being constructed (the macro assumes that "parser"
+   exists in the current scope).  */
+
 #define CURRENT_BLT_NODE (parser->blt_current_node)
 
+/* FIXME.  */
+
 #define BLT_ADD_NEXT_TOKEN(PARSER) \
   add_node_for_next_token (PARSER)
 
+/* FIXME.  */
+
 #define ADD_NODE_FOR_NEXT_TOKEN(KIND) \
   add_node_for_next_token (parser, (KIND))
 
+/* FIXME.  */
+
 #define BLT_SET_TREE(TREE_NODE) \
   tmp_blt_node.set_tree (TREE_NODE);
 
@@ -1423,16 +1436,28 @@ add_node_for_next_token (cp_parser *parser, enum blt_kind kind)
   return node;
 }
 
-/* FIXME.  */
+/* auto_blt_node's constructor.
+
+   If -fblt was not enabled, return immediately.
+
+   Otherwise push a new blt_node of KIND as a child of the previous top
+   of the blt stack, effectively constructing a tree.
+
+   Set the new blt_node's start location to that of the next token
+   within PARSER.  */
 
 auto_blt_node::auto_blt_node (cp_parser *parser, enum blt_kind kind,
                               cp_expr *expr_ptr)
-: m_parser (parser), m_expr_ptr (expr_ptr),
-  m_parent (parser->blt_current_node)
 {
   if (!flag_blt)
     return;
 
+  /* Do this here rather than as an initializer list
+     to avoid doing work when -fblt is not set.  */
+  m_parser = parser;
+  m_expr_ptr = expr_ptr;
+  m_parent = parser->blt_current_node;
+
   cp_token *first_token = cp_lexer_peek_token (parser->lexer);
   blt_node *node = new blt_node (kind, first_token->location);
   parser->blt_current_node = node;
@@ -1442,7 +1467,16 @@ auto_blt_node::auto_blt_node (cp_parser *parser, enum blt_kind kind,
     parser->blt_root_node = node;
 }
 
-/* FIXME.  */
+/* auto_blt_node's destructor.
+
+   If -fblt was not enabled, return immediately.
+
+   Otherwise, pop the current blt_node from the stack,
+   and set its finish location to that of the last
+   token that was consumed.
+
+   If m_expr_ptr was set, set the blt_node's tree to be that
+   of the m_expr_ptr.  */
 
 auto_blt_node::~auto_blt_node ()
 {
@@ -1471,6 +1505,8 @@ auto_blt_node::~auto_blt_node ()
   m_parser->blt_current_node = m_parent;
 }
 
+/* FIXME.  */
+
 void
 auto_blt_node::set_tree (tree tree_node)
 {
diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c
index f35e32e..e38a372 100644
--- a/gcc/diagnostic-show-locus.c
+++ b/gcc/diagnostic-show-locus.c
@@ -1869,6 +1869,7 @@ layout::show_ruler (int max_column) const
   /* Hundreds.  */
   if (max_column > 99)
     {
+      pp_emit_prefix (m_pp);
       pp_space (m_pp);
       for (int column = 1 + m_x_offset; column <= max_column; column++)
 	if (0 == column % 10)
@@ -1879,6 +1880,7 @@ layout::show_ruler (int max_column) const
     }
 
   /* Tens.  */
+  pp_emit_prefix (m_pp);
   pp_space (m_pp);
   for (int column = 1 + m_x_offset; column <= max_column; column++)
     if (0 == column % 10)
@@ -1888,6 +1890,7 @@ layout::show_ruler (int max_column) const
   pp_newline (m_pp);
 
   /* Units.  */
+  pp_emit_prefix (m_pp);
   pp_space (m_pp);
   for (int column = 1 + m_x_offset; column <= max_column; column++)
     pp_character (m_pp, '0' + (column % 10));
-- 
1.8.5.3

