[COMMITTED 07/12] gccrs: Add dump configuration options, fix node exclusion

arthur.cohen@embecosm.com arthur.cohen@embecosm.com
Wed Dec 17 06:38:19 GMT 2025


From: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

gcc/rust/ChangeLog:

	* ast/rust-ast-collector.cc (TokenCollector::begin_describe_node):
	Remove function.
	(TokenCollector::end_describe_node): Likewise.
	(TokenCollector::describe_node): Remove calls to begin/end.
	* ast/rust-ast-collector.h: Specialize begin and end collect items. Add
	more constructors to begin/end description.
	* ast/rust-ast-dump.cc (Dump::Dump): Adapt to new configuration
	options.
	* ast/rust-ast-dump.h: Add new configuration options.
	* rust-session-manager.cc (Session::dump_ast_pretty_internal): Use new
	configuration options.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
---
 gcc/rust/ast/rust-ast-collector.cc | 22 +----------
 gcc/rust/ast/rust-ast-collector.h  | 20 +++++++---
 gcc/rust/ast/rust-ast-dump.cc      | 17 +++++----
 gcc/rust/ast/rust-ast-dump.h       | 59 ++++++++++++++++++++++--------
 gcc/rust/rust-session-manager.cc   |  9 ++++-
 5 files changed, 78 insertions(+), 49 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc
index 737100a59b3..db146cdd24c 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -103,33 +103,15 @@ TokenCollector::comment (std::string comment)
   tokens.emplace_back (CollectItem::make_comment (comment));
 }
 
-void
-TokenCollector::begin_describe_node (const std::string &node_name)
-{
-  const std::string symbol_begin ("(");
-
-  tokens.emplace_back (
-    CollectItem::make_node_description (node_name + symbol_begin));
-}
-
-void
-TokenCollector::end_describe_node (const std::string &node_name)
-{
-  const std::string symbol_end (")!");
-
-  tokens.push_back (
-    CollectItem::make_node_description (symbol_end + node_name));
-}
-
 void
 TokenCollector::describe_node (const std::string &node_name,
 			       std::function<void ()> visitor)
 {
-  begin_describe_node (node_name);
+  tokens.emplace_back (CollectItem::make_begin_node_description (node_name));
 
   visitor ();
 
-  end_describe_node (node_name);
+  tokens.push_back (CollectItem::make_end_node_description (node_name));
 }
 
 void
