aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/util/rust-attributes.cc
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-09-01 13:14:09 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2024-01-16 19:04:35 +0100
commitfc024ea79deb5a9ec3cfd68b59719bef52db49ff (patch)
tree5f747b69d801d1f231bb391591ade781db3e7581 /gcc/rust/util/rust-attributes.cc
parent1f09a4fedca20c0b9068d4c466b360b449af5d56 (diff)
downloadgcc-fc024ea79deb5a9ec3cfd68b59719bef52db49ff.zip
gcc-fc024ea79deb5a9ec3cfd68b59719bef52db49ff.tar.gz
gcc-fc024ea79deb5a9ec3cfd68b59719bef52db49ff.tar.bz2
gccrs: Unify raw attribute values
Attribute values were used as raw string, this is error prone and makes renaming harder. Using a constexpr instead will leverage the power of the compiler and emit an error when an incorrect builtin attribute value is used. gcc/rust/ChangeLog: * ast/rust-ast.cc (Attribute::check_cfg_predicate): Change raw string to constexpr call. (Attribute::separate_cfg_attrs): Likewise. * backend/rust-compile-base.cc (should_mangle_item): Likewise. (HIRCompileBase::setup_fndecl): Likewise. (HIRCompileBase::handle_cold_attribute_on_fndecl): Likewise. * checks/errors/privacy/rust-privacy-reporter.cc (find_proc_macro_attribute): Likewise. * checks/errors/rust-unsafe-checker.cc (check_target_attr): Likewise. * expand/rust-cfg-strip.cc (fails_cfg): Likewise. (fails_cfg_with_expand): Likewise. (expand_cfg_attrs): Likewise. * expand/rust-macro-builtins.cc: Likewise. * hir/rust-ast-lower-base.cc (ASTLoweringBase::handle_outer_attributes): Likewise. (ASTLoweringBase::lower_macro_definition): Likewise. * hir/rust-hir-dump.cc (Dump::visit): Likewise. * parse/rust-parse-impl.h (Parser::parse_doc_comment): Likewise. * parse/rust-parse.cc (extract_module_path): Likewise. * resolve/rust-early-name-resolver.cc (is_macro_use_module): Likewise. (EarlyNameResolver::visit): Likewise. * resolve/rust-toplevel-name-resolver-2.0.cc (is_macro_export): Likwise. * rust-session-manager.cc (Session::injection): Likewise. * typecheck/rust-hir-type-check-base.cc (TypeCheckBase::parse_repr_options): Likewise. * util/rust-attributes.cc (is_proc_macro_type): Likewise. (AttributeChecker::check_attribute): Likewise. (AttributeChecker::visit): Likewise. * util/rust-hir-map.cc (Mappings::insert_macro_def): Likewise. * util/rust-attribute-values.h: New file. 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.cc61
1 files changed, 32 insertions, 29 deletions
diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index 5a91e9d..683bc8a 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -23,39 +23,42 @@
#include "rust-ast-full.h"
#include "rust-diagnostics.h"
#include "rust-unicode.h"
+#include "rust-attribute-values.h"
namespace Rust {
namespace Analysis {
+using Attrs = Values::Attributes;
+
// https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248
static const BuiltinAttrDefinition __definitions[]
- = {{"inline", CODE_GENERATION},
- {"cold", CODE_GENERATION},
- {"cfg", EXPANSION},
- {"cfg_attr", EXPANSION},
- {"deprecated", STATIC_ANALYSIS},
- {"allow", STATIC_ANALYSIS},
- {"allow_internal_unstable", STATIC_ANALYSIS},
- {"doc", HIR_LOWERING},
- {"must_use", STATIC_ANALYSIS},
- {"lang", HIR_LOWERING},
- {"link_section", CODE_GENERATION},
- {"no_mangle", CODE_GENERATION},
- {"repr", CODE_GENERATION},
- {"rustc_builtin_macro", EXPANSION},
- {"path", EXPANSION},
- {"macro_use", NAME_RESOLUTION},
- {"macro_export", NAME_RESOLUTION},
- {"proc_macro", EXPANSION},
- {"proc_macro_derive", EXPANSION},
- {"proc_macro_attribute", EXPANSION},
+ = {{Attrs::INLINE, CODE_GENERATION},
+ {Attrs::COLD, CODE_GENERATION},
+ {Attrs::CFG, EXPANSION},
+ {Attrs::CFG_ATTR, EXPANSION},
+ {Attrs::DEPRECATED, STATIC_ANALYSIS},
+ {Attrs::ALLOW, STATIC_ANALYSIS},
+ {Attrs::ALLOW_INTERNAL_UNSTABLE, STATIC_ANALYSIS},
+ {Attrs::DOC, HIR_LOWERING},
+ {Attrs::MUST_USE, STATIC_ANALYSIS},
+ {Attrs::LANG, HIR_LOWERING},
+ {Attrs::LINK_SECTION, CODE_GENERATION},
+ {Attrs::NO_MANGLE, CODE_GENERATION},
+ {Attrs::REPR, CODE_GENERATION},
+ {Attrs::RUSTC_BUILTIN_MACRO, EXPANSION},
+ {Attrs::PATH, EXPANSION},
+ {Attrs::MACRO_USE, NAME_RESOLUTION},
+ {Attrs::MACRO_EXPORT, NAME_RESOLUTION},
+ {Attrs::PROC_MACRO, EXPANSION},
+ {Attrs::PROC_MACRO_DERIVE, EXPANSION},
+ {Attrs::PROC_MACRO_ATTRIBUTE, EXPANSION},
// FIXME: This is not implemented yet, see
// https://github.com/Rust-GCC/gccrs/issues/1475
- {"target_feature", CODE_GENERATION},
+ {Attrs::TARGET_FEATURE, CODE_GENERATION},
// From now on, these are reserved by the compiler and gated through
// #![feature(rustc_attrs)]
- {"rustc_inherit_overflow_checks", CODE_GENERATION},
- {"stable", STATIC_ANALYSIS}};
+ {Attrs::RUSTC_INHERIT_OVERFLOW_CHECKS, CODE_GENERATION},
+ {Attrs::STABLE, STATIC_ANALYSIS}};
BuiltinAttributeMappings *
BuiltinAttributeMappings::get ()
@@ -207,8 +210,8 @@ is_proc_macro_type (const AST::Attribute &attribute)
return false;
auto name = result.name;
- return name == "proc_macro" || name == "proc_macro_derive"
- || name == "proc_macro_attribute";
+ return name == Attrs::PROC_MACRO || name == Attrs::PROC_MACRO_DERIVE
+ || name == Attrs::PROC_MACRO_ATTRIBUTE;
}
// Emit an error when one encountered attribute is either #[proc_macro],
@@ -256,7 +259,7 @@ AttributeChecker::check_attribute (const AST::Attribute &attribute)
// TODO: Add checks here for each builtin attribute
// TODO: Have an enum of builtins as well, switching on strings is annoying
// and costly
- if (result.name == "doc")
+ if (result.name == Attrs::DOC)
check_doc_attribute (attribute);
}
@@ -648,7 +651,7 @@ AttributeChecker::visit (AST::Function &fun)
auto name = result.name.c_str ();
- if (result.name == "proc_macro_derive")
+ if (result.name == Attrs::PROC_MACRO_DERIVE)
{
if (!attribute.has_attr_input ())
{
@@ -661,8 +664,8 @@ AttributeChecker::visit (AST::Function &fun)
}
check_crate_type (name, attribute);
}
- else if (result.name == "proc_macro"
- || result.name == "proc_macro_attribute")
+ else if (result.name == Attrs::PROC_MACRO
+ || result.name == Attrs::PROC_MACRO_ATTRIBUTE)
{
check_crate_type (name, attribute);
}