]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: expand: Add builtin attribute identification
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Mon, 3 Apr 2023 09:03:37 +0000 (11:03 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:28:45 +0000 (18:28 +0100)
Add a function to identify builtin attributes and skip those on proc
macro expansion phase.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (ExpandVisitor::visit_outer_attrs):
Change condition order.
(ExpandVisitor::visit_inner_using_attrs): Likewise.
(ExpandVisitor::visit_attrs_with_derive): Likewise.
(ExpandVisitor::is_builtin): Add builtin identification
function.
* expand/rust-expand-visitor.h: Add function prototype.
* util/rust-attributes.cc: Add missing rustc_builtin_macro
attribute.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/expand/rust-expand-visitor.cc
gcc/rust/expand/rust-expand-visitor.h
gcc/rust/util/rust-attributes.cc

index c25c017e40c6f0cc93a105436701ef6ab8177133..dac19481a7e738d4bf3136f6d924ffdb7bb51a1d 100644 (file)
@@ -17,6 +17,7 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-expand-visitor.h"
+#include "rust-attributes.h"
 
 namespace Rust {
 
@@ -1400,8 +1401,15 @@ ExpandVisitor::visit_outer_attrs (T &item, std::vector<AST::Attribute> &attrs)
     {
       auto current = *it;
 
-      it = attrs.erase (it);
-      expand_outer_attribute (item, current.get_path ());
+      if (!is_builtin (current) && !is_derive (current))
+       {
+         it = attrs.erase (it);
+         expand_outer_attribute (item, current.get_path ());
+       }
+      else
+       {
+         it++;
+       }
     }
 }
 
@@ -1429,8 +1437,15 @@ ExpandVisitor::visit_inner_using_attrs (T &item,
     {
       auto current = *it;
 
-      it = attrs.erase (it);
-      expand_inner_attribute (item, current.get_path ());
+      if (!is_builtin (current) && !is_derive (current))
+       {
+         it = attrs.erase (it);
+         expand_inner_attribute (item, current.get_path ());
+       }
+      else
+       {
+         it++;
+       }
     }
 }
 
@@ -1476,7 +1491,7 @@ ExpandVisitor::visit_attrs_with_derive (T &item)
     {
       auto current = *it;
 
-      if (is_derive (current))
+      if (!is_builtin (current) && is_derive (current))
        {
          it = attrs.erase (it);
          // Downcasting checked in is_derive
@@ -1500,4 +1515,14 @@ ExpandVisitor::is_derive (AST::Attribute &attr)
         && !segments.empty () && "derive" == segments[0].get_segment_name ();
 }
 
+bool
+ExpandVisitor::is_builtin (AST::Attribute &attr)
+{
+  auto &segments = attr.get_path ().get_segments ();
+  return !segments.empty ()
+        && !Analysis::BuiltinAttributeMappings::get ()
+              ->lookup_builtin (segments[0].get_segment_name ())
+              .is_error ();
+}
+
 } // namespace Rust
index e0ff37cf1750d0d4675e14c65e702431379d3c69..c2b3e781808faef852f0bc6eb8b706024b848964 100644 (file)
@@ -339,6 +339,8 @@ public:
 
   template <typename T> void visit_attrs_with_derive (T &item);
 
+  bool is_builtin (AST::Attribute &attr);
+
 private:
   MacroExpander &expander;
 };
index 941d4459bd4b14fb5f357dd7ed577a8301a56922..372762d5fa6fc6ccd03b43f4ddc99f436617c587 100644 (file)
@@ -39,6 +39,7 @@ static const BuiltinAttrDefinition __definitions[]
      {"link_section", CODE_GENERATION},
      {"no_mangle", CODE_GENERATION},
      {"repr", CODE_GENERATION},
+     {"rustc_builtin_macro", EXPANSION},
      {"path", EXPANSION},
      {"macro_use", NAME_RESOLUTION},
      // FIXME: This is not implemented yet, see