From: Arthur Cohen Date: Fri, 21 Jul 2023 16:23:00 +0000 (+0200) Subject: gccrs: toplevel: Handle macro definitions properly X-Git-Tag: basepoints/gcc-15~2253 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3a5a74d91774dccd4ff7adc8b809781dd25a0f44;p=thirdparty%2Fgcc.git gccrs: toplevel: Handle macro definitions properly gcc/rust/ChangeLog: * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::insert_or_error_out): Fix format string. (is_macro_export): New method. (TopLevel::visit): Handle macro definitions. --- diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc index 7923342b39a6..2c0e0779acd9 100644 --- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc @@ -45,7 +45,8 @@ TopLevel::insert_or_error_out (const Identifier &identifier, const T &node, rich_location rich_loc (line_table, loc); rich_loc.add_range (node_locations[result.error ().existing]); - rust_error_at (rich_loc, "already defined"); + rust_error_at (rich_loc, ErrorCode::E0428, "%qs defined multiple times", + identifier.as_string ().c_str ()); } } @@ -70,12 +71,38 @@ TopLevel::visit (AST::Module &module) module.get_name ()); } +static bool +is_macro_export (AST::MacroRulesDefinition &def) +{ + for (const auto &attr : def.get_outer_attrs ()) + if (attr.get_path ().as_string () == "macro_export") + return true; + + return false; +} + void TopLevel::visit (AST::MacroRulesDefinition ¯o) { - // FIXME: Do we want to insert macro rules here already? Probably, right? - // So that we can easily resolve in `Early`? - insert_or_error_out (macro.get_rule_name (), macro, Namespace::Macros); + // we do not insert macros in the current rib as that needs to be done in the + // textual scope of the Early pass. we only insert them in the root of the + // crate if they are marked with #[macro_export] + + if (is_macro_export (macro)) + { + auto res = ctx.macros.insert_at_root (macro.get_rule_name (), + macro.get_node_id ()); + if (!res) + { + // TODO: Factor this + rich_location rich_loc (line_table, macro.get_locus ()); + rich_loc.add_range (node_locations[res.error ().existing]); + + rust_error_at (rich_loc, ErrorCode::E0428, + "macro %qs defined multiple times", + macro.get_rule_name ().as_string ().c_str ()); + } + } } void