aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-06-23 14:33:53 +0200
committerCohenArthur <arthur.cohen@embecosm.com>2023-06-29 12:30:17 +0000
commit2ddf542ed6710dd428ef810c1c2c618d4bd55388 (patch)
treefe01d5dbdba8cb7a55029208f449c5cc2715ad05 /gcc
parent18a67ff6bfd0d4e8219e2f5449831ab19b8a8ea7 (diff)
downloadgcc-2ddf542ed6710dd428ef810c1c2c618d4bd55388.zip
gcc-2ddf542ed6710dd428ef810c1c2c618d4bd55388.tar.gz
gcc-2ddf542ed6710dd428ef810c1c2c618d4bd55388.tar.bz2
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>
Diffstat (limited to 'gcc')
-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 dae93cf..3243e34 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 16c43f3..d0e0277 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 ();
+ }
}
}