diff options
author | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-07-18 17:36:11 +0200 |
---|---|---|
committer | P-E-P <32375388+P-E-P@users.noreply.github.com> | 2023-07-27 12:24:54 +0000 |
commit | ac850d186ac945df00eda97555d273109250554c (patch) | |
tree | c978a08b3aa197b9e705b03e7e5ecf392e75656a /gcc | |
parent | 9a906fea9e4230c625ba5fe169f447450ab5f4c9 (diff) | |
download | gcc-ac850d186ac945df00eda97555d273109250554c.zip gcc-ac850d186ac945df00eda97555d273109250554c.tar.gz gcc-ac850d186ac945df00eda97555d273109250554c.tar.bz2 |
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 <pierre-emmanuel.patry@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc index 5932600..c5a6681 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 @@ // <http://www.gnu.org/licenses/>. #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<std::string> 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<HIR::VisItem *> (item.get ()); + if (vis_item->get_visibility ().is_public () + && !proc_macro_attribute.has_value ()) + rust_error_at ( + item->get_locus (), + "%<proc-macro%> 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 %<pub%>", + proc_macro_attribute.value ().c_str ()); + } + } + item->accept_vis (*this); + } } static bool |