aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-06-23 14:33:53 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2024-01-16 18:46:31 +0100
commit671517fc2e28d7b3f8caa2d2d1b26365285d3d99 (patch)
tree33119b5f8f5c0edac54b8b44313a036db58079c8
parent1d28a62a0d75be55fd08c7cf163825d1b7caf967 (diff)
downloadgcc-671517fc2e28d7b3f8caa2d2d1b26365285d3d99.zip
gcc-671517fc2e28d7b3f8caa2d2d1b26365285d3d99.tar.gz
gcc-671517fc2e28d7b3f8caa2d2d1b26365285d3d99.tar.bz2
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 <pierre-emmanuel.patry@embecosm.com>
-rw-r--r--gcc/rust/ast/rust-ast-collector.cc1
-rw-r--r--gcc/rust/ast/rust-ast-dump.cc5
-rw-r--r--gcc/rust/ast/rust-ast-dump.h35
3 files changed, 34 insertions, 7 deletions
diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc
index 89c693e..38eb317 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 7c90592..e8a3c08 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 ec72ba1..38df5f3 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 ();
+ }
}
}