public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-7837] gccrs: proc macro: Add privacy check
@ 2024-01-16 18:02 Arthur Cohen
  0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-01-16 18:02 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:e615b4fa5f3b2fda29f13db216c59d46837cfb85

commit r14-7837-ge615b4fa5f3b2fda29f13db216c59d46837cfb85
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Tue Jul 18 17:36:11 2023 +0200

    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 <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 .../checks/errors/privacy/rust-privacy-reporter.cc | 40 +++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
index 1f1551a7176..be357d159b1 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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-01-16 18:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-16 18:02 [gcc r14-7837] gccrs: proc macro: Add privacy check Arthur Cohen

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).