Skip to content

Commit 7d9b3fd

Browse files
committed
Add clarity to the code
Use an enum instead of bool to check if a comment is internal, use good include and replace some variable name. gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::begin_internal_comment): Change a boolean with an enum. (TokenCollector::end_internal_comment): Likewise. * ast/rust-ast-collector.h: Likewise + change include. * ast/rust-ast-dump.cc (Dump::Dump): Change variable name. * ast/rust-ast-dump.h: Likewise + replace vector with a set. * rust-session-manager.cc (Session::enable_dump): Change variable name. (Session::handle_internal_blacklist): Change function name. (Session::handle_excluded_node): Likewise. (Session::dump_ast_pretty_internal): Change vector with a set. * rust-session-manager.h (struct CompileOptions): Likewise + change variable name. Signed-off-by: Benjamin Thos <[email protected]>
1 parent e51ebf9 commit 7d9b3fd

File tree

6 files changed

+35
-29
lines changed

6 files changed

+35
-29
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ TokenCollector::begin_internal_comment (std::string comment)
106106
{
107107
std::string symbol_begin ("(");
108108

109-
tokens.push_back ({comment + symbol_begin, true});
109+
tokens.push_back ({comment + symbol_begin, CollectItem::Comment::Internal});
110110
}
111111

112112
void
113113
TokenCollector::end_internal_comment (std::string comment)
114114
{
115115
std::string symbol_end (")!");
116116

117-
tokens.push_back ({symbol_end + comment, true});
117+
tokens.push_back ({symbol_end + comment, CollectItem::Comment::Internal});
118118
}
119119

120120
void

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "rust-ast-visitor.h"
2424
#include "rust-ast.h"
2525
#include "rust-ast-full.h"
26-
#include <sstream>
26+
#include "rust-system.h"
2727

2828
namespace Rust {
2929
namespace AST {
@@ -40,9 +40,16 @@ class CollectItem
4040
Token,
4141
};
4242

43+
enum class Comment
44+
{
45+
Regular,
46+
Internal,
47+
};
48+
4349
CollectItem (TokenPtr token) : token (token), kind (Kind::Token) {}
44-
CollectItem (std::string comment, bool internal = false)
45-
: comment (comment), kind (internal ? Kind::InternalComment : Kind::Comment)
50+
CollectItem (std::string comment, Comment type = Comment::Regular)
51+
: comment (comment),
52+
kind (type == Comment::Internal ? Kind::InternalComment : Kind::Comment)
4653
{}
4754
CollectItem (Kind kind) : kind (kind) { rust_assert (kind != Kind::Token); }
4855
CollectItem (size_t level) : indent_level (level), kind (Kind::Indentation) {}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ Dump::Dump (std::ostream &stream)
2828
{}
2929

3030
Dump::Dump (std::ostream &stream, bool print_internal,
31-
std::vector<std::string> blacklist)
31+
std::set<std::string> excluded_node)
3232
: stream (stream), indentation (Indent ()), print_internal (print_internal)
3333
{
34-
internal_blacklist = blacklist;
34+
excluded_node = excluded_node;
3535
}
3636

3737
bool

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "rust-ast.h"
2323
#include "rust-ast-full.h"
2424
#include "rust-dump.h"
25+
#include "rust-system.h"
2526

2627
#include "rust-ast-collector.h"
2728

@@ -33,7 +34,7 @@ class Dump
3334
public:
3435
Dump (std::ostream &stream);
3536
Dump (std::ostream &stream, bool print_internal,
36-
std::vector<std::string> blacklist);
37+
std::set<std::string> excluded_node);
3738

