public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Arthur Cohen <cohenarthur@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-7833] gccrs: Emit error with proc macro on non functions Date: Tue, 16 Jan 2024 18:02:28 +0000 (GMT) [thread overview] Message-ID: <20240116180228.F38C73858296@sourceware.org> (raw) https://gcc.gnu.org/g:cd90ba84e1725449c7ca0c3ff6ec4304c1504b9f commit r14-7833-gcd90ba84e1725449c7ca0c3ff6ec4304c1504b9f Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> Date: Fri Jul 21 12:41:12 2023 +0200 gccrs: Emit error with proc macro on non functions An error should be emitted when a proc_macro, proc_macro_attribute or proc_macro_derive attribute is met on any non function item. Those attribute shall be placed only on bare functions. gcc/rust/ChangeLog: * util/rust-attributes.cc (is_proc_macro_type): Add function to identify matching proc macro attribute type. (check_proc_macro_non_function): Add function to check and emit error. (AttributeChecker::visit): Add cal to new check function. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> Diff: --- gcc/rust/util/rust-attributes.cc | 106 +++++++++++++++++++++++++++++---------- 1 file changed, 80 insertions(+), 26 deletions(-) diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc index 3ee7f2b6f0c..a39c843bc1d 100644 --- a/gcc/rust/util/rust-attributes.cc +++ b/gcc/rust/util/rust-attributes.cc @@ -198,6 +198,33 @@ check_doc_attribute (const AST::Attribute &attribute) } } +static bool +is_proc_macro_type (const AST::Attribute &attribute) +{ + BuiltinAttrDefinition result; + if (!is_builtin (attribute, result)) + return false; + + auto name = result.name; + return name == "proc_macro" || name == "proc_macro_derive" + || name == "proc_macro_attribute"; +} + +// Emit an error when one encountered attribute is either #[proc_macro], +// #[proc_macro_attribute] or #[proc_macro_derive] +static void +check_proc_macro_non_function (const AST::AttrVec &attributes) +{ + for (auto &attr : attributes) + { + if (is_proc_macro_type (attr)) + rust_error_at ( + attr.get_locus (), + "the %<#[%s]%> attribute may only be used on bare functions", + attr.get_path ().get_segments ()[0].as_string ().c_str ()); + } +} + void AttributeChecker::check_attribute (const AST::Attribute &attribute) { @@ -517,12 +544,16 @@ AttributeChecker::visit (AST::Method &) {} void -AttributeChecker::visit (AST::Module &) -{} +AttributeChecker::visit (AST::Module &module) +{ + check_proc_macro_non_function (module.get_outer_attrs ()); +} void -AttributeChecker::visit (AST::ExternCrate &) -{} +AttributeChecker::visit (AST::ExternCrate &crate) +{ + check_proc_macro_non_function (crate.get_outer_attrs ()); +} void AttributeChecker::visit (AST::UseTreeGlob &) @@ -537,8 +568,10 @@ AttributeChecker::visit (AST::UseTreeRebind &) {} void -AttributeChecker::visit (AST::UseDeclaration &) -{} +AttributeChecker::visit (AST::UseDeclaration &declaration) +{ + check_proc_macro_non_function (declaration.get_outer_attrs ()); +} void AttributeChecker::visit (AST::Function &fun) @@ -581,18 +614,23 @@ AttributeChecker::visit (AST::Function &fun) } void -AttributeChecker::visit (AST::TypeAlias &) -{} +AttributeChecker::visit (AST::TypeAlias &alias) +{ + check_proc_macro_non_function (alias.get_outer_attrs ()); +} void AttributeChecker::visit (AST::StructStruct &struct_item) { check_attributes (struct_item.get_outer_attrs ()); + check_proc_macro_non_function (struct_item.get_outer_attrs ()); } void -AttributeChecker::visit (AST::TupleStruct &) -{} +AttributeChecker::visit (AST::TupleStruct &tuplestruct) +{ + check_proc_macro_non_function (tuplestruct.get_outer_attrs ()); +} void AttributeChecker::visit (AST::EnumItem &) @@ -611,20 +649,28 @@ AttributeChecker::visit (AST::EnumItemDiscriminant &) {} void -AttributeChecker::visit (AST::Enum &) -{} +AttributeChecker::visit (AST::Enum &enumeration) +{ + check_proc_macro_non_function (enumeration.get_outer_attrs ()); +} void -AttributeChecker::visit (AST::Union &) -{} +AttributeChecker::visit (AST::Union &u) +{ + check_proc_macro_non_function (u.get_outer_attrs ()); +} void -AttributeChecker::visit (AST::ConstantItem &) -{} +AttributeChecker::visit (AST::ConstantItem &item) +{ + check_proc_macro_non_function (item.get_outer_attrs ()); +} void -AttributeChecker::visit (AST::StaticItem &) -{} +AttributeChecker::visit (AST::StaticItem &item) +{ + check_proc_macro_non_function (item.get_outer_attrs ()); +} void AttributeChecker::visit (AST::TraitItemFunc &) @@ -643,16 +689,22 @@ AttributeChecker::visit (AST::TraitItemType &) {} void -AttributeChecker::visit (AST::Trait &) -{} +AttributeChecker::visit (AST::Trait &trait) +{ + check_proc_macro_non_function (trait.get_outer_attrs ()); +} void -AttributeChecker::visit (AST::InherentImpl &) -{} +AttributeChecker::visit (AST::InherentImpl &impl) +{ + check_proc_macro_non_function (impl.get_outer_attrs ()); +} void -AttributeChecker::visit (AST::TraitImpl &) -{} +AttributeChecker::visit (AST::TraitImpl &impl) +{ + check_proc_macro_non_function (impl.get_outer_attrs ()); +} void AttributeChecker::visit (AST::ExternalTypeItem &) @@ -667,8 +719,10 @@ AttributeChecker::visit (AST::ExternalFunctionItem &) {} void -AttributeChecker::visit (AST::ExternBlock &) -{} +AttributeChecker::visit (AST::ExternBlock &block) +{ + check_proc_macro_non_function (block.get_outer_attrs ()); +} // rust-macro.h void
reply other threads:[~2024-01-16 18:02 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20240116180228.F38C73858296@sourceware.org \ --to=cohenarthur@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).