From f1c4b346ea2f42245bd6f45d5b2d274fc68224e3 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Tue, 19 Feb 2019 18:44:24 +0000 Subject: [PATCH] compiler: add debugger-callable AST dump functins Introduce a set debug_go_* global functions that can be used to emit AST dumps for Go statements and expressions from within GDB (for use by people developing gccgo). Reviewed-on: https://go-review.googlesource.com/c/162903 From-SVN: r269027 --- gcc/go/gofrontend/MERGE | 2 +- gcc/go/gofrontend/ast-dump.cc | 83 ++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/gcc/go/gofrontend/MERGE b/gcc/go/gofrontend/MERGE index e3db88519f4..12dd965b748 100644 --- a/gcc/go/gofrontend/MERGE +++ b/gcc/go/gofrontend/MERGE @@ -1,4 +1,4 @@ -fe0382eabbf1e8b148dc8cb7733348bd9d887e10 +08cd59a502127da776e076a8a37016a668ef27fa The first line of this file holds the git revision number of the last merge done from the gofrontend repository. diff --git a/gcc/go/gofrontend/ast-dump.cc b/gcc/go/gofrontend/ast-dump.cc index 1fbc890f53e..9b4d7080623 100644 --- a/gcc/go/gofrontend/ast-dump.cc +++ b/gcc/go/gofrontend/ast-dump.cc @@ -482,7 +482,7 @@ Ast_dump_context::write_string(const std::string& s) this->ostream() << s; } -// Dump statment to stream. +// Dump statement to stream. void Ast_dump_context::dump_to_stream(const Statement* stm, std::ostream* out) @@ -499,3 +499,84 @@ Ast_dump_context::dump_to_stream(const Expression* expr, std::ostream* out) Ast_dump_context adc(out, false); expr->dump_expression(&adc); } + +// Dump an expression to std::cerr. This is intended to be used +// from within a debugging session. + +void +debug_go_expression(const Expression* expr) +{ + if (expr == NULL) + std::cerr << ""; + else + { + Ast_dump_context::dump_to_stream(expr, &std::cerr); + std::string lstr = Linemap::location_to_string(expr->location()); + std::cerr << " // loc " << lstr << std::endl; + } +} + +// Shallow dump of stmt to std::cerr. This is intended to be used +// from within a debugging session. + +void +debug_go_statement(const Statement* stmt) +{ + if (stmt == NULL) + std::cerr << "\n"; + else + { + std::string lstr = Linemap::location_to_string(stmt->location()); + Statement *ncstmt = const_cast(stmt); + Block_statement* bs = ncstmt->block_statement(); + if (bs != NULL) + std::cerr << "Block " << bs->block() + << " // location: " << lstr << std::endl; + else + Ast_dump_context::dump_to_stream(stmt, &std::cerr); + } +} + +// Deep dump of statement to std::cerr. This is intended to be used +// from within a debugging session. + +void +debug_go_statement_deep(const Statement* statement) +{ + Ast_dump_context adc(&std::cerr, true); + statement->dump_statement(&adc); +} + +// Shallow dump of a block to std::cerr. This is intended to be used +// from within a debugging session. + +void +debug_go_block(const Block* block) +{ + if (block == NULL) + std::cerr << ""; + else + { + std::cerr << "Block " << block + << " (enclosing " << block->enclosing() << "):\n"; + const std::vector* stmts = block->statements(); + if (stmts != NULL) + { + for (size_t i = 0; i < stmts->size(); ++i) + { + debug_go_statement(stmts->at(i)); + } + } + } +} + +// Deep dump of a block to std:cerr. This is intended to be used +// from within a debugging session. + +void +debug_go_block_deep(const Block* block) +{ + Ast_dump_context adc(&std::cerr, true); + Block* ncblock = const_cast(block); + adc.dump_block(ncblock); +} -- 2.30.2