Skip to content

Commit 369888f

Browse files
committed
Add dump configuration options, fix node exclusion
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>
1 parent 38ea79d commit 369888f

File tree

5 files changed

+78
-49
lines changed

5 files changed

+78
-49
lines changed

gcc/rust/ast/rust-ast-collector.cc

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,33 +103,15 @@ TokenCollector::comment (std::string comment)
103103
tokens.emplace_back (CollectItem::make_comment (comment));
104104
}
105105

106-
void
107-
TokenCollector::begin_describe_node (const std::string &node_name)
108-
{
109-
const std::string symbol_begin ("(");
110-
111-
tokens.emplace_back (
112-
CollectItem::make_node_description (node_name + symbol_begin));
113-
}
114-
115-
void
116-
TokenCollector::end_describe_node (const std::string &node_name)
117-
{
118-
const std::string symbol_end (")!");
119-
120-
tokens.push_back (
121-
CollectItem::make_node_description (symbol_end + node_name));
122-
}
123-
124106
void
125107
TokenCollector::describe_node (const std::string &node_name,
126108
std::function<void ()> visitor)
127109
{
128-
begin_describe_node (node_name);
110+
tokens.emplace_back (CollectItem::make_begin_node_description (node_name));
129111

130112
visitor ();
131113

132-
end_describe_node (node_name);
114+
tokens.push_back (CollectItem::make_end_node_description (node_name));
133115
}
134116

135117
void

