Skip to content

Commit 7fa6e72

Browse files
committed
macros: Parse :meta properly
This allows us to match attribute bodies in macro invocations, which we can use later down the line to perform conditional compilation
1 parent 6c99a5a commit 7fa6e72

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

gcc/rust/expand/rust-macro-expand.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,7 @@ MacroExpander::match_fragment (Parser<MacroInvocLexer> &parser,
490490

491491
// is meta attributes?
492492
case AST::MacroFragSpec::META:
493-
// parser.parse_inner_attribute ?
494-
// parser.parse_outer_attribute ?
495-
// parser.parse_attribute_body ?
496-
// parser.parse_doc_comment ?
497-
gcc_unreachable ();
493+
parser.parse_attribute_body ();
498494
break;
499495

500496
case AST::MacroFragSpec::TT:

gcc/rust/parse/rust-parse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ template <typename ManagedTokenSource> class Parser
143143
AST::Visibility parse_visibility ();
144144
std::unique_ptr<AST::IdentifierPattern> parse_identifier_pattern ();
145145
std::unique_ptr<AST::TokenTree> parse_token_tree ();
146+
AST::Attribute parse_attribute_body ();
146147

147148
private:
148149
void skip_after_semicolon ();
@@ -162,7 +163,6 @@ template <typename ManagedTokenSource> class Parser
162163
AST::Attribute parse_inner_attribute ();
163164
AST::AttrVec parse_outer_attributes ();
164165
AST::Attribute parse_outer_attribute ();
165-
AST::Attribute parse_attribute_body ();
166166
std::unique_ptr<AST::AttrInput> parse_attr_input ();
167167
AST::Attribute parse_doc_comment ();
168168

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// { dg-additional-options "-frust-cfg=A" }
2+
3+
macro_rules! attr {
4+
(#[$attr:meta] $s:stmt) => {
5+
#[$attr]
6+
$s;
7+
};
8+
}
9+
10+
fn main() -> i32 {
11+
let mut a = 0;
12+
13+
attr! {
14+
#[cfg(A)]
15+
a = 3
16+
};
17+
18+
attr! {
19+
#[cfg(B)]
20+
a = 40
21+
};
22+
23+
a - 3
24+
}

0 commit comments

Comments
 (0)