]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: libproc_macro: Change Ident structure
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Wed, 19 Jul 2023 09:50:23 +0000 (11:50 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:55:56 +0000 (18:55 +0100)
Use FFIString in Ident structure rather that a raw pointer and a
length, this will reduce the size of the code dealing with raw
pointers. Which should prevent some error.

gcc/rust/ChangeLog:

* util/rust-token-converter.cc (from_ident): Adapt code to new
constructor.

libgrust/ChangeLog:

* libproc_macro/ident.cc (Ident__new): Constructor
accepts an FFIString now.
(Ident__new_raw): Likewise.
(Ident::clone): Internal members change means clone also change.
(Ident::make_ident): Change constructor call.
(Ident::drop): Add call to FFIString::clone.
* libproc_macro/ident.h (struct Ident): Remove raw
pointer and length, add an FFIString inside instead.
(Ident__new): Change constructor.
(Ident__new_raw): Change constructor.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/util/rust-token-converter.cc
libgrust/libproc_macro/ident.cc
libgrust/libproc_macro/ident.h

index 3206cedda101b5639995c189f7a819ec711f9153..ffda24e04fe217854053ff08ffded62a301c06a1 100644 (file)
@@ -269,7 +269,7 @@ from_tokenstream (const ProcMacro::TokenStream &ts,
 static void
 from_ident (const ProcMacro::Ident &ident, std::vector<const_TokenPtr> &result)
 {
-  std::string value (reinterpret_cast<const char *> (ident.val), ident.len);
+  std::string value (ident.value.to_string ());
   if (ident.is_raw)
     value = "r#" + value;
 
index 6c8472dad208cdb706bcca41ba117af90d2aa7e2..221d38ec2fd94ee316060817b3b47681e0286d3a 100644 (file)
@@ -28,15 +28,15 @@ namespace ProcMacro {
 extern "C" {
 
 Ident
-Ident__new (unsigned char *str, std::uint64_t len, Span span)
+Ident__new (FFIString str, Span span)
 {
-  return Ident::make_ident (str, len, span);
+  return Ident::make_ident (str, span);
 }
 
 Ident
-Ident__new_raw (unsigned char *str, std::uint64_t len, Span span)
+Ident__new_raw (FFIString str, Span span)
 {
-  return Ident::make_ident (str, len, span, true);
+  return Ident::make_ident (str, span, true);
 }
 
 void
@@ -55,35 +55,25 @@ Ident__clone (const Ident *ident)
 Ident
 Ident::clone () const
 {
-  unsigned char *val = new unsigned char[this->len];
-  // FIXME: UTF-8 Update this with sizeof codepoint instead
-  std::memcpy (val, this->val, this->len * sizeof (char));
-  return {this->is_raw, val, this->len};
+  return {this->is_raw, value.clone (), this->span};
 }
 
 Ident
 Ident::make_ident (std::string str, Span span, bool raw)
 {
-  return Ident::make_ident (reinterpret_cast<const unsigned char *> (
-                             str.c_str ()),
-                           str.length (), span, raw);
+  return Ident::make_ident (FFIString::make_ffistring (str), span, raw);
 }
 
 Ident
-Ident::make_ident (const unsigned char *str, std::uint64_t len, Span span,
-                  bool raw)
+Ident::make_ident (FFIString str, Span span, bool raw)
 {
-  unsigned char *val = new unsigned char[len];
-  // FIXME: UTF-8 Update this with sizeof codepoint instead
-  std::memcpy (val, str, len * sizeof (char));
-  return {raw, val, len};
+  return {raw, str, span};
 }
 
 void
 Ident::drop (Ident *ident)
 {
-  delete[] ident->val;
-  ident->len = 0;
+  FFIString::drop (&ident->value);
 }
 
 } // namespace ProcMacro
index 28d6ebe825f59ff2cbd4e5dcae4999f8772a42c5..66547d95585294f4c4f76469a9b2dc8a20308164 100644 (file)
 #include <string>
 
 #include "span.h"
+#include "ffistring.h"
 
 namespace ProcMacro {
 
 struct Ident
 {
   bool is_raw;
-  // TODO: Adapt this to UTF-8
-  unsigned char *val;
-  // Length in bytes
-  std::uint64_t len;
+  FFIString value;
   Span span;
 
 public:
   Ident clone () const;
   static Ident make_ident (std::string str, Span span, bool raw = false);
-  static Ident make_ident (const unsigned char *str, std::uint64_t len,
-                          Span span, bool raw = false);
+  static Ident make_ident (FFIString str, Span span, bool raw = false);
 
   static void drop (Ident *ident);
 };
@@ -51,10 +48,10 @@ public:
 extern "C" {
 
 Ident
-Ident__new (unsigned char *str, std::uint64_t len, Span span);
+Ident__new (FFIString str, Span span);
 
 Ident
-Ident__new_raw (unsigned char *str, std::uint64_t len, Span span);
+Ident__new_raw (FFIString str, Span span);
 
 void
 Ident__drop (Ident *ident);