aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/util/rust-attributes.cc
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-07-18 13:26:46 +0200
committerP-E-P <32375388+P-E-P@users.noreply.github.com>2023-07-20 08:21:00 +0000
commitf715169a6ec1b504354e0fc3e2a46bbf29d8715c (patch)
tree2de290eb624fcf02a51e12c6489d59a8bc1c26ed /gcc/rust/util/rust-attributes.cc
parentb8268b6b7bb7c0353bfda2c88889b0f0ec71e7c8 (diff)
downloadgcc-f715169a6ec1b504354e0fc3e2a46bbf29d8715c.zip
gcc-f715169a6ec1b504354e0fc3e2a46bbf29d8715c.tar.gz
gcc-f715169a6ec1b504354e0fc3e2a46bbf29d8715c.tar.bz2
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>
Diffstat (limited to 'gcc/rust/util/rust-attributes.cc')
-rw-r--r--gcc/rust/util/rust-attributes.cc35
1 files changed, 33 insertions, 2 deletions
diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index 504853f..e5670f9 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 &)