]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Unify raw attribute values
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Fri, 1 Sep 2023 11:14:09 +0000 (13:14 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 18:04:35 +0000 (19:04 +0100)
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>
17 files changed:
gcc/rust/ast/rust-ast.cc
gcc/rust/backend/rust-compile-base.cc
gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
gcc/rust/checks/errors/rust-unsafe-checker.cc
gcc/rust/expand/rust-cfg-strip.cc
gcc/rust/expand/rust-macro-builtins.cc
gcc/rust/hir/rust-ast-lower-base.cc
gcc/rust/hir/rust-hir-dump.cc
gcc/rust/parse/rust-parse-impl.h
gcc/rust/parse/rust-parse.cc
gcc/rust/resolve/rust-early-name-resolver.cc
gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
gcc/rust/rust-session-manager.cc
gcc/rust/typecheck/rust-hir-type-check-base.cc
gcc/rust/util/rust-attribute-values.h [new file with mode: 0644]
gcc/rust/util/rust-attributes.cc
gcc/rust/util/rust-hir-map.cc

index cb0281ec95699150de58dccaeb40150a233c55c2..5d875fd4504975a7b574b65de92aab2e93001723 100644 (file)
@@ -27,6 +27,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "rust-parse.h"
 #include "rust-operators.h"
 #include "rust-dir-owner.h"
+#include "rust-attribute-values.h"
 
 /* Compilation unit used for various AST-related functions that would make
  * the headers too long if they were defined inline and don't receive any
@@ -4271,7 +4272,8 @@ Attribute::check_cfg_predicate (const Session &session) const
   /* assume that cfg predicate actually can exist, i.e. attribute has cfg or
    * cfg_attr path */
   if (!has_attr_input ()
-      || (path.as_string () != "cfg" && path.as_string () != "cfg_attr"))
+      || (path.as_string () != Values::Attributes::CFG
+         && path.as_string () != Values::Attributes::CFG_ATTR))
     {
       // DEBUG message
       rust_debug (
@@ -4293,7 +4295,7 @@ Attribute::check_cfg_predicate (const Session &session) const
 std::vector<Attribute>
 Attribute::separate_cfg_attrs () const
 {
-  if (!has_attr_input () || path.as_string () != "cfg_attr")
+  if (!has_attr_input () || path.as_string () != Values::Attributes::CFG_ATTR)
     return {};
 
   // assume that it has already been parsed
index 492588dfdf99f3739a5e18c1619b0ce575f461f9..c11b6cca52a84fd192286217598e0e9daa6075fe 100644 (file)
@@ -30,6 +30,7 @@
 #include "rust-hir-path-probe.h"
 #include "rust-type-util.h"
 #include "rust-compile-implitem.h"
+#include "rust-attribute-values.h"
 
 #include "fold-const.h"
 #include "stringpool.h"
@@ -42,7 +43,9 @@ namespace Compile {
 
 bool inline should_mangle_item (const tree fndecl)
 {
-  return lookup_attribute ("no_mangle", DECL_ATTRIBUTES (fndecl)) == NULL_TREE;
+  return lookup_attribute (Values::Attributes::NO_MANGLE,
+                          DECL_ATTRIBUTES (fndecl))
+        == NULL_TREE;
 }
 
 void
@@ -69,15 +72,17 @@ HIRCompileBase::setup_fndecl (tree fndecl, bool is_main_entry_point,
   // is it inline?
   for (const auto &attr : attrs)
     {
-      bool is_inline = attr.get_path ().as_string ().compare ("inline") == 0;
+      bool is_inline
+       = attr.get_path ().as_string () == Values::Attributes::INLINE;
       bool is_must_use
-       = attr.get_path ().as_string ().compare ("must_use") == 0;
-      bool is_cold = attr.get_path ().as_string ().compare ("cold") == 0;
+       = attr.get_path ().as_string () == Values::Attributes::MUST_USE;
+      bool is_cold = attr.get_path ().as_string () == Values::Attributes::COLD;
       bool is_link_section
-       = attr.get_path ().as_string ().compare ("link_section") == 0;
-      bool no_mangle = attr.get_path ().as_string ().compare ("no_mangle") == 0;
+       = attr.get_path ().as_string () == Values::Attributes::LINK_SECTION;
+      bool no_mangle
+       = attr.get_path ().as_string () == Values::Attributes::NO_MANGLE;
       bool is_deprecated
-       = attr.get_path ().as_string ().compare ("deprecated") == 0;
+       = attr.get_path ().as_string () == Values::Attributes::DEPRECATED;
 
       if (is_inline)
        {
@@ -113,7 +118,7 @@ HIRCompileBase::handle_cold_attribute_on_fndecl (tree fndecl,
   // simple #[cold]
   if (!attr.has_attr_input ())
     {
-      tree cold = get_identifier ("cold");
+      tree cold = get_identifier (Values::Attributes::COLD);
       // this will get handled by the GCC backend later
       DECL_ATTRIBUTES (fndecl)
        = tree_cons (cold, NULL_TREE, DECL_ATTRIBUTES (fndecl));
@@ -160,8 +165,9 @@ HIRCompileBase::handle_no_mangle_attribute_on_fndecl (
       return;
     }
 
-  DECL_ATTRIBUTES (fndecl) = tree_cons (get_identifier ("no_mangle"), NULL_TREE,
-                                       DECL_ATTRIBUTES (fndecl));
+  DECL_ATTRIBUTES (fndecl)
+    = tree_cons (get_identifier (Values::Attributes::NO_MANGLE), NULL_TREE,
+                DECL_ATTRIBUTES (fndecl));
 }
 
 void
@@ -223,7 +229,7 @@ HIRCompileBase::handle_deprecated_attribute_on_fndecl (
     {
       tree attr_list = build_tree_list (NULL_TREE, value);
       DECL_ATTRIBUTES (fndecl)
-       = tree_cons (get_identifier ("deprecated"), attr_list,
+       = tree_cons (get_identifier (Values::Attributes::DEPRECATED), attr_list,
                     DECL_ATTRIBUTES (fndecl));
     }
 }
index 7c3b4149d61d9e87e8db84e74444ebd702da1ab7..b37753211180d8a05c74bab45b23f2aecfd41e53 100644 (file)
@@ -21,6 +21,7 @@
 #include "rust-hir-expr.h"
 #include "rust-hir-stmt.h"
 #include "rust-hir-item.h"
+#include "rust-attribute-values.h"
 
 namespace Rust {
 namespace Privacy {
@@ -43,8 +44,9 @@ find_proc_macro_attribute (const AST::AttrVec &outer_attrs)
       if (segments.size () != 1)
        continue;
       auto name = segments.at (0).get_segment_name ();
-      if (name == "proc_macro" || name == "proc_macro_attribute"
-         || name == "proc_macro_derive")
+      if (name == Values::Attributes::PROC_MACRO
+         || name == Values::Attributes::PROC_MACRO_ATTRIBUTE
+         || name == Values::Attributes::PROC_MACRO_DERIVE)
        return name;
     }
 
index 4d4b5e8615335330e505059ef102a46b799b6b76..93ec6cf6edf3dbfb3b1f6cca702759cfa976c937 100644 (file)
@@ -21,6 +21,7 @@
 #include "rust-hir-expr.h"
 #include "rust-hir-stmt.h"
 #include "rust-hir-item.h"
+#include "rust-attribute-values.h"
 
 namespace Rust {
 namespace HIR {
@@ -186,7 +187,8 @@ check_target_attr (HIR::Function *fn, location_t locus)
   if (std::any_of (fn->get_outer_attrs ().begin (),
                   fn->get_outer_attrs ().end (),
                   [] (const AST::Attribute &attr) {
-                    return attr.get_path ().as_string () == "target_feature";
+                    return attr.get_path ().as_string ()
+                           == Values::Attributes::TARGET_FEATURE;
                   }))
     rust_error_at (locus,
                   "call to function with %<#[target_feature]%> requires "
index 099a171f9caf0c9cc8f057e2f14a7a3a323f50a2..70df4460f68982be45d7ab298add01de87f7d18a 100644 (file)
@@ -19,6 +19,7 @@
 #include "rust-cfg-strip.h"
 #include "rust-ast-full.h"
 #include "rust-session-manager.h"
+#include "rust-attribute-values.h"
 
 namespace Rust {
 
@@ -33,7 +34,8 @@ fails_cfg (const AST::AttrVec &attrs)
 
   for (const auto &attr : attrs)
     {
-      if (attr.get_path () == "cfg" && !attr.check_cfg_predicate (session))
+      if (attr.get_path () == Values::Attributes::CFG
+         && !attr.check_cfg_predicate (session))
        return true;
     }
   return false;
@@ -51,7 +53,7 @@ fails_cfg_with_expand (AST::AttrVec &attrs)
   // TODO: maybe have something that strips cfg attributes that evaluate true?
   for (auto &attr : attrs)
     {
-      if (attr.get_path () == "cfg")
+      if (attr.get_path () == Values::Attributes::CFG)
        {
          if (!attr.is_parsed_to_meta_item ())
            attr.parse_attr_to_meta_item ();
@@ -96,7 +98,7 @@ expand_cfg_attrs (AST::AttrVec &attrs)
   for (std::size_t i = 0; i < attrs.size (); i++)
     {
       auto &attr = attrs[i];
-      if (attr.get_path () == "cfg_attr")
+      if (attr.get_path () == Values::Attributes::CFG_ATTR)
        {
          if (!attr.is_parsed_to_meta_item ())
            attr.parse_attr_to_meta_item ();
index b36a46f0affdee47d59e65901a6c68247d8e9dae..3397d9d687c32381db0e5b509ebaa2930752f6b7 100644 (file)
@@ -29,6 +29,7 @@
 #include "rust-macro.h"
 #include "rust-parse.h"
 #include "rust-session-manager.h"
+#include "rust-attribute-values.h"
 
 namespace Rust {
 
index da36d75bc67e24f83defa2c7dd5b0911caf936ad..cec2410112a51f75c0ebd7f53135522298cf4ecf 100644 (file)
@@ -20,6 +20,7 @@
 #include "rust-ast-lower-type.h"
 #include "rust-ast-lower-pattern.h"
 #include "rust-ast-lower-extern.h"
+#include "rust-attribute-values.h"
 
 namespace Rust {
 namespace HIR {
@@ -722,12 +723,12 @@ ASTLoweringBase::handle_outer_attributes (const ItemWrapper &item)
          continue;
        }
 
-      bool is_lang_item = str_path.compare ("lang") == 0
+      bool is_lang_item = str_path == Values::Attributes::LANG
                          && attr.has_attr_input ()
                          && attr.get_attr_input ().get_attr_input_type ()
                               == AST::AttrInput::AttrInputType::LITERAL;
 
-      bool is_doc_item = str_path.compare ("doc") == 0;
+      bool is_doc_item = str_path == Values::Attributes::DOC;
 
       if (is_doc_item)
        handle_doc_item_attribute (item, attr);
@@ -967,7 +968,7 @@ ASTLoweringBase::lower_macro_definition (AST::MacroRulesDefinition &def)
 {
   auto is_export = false;
   for (const auto &attr : def.get_outer_attrs ())
-    if (attr.get_path ().as_string () == "macro_export")
+    if (attr.get_path ().as_string () == Values::Attributes::MACRO_EXPORT)
       is_export = true;
 
   if (is_export)
index 8870ece2074b06359df6749804bed52c81441c8d..8078ed14f3bafc0ea3d881ebf1e559aa56f2ec6b 100644 (file)
@@ -23,6 +23,7 @@
 #include "rust-hir-type.h"
 #include "rust-hir.h"
 #include <string>
+#include "rust-attribute-values.h"
 
 namespace Rust {
 namespace HIR {
@@ -633,7 +634,7 @@ void
 Dump::visit (AST::Attribute &attribute)
 {
   // Special, no begin/end as this is called by do_inner_attrs.
-  put_field ("path", attribute.get_path ().as_string ());
+  put_field (Values::Attributes::PATH, attribute.get_path ().as_string ());
 
   std::string str = "none";
   if (attribute.has_attr_input ())
index d156ac2e48b1f5d24d6f833d7e7b2834af422e01..830845ba656588a2649c50c652cd0ba2fc4d3009 100644 (file)
@@ -26,6 +26,7 @@
 #include "rust-diagnostics.h"
 #include "rust-make-unique.h"
 #include "rust-dir-owner.h"
+#include "rust-attribute-values.h"
 
 namespace Rust {
 // Left binding powers of operations.
@@ -490,7 +491,7 @@ Parser<ManagedTokenSource>::parse_doc_comment ()
 {
   const_TokenPtr token = lexer.peek_token ();
   location_t locus = token->get_locus ();
-  AST::SimplePathSegment segment ("doc", locus);
+  AST::SimplePathSegment segment (Values::Attributes::DOC, locus);
   std::vector<AST::SimplePathSegment> segments;
   segments.push_back (std::move (segment));
   AST::SimplePath attr_path (std::move (segments), false, locus);
index 56b9769b492de39c81d18c36ed3d09da9f565e57..0f91c7ffe61a35a7477e3106da7cdb485d84b1ba 100644 (file)
@@ -18,6 +18,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "rust-linemap.h"
 #include "rust-diagnostics.h"
 #include "rust-token.h"
+#include "rust-attribute-values.h"
 
 namespace Rust {
 
@@ -28,7 +29,7 @@ extract_module_path (const AST::AttrVec &inner_attrs,
   AST::Attribute path_attr = AST::Attribute::create_empty ();
   for (const auto &attr : inner_attrs)
     {
-      if (attr.get_path ().as_string () == "path")
+      if (attr.get_path ().as_string () == Values::Attributes::PATH)
        {
          path_attr = attr;
          break;
@@ -48,7 +49,7 @@ extract_module_path (const AST::AttrVec &inner_attrs,
 
   for (const auto &attr : outer_attrs)
     {
-      if (attr.get_path ().as_string () == "path")
+      if (attr.get_path ().as_string () == Values::Attributes::PATH)
        {
          path_attr = attr;
          break;
index 5b701f5c0c1af7642407a00d5fd0cff09520c2a4..cdebca6389298c5b2f8d7967b839a7e9beb7b35c 100644 (file)
@@ -20,6 +20,7 @@
 #include "rust-ast-full.h"
 #include "rust-name-resolver.h"
 #include "rust-macro-builtins.h"
+#include "rust-attribute-values.h"
 
 namespace Rust {
 namespace Resolver {
@@ -29,7 +30,7 @@ static bool
 is_macro_use_module (const AST::Module &mod)
 {
   for (const auto &attr : mod.get_outer_attrs ())
-    if (attr.get_path ().as_string () == "macro_use")
+    if (attr.get_path ().as_string () == Values::Attributes::MACRO_USE)
       return true;
 
   return false;
@@ -973,7 +974,8 @@ EarlyNameResolver::visit (AST::MacroInvocation &invoc)
   bool is_builtin
     = std::any_of (outer_attrs.begin (), outer_attrs.end (),
                   [] (AST::Attribute attr) {
-                    return attr.get_path () == "rustc_builtin_macro";
+                    return attr.get_path ()
+                           == Values::Attributes::RUSTC_BUILTIN_MACRO;
                   });
 
   if (is_builtin)
index f24c91de9221a9d31224cbc9d196e9e2c5ca7a9a..486998dc6341de7410e6b6ce0cd33db1639bd3e9 100644 (file)
@@ -19,6 +19,7 @@
 #include "rust-toplevel-name-resolver-2.0.h"
 #include "rust-ast-full.h"
 #include "rust-hir-map.h"
+#include "rust-attribute-values.h"
 
 namespace Rust {
 namespace Resolver2_0 {
@@ -137,7 +138,7 @@ static bool
 is_macro_export (AST::MacroRulesDefinition &def)
 {
   for (const auto &attr : def.get_outer_attrs ())
-    if (attr.get_path ().as_string () == "macro_export")
+    if (attr.get_path ().as_string () == Values::Attributes::MACRO_EXPORT)
       return true;
 
   return false;
index 63f839e7c9db53bc6d2a98beff8169329398f6bf..9ce2d7f29abc52444e1b102649801600b9b0361b 100644 (file)
@@ -45,6 +45,7 @@
 #include "rust-cfg-strip.h"
 #include "rust-expand-visitor.h"
 #include "rust-unicode.h"
+#include "rust-attribute-values.h"
 
 #include "diagnostic.h"
 #include "input.h"
@@ -807,8 +808,8 @@ Session::injection (AST::Crate &crate)
     {
       // create "macro use" attribute for use on extern crate item to enable
       // loading macros from it
-      AST::Attribute attr (AST::SimplePath::from_str ("macro_use",
-                                                     UNDEF_LOCATION),
+      AST::Attribute attr (AST::SimplePath::from_str (
+                            Values::Attributes::MACRO_USE, UNDEF_LOCATION),
                           nullptr);
 
       // create "extern crate" item with the name
index ac9d0e9ea24e5880dd66df8795a21e1f90eaeb27..871b8920572c76ec5aeb32fc06a6e37acb20df87 100644 (file)
@@ -21,6 +21,7 @@
 #include "rust-hir-type-check-type.h"
 #include "rust-hir-trait-resolve.h"
 #include "rust-type-util.h"
+#include "rust-attribute-values.h"
 
 namespace Rust {
 namespace Resolver {
@@ -291,7 +292,7 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus)
 
   for (const auto &attr : attrs)
     {
-      bool is_repr = attr.get_path ().as_string ().compare ("repr") == 0;
+      bool is_repr = attr.get_path ().as_string () == Values::Attributes::REPR;
       if (is_repr)
        {
          const AST::AttrInput &input = attr.get_attr_input ();
diff --git a/gcc/rust/util/rust-attribute-values.h b/gcc/rust/util/rust-attribute-values.h
new file mode 100644 (file)
index 0000000..513550a
--- /dev/null
@@ -0,0 +1,40 @@
+#ifndef RUST_ATTRIBUTES_VALUE_H
+#define RUST_ATTRIBUTES_VALUE_H
+
+namespace Rust {
+namespace Values {
+// TODO: Change this to a namespace + inline constexpr in the future
+class Attributes
+{
+public:
+  static constexpr auto &INLINE = "inline";
+  static constexpr auto &COLD = "cold";
+  static constexpr auto &CFG = "cfg";
+  static constexpr auto &CFG_ATTR = "cfg_attr";
+  static constexpr auto &DEPRECATED = "deprecated";
+  static constexpr auto &ALLOW = "allow";
+  static constexpr auto &ALLOW_INTERNAL_UNSTABLE = "allow_internal_unstable";
+  static constexpr auto &DOC = "doc";
+  static constexpr auto &MUST_USE = "must_use";
+  static constexpr auto &LANG = "lang";
+  static constexpr auto &LINK_SECTION = "link_section";
+  static constexpr auto &NO_MANGLE = "no_mangle";
+  static constexpr auto &REPR = "repr";
+  static constexpr auto &RUSTC_BUILTIN_MACRO = "rustc_builtin_macro";
+  static constexpr auto &PATH = "path";
+  static constexpr auto &MACRO_USE = "macro_use";
+  static constexpr auto &MACRO_EXPORT = "macro_export";
+  static constexpr auto &PROC_MACRO = "proc_macro";
+  static constexpr auto &PROC_MACRO_DERIVE = "proc_macro_derive";
+  static constexpr auto &PROC_MACRO_ATTRIBUTE = "proc_macro_attribute";
+  static constexpr auto &TARGET_FEATURE = "target_feature";
+  // From now on, these are reserved by the compiler and gated through
+  // #![feature(rustc_attrs)]
+  static constexpr auto &RUSTC_INHERIT_OVERFLOW_CHECKS
+    = "rustc_inherit_overflow_checks";
+  static constexpr auto &STABLE = "stable";
+};
+} // namespace Values
+} // namespace Rust
+
+#endif /* !RUST_ATTRIBUTES_VALUE_H */
index 5a91e9da90f80370a0295a9555f125e1fc104fa2..683bc8a2b65e6faca3ac03ed20c8da979f1dbf1b 100644 (file)
 #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);
        }
index 748b15b9afae911066032a75acf2d3eb45471348..1f4dd78de76b775478a4b01d1c15f3ea8c75b739 100644 (file)
@@ -22,6 +22,7 @@
 #include "rust-hir-full.h"
 #include "rust-macro-builtins.h"
 #include "rust-mapping-common.h"
+#include "rust-attribute-values.h"
 
 namespace Rust {
 namespace Analysis {
@@ -872,7 +873,8 @@ Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
   bool should_be_builtin
     = std::any_of (outer_attrs.begin (), outer_attrs.end (),
                   [] (AST::Attribute attr) {
-                    return attr.get_path () == "rustc_builtin_macro";
+                    return attr.get_path ()
+                           == Values::Attributes::RUSTC_BUILTIN_MACRO;
                   });
   if (should_be_builtin)
     {