-
Notifications
You must be signed in to change notification settings - Fork 157
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
base: master
Are you sure you want to change the base?
Conversation
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]>
640795a
to
9e874a5
Compare
gcc/rust/ast/rust-ast-collector.h
Outdated
@@ -23,6 +23,7 @@ | |||
#include "rust-ast-visitor.h" | |||
#include "rust-ast.h" | |||
#include "rust-ast-full.h" | |||
#include <sstream> |
There was a problem hiding this comment.
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.
There was a problem hiding this 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!!!!
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"); |
There was a problem hiding this comment.
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)
gcc/rust/ast/rust-ast-collector.h
Outdated
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) |
There was a problem hiding this comment.
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) { ... }
gcc/rust/ast/rust-ast-dump.h
Outdated
@@ -32,6 +32,8 @@ class Dump | |||
{ | |||
public: | |||
Dump (std::ostream &stream); | |||
Dump (std::ostream &stream, bool print_internal, | |||
std::vector<std::string> blacklist); |
There was a problem hiding this comment.
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
gcc/rust/rust-session-manager.cc
Outdated
#include <sstream> | ||
#include <vector> | ||
|
There was a problem hiding this comment.
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
gcc/rust/rust-session-manager.cc
Outdated
void | ||
Session::handle_internal_blacklist (std::string arg) | ||
{ | ||
std::istringstream blist_str (arg.substr (arg.find (":") + 1, 50)); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
gcc/rust/rust-session-manager.h
Outdated
}; | ||
|
||
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::vector<std::string> internal_blacklist; | |
std::set<std::string> excluded_nodes; |
Add a new dump option to show node node as
internal comment with a blacklist to ignore some
node.
gcc/rust/ChangeLog: