public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Go patch committed: add src information to AST dumps
@ 2016-09-27 21:15 Ian Lance Taylor
  0 siblings, 0 replies; only message in thread
From: Ian Lance Taylor @ 2016-09-27 21:15 UTC (permalink / raw)
  To: gcc-patches, gofrontend-dev

[-- Attachment #1: Type: text/plain, Size: 297 bytes --]

This patch by Than McIntosh adds some file/line information to the AST
dumps generated by the Go frontend.  Bootstrapped and ran Go testsuite
on x86_64-pc-linux-gnu.  Committed to mainline.

Ian

2016-09-27  Than McIntosh  <thanm@google.com>

* go-linemap.cc (Gcc_linemap::to_string): New method.

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 13271 bytes --]

Index: gcc/go/ChangeLog
===================================================================
--- gcc/go/ChangeLog	(revision 240453)
+++ gcc/go/ChangeLog	(working copy)
@@ -1,3 +1,7 @@
+2016-09-27  Ian Lance Taylor  <iant@google.com>
+
+	* go-linemap.cc (Gcc_linemap::to_string): New method.
+
 2016-09-23  Than McIntosh  <thanm@google.com>
 
 	* go-gcc-diagnostics.cc: New file.
Index: gcc/go/go-linemap.cc
===================================================================
--- gcc/go/go-linemap.cc	(revision 240053)
+++ gcc/go/go-linemap.cc	(working copy)
@@ -29,6 +29,9 @@ class Gcc_linemap : public Linemap
   void
   stop();
 
+  std::string
+  to_string(Location);
+
  protected:
   Location
   get_predeclared_location();
@@ -60,6 +63,31 @@ Gcc_linemap::start_file(const char *file
   this->in_file_ = true;
 }
 
+// Stringify a location
+
+std::string
+Gcc_linemap::to_string(Location location)
+{
+  const line_map_ordinary *lmo;
+  source_location resolved_location;
+
+  // Screen out unknown and predeclared locations; produce output
+  // only for simple file:line locations.
+  resolved_location =
+      linemap_resolve_location (line_table, location.gcc_location(),
+                                LRK_SPELLING_LOCATION, &lmo);
+  if (lmo == NULL || resolved_location < RESERVED_LOCATION_COUNT)
+    return "";
+  const char *path = LINEMAP_FILE (lmo);
+  if (!path)
+    return "";
+
+  // Strip the source file down to the base file, to reduce clutter.
+  std::stringstream ss;
+  ss << lbasename(path) << ":" << SOURCE_LINE (lmo, location.gcc_location());
+  return ss.str();
+}
+
 // Stop getting locations.
 
 void
Index: gcc/go/gofrontend/MERGE
===================================================================
--- gcc/go/gofrontend/MERGE	(revision 240457)
+++ gcc/go/gofrontend/MERGE	(working copy)
@@ -1,4 +1,4 @@
-28b79f1d5a3a8924329128999a21d0693e08a603
+1d8d834b5eb9f683cc06529145b353bb5b08e7ea
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
Index: gcc/go/gofrontend/go-linemap.h
===================================================================
--- gcc/go/gofrontend/go-linemap.h	(revision 240053)
+++ gcc/go/gofrontend/go-linemap.h	(working copy)
@@ -17,7 +17,6 @@
 // The type is normally passed by value rather than by reference, and
 // it should support that efficiently.  The type should be defined in
 // "go-location.h".
-
 #include "go-location.h"
 
 // The Linemap class is a pure abstract interface, plus some static
@@ -58,6 +57,12 @@ class Linemap
   virtual void
   stop() = 0;
 
+  // Produce a human-readable description of a Location, e.g.
+  // "foo.go:10". Returns an empty string for predeclared, builtin or
+  // unknown locations.
+  virtual std::string
+  to_string(Location) = 0;
+
  protected:
   // Return a special Location used for predeclared identifiers.  This
   // Location should be different from that for any actual source
@@ -122,6 +127,14 @@ class Linemap
     go_assert(Linemap::instance_ != NULL);
     return Linemap::instance_->is_unknown(loc);
   }
+
+  // Produce a human-readable description of a Location.
+  static std::string
+  location_to_string(Location loc)
+  {
+    go_assert(Linemap::instance_ != NULL);
+    return Linemap::instance_->to_string(loc);
+  }
 };
 
 // The backend interface must define this function.  It should return
Index: gcc/go/gofrontend/statements.cc
===================================================================
--- gcc/go/gofrontend/statements.cc	(revision 240453)
+++ gcc/go/gofrontend/statements.cc	(working copy)
@@ -191,6 +191,21 @@ class Error_statement : public Statement
   do_dump_statement(Ast_dump_context*) const;
 };
 
