From: Pierre-Emmanuel Patry Date: Fri, 23 Jun 2023 12:33:53 +0000 (+0200) Subject: gccrs: dump: Add formatting to dump X-Git-Tag: basepoints/gcc-15~2427 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=671517fc2e28d7b3f8caa2d2d1b26365285d3d99;p=thirdparty%2Fgcc.git gccrs: dump: Add formatting to dump Add formatting character output to ast dump. gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Add comment after tail expressions. * ast/rust-ast-dump.cc (Dump::require_spacing): Check for missing previous token. * ast/rust-ast-dump.h: Output formatting characters. Signed-off-by: Pierre-Emmanuel Patry --- diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc index 89c693e0891d..38eb31780f95 100644 --- a/gcc/rust/ast/rust-ast-collector.cc +++ b/gcc/rust/ast/rust-ast-collector.cc @@ -1276,6 +1276,7 @@ TokenCollector::visit (BlockExpr &expr) if (expr.has_tail_expr ()) { visit (expr.get_tail_expr ()); + comment ("tail expr"); newline (); } diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index 7c90592ffa31..e8a3c08834c0 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -27,6 +27,11 @@ Dump::Dump (std::ostream &stream) : stream (stream), indentation (Indent ()) {} bool Dump::require_spacing (TokenPtr previous, TokenPtr current) { + if (previous == nullptr) + { + return false; + } + switch (current->get_id ()) { case EXCLAM: diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index ec72ba14c4ad..38df5f3951b4 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -44,14 +44,35 @@ public: TokenCollector collector; collector.visit (v); - auto tokens = collector.collect_tokens (); - if (!tokens.empty ()) - stream << tokens.front ()->as_string (); - for (auto it = tokens.cbegin () + 1; it < tokens.cend (); it++) + TokenPtr previous = nullptr; + for (auto item : collector.collect ()) { - if (require_spacing (*(it - 1), *it)) - stream << " "; - stream << (*it)->as_string (); + switch (item.get_kind ()) + { + case AST::CollectItem::Kind::Token: { + TokenPtr current = item.get_token (); + if (require_spacing (previous, current)) + stream << " "; + stream << current->as_string (); + previous = current; + } + break; + case AST::CollectItem::Kind::Comment: + stream << " /* " << item.get_comment () << " */ "; + break; + case AST::CollectItem::Kind::Indentation: + for (size_t i = 0; i < item.get_indent_level (); i++) + { + stream << " "; + } + break; + case AST::CollectItem::Kind::Newline: + stream << "\n"; + previous = nullptr; + break; + default: + gcc_unreachable (); + } } }