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-7805] gccrs: proc macros: Add crate type attribute check
Date: Tue, 16 Jan 2024 18:00:04 +0000 (GMT)	[thread overview]
Message-ID: <20240116180004.2F1A63858415@sourceware.org> (raw)

https://gcc.gnu.org/g:6bed91c311e0fbfc72ad4de605335900fc98a5e6

commit r14-7805-g6bed91c311e0fbfc72ad4de605335900fc98a5e6
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Tue Jul 18 13:26:46 2023 +0200

    gccrs: proc macros: Add crate type attribute check
    
    A procedural macroa attribute can only be used on proc macro crates.
    
    gcc/rust/ChangeLog:
    
            * util/rust-attributes.cc (AttributeChecker::visit): Add
            attribute check on functions.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/util/rust-attributes.cc | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index 27817acf0c3..a174edc625f 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -17,6 +17,7 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-system.h"
+#include "rust-session-manager.h"
 #include "rust-attributes.h"
 #include "rust-ast.h"
 #include "rust-ast-full.h"
@@ -44,6 +45,9 @@ static const BuiltinAttrDefinition __definitions[]
      {"path", EXPANSION},
      {"macro_use", NAME_RESOLUTION},
      {"macro_export", NAME_RESOLUTION},
+     {"proc_macro", EXPANSION},
+     {"proc_macro_derive", EXPANSION},
+     {"proc_macro_attribute", EXPANSION},
      // FIXME: This is not implemented yet, see
      // https://github.com/Rust-GCC/gccrs/issues/1475
      {"target_feature", CODE_GENERATION},
@@ -537,8 +541,35 @@ AttributeChecker::visit (AST::UseDeclaration &)
 {}
 
 void
-AttributeChecker::visit (AST::Function &)
-{}
+AttributeChecker::visit (AST::Function &fun)
+{
+  auto check_crate_type = [] (const char *name, AST::Attribute &attribute) {
+    if (!Session::get_instance ().options.is_proc_macro ())
+      rust_error_at (attribute.get_locus (),
+		     "the %<#[%s]%> attribute is only usable with crates of "
+		     "the %<proc-macro%> crate type",
+		     name);
+  };
+
+  BuiltinAttrDefinition result;
+  for (auto &attribute : fun.get_outer_attrs ())
+    {
+      if (!is_builtin (attribute, result))
+	return;
+
+      auto name = result.name.c_str ();
+
+      if (result.name == "proc_macro_derive")
+	{
+	  check_crate_type (name, attribute);
+	}
+      else if (result.name == "proc_macro"
+	       || result.name == "proc_macro_attribute")
+	{
+	  check_crate_type (name, attribute);
+	}
+    }
+}
 
 void
 AttributeChecker::visit (AST::TypeAlias &)

                 reply	other threads:[~2024-01-16 18:00 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=20240116180004.2F1A63858415@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: link
Be 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).