+//
+// Helper to tack on available source position information
+// at the end of a statement.
+//
+static std::string
+dsuffix(Location location)
+{
+  std::string lstr = Linemap::location_to_string(location);
+  if (lstr == "")
+    return lstr;
+  std::string rval(" // ");
+  rval += lstr;
+  return rval;
+}
+
 // Dump the AST representation for an error statement.
 
 void
@@ -338,7 +353,7 @@ Variable_declaration_statement::do_dump_
       ast_dump_context->ostream() <<  "= ";
       ast_dump_context->dump_expression(var->init());
     }
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make a variable declaration.
@@ -533,7 +548,7 @@ Temporary_statement::do_dump_statement(A
       ast_dump_context->ostream() << " = ";
       ast_dump_context->dump_expression(this->init_);
     }
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make and initialize a temporary variable in BLOCK.
@@ -839,7 +854,7 @@ Assignment_statement::do_dump_statement(
   ast_dump_context->dump_expression(this->lhs_);
   ast_dump_context->ostream() << " = " ;
   ast_dump_context->dump_expression(this->rhs_);
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make an assignment statement.
@@ -980,7 +995,7 @@ Assignment_operation_statement::do_dump_
   ast_dump_context->dump_expression(this->lhs_);
   ast_dump_context->dump_operator(this->op_);
   ast_dump_context->dump_expression(this->rhs_);
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make an assignment operation statement.
@@ -1126,7 +1141,7 @@ Tuple_assignment_statement::do_dump_stat
   ast_dump_context->dump_expression_list(this->lhs_);
   ast_dump_context->ostream() << " = ";
   ast_dump_context->dump_expression_list(this->rhs_);
-  ast_dump_context->ostream()  << std::endl;
+  ast_dump_context->ostream()  << dsuffix(location()) << std::endl;
 }
 
 // Make a tuple assignment statement.
@@ -1287,7 +1302,7 @@ Tuple_map_assignment_statement::do_dump_
   ast_dump_context->dump_expression(this->present_);
   ast_dump_context->ostream() << " = ";
   ast_dump_context->dump_expression(this->map_index_);
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make a map assignment statement which returns a pair of values.
@@ -1429,7 +1444,7 @@ Tuple_receive_assignment_statement::do_d
   ast_dump_context->dump_expression(this->closed_);
   ast_dump_context->ostream() << " <- ";
   ast_dump_context->dump_expression(this->channel_);
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make a nonblocking receive statement.
@@ -1623,7 +1638,7 @@ Tuple_type_guard_assignment_statement::d
   ast_dump_context->dump_expression(this->expr_);
   ast_dump_context->ostream() << " . ";
   ast_dump_context->dump_type(this->type_);
-  ast_dump_context->ostream()  << std::endl;
+  ast_dump_context->ostream()  << dsuffix(location()) << std::endl;
 }
 
 // Make an assignment from a type guard to a pair of variables.