diff --git a/gcc/rust/ast/rust-ast-collector.h b/gcc/rust/ast/rust-ast-collector.h
index 6658cdaa703..83297c9a61c 100644
--- a/gcc/rust/ast/rust-ast-collector.h
+++ b/gcc/rust/ast/rust-ast-collector.h
@@ -35,7 +35,8 @@ public:
   {
     Comment,
     InternalComment,
-    NodeDescription,
+    BeginNodeDescription,
+    EndNodeDescription,
     Newline,
     Indentation,
     Token,
@@ -54,9 +55,17 @@ public:
   {
     return CollectItem (comment, Kind::Comment);
   }
-  static CollectItem make_node_description (const std::string &node_description)
+
+  static CollectItem
+  make_begin_node_description (const std::string &node_description)
+  {
+    return CollectItem (node_description, Kind::BeginNodeDescription);
+  }
+
+  static CollectItem
+  make_end_node_description (const std::string &node_description)
   {
-    return CollectItem (node_description, Kind::NodeDescription);
+    return CollectItem (node_description, Kind::EndNodeDescription);
   }
 
   Kind get_kind () { return kind; }
@@ -87,7 +96,8 @@ public:
 
   std::string get_node_description ()
   {
-    rust_assert (kind == Kind::NodeDescription);
+    rust_assert (kind == Kind::BeginNodeDescription
+		 || kind == Kind::EndNodeDescription);
     return comment;
   }
 
@@ -207,8 +217,6 @@ private:
   void increment_indentation ();
   void decrement_indentation ();
   void comment (std::string comment);
-  void begin_describe_node (const std::string &node_name);
-  void end_describe_node (const std::string &node_name);
   /**
    * Visit common items of functions: Parameters, return type, block
    */
diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc
index 6e9c9d437de..e22b5a9311b 100644
--- a/gcc/rust/ast/rust-ast-dump.cc
+++ b/gcc/rust/ast/rust-ast-dump.cc
@@ -18,21 +18,24 @@
 
 #include "rust-ast-dump.h"
 #include "rust-expr.h"
-#include <vector>
 
 namespace Rust {
 namespace AST {
 
 Dump::Dump (std::ostream &stream)
-  : stream (stream), indentation (Indent ()), print_internal (false)
+  : stream (stream), indentation (Indent ()),
+    configuration (Configuration{
+      Configuration::InternalComment::Hide,
+      Configuration::NodeDescription::Hide,
+      Configuration::Comment::Dump,
+    })
 {}
 
-Dump::Dump (std::ostream &stream, bool print_internal,
+Dump::Dump (std::ostream &stream, Configuration configuration,
 	    std::set<std::string> excluded_node)
-  : stream (stream), indentation (Indent ()), print_internal (print_internal)
-{
-  excluded_node = excluded_node;
-}
+  : stream (stream), indentation (Indent ()), configuration (configuration),
+    excluded_node (excluded_node)
+{}
 
 bool
 Dump::require_spacing (TokenPtr previous, TokenPtr current)
diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h
index 2d32e68932d..6d4476436e4 100644
--- a/gcc/rust/ast/rust-ast-dump.h
+++ b/gcc/rust/ast/rust-ast-dump.h
@@ -32,8 +32,27 @@ namespace AST {
 class Dump
 {
 public:
+  struct Configuration
+  {
+    enum class InternalComment
+    {
+      Dump,
+      Hide,
+    } dump_internal_comments;
+    enum class NodeDescription
+    {
+      Dump,
+      Hide,
+    } dump_node_description;
+    enum class Comment
+    {
+      Dump,
+      Hide,
+    } dump_comments;
+  };
+
   Dump (std::ostream &stream);
-  Dump (std::ostream &stream, bool print_internal,
+  Dump (std::ostream &stream, Configuration configuration,
 	std::set<std::string> excluded_node);
 
   /**
@@ -62,7 +81,8 @@ public:
 	    }
 	    break;
 	  case AST::CollectItem::Kind::Comment:
-	    stream << " /* " << item.get_comment () << " */ ";
+	    if (configuration.dump_comments == Configuration::Comment::Dump)
+	      stream << " /* " << item.get_comment () << " */ ";
 	    break;
 	  case AST::CollectItem::Kind::Indentation:
 	    for (size_t i = 0; i < item.get_indent_level (); i++)
@@ -74,21 +94,30 @@ public:
 	    stream << "\n";
 	    previous = nullptr;
 	    break;
+	  case AST::CollectItem::Kind::BeginNodeDescription:
+	    if (configuration.dump_node_description
+		== Configuration::NodeDescription::Dump)
+	      {
+		std::string comment = item.get_node_description ();
+		if (excluded_node.find (comment) == excluded_node.end ())
+		  stream << " /* " << comment << " */ ";
+	      }
+	    break;
+	  case AST::CollectItem::Kind::EndNodeDescription:
+	    if (configuration.dump_node_description
+		== Configuration::NodeDescription::Dump)
+	      {
+		std::string comment = item.get_node_description ();
+		if (excluded_node.find (comment) == excluded_node.end ())
+		  stream << " /* !" << comment << " */ ";
+	      }
+	    break;
 	  case AST::CollectItem::Kind::InternalComment:
-	    if (print_internal)
+	    if (configuration.dump_internal_comments
+		== Configuration::InternalComment::Dump)
 	      {
-		bool is_excluded = false;
 		std::string comment = item.get_internal_comment ();
-		for (auto &node : excluded_node)
-		  {
-		    if (comment.find (node) != std::string::npos)
-		      {
-			is_excluded = true;
-			break;
-		      }
-		  }
-		if (!is_excluded)
-		  stream << " /* " << comment << " */ ";
+		stream << " /* " << comment << " */ ";
 	      }
 	    break;
 	  default:
@@ -103,7 +132,7 @@ public:
 private:
   std::ostream &stream;
   Indent indentation;
-  bool print_internal;
+  Configuration configuration;
   std::set<std::string> excluded_node;
 
   static bool require_spacing (TokenPtr previous, TokenPtr current);
diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc
index 901339c9c41..c948f3d1548 100644
--- a/gcc/rust/rust-session-manager.cc
+++ b/gcc/rust/rust-session-manager.cc
@@ -1081,7 +1081,14 @@ Session::dump_ast_pretty_internal (AST::Crate &crate) const
 
   std::set<std::string> str_tmp = options.get_excluded ();
 
-  AST::Dump (out, true, str_tmp).go (crate);
+  AST::Dump (out,
+	     AST::Dump::Configuration{
+	       AST::Dump::Configuration::InternalComment::Dump,
+	       AST::Dump::Configuration::NodeDescription::Dump,
+	       AST::Dump::Configuration::Comment::Dump,
+	     },
+	     str_tmp)
+    .go (crate);
 
   out.close ();
 }
-- 
2.50.1



More information about the Gcc-rust mailing list