Skip to content

Commit

Permalink
Add clarity to the code
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Kamiinarii committed Dec 16, 2024
1 parent e51ebf9 commit 7d9b3fd
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 29 deletions.
4 changes: 2 additions & 2 deletions gcc/rust/ast/rust-ast-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ TokenCollector::begin_internal_comment (std::string comment)
{
std::string symbol_begin ("(");

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

void
TokenCollector::end_internal_comment (std::string comment)
{
std::string symbol_end (")!");

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

void
Expand Down
13 changes: 10 additions & 3 deletions gcc/rust/ast/rust-ast-collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "rust-ast-visitor.h"
#include "rust-ast.h"
#include "rust-ast-full.h"
#include <sstream>
#include "rust-system.h"

namespace Rust {
namespace AST {
Expand All @@ -40,9 +40,16 @@ class CollectItem
Token,
};

enum class Comment
{
Regular,
Internal,
};

CollectItem (TokenPtr token) : token (token), kind (Kind::Token) {}
CollectItem (std::string comment, bool internal = false)
: comment (comment), kind (internal ? Kind::InternalComment : Kind::Comment)
CollectItem (std::string comment, Comment type = Comment::Regular)
: comment (comment),
kind (type == Comment::Internal ? Kind::InternalComment : Kind::Comment)
{}
CollectItem (Kind kind) : kind (kind) { rust_assert (kind != Kind::Token); }
CollectItem (size_t level) : indent_level (level), kind (Kind::Indentation) {}
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/ast/rust-ast-dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ Dump::Dump (std::ostream &stream)
{}

Dump::Dump (std::ostream &stream, bool print_internal,
std::vector<std::string> blacklist)
std::set<std::string> excluded_node)
: stream (stream), indentation (Indent ()), print_internal (print_internal)
{
internal_blacklist = blacklist;
excluded_node = excluded_node;
}

bool
Expand Down
13 changes: 7 additions & 6 deletions gcc/rust/ast/rust-ast-dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "rust-ast.h"
#include "rust-ast-full.h"
#include "rust-dump.h"
#include "rust-system.h"

#include "rust-ast-collector.h"

Expand All @@ -33,7 +34,7 @@ class Dump
public:
Dump (std::ostream &stream);
Dump (std::ostream &stream, bool print_internal,
std::vector<std::string> blacklist);
std::set<std::string> excluded_node);

/**
* Run the visitor on an entire crate and its items
Expand Down Expand Up @@ -75,17 +76,17 @@ class Dump
case AST::CollectItem::Kind::InternalComment:
if (print_internal)
{
bool is_blacklisted = false;
bool is_excluded = false;
std::string comment = item.get_internal_comment ();
for (auto &node : internal_blacklist)
for (auto &node : excluded_node)
{
if (comment.find (node) != std::string::npos)
{
is_blacklisted = true;
is_excluded = true;
break;
}
}
if (!is_blacklisted)
if (!is_excluded)
stream << " /* " << comment << " */ ";
}
break;
Expand All @@ -102,7 +103,7 @@ class Dump
std::ostream &stream;
Indent indentation;
bool print_internal;
std::vector<std::string> internal_blacklist;
std::set<std::string> excluded_node;

static bool require_spacing (TokenPtr previous, TokenPtr current);
};
Expand Down
17 changes: 9 additions & 8 deletions gcc/rust/rust-session-manager.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2024 Free Software Foundation, Inc.
// Copyright (C) 2020 - 2024 Free Software Foundation, Inc.

// This file is part of GCC.

Expand Down Expand Up @@ -59,8 +59,7 @@
#include "selftest.h"
#include "tm.h"
#include "rust-target.h"
#include <sstream>
#include <vector>
#include "rust-system.h"

extern bool
saw_errors (void);
Expand Down Expand Up @@ -394,7 +393,7 @@ Session::enable_dump (std::string arg)
arg.c_str (), ":");
return false;
}
handle_internal_blacklist (arg);
handle_excluded_node (arg);
options.enable_dump_option (CompileOptions::INTERNAL_DUMP);
}
}
Expand All @@ -416,13 +415,15 @@ Session::enable_dump (std::string arg)
*/

void
Session::handle_internal_blacklist (std::string arg)
Session::handle_excluded_node (std::string arg)
{
std::istringstream blist_str (arg.substr (arg.find (":") + 1, 50));
const int size_node_string = 50;
std::istringstream blist_str (
arg.substr (arg.find (":") + 1, size_node_string));
std::string token;
while (std::getline (blist_str, token, ','))
{
options.add_blacklist (token);
options.add_excluded (token);
}
}

Expand Down Expand Up @@ -1058,7 +1059,7 @@ Session::dump_ast_pretty_internal (AST::Crate &crate) const
return;
}

std::vector<std::string> str_tmp = options.get_blacklist ();
std::set<std::string> str_tmp = options.get_excluded ();

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

Expand Down
13 changes: 5 additions & 8 deletions gcc/rust/rust-session-manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ struct CompileOptions

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

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

void add_blacklist (std::string node)
void add_excluded (std::string node)
{
rust_assert (!node.empty ());
internal_blacklist.push_back (node);
excluded_node.insert (node);
}

const std::vector<std::string> get_blacklist () const
{
return internal_blacklist;
}
const std::set<std::string> get_excluded () const { return excluded_node; }

void set_crate_name (std::string name)
{
Expand Down Expand Up @@ -418,7 +415,7 @@ struct Session
void dump_hir (HIR::Crate &crate) const;
void dump_hir_pretty (HIR::Crate &crate) const;

void handle_internal_blacklist (std::string arg);
void handle_excluded_node (std::string arg);

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

0 comments on commit 7d9b3fd

Please sign in to comment.