]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: dump: Output separating space under condition
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Wed, 21 Jun 2023 22:16:17 +0000 (00:16 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:46:31 +0000 (18:46 +0100)
Separating space shall not be output between every tokens to make the
dump clear and easy to read.

gcc/rust/ChangeLog:

* ast/rust-ast-dump.cc (Dump::require_spacing): Add a function
to determine wether a space shall output.
* ast/rust-ast-dump.h: Add function prototype as well as
condition.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/ast/rust-ast-dump.cc
gcc/rust/ast/rust-ast-dump.h

index 56202a7614b95851ec8ee4925dff1c41db17b3aa..7c90592ffa31b654c312251dbb3c7935ccf319c9 100644 (file)
@@ -24,6 +24,38 @@ namespace AST {
 
 Dump::Dump (std::ostream &stream) : stream (stream), indentation (Indent ()) {}
 
+bool
+Dump::require_spacing (TokenPtr previous, TokenPtr current)
+{
+  switch (current->get_id ())
+    {
+    case EXCLAM:
+    case DOT_DOT:
+    case DOT_DOT_EQ:
+    case SCOPE_RESOLUTION:
+    case LEFT_PAREN:
+    case LEFT_ANGLE:
+    case LEFT_SQUARE:
+    case RIGHT_SQUARE:
+    case RIGHT_PAREN:
+    case DOLLAR_SIGN:
+    case SEMICOLON:
+      return false;
+    default:
+      break;
+    }
+
+  switch (previous->get_id ())
+    {
+    case SCOPE_RESOLUTION:
+    case LEFT_SQUARE:
+    case LEFT_PAREN:
+      return false;
+    default:
+      return true;
+    }
+}
+
 void
 Dump::debug (Visitable &v)
 {
index 14d22ecf06bc4bff7cb29aa3760d74dab8d19750..c5b4ab125db4530dedaeb4ced913166113fd7872 100644 (file)
@@ -45,9 +45,14 @@ public:
     TokenCollector collector (container);
     collector.visit (v);
 
-    for (auto &token : collector.collect_tokens ())
+    auto tokens = collector.collect_tokens ();
+    if (!tokens.empty ())
+      stream << tokens.front ()->as_string ();
+    for (auto it = tokens.cbegin () + 1; it < tokens.cend (); it++)
       {
-       stream << token->as_string () << " ";
+       if (require_spacing (*(it - 1), *it))
+         stream << " ";
+       stream << (*it)->as_string ();
       }
   }
 
@@ -57,6 +62,8 @@ public:
 private:
   std::ostream &stream;
   Indent indentation;
+
+  static bool require_spacing (TokenPtr previous, TokenPtr current);
 };
 
 } // namespace AST