From e615b4fa5f3b2fda29f13db216c59d46837cfb85 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Tue, 18 Jul 2023 17:36:11 +0200 Subject: gccrs: proc macro: Add privacy check Proc macro crates cannot have any public function but proc macros. Proc macros should be public. gcc/rust/ChangeLog: * checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::go): Add visibility verification. Signed-off-by: Pierre-Emmanuel Patry --- .../checks/errors/privacy/rust-privacy-reporter.cc | 40 +++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'gcc') diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc index 1f1551a..be357d1 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc @@ -17,6 +17,7 @@ // . #include "rust-privacy-reporter.h" +#include "rust-session-manager.h" #include "rust-hir-expr.h" #include "rust-hir-stmt.h" #include "rust-hir-item.h" @@ -35,7 +36,44 @@ void PrivacyReporter::go (HIR::Crate &crate) { for (auto &item : crate.get_items ()) - item->accept_vis (*this); + { + 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 ()); + } + } + item->accept_vis (*this); + } } static bool -- cgit v1.1