From 1346a7652e8cf70b54f0b12f6844f0c929781c2f Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Thu, 20 Jul 2023 13:40:35 +0200 Subject: [PATCH] gccrs: privacy: Refactor proc macro privacy check Refactor proc macro specific privacy check in multiple shorter functions. gcc/rust/ChangeLog: * checks/errors/privacy/rust-privacy-reporter.cc (find_proc_macro_attribute): Add a new function to find a potential proc macro type attribute on a given item. (proc_macro_privacy_check): Move all proc macro privacy check in their own function to avoid cluttering the usual privacy check. (PrivacyReporter::go): Add call to newly created proc macro privacy check function. Signed-off-by: Pierre-Emmanuel Patry --- .../errors/privacy/rust-privacy-reporter.cc | 81 +++++++++++-------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc index be357d159b14..fc00dcbbae6d 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc @@ -32,46 +32,59 @@ PrivacyReporter::PrivacyReporter ( current_module (tl::nullopt) {} +// Find a proc_macro, proc_macro_derive or proc_macro_attribute +// attribute in a vector of attribute +static tl::optional +find_proc_macro_attribute (const AST::AttrVec &outer_attrs) +{ + tl::optional result; + + for (auto &a : outer_attrs) + { + auto &segments = a.get_path ().get_segments (); + if (segments.size () != 1) + continue; + auto name = segments.at (0).get_segment_name (); + if (name == "proc_macro" || name == "proc_macro_attribute" + || name == "proc_macro_derive") + result = {name}; + } + + return result; +} + +// Common check on crate items when dealing with 'proc-macro' crate type. +static void +proc_macro_privacy_check (std::unique_ptr &item) +{ + if (item->get_hir_kind () == HIR::Node::BaseKind::VIS_ITEM) + { + auto attribute = find_proc_macro_attribute (item->get_outer_attrs ()); + + bool pub_item = static_cast (item.get ()) + ->get_visibility () + .is_public (); + + if (pub_item && !attribute.has_value ()) // Public non proc-macro + rust_error_at ( + item->get_locus (), + "% crate types currently cannot export any " + "items other than functions tagged with %<#[proc_macro]%>, " + "%<#[proc_macro_derive]%> or %<#[proc_macro_attribute]%>"); + else if (!pub_item && attribute.has_value ()) // Private proc-macro + rust_error_at (item->get_locus (), + "functions tagged with %<#[%s]%> must be %", + attribute.value ().c_str ()); + } +} + void PrivacyReporter::go (HIR::Crate &crate) { for (auto &item : crate.get_items ()) { if (Session::get_instance ().options.is_proc_macro ()) - { - if (item->get_hir_kind () == HIR::Node::BaseKind::VIS_ITEM) - { - auto &outer_attrs = item->get_outer_attrs (); - // Find a proc_macro, proc_macro_derive or proc_macro_attribute - // attribute - tl::optional proc_macro_attribute; - for (auto &a : outer_attrs) - { - auto &segments = a.get_path ().get_segments (); - if (segments.size () != 1) - break; - auto name = segments.at (0).get_segment_name (); - if (name == "proc_macro" || name == "proc_macro_attribute" - || name == "proc_macro_derive") - proc_macro_attribute = {name}; - } - - auto vis_item = static_cast (item.get ()); - if (vis_item->get_visibility ().is_public () - && !proc_macro_attribute.has_value ()) - rust_error_at ( - item->get_locus (), - "% crate types currently cannot export any " - "items other than functions tagged with %<#[proc_macro]%>, " - "%<#[proc_macro_derive]%> or %<#[proc_macro_attribute]%>"); - else if (!vis_item->get_visibility ().is_public () - && proc_macro_attribute.has_value ()) - rust_error_at ( - item->get_locus (), - "functions tagged with %<#[%s]%> must be %", - proc_macro_attribute.value ().c_str ()); - } - } + proc_macro_privacy_check (item); item->accept_vis (*this); } } -- 2.47.2