From: Arthur Cohen <arthur.cohen@embecosm.com>
To: gcc-patches@gcc.gnu.org
Cc: gcc-rust@gcc.gnu.org, Arthur Cohen <arthur.cohen@embecosm.com>,
philberty <philip.herron@embecosm.com>
Subject: [COMMITTED] gccrs: ast: Add better assertion on AST fragments
Date: Tue, 31 Jan 2023 14:25:10 +0100 [thread overview]
Message-ID: <20230131132510.661917-1-arthur.cohen@embecosm.com> (raw)
gcc/rust/ChangeLog:
* ast/rust-ast.h: Improve assertions within ASTFragment API.
Co-authored-by: philberty <philip.herron@embecosm.com>
Tested on x86_64-pc-linux-gnu, committed on master.
---
gcc/rust/ast/rust-ast.h | 47 ++++++++++++++++++++++++++++++++++-------
1 file changed, 39 insertions(+), 8 deletions(-)
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 58fe2674479..9e1b8b11373 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -1819,7 +1819,7 @@ public:
return true;
}
- std::string as_string ()
+ std::string as_string () const
{
switch (kind)
{
@@ -1869,14 +1869,45 @@ private:
*/
bool is_single_fragment () const { return nodes.size () == 1; }
- bool is_single_fragment (SingleASTNode::NodeType expected) const
+
+ bool is_single_fragment_of_kind (SingleASTNode::NodeType expected) const
{
return is_single_fragment () && nodes[0].get_kind () == expected;
}
- bool is_single_fragment_kind (SingleASTNode::NodeType kind) const
+ void assert_single_fragment (SingleASTNode::NodeType expected) const
{
- return is_single_fragment () && nodes[0].get_kind () == kind;
+ static const std::map<SingleASTNode::NodeType, const char *> str_map = {
+ {SingleASTNode::NodeType::IMPL, "impl"},
+ {SingleASTNode::NodeType::ITEM, "item"},
+ {SingleASTNode::NodeType::TYPE, "type"},
+ {SingleASTNode::NodeType::EXPRESSION, "expr"},
+ {SingleASTNode::NodeType::STMT, "stmt"},
+ {SingleASTNode::NodeType::EXTERN, "extern"},
+ {SingleASTNode::NodeType::TRAIT, "trait"},
+ {SingleASTNode::NodeType::TRAIT_IMPL, "trait impl"},
+ };
+
+ auto actual = nodes[0].get_kind ();
+ auto fail = false;
+
+ if (!is_single_fragment ())
+ {
+ rust_error_at (Location (), "fragment is not single");
+ fail = true;
+ }
+
+ if (actual != expected)
+ {
+ rust_error_at (
+ Location (),
+ "invalid fragment operation: expected %qs node, got %qs node",
+ str_map.find (expected)->second,
+ str_map.find (nodes[0].get_kind ())->second);
+ fail = true;
+ }
+
+ rust_assert (!fail);
}
public:
@@ -1920,23 +1951,23 @@ public:
bool is_expression_fragment () const
{
- return is_single_fragment (SingleASTNode::NodeType::EXPRESSION);
+ return is_single_fragment_of_kind (SingleASTNode::NodeType::EXPRESSION);
}
bool is_type_fragment () const
{
- return is_single_fragment (SingleASTNode::NodeType::TYPE);
+ return is_single_fragment_of_kind (SingleASTNode::NodeType::TYPE);
}
std::unique_ptr<Expr> take_expression_fragment ()
{
- rust_assert (is_single_fragment_kind (SingleASTNode::NodeType::EXPRESSION));
+ assert_single_fragment (SingleASTNode::NodeType::EXPRESSION);
return nodes[0].take_expr ();
}
std::unique_ptr<Type> take_type_fragment ()
{
- rust_assert (is_single_fragment_kind (SingleASTNode::NodeType::TYPE));
+ assert_single_fragment (SingleASTNode::NodeType::TYPE);
return nodes[0].take_type ();
}
--
2.39.1
reply other threads:[~2023-01-31 13:21 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230131132510.661917-1-arthur.cohen@embecosm.com \
--to=arthur.cohen@embecosm.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=gcc-rust@gcc.gnu.org \
--cc=philip.herron@embecosm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).