gccrs: libproc_macro: Change Ident structure

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>
This commit is contained in:
Pierre-Emmanuel Patry 2023-07-19 11:50:23 +02:00 committed by Arthur Cohen
parent bee1255541
commit f7af0b90ef
3 changed files with 15 additions and 28 deletions

View 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;

View 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

View File

@ -27,23 +27,20 @@
#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);