gcc/rust/ast/rust-ast-collector.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class CollectItem
3535
{
3636
Comment,
3737
InternalComment,
38-
NodeDescription,
38+
BeginNodeDescription,
39+
EndNodeDescription,
3940
Newline,
4041
Indentation,
4142
Token,
@@ -54,9 +55,17 @@ class CollectItem
5455
{
5556
return CollectItem (comment, Kind::Comment);
5657
}
57-
static CollectItem make_node_description (const std::string &node_description)
58+
59+
static CollectItem
60+
make_begin_node_description (const std::string &node_description)
61+
{
62+
return CollectItem (node_description, Kind::BeginNodeDescription);
63+
}
64+
65+
static CollectItem
66+
make_end_node_description (const std::string &node_description)
5867
{
59-
return CollectItem (node_description, Kind::NodeDescription);
68+
return CollectItem (node_description, Kind::EndNodeDescription);
6069
}
6170

6271
Kind get_kind () { return kind; }
@@ -87,7 +96,8 @@ class CollectItem
8796

8897
std::string get_node_description ()
8998
{
90-
rust_assert (kind == Kind::NodeDescription);
99+
rust_assert (kind == Kind::BeginNodeDescription
100+
|| kind == Kind::EndNodeDescription);
91101
return comment;
92102
}
93103

@@ -207,8 +217,6 @@ class TokenCollector : public ASTVisitor
207217
void increment_indentation ();
208218
void decrement_indentation ();
209219
void comment (std::string comment);
210-
void begin_describe_node (const std::string &node_name);
211-
void end_describe_node (const std::string &node_name);
212220
/**
213221
* Visit common items of functions: Parameters, return type, block
214222
*/

gcc/rust/ast/rust-ast-dump.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,24 @@
1818

1919
#include "rust-ast-dump.h"
2020
#include "rust-expr.h"
21-
#include <vector>
2221

2322
namespace Rust {
2423
namespace AST {
2524

2625
Dump::Dump (std::ostream &stream)
27-
: stream (stream), indentation (Indent ()), print_internal (false)
26+
: stream (stream), indentation (Indent ()),
27+
configuration (Configuration{
28+
Configuration::InternalComment::Hide,
29+
Configuration::NodeDescription::Hide,
30+
Configuration::Comment::Dump,
31+
})
2832
{}
2933

30-
Dump::Dump (std::ostream &stream, bool print_internal,
34+
Dump::Dump (std::ostream &stream, Configuration configuration,
3135
std::set<std::string> excluded_node)
32-
: stream (stream), indentation (Indent ()), print_internal (print_internal)
33-
{
34-
excluded_node = excluded_node;
35-
}
36+
: stream (stream), indentation (Indent ()), configuration (configuration),
37+
excluded_node (excluded_node)
38+
{}
3639

3740
bool
3841
Dump::require_spacing (TokenPtr previous, TokenPtr current)

gcc/rust/ast/rust-ast-dump.h

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,27 @@ namespace AST {
3232
class Dump
3333
{
3434
public:
35+
struct Configuration
36+
{
37+
enum class InternalComment
38+
{
39+
Dump,
40+
Hide,
41+
} dump_internal_comments;
42+
enum class NodeDescription
43+
{
44+
Dump,
45+
Hide,
46+
} dump_node_description;
47+
enum class Comment
48+
{
49+
Dump,
50+
Hide,
51+
} dump_comments;
52+
};
53+
3554
Dump (std::ostream &stream);
36-
Dump (std::ostream &stream, bool print_internal,
55+
Dump (std::ostream &stream, Configuration configuration,
3756
std::set<std::string> excluded_node);
3857

3958
/**
@@ -62,7 +81,8 @@ class Dump
6281
}
6382
break;
6483
case AST::CollectItem::Kind::Comment:
65-
stream << " /* " << item.get_comment () << " */ ";
84+
if (configuration.dump_comments == Configuration::Comment::Dump)
85+
stream << " /* " << item.get_comment () << " */ ";
6686
break;
6787
case AST::CollectItem::Kind::Indentation:
6888
for (size_t i = 0; i < item.get_indent_level (); i++)
@@ -74,21 +94,30 @@ class Dump
7494
stream << "\n";
7595
previous = nullptr;
7696
break;
97+
case AST::CollectItem::Kind::BeginNodeDescription:
98+
if (configuration.dump_node_description
99+
== Configuration::NodeDescription::Dump)
100+
{
101+
std::string comment = item.get_node_description ();
102+
if (excluded_node.find (comment) == excluded_node.end ())
103+
stream << " /* " << comment << " */ ";
104+
}
105+
break;
106+
case AST::CollectItem::Kind::EndNodeDescription:
107+
if (configuration.dump_node_description
108+
== Configuration::NodeDescription::Dump)
109+
{
110+
std::string comment = item.get_node_description ();
111+
if (excluded_node.find (comment) == excluded_node.end ())
112+
stream << " /* !" << comment << " */ ";
113+
}
114+
break;
77115
case AST::CollectItem::Kind::InternalComment:
78-
if (print_internal)
116+
if (configuration.dump_internal_comments
117+
== Configuration::InternalComment::Dump)
79118
{
80-
bool is_excluded = false;
81119
std::string comment = item.get_internal_comment ();
82-
for (auto &node : excluded_node)
83-
{
84-
if (comment.find (node) != std::string::npos)
85-
{
86-
is_excluded = true;
87-
break;
88-
}
89-
}
90-
if (!is_excluded)
91-
stream << " /* " << comment << " */ ";
120+
stream << " /* " << comment << " */ ";
92121
}
93122
break;
94123
default:
@@ -103,7 +132,7 @@ class Dump
103132
private:
104133
std::ostream &stream;
105134
Indent indentation;
106-
bool print_internal;
135+
Configuration configuration;
107136
std::set<std::string> excluded_node;
108137

109138
static bool require_spacing (TokenPtr previous, TokenPtr current);

gcc/rust/rust-session-manager.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,14 @@ Session::dump_ast_pretty_internal (AST::Crate &crate) const
10811081

10821082
std::set<std::string> str_tmp = options.get_excluded ();
10831083

1084-
AST::Dump (out, true, str_tmp).go (crate);
1084+
AST::Dump (out,
1085+
AST::Dump::Configuration{
1086+
AST::Dump::Configuration::InternalComment::Dump,
1087+
AST::Dump::Configuration::NodeDescription::Dump,
1088+
AST::Dump::Configuration::Comment::Dump,
1089+
},
1090+
str_tmp)
1091+
.go (crate);
10851092

10861093
out.close ();
10871094
}

0 commit comments

Comments
 (0)