Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dump option to show node #3212

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Kamiinarii78
Copy link

Add a new dump option to show node node as
internal comment with a blacklist to ignore some
node.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::begin_internal_comment): Add internal comment to print node beginning. (TokenCollector::end_internal_comment): Add internal comment to print node end. (TokenCollector::visit): Add the comments of the node visited. (TokenCollector::visit_closure_common): Likewise. (TokenCollector::visit_loop_common): Likewise.
* ast/rust-ast-collector.h: Add internal comment as a nes Kind.
* ast/rust-ast-dump.cc (Dump::Dump): add a Dump constructor to enable internal.
* ast/rust-ast-dump.h: Add printing of internal comment in the dump
* rust-session-manager.cc (Session::enable_dump): Activate ast dump and fill the blacklist. (Session::handle_internal_blacklist): Parse the flag to get node to be blacklisted. (Session::compile_crate): Launch ast dump internal when asked. (Session::dump_ast_pretty_internal): Call the visitor to dump the internals.
* rust-session-manager.h (struct CompileOptions): add Interal in Dump option enum.

Add a new dump option to show node node as
internal comment with a blacklist to ignore some
node.

gcc/rust/ChangeLog:

	* ast/rust-ast-collector.cc (TokenCollector::begin_internal_comment):
	Add internal comment to print node beginning.
	(TokenCollector::end_internal_comment): Add internal comment to print
	node end.
	(TokenCollector::visit): Add the comments of the node visited.
	(TokenCollector::visit_closure_common): Likewise.
	(TokenCollector::visit_loop_common): Likewise.
	* ast/rust-ast-collector.h: Add internal comment as a nes Kind.
	* ast/rust-ast-dump.cc (Dump::Dump): add a Dump constructor to enable
	internal.
	* ast/rust-ast-dump.h: Add printing of internal comment in the dump
	* rust-session-manager.cc (Session::enable_dump): Activate ast dump
	and fill the blacklist.
	(Session::handle_internal_blacklist): Parse the flag to get node to
	be blacklisted.
	(Session::compile_crate): Launch ast dump internal when asked.
	(Session::dump_ast_pretty_internal): Call the visitor to dump
	the internals.
	* rust-session-manager.h (struct CompileOptions): add Interal in
	Dump option enum.

Signed-off-by: Benjamin Thos <[email protected]>
@@ -23,6 +23,7 @@
#include "rust-ast-visitor.h"
#include "rust-ast.h"
#include "rust-ast-full.h"
#include <sstream>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to include "rust-system.h" instead.

Copy link
Member

@CohenArthur CohenArthur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great work!! I've added a couple comments but they're mostly nitpick. The feature in itself is solid and you did good :) Thanks!!!!

Comment on lines +154 to +163
begin_internal_comment ("VariadicParam");

if (param.has_pattern ())
{
visit (param.get_pattern ());
push (Rust::Token::make (COLON, UNDEF_LOCATION));
}
push (Rust::Token::make (ELLIPSIS, UNDEF_LOCATION));

end_internal_comment ("VariadicParam");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is nice but if forces you to write the node's "name" twice - once when you begin the comment, and once when you end it. It would be nicer and easier to maintain if this was wrapped in a lambda of some sort, e.g.:

internal_comment("VariadicParam", []() {
      if (param.has_pattern ())
    {
      visit (param.get_pattern ());
      push (Rust::Token::make (COLON, UNDEF_LOCATION));
    }
  push (Rust::Token::make (ELLIPSIS, UNDEF_LOCATION));
  });

and the code for internal_comment could be something like the following:

template <typename T> internal_comment(std::string node_name, std::function<void(T& node)> visitor) {
    begin_internal_comment(node_name);
    
    visitor();
    
    end_internal_comment(node_name);
}

note that the ergonomics might not be amazing, in which case we can look at something else like a custom destructor like what @jdupak did recently (we talked about it a little bit on Zulip)

Newline,
Indentation,
Token,
};

CollectItem (TokenPtr token) : token (token), kind (Kind::Token) {}
CollectItem (std::string comment) : comment (comment), kind (Kind::Comment) {}
CollectItem (std::string comment, bool internal = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid using the boolean here and expose a new enum instead, which would make the code clearer

enum class Comment {
    Regular,
    Internal,
};

CollectItem (std::string comment, Comment kind = Comment::Regular) : comment(comment), kind(kind == Comment::Internal ? Kind::InternalComment : Kind::Comment) { ... }

@@ -32,6 +32,8 @@ class Dump
{
public:
Dump (std::ostream &stream);
Dump (std::ostream &stream, bool print_internal,
std::vector<std::string> blacklist);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can use a std::set<std::string> here for faster lookup, and for slightly better APIs as well since we can do blacklist.contains(...). also, I would call the set forbidden or excluded instead of blacklist

Comment on lines 61 to 63
#include <sstream>
#include <vector>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These includes are prohibited here for obscure bootstrapping compiler reasons, but you can include rust-system.h instead

void
Session::handle_internal_blacklist (std::string arg)
{
std::istringstream blist_str (arg.substr (arg.find (":") + 1, 50));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is 50 here? can we use a define or constant instead?


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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, ideally we'd use something other than true like an enum

};

std::set<DumpOption> dump_options;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::vector<std::string> internal_blacklist;
std::set<std::string> excluded_nodes;

@CohenArthur CohenArthur marked this pull request as draft October 31, 2024 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants