]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: dump: Add formatting to dump
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Fri, 23 Jun 2023 12:33:53 +0000 (14:33 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:46:31 +0000 (18:46 +0100)
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>
gcc/rust/ast/rust-ast-collector.cc
gcc/rust/ast/rust-ast-dump.cc
gcc/rust/ast/rust-ast-dump.h

index 89c693e0891dda540e5f47511362191133445986..38eb31780f95afe080d782ac1774c5e6f0dbb28c 100644 (file)
@@ -1276,6 +1276,7 @@ TokenCollector::visit (BlockExpr &expr)
   if (expr.has_tail_expr ())
     {
       visit (expr.get_tail_expr ());
+      comment ("tail expr");
       newline ();
     }
 
index 7c90592ffa31b654c312251dbb3c7935ccf319c9..e8a3c08834c05d926c10d85d6bddbe63b494236e 100644 (file)
@@ -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:
index ec72ba14c4adc7ab62e71e9986dbea1f38f4c5d4..38df5f3951b43c15c71c0c4571351fdc81e4678d 100644 (file)
@@ -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 ();
+         }
       }
   }