3839
/**
3940
* Run the visitor on an entire crate and its items
@@ -75,17 +76,17 @@ class Dump
7576
case AST::CollectItem::Kind::InternalComment:
7677
if (print_internal)
7778
{
78-
bool is_blacklisted = false;
79+
bool is_excluded = false;
7980
std::string comment = item.get_internal_comment ();
80-
for (auto &node : internal_blacklist)
81+
for (auto &node : excluded_node)
8182
{
8283
if (comment.find (node) != std::string::npos)
8384
{
84-
is_blacklisted = true;
85+
is_excluded = true;
8586
break;
8687
}
8788
}
88-
if (!is_blacklisted)
89+
if (!is_excluded)
8990
stream << " /* " << comment << " */ ";
9091
}
9192
break;
@@ -102,7 +103,7 @@ class Dump
102103
std::ostream &stream;
103104
Indent indentation;
104105
bool print_internal;
105-
std::vector<std::string> internal_blacklist;
106+
std::set<std::string> excluded_node;
106107

107108
static bool require_spacing (TokenPtr previous, TokenPtr current);
108109
};

gcc/rust/rust-session-manager.cc

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2020-2024 Free Software Foundation, Inc.
1+
// Copyright (C) 2020 - 2024 Free Software Foundation, Inc.
22

33
// This file is part of GCC.
44

@@ -59,8 +59,7 @@
5959
#include "selftest.h"
6060
#include "tm.h"
6161
#include "rust-target.h"
62-
#include <sstream>
63-
#include <vector>
62+
#include "rust-system.h"
6463

6564
extern bool
6665
saw_errors (void);
@@ -394,7 +393,7 @@ Session::enable_dump (std::string arg)
394393
arg.c_str (), ":");
395394
return false;
396395
}
397-
handle_internal_blacklist (arg);
396+
handle_excluded_node (arg);
398397
options.enable_dump_option (CompileOptions::INTERNAL_DUMP);
399398
}
400399
}
@@ -416,13 +415,15 @@ Session::enable_dump (std::string arg)
416415
*/
417416

418417
void
419-
Session::handle_internal_blacklist (std::string arg)
418+
Session::handle_excluded_node (std::string arg)
420419
{
421-
std::istringstream blist_str (arg.substr (arg.find (":") + 1, 50));
420+
const int size_node_string = 50;
421+
std::istringstream blist_str (
422+
arg.substr (arg.find (":") + 1, size_node_string));
422423
std::string token;
423424
while (std::getline (blist_str, token, ','))
424425
{
425-
options.add_blacklist (token);
426+
options.add_excluded (token);
426427
}
427428
}
428429

@@ -1058,7 +1059,7 @@ Session::dump_ast_pretty_internal (AST::Crate &crate) const
10581059
return;
10591060
}
10601061

1061-
std::vector<std::string> str_tmp = options.get_blacklist ();
1062+
std::set<std::string> str_tmp = options.get_excluded ();
10621063

10631064
AST::Dump (out, true, str_tmp).go (crate);
10641065

gcc/rust/rust-session-manager.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ struct CompileOptions
230230

231231
/* List of node that is not print during the dump of the ast with internal
232232
* comment */
233-
std::vector<std::string> internal_blacklist;
233+
std::set<std::string> excluded_node;
234234

235235
/* configuration options - actually useful for conditional compilation and
236236
* whatever data related to target arch, features, os, family, env, endian,
@@ -291,16 +291,13 @@ struct CompileOptions
291291
enable_dump_option (DumpOption::INTERNAL_DUMP);
292292
}
293293

294-
void add_blacklist (std::string node)
294+
void add_excluded (std::string node)
295295
{
296296
rust_assert (!node.empty ());
297-
internal_blacklist.push_back (node);
297+
excluded_node.insert (node);
298298
}
299299

300-
const std::vector<std::string> get_blacklist () const
301-
{
302-
return internal_blacklist;
303-
}
300+
const std::set<std::string> get_excluded () const { return excluded_node; }
304301

305302
void set_crate_name (std::string name)
306303
{
@@ -418,7 +415,7 @@ struct Session
418415
void dump_hir (HIR::Crate &crate) const;
419416
void dump_hir_pretty (HIR::Crate &crate) const;
420417

421-
void handle_internal_blacklist (std::string arg);
418+
void handle_excluded_node (std::string arg);
422419

423420
// pipeline stages - TODO maybe move?
424421
/* Register plugins pipeline stage. TODO maybe move to another object?

0 commit comments

Comments
 (0)