@@ -1720,7 +1735,7 @@ Expression_statement::do_dump_statement(
 {
   ast_dump_context->print_indent();
   ast_dump_context->dump_expression(expr_);
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make an expression statement from an Expression.
@@ -1810,7 +1825,7 @@ Inc_dec_statement::do_dump_statement(Ast
 {
   ast_dump_context->print_indent();
   ast_dump_context->dump_expression(expr_);
-  ast_dump_context->ostream() << (is_inc_? "++": "--") << std::endl;
+  ast_dump_context->ostream() << (is_inc_? "++": "--") << dsuffix(location()) << std::endl;
 }
 
 // Make an increment statement.
@@ -2501,7 +2516,7 @@ Go_statement::do_dump_statement(Ast_dump
   ast_dump_context->print_indent();
   ast_dump_context->ostream() << "go ";
   ast_dump_context->dump_expression(this->call());
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make a go statement.
@@ -2539,7 +2554,7 @@ Defer_statement::do_dump_statement(Ast_d
   ast_dump_context->print_indent();
   ast_dump_context->ostream() << "defer ";
   ast_dump_context->dump_expression(this->call());
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make a defer statement.
@@ -2733,7 +2748,7 @@ Return_statement::do_dump_statement(Ast_
   ast_dump_context->print_indent();
   ast_dump_context->ostream() << "return " ;
   ast_dump_context->dump_expression_list(this->vals_);
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make a return statement.
@@ -2816,7 +2831,7 @@ Bc_statement::do_dump_statement(Ast_dump
       ast_dump_context->ostream() << " ";
       ast_dump_context->dump_label_name(this->label_);
     }
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make a break statement.
@@ -2873,7 +2888,7 @@ void
 Goto_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 {
   ast_dump_context->print_indent();
-  ast_dump_context->ostream() << "goto " << this->label_->name() << std::endl;
+  ast_dump_context->ostream() << "goto " << this->label_->name() << dsuffix(location()) << std::endl;
 }
 
 // Make a goto statement.
@@ -2909,7 +2924,7 @@ Goto_unnamed_statement::do_dump_statemen
   ast_dump_context->print_indent();
   ast_dump_context->ostream() << "goto ";
   ast_dump_context->dump_label_name(this->label_);
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make a goto statement to an unnamed label.
@@ -2952,7 +2967,7 @@ void
 Label_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 {
   ast_dump_context->print_indent();
-  ast_dump_context->ostream() << this->label_->name() << ":" << std::endl;
+  ast_dump_context->ostream() << this->label_->name() << ":" << dsuffix(location()) << std::endl;
 }
 
 // Make a label statement.
@@ -2992,7 +3007,7 @@ Unnamed_label_statement::do_dump_stateme
 {
   ast_dump_context->print_indent();
   ast_dump_context->dump_label_name(this->label_);
-  ast_dump_context->ostream() << ":" << std::endl;
+  ast_dump_context->ostream() << ":" << dsuffix(location()) << std::endl;
 }
 
 // Make an unnamed label statement.
@@ -3077,7 +3092,7 @@ If_statement::do_dump_statement(Ast_dump
   ast_dump_context->print_indent();
   ast_dump_context->ostream() << "if ";
   ast_dump_context->dump_expression(this->cond_);
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
   if (ast_dump_context->dump_subblocks())
     {
       ast_dump_context->dump_block(this->then_block_);
@@ -3391,7 +3406,7 @@ Case_clauses::Case_clause::dump_clause(A
   if (this->is_fallthrough_)
     {
       ast_dump_context->print_indent();
-      ast_dump_context->ostream() <<  " (fallthrough)" << std::endl;
+      ast_dump_context->ostream() <<  " (fallthrough)" << dsuffix(location()) << std::endl;
     }
 }
 
@@ -3782,7 +3797,7 @@ Switch_statement::do_dump_statement(Ast_
     }
   if (ast_dump_context->dump_subblocks())
     {
-      ast_dump_context->ostream() << " {" << std::endl;
+      ast_dump_context->ostream() << " {" << dsuffix(location()) << std::endl;
       this->clauses_->dump_clauses(ast_dump_context);
       ast_dump_context->print_indent();
       ast_dump_context->ostream() << "}";
@@ -4202,7 +4217,7 @@ Type_switch_statement::do_dump_statement
   ast_dump_context->ostream() << " .(type)";
   if (ast_dump_context->dump_subblocks())
     {
-      ast_dump_context->ostream() << " {" << std::endl;
+      ast_dump_context->ostream() << " {" << dsuffix(location()) << std::endl;
       this->clauses_->dump_clauses(ast_dump_context);
       ast_dump_context->ostream() << "}";
     }
@@ -4419,7 +4434,7 @@ Send_statement::do_dump_statement(Ast_du
   ast_dump_context->dump_expression(this->channel_);
   ast_dump_context->ostream() << " <- ";
   ast_dump_context->dump_expression(this->val_);
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make a send statement.
@@ -4950,7 +4965,7 @@ Select_statement::do_dump_statement(Ast_
   ast_dump_context->ostream() << "select";
   if (ast_dump_context->dump_subblocks())
     {
-      ast_dump_context->ostream() << " {" << std::endl;
+      ast_dump_context->ostream() << " {" << dsuffix(location()) << std::endl;
       this->clauses_->dump_clauses(ast_dump_context);
       ast_dump_context->ostream() << "}";
     }
@@ -5143,7 +5158,7 @@ For_statement::do_dump_statement(Ast_dum
       ast_dump_context->ostream() << "}";
     }
 
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make a for statement.
@@ -5895,7 +5910,7 @@ For_range_statement::do_dump_statement(A
       ast_dump_context->print_indent();
       ast_dump_context->ostream() << "}";
     }
-  ast_dump_context->ostream() << std::endl;
+  ast_dump_context->ostream() << dsuffix(location()) << std::endl;
 }
 
 // Make a for statement with a range clause.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2016-09-27 20:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-27 21:15 Go patch committed: add src information to AST dumps Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).