]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Handle attributes in expression macros
authorOwen Avery <powerboat9.gamer@gmail.com>
Sat, 12 Jul 2025 03:35:53 +0000 (23:35 -0400)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 19:58:36 +0000 (20:58 +0100)
gcc/rust/ChangeLog:

* expand/rust-macro-expand.cc (transcribe_expression): Parse any
outer attributes before parsing an expression.
* parse/rust-parse.h (Parser::parse_outer_attributes): Make
public.

gcc/testsuite/ChangeLog:

* rust/compile/attr-macro.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
gcc/rust/expand/rust-macro-expand.cc
gcc/rust/parse/rust-parse.h
gcc/testsuite/rust/compile/attr-macro.rs [new file with mode: 0644]

index 5667a1a4f4f820d769b45cc4d1a7684958e91d67..dfead3acc1d6fd5568e363728f9c45436aaf41cf 100644 (file)
@@ -961,7 +961,8 @@ transcribe_expression (Parser<MacroInvocLexer> &parser)
   auto &lexer = parser.get_token_source ();
   auto start = lexer.get_offs ();
 
-  auto expr = parser.parse_expr ();
+  auto attrs = parser.parse_outer_attributes ();
+  auto expr = parser.parse_expr (std::move (attrs));
   if (expr == nullptr)
     return AST::Fragment::create_error ();
 
index fa518ac5544e2662d98ea6a00805ab79fadb1698..fa9c909338bb59ae8e310c3422020d3a789a02c2 100644 (file)
@@ -212,6 +212,11 @@ public:
   std::unique_ptr<AST::MacroInvocation>
   parse_macro_invocation (AST::AttrVec outer_attrs);
 
+  /*
+   * This has to be public for parsing expressions with outer attributes
+   */
+  AST::AttrVec parse_outer_attributes ();
+
 private:
   void skip_after_semicolon ();
   void skip_after_end ();
@@ -228,7 +233,6 @@ private:
 
   // AST-related stuff - maybe move or something?
   AST::Attribute parse_inner_attribute ();
-  AST::AttrVec parse_outer_attributes ();
   AST::Attribute parse_outer_attribute ();
   std::unique_ptr<AST::AttrInput> parse_attr_input ();
   std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, location_t>
diff --git a/gcc/testsuite/rust/compile/attr-macro.rs b/gcc/testsuite/rust/compile/attr-macro.rs
new file mode 100644 (file)
index 0000000..de9fce1
--- /dev/null
@@ -0,0 +1,7 @@
+macro_rules! foo {
+    () => { #[cfg(all())] 12 }
+}
+
+fn main() -> i32 {
+    foo!()
+}