Skip to content

Commit af5f096

Browse files
powerboat9philberty
authored andcommitted
Handle attributes in expression macros
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 <[email protected]>
1 parent a1b7b00 commit af5f096

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,8 @@ transcribe_expression (Parser<MacroInvocLexer> &parser)
961961
auto &lexer = parser.get_token_source ();
962962
auto start = lexer.get_offs ();
963963

964-
auto expr = parser.parse_expr ();
964+
auto attrs = parser.parse_outer_attributes ();
965+
auto expr = parser.parse_expr (std::move (attrs));
965966
if (expr == nullptr)
966967
return AST::Fragment::create_error ();
967968

gcc/rust/parse/rust-parse.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ template <typename ManagedTokenSource> class Parser
212212
std::unique_ptr<AST::MacroInvocation>
213213
parse_macro_invocation (AST::AttrVec outer_attrs);
214214

215+
/*
216+
* This has to be public for parsing expressions with outer attributes
217+
*/
218+
AST::AttrVec parse_outer_attributes ();
219+
215220
private:
216221
void skip_after_semicolon ();
217222
void skip_after_end ();
@@ -228,7 +233,6 @@ template <typename ManagedTokenSource> class Parser
228233

229234
// AST-related stuff - maybe move or something?
230235
AST::Attribute parse_inner_attribute ();
231-
AST::AttrVec parse_outer_attributes ();
232236
AST::Attribute parse_outer_attribute ();
233237
std::unique_ptr<AST::AttrInput> parse_attr_input ();
234238
std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, location_t>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
macro_rules! foo {
2+
() => { #[cfg(all())] 12 }
3+
}
4+
5+
fn main() -> i32 {
6+
foo!()
7+
}

0 commit comments

Comments
 (0)