From: Pierre-Emmanuel Patry Date: Mon, 5 Jun 2023 09:24:35 +0000 (+0200) Subject: gccrs: expand: Expand item level attribute proc macros X-Git-Tag: basepoints/gcc-15~2476 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6925358a440bd9dfb33dc79d8f345530b4cb94cc;p=thirdparty%2Fgcc.git gccrs: expand: Expand item level attribute proc macros Expand custom attributes from procedural macros at item level. gcc/rust/ChangeLog: * expand/rust-expand-visitor.cc (expand_attribute): Add function to expand a given attribute on a given item. * expand/rust-macro-expand.h (struct MacroExpander): Change return type to ast fragment. Signed-off-by: Pierre-Emmanuel Patry --- diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc index b63e3ba82c94..927d9225049e 100644 --- a/gcc/rust/expand/rust-expand-visitor.cc +++ b/gcc/rust/expand/rust-expand-visitor.cc @@ -126,6 +126,29 @@ derive_item (std::unique_ptr &item, std::string &to_derive, return result; } +static std::vector> +expand_attribute (std::unique_ptr &item, AST::SimplePath &name, + MacroExpander &expander) +{ + std::vector> result; + auto frag = expander.expand_attribute_proc_macro (item, name); + if (!frag.is_error ()) + { + for (auto &node : frag.get_nodes ()) + { + switch (node.get_kind ()) + { + case AST::SingleASTNode::ITEM: + result.push_back (node.take_item ()); + break; + default: + gcc_unreachable (); + } + } + } + return result; +} + void ExpandVisitor::expand_inner_items ( std::vector> &items) @@ -173,8 +196,25 @@ ExpandVisitor::expand_inner_items ( } else /* Attribute */ { - // Ignore for now - attr_it++; + if (is_builtin (current)) + { + attr_it++; + } + else + { + attr_it = attrs.erase (attr_it); + auto new_items + = expand_attribute (item, current.get_path (), + expander); + it = items.erase (it); + std::move (new_items.begin (), new_items.end (), + std::inserter (items, it)); + // TODO: Improve this ? + // item is invalid since it refers to now deleted, + // cancel the loop increment and break. + it--; + break; + } } } } diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index 676a9d14790e..0e53913438a7 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -406,7 +406,7 @@ struct MacroExpander } template - void expand_attribute_proc_macro (T &item, AST::SimplePath &path) + AST::Fragment expand_attribute_proc_macro (T &item, AST::SimplePath &path) { ProcMacro::Attribute macro; @@ -436,7 +436,7 @@ struct MacroExpander std::vector vec (c.cbegin (), c.cend ()); // FIXME: Handle attributes - parse_proc_macro_output ( + return parse_proc_macro_output ( macro.macro (ProcMacro::TokenStream::make_tokenstream (), convert (vec))); }