]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Add dump configuration options, fix node exclusion
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Tue, 9 Dec 2025 04:49:03 +0000 (05:49 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Wed, 17 Dec 2025 06:20:12 +0000 (07:20 +0100)
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
gcc/rust/ast/rust-ast-collector.h
gcc/rust/ast/rust-ast-dump.cc
gcc/rust/ast/rust-ast-dump.h
gcc/rust/rust-session-manager.cc

index 737100a59b32a8f4600c7441d59a33af07774074..db146cdd24cb4ecc3eed6240d14214305cb83d2e 100644 (file)
@@ -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
index 6658cdaa70321b2b1fd7e4e12edf4e15d7a8d4cf..83297c9a61cfe344a4f067d419d602e3620742b7 100644 (file)
@@ -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
    */
index 6e9c9d437defafdb5e76026ca268f410fff019b4..e22b5a9311b489bf9ae8c239ce458c4367596bb5 100644 (file)
 
 #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)
index 2d32e68932d156090081c0d0a252e3d77d49740a..6d4476436e41e6e70fcf903be94ab5b828bff272 100644 (file)
@@ -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);
index 901339c9c412d42230c1ab2188cceec96881dd1f..c948f3d1548604ebe67f89a0e2ad8067c221a2f1 100644 (file)
@@ -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 ();
 }