From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7905) id 93B953857C55; Tue, 16 Jan 2024 18:03:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 93B953857C55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705428236; bh=XYJW47Vps7Vebp8hxiAVeoV2JMOv9hAsS7LXcxOvRDk=; h=From:To:Subject:Date:From; b=pSbVlEKr1cG2Xdzs4C73lVewB6RvD43wJb7MywXngmy4golpP96D/MjEmoq143nIF d4WUKWTr80U1nr4MWXwh+f6fBf1rLdTMHPD5dHh/eoyD6qdTGpaiOSzN2/MDY9MjLP lDZjZuefDcxR38NVxSZx1DSfBnGpmp9sVhw7Q49o= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Arthur Cohen To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-7850] gccrs: Check proc_macro attributes on non root functions X-Act-Checkin: gcc X-Git-Author: Pierre-Emmanuel Patry X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 96e4e5d27ec49416148b9a22a6c9bb050d562401 X-Git-Newrev: b05d874fc527f81c54b50e62f204eb8cd04250d8 Message-Id: <20240116180356.93B953857C55@sourceware.org> Date: Tue, 16 Jan 2024 18:03:56 +0000 (GMT) List-Id: https://gcc.gnu.org/g:b05d874fc527f81c54b50e62f204eb8cd04250d8 commit r14-7850-gb05d874fc527f81c54b50e62f204eb8cd04250d8 Author: Pierre-Emmanuel Patry Date: Wed Jul 26 12:16:12 2023 +0200 gccrs: Check proc_macro attributes on non root functions Check proc_macro, proc_macro_attribute and proc_macro_derive attributes on non root functions, emit an error when one is found. gcc/rust/ChangeLog: * util/rust-attributes.cc (check_proc_macro_non_root): Add function to emit this specific error. (AttributeChecker::visit): Modify visitor to propagate to some containers that were not handled correctly. Signed-off-by: Pierre-Emmanuel Patry Diff: --- gcc/rust/util/rust-attributes.cc | 56 +++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc index a39c843bc1d..8b133cc7e7d 100644 --- a/gcc/rust/util/rust-attributes.cc +++ b/gcc/rust/util/rust-attributes.cc @@ -225,6 +225,24 @@ check_proc_macro_non_function (const AST::AttrVec &attributes) } } +// Emit an error when one attribute is either proc_macro, proc_macro_attribute +// or proc_macro_derive +static void +check_proc_macro_non_root (AST::AttrVec attributes, location_t loc) +{ + for (auto &attr : attributes) + { + if (is_proc_macro_type (attr)) + { + rust_error_at ( + loc, + "functions tagged with %<#[%s]%> must currently " + "reside in the root of the crate", + attr.get_path ().get_segments ().at (0).as_string ().c_str ()); + } + } +} + void AttributeChecker::check_attribute (const AST::Attribute &attribute) { @@ -435,8 +453,20 @@ AttributeChecker::visit (AST::ClosureExprInner &) {} void -AttributeChecker::visit (AST::BlockExpr &) -{} +AttributeChecker::visit (AST::BlockExpr &expr) +{ + for (auto &stmt : expr.get_statements ()) + { + if (stmt->get_stmt_kind () == AST::Stmt::Kind::Item) + { + // Non owning pointer, let it go out of scope + auto item = static_cast (stmt.get ()); + check_proc_macro_non_root (item->get_outer_attrs (), + item->get_locus ()); + } + stmt->accept_vis (*this); + } +} void AttributeChecker::visit (AST::ClosureExprInnerTyped &) @@ -479,8 +509,10 @@ AttributeChecker::visit (AST::ReturnExpr &) {} void -AttributeChecker::visit (AST::UnsafeBlockExpr &) -{} +AttributeChecker::visit (AST::UnsafeBlockExpr &expr) +{ + expr.get_block_expr ()->accept_vis (*this); +} void AttributeChecker::visit (AST::LoopExpr &) @@ -540,13 +572,20 @@ AttributeChecker::visit (AST::TypeBoundWhereClauseItem &) {} void -AttributeChecker::visit (AST::Method &) -{} +AttributeChecker::visit (AST::Method &method) +{ + method.get_definition ()->accept_vis (*this); +} void AttributeChecker::visit (AST::Module &module) { check_proc_macro_non_function (module.get_outer_attrs ()); + for (auto &item : module.get_items ()) + { + check_proc_macro_non_root (item->get_outer_attrs (), item->get_locus ()); + item->accept_vis (*this); + } } void @@ -611,6 +650,7 @@ AttributeChecker::visit (AST::Function &fun) check_crate_type (name, attribute); } } + fun.get_definition ()->accept_vis (*this); } void @@ -698,12 +738,16 @@ void AttributeChecker::visit (AST::InherentImpl &impl) { check_proc_macro_non_function (impl.get_outer_attrs ()); + for (auto &item : impl.get_impl_items ()) + item->accept_vis (*this); } void AttributeChecker::visit (AST::TraitImpl &impl) { check_proc_macro_non_function (impl.get_outer_attrs ()); + for (auto &item : impl.get_impl_items ()) + item->accept_vis (*this); } void