[gcc r14-7696] gccrs: Match tokens in macros more closely

Arthur Cohen cohenarthur@gcc.gnu.org
Tue Jan 16 17:54:35 GMT 2024


https://gcc.gnu.org/g:497632d136c6930fab94709e03b48a8a6735de3c

commit r14-7696-g497632d136c6930fab94709e03b48a8a6735de3c
Author: Owen Avery <powerboat9.gamer@gmail.com>
Date:   Mon Jun 26 13:06:10 2023 -0400

    gccrs: Match tokens in macros more closely
    
    gcc/rust/ChangeLog:
    
            * expand/rust-macro-expand.cc
            (MacroExpander::match_token): Match token instead of token id.
            * parse/rust-parse-impl.h
            (Parser::skip_token): Add token-skipping variant.
            (Parser::expect_token): Likewise.
            * parse/rust-parse.h
            (Parser::skip_token): Likewise.
            (Parser::expect_token): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/macro-issue2264.rs: New test.
    
    Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>

Diff:
---
 gcc/rust/expand/rust-macro-expand.cc          |  3 +--
 gcc/rust/parse/rust-parse-impl.h              | 33 +++++++++++++++++++++++++++
 gcc/rust/parse/rust-parse.h                   | 10 ++++++++
 gcc/testsuite/rust/compile/macro-issue2264.rs | 12 ++++++++++
 4 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index d14b3b3806a..27a298efc30 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -569,8 +569,7 @@ MacroExpander::match_matcher (Parser<MacroInvocLexer> &parser,
 bool
 MacroExpander::match_token (Parser<MacroInvocLexer> &parser, AST::Token &token)
 {
-  // FIXME this needs to actually match the content and the type
-  return parser.skip_token (token.get_id ());
+  return parser.skip_token (token.get_tok_ptr ());
 }
 
 bool
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index ff929d3c625..bc87ed72345 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -11936,6 +11936,15 @@ Parser<ManagedTokenSource>::skip_token (TokenId token_id)
   return expect_token (token_id) != const_TokenPtr ();
 }
 
+/* Checks if current token is similar to inputted token - skips it and returns
+ * true if so, diagnoses an error and returns false otherwise. */
+template <typename ManagedTokenSource>
+bool
+Parser<ManagedTokenSource>::skip_token (const_TokenPtr token)
+{
+  return expect_token (token) != const_TokenPtr ();
+}
+
 /* Checks if current token has inputted id - skips it and returns true if so,
  * returns false otherwise without diagnosing an error */
 template <typename ManagedTokenSource>
@@ -11971,6 +11980,30 @@ Parser<ManagedTokenSource>::expect_token (TokenId token_id)
     }
 }
 
+/* Checks the current token - if same as expected, skips and returns it,
+ * otherwise diagnoses error and returns null. */
+template <typename ManagedTokenSource>
+const_TokenPtr
+Parser<ManagedTokenSource>::expect_token (const_TokenPtr token_expect)
+{
+  const_TokenPtr t = lexer.peek_token ();
+  if (t->get_id () == token_expect->get_id ()
+      && (!t->should_have_str () || t->get_str () == token_expect->get_str ()))
+    {
+      lexer.skip_token ();
+      return t;
+    }
+  else
+    {
+      Error error (t->get_locus (), "expecting %qs but %qs found",
+		   token_expect->get_token_description (),
+		   t->get_token_description ());
+      add_error (std::move (error));
+
+      return const_TokenPtr ();
+    }
+}
+
 // Skips all tokens until EOF or }. Don't use.
 template <typename ManagedTokenSource>
 void
diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h
index 62025745662..1d49c956b5c 100644
--- a/gcc/rust/parse/rust-parse.h
+++ b/gcc/rust/parse/rust-parse.h
@@ -111,6 +111,15 @@ public:
    */
   bool skip_token (TokenId t);
 
+  /**
+   * Consume a token, reporting an error if it isn't the next token
+   *
+   * @param token pointer to similar token to consume
+   *
+   * @return true if the token was next, false if it wasn't found
+   */
+  bool skip_token (const_TokenPtr token);
+
   /**
    * Same as `skip_token` but allows for failure without necessarily reporting
    * an error
@@ -172,6 +181,7 @@ private:
   void skip_after_end_attribute ();
 
   const_TokenPtr expect_token (TokenId t);
+  const_TokenPtr expect_token (const_TokenPtr token_expect);
   void unexpected_token (const_TokenPtr t);
   bool skip_generics_right_angle ();
 
diff --git a/gcc/testsuite/rust/compile/macro-issue2264.rs b/gcc/testsuite/rust/compile/macro-issue2264.rs
new file mode 100644
index 00000000000..497dd3c5523
--- /dev/null
+++ b/gcc/testsuite/rust/compile/macro-issue2264.rs
@@ -0,0 +1,12 @@
+macro_rules! a {
+    (1) => {x};
+    (2) => {};
+}
+
+macro_rules! b {
+    (a) => {x};
+    (b) => {};
+}
+
+a!(2);
+b!(b);


More information about the Gcc-cvs mailing list