diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-08-25 14:03:30 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-25 14:03:30 +0000 |
commit | 5b3c4be73fb72cb87d9a135415ed421a0808ff3b (patch) | |
tree | a78b71c36000744b2c44bac59306a4bd6cce6bbe /gcc | |
parent | d3cf195ab46d7effe806990aa6b7a409bf8e46df (diff) | |
parent | a9055d8294f28ae84023cc93ae8d8b14747a2d0c (diff) | |
download | gcc-5b3c4be73fb72cb87d9a135415ed421a0808ff3b.zip gcc-5b3c4be73fb72cb87d9a135415ed421a0808ff3b.tar.gz gcc-5b3c4be73fb72cb87d9a135415ed421a0808ff3b.tar.bz2 |
Merge #1473
1473: Improve AST dump r=CohenArthur a=CohenArthur
Let's abuse C++ a little bit to make dumping and the chaining of dumping operations easier. If anyone has input feel free, this is definitely a roundabout way to go about it.
This also adds pretty printing of the expanded AST, which can be useful to debug macros or `cfg` invocations
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/ast/rust-ast-dump.cc | 96 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast-dump.h | 6 | ||||
-rw-r--r-- | gcc/rust/rust-session-manager.cc | 10 | ||||
-rw-r--r-- | gcc/rust/rust-session-manager.h | 2 |
4 files changed, 79 insertions, 35 deletions
diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index ad9ad0b..8ad00b5 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -17,6 +17,7 @@ // <http://www.gnu.org/licenses/>. #include "rust-ast-dump.h" +#include "rust-diagnostics.h" namespace Rust { namespace AST { @@ -51,7 +52,7 @@ Dump::go (AST::Crate &crate) { stream << indentation; item->accept_vis (*this); - stream << "\n"; + stream << '\n'; } } @@ -72,8 +73,7 @@ Dump::format_function_param (FunctionParam ¶m) void Dump::emit_attrib (const Attribute &attrib) { - stream << "#"; - stream << "["; + stream << "#["; for (size_t i = 0; i < attrib.get_path ().get_segments ().size (); i++) { @@ -108,6 +108,13 @@ Dump::emit_attrib (const Attribute &attrib) stream << "]"; } +std::ostream & +Dump::emit_indented_string (const std::string &value, + const std::string &comment) +{ + return stream << indentation << value << comment; +} + void Dump::visit (Token &tok) {} @@ -141,7 +148,9 @@ Dump::visit (ConstGenericParam &lifetime_param) // rust-path.h void Dump::visit (PathInExpression &path) -{} +{ + stream << path.as_string (); +} void Dump::visit (TypePathSegment &segment) @@ -163,7 +172,9 @@ Dump::visit (TypePath &path) void Dump::visit (QualifiedPathInExpression &path) -{} +{ + stream << path.as_string (); +} void Dump::visit (QualifiedPathInType &path) @@ -207,53 +218,52 @@ Dump::visit (NegationExpr &expr) void Dump::visit (ArithmeticOrLogicalExpr &expr) { - expr.get_left_expr ()->accept_vis (*this); - stream << " "; - + auto op = ""; switch (expr.get_expr_type ()) { case ArithmeticOrLogicalOperator::ADD: - stream << "+"; + op = "+"; break; case ArithmeticOrLogicalOperator::SUBTRACT: - stream << "-"; + op = "-"; break; case ArithmeticOrLogicalOperator::MULTIPLY: - stream << "*"; + op = "*"; break; case ArithmeticOrLogicalOperator::DIVIDE: - stream << "/"; + op = "/"; break; case ArithmeticOrLogicalOperator::MODULUS: - stream << "%"; + op = "%"; break; case ArithmeticOrLogicalOperator::BITWISE_AND: - stream << "&"; + op = "&"; break; case ArithmeticOrLogicalOperator::BITWISE_OR: - stream << "|"; + op = "|"; break; case ArithmeticOrLogicalOperator::BITWISE_XOR: - stream << "^"; + op = "^"; break; case ArithmeticOrLogicalOperator::LEFT_SHIFT: - stream << "<<"; + op = "<<"; break; case ArithmeticOrLogicalOperator::RIGHT_SHIFT: - stream << ">>"; + op = ">>"; break; } - stream << " "; + expr.get_left_expr ()->accept_vis (*this); + stream << " " << op << " "; expr.get_right_expr ()->accept_vis (*this); } @@ -331,7 +341,23 @@ Dump::visit (StructExprStructBase &expr) void Dump::visit (CallExpr &expr) -{} +{ + expr.get_function_expr ()->accept_vis (*this); + stream << '('; + + indentation.increment (); + + for (auto &arg : expr.get_params ()) + { + stream << '\n' << indentation; + arg->accept_vis (*this); + stream << ','; + } + + indentation.decrement (); + + stream << '\n' << indentation << ')'; +} void Dump::visit (MethodCallExpr &expr) @@ -355,13 +381,14 @@ Dump::visit (BlockExpr &expr) { stream << indentation; stmt->accept_vis (*this); - stream << ";\n"; + stream << "; /* stmt */\n"; } if (expr.has_tail_expr ()) { stream << indentation; expr.get_tail_expr ()->accept_vis (*this); + stream << " /* tail expr */"; } indentation.decrement (); @@ -656,8 +683,10 @@ Dump::format_function_common (std::unique_ptr<Type> &return_type, if (block) { if (return_type) - stream << ' '; - block->accept_vis (*this); + { + stream << ' '; + block->accept_vis (*this); + } } else stream << ";\n"; @@ -784,12 +813,15 @@ Dump::visit (TraitImpl &impl) impl.get_trait_path ().accept_vis (*this); stream << " for "; impl.get_type ()->accept_vis (*this); - stream << " {\n"; + indentation.increment (); for (auto &item : impl.get_impl_items ()) - item->accept_vis (*this); + { + stream << indentation; + item->accept_vis (*this); + } indentation.decrement (); stream << "\n}\n"; @@ -830,11 +862,7 @@ Dump::visit (ExternBlock &block) stream << "extern "; if (block.has_abi ()) - { - stream << "\""; - stream << block.get_abi (); - stream << "\" "; - } + stream << "\"" << block.get_abi () << "\" "; stream << "{\n"; indentation.increment (); @@ -1014,11 +1042,15 @@ Dump::visit (LetStmt &stmt) void Dump::visit (ExprStmtWithoutBlock &stmt) -{} +{ + stmt.get_expr ()->accept_vis (*this); +} void Dump::visit (ExprStmtWithBlock &stmt) -{} +{ + stmt.get_expr ()->accept_vis (*this); +} // rust-type.h void diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index c3854e8..7dd38fe 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -66,6 +66,12 @@ private: void format_function_param (FunctionParam ¶m); void emit_attrib (const Attribute &attrib); + /** + * Emit an indented string with an optional extra comment + */ + std::ostream &emit_indented_string (const std::string &value, + const std::string &comment = ""); + // rust-ast.h void visit (Token &tok); void visit (DelimTokenTree &delim_tok_tree); diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index 6d7f1a8..fc66b69 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -58,6 +58,7 @@ namespace Rust { const char *kLexDumpFile = "gccrs.lex.dump"; const char *kASTDumpFile = "gccrs.ast.dump"; const char *kASTPrettyDumpFile = "gccrs.ast-pretty.dump"; +const char *kASTPrettyDumpFileExpanded = "gccrs.ast-pretty-expanded.dump"; const char *kASTExpandedDumpFile = "gccrs.ast-expanded.dump"; const char *kHIRDumpFile = "gccrs.hir.dump"; const char *kHIRPrettyDumpFile = "gccrs.hir-pretty.dump"; @@ -509,6 +510,7 @@ Session::compile_crate (const char *filename) // dump AST with expanded stuff rust_debug ("BEGIN POST-EXPANSION AST DUMP"); dump_ast_expanded (parser, parsed_crate); + dump_ast_pretty (parsed_crate, true); rust_debug ("END POST-EXPANSION AST DUMP"); } @@ -810,10 +812,14 @@ Session::dump_ast (Parser<Lexer> &parser, AST::Crate &crate) const } void -Session::dump_ast_pretty (AST::Crate &crate) const +Session::dump_ast_pretty (AST::Crate &crate, bool expanded) const { std::ofstream out; - out.open (kASTPrettyDumpFile); + if (expanded) + out.open (kASTPrettyDumpFileExpanded); + else + out.open (kASTPrettyDumpFile); + if (out.fail ()) { rust_error_at (Linemap::unknown_location (), "cannot open %s:%m; ignored", diff --git a/gcc/rust/rust-session-manager.h b/gcc/rust/rust-session-manager.h index 99dd107..b3724c5 100644 --- a/gcc/rust/rust-session-manager.h +++ b/gcc/rust/rust-session-manager.h @@ -319,7 +319,7 @@ private: void dump_lex (Parser<Lexer> &parser) const; void dump_ast (Parser<Lexer> &parser, AST::Crate &crate) const; - void dump_ast_pretty (AST::Crate &crate) const; + void dump_ast_pretty (AST::Crate &crate, bool expanded = false) const; void dump_ast_expanded (Parser<Lexer> &parser, AST::Crate &crate) const; void dump_hir (HIR::Crate &crate) const; void dump_hir_pretty (HIR::Crate &crate) const; |