compiler: add src information to AST dumps (part 1 of 2).

When emitting AST dumps, tag the end of selected statements with with
    source file and line number information where available. Example:
    
      tmp.76832448 = 0 // p.go:6
    
    Requires a corresponding change in gcc/go as well as this change to
    gofrontend.
    
    Reviewed-on: https://go-review.googlesource.com/29856

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

From-SVN: r240558
This commit is contained in:
Than McIntosh 2016-09-27 20:49:05 +00:00 committed by Ian Lance Taylor
parent 6e39060a7f
commit 437018ea2b
5 changed files with 88 additions and 28 deletions

View File

@ -1,3 +1,7 @@
2016-09-27 Than McIntosh <thanm@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.

View File

@ -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_name, unsigned line_begin)
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

View File

@ -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.

View File

@ -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

View File

@ -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_statement(
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(Ast_dump_context* ast_dump_context) const
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* ast_dump_context)
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_statement(
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_statement(
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_statement(
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_dump_statement(
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::do_dump_statement(
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* ast_dump_context)
{
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_dump_context* ast_dump_context) const
{
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_context* ast_dump_context) const
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_dump_context* ast_dump_context) const
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_dump_context* ast_dump_context) const
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_context* ast_dump_context) const
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_statement(
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_statement(Ast_dump_context* ast_dump_context)
{
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_context* ast_dump_context) const
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(Ast_dump_context* ast_dump_context)
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_dump_context* ast_dump_context) const
}
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* ast_dump_context)
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_dump_context* ast_dump_context) const
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_dump_context* ast_dump_context) const
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_dump_context* ast_dump_context) const
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(Ast_dump_context* ast_dump_context) const
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.