aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-07-26 15:26:55 +0200
committerP-E-P <32375388+P-E-P@users.noreply.github.com>2023-07-27 09:03:08 +0000
commit3e98458f7eabc9f703743e152547a467f84a7040 (patch)
tree0e05c1e06968e68f47bc036f33d3ef982d823b0b
parentccac7ed46c2e81434714d74a86d58d2e5e1df7c8 (diff)
downloadgcc-3e98458f7eabc9f703743e152547a467f84a7040.zip
gcc-3e98458f7eabc9f703743e152547a467f84a7040.tar.gz
gcc-3e98458f7eabc9f703743e152547a467f84a7040.tar.bz2
proc_macro: Increase FFIString usage
Two remaining structures in the rust interface were still using raw string pointer and length couples to communicate with the C++ library throught extern C functions. Using FFIString instead allow us to reduce the scope of potential errors using those raw pointers. As FFIString encapsulate raw pointer operations there will be only one locaiton to look after. ChangeLog: * libgrust/libproc_macro/rust/bridge/literal.rs: Change extern C function argument from raw string pointer and length to FFIString. * libgrust/libproc_macro/rust/bridge/token_stream.rs: Likewise. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
-rw-r--r--libgrust/libproc_macro/rust/bridge/literal.rs12
-rw-r--r--libgrust/libproc_macro/rust/bridge/token_stream.rs13
2 files changed, 5 insertions, 20 deletions
diff --git a/libgrust/libproc_macro/rust/bridge/literal.rs b/libgrust/libproc_macro/rust/bridge/literal.rs
index b03e363..5982504 100644
--- a/libgrust/libproc_macro/rust/bridge/literal.rs
+++ b/libgrust/libproc_macro/rust/bridge/literal.rs
@@ -1,12 +1,10 @@
use bridge::{ffistring::FFIString, span::Span};
-use std::convert::TryInto;
-use std::ffi::c_uchar;
use std::fmt;
use std::str::FromStr;
use LexError;
extern "C" {
- fn Literal__from_string(str: *const c_uchar, len: u64, lit: *mut Literal) -> bool;
+ fn Literal__from_string(str: FFIString, lit: *mut Literal) -> bool;
}
#[repr(C)]
@@ -234,13 +232,7 @@ impl FromStr for Literal {
};
// TODO: We might want to pass a LexError by reference to retrieve
// error information
- if unsafe {
- Literal__from_string(
- string.as_ptr(),
- string.len().try_into().unwrap(),
- &mut lit as *mut Literal,
- )
- } {
+ if unsafe { Literal__from_string(string.into(), &mut lit as *mut Literal) } {
Err(LexError)
} else {
Ok(lit)
diff --git a/libgrust/libproc_macro/rust/bridge/token_stream.rs b/libgrust/libproc_macro/rust/bridge/token_stream.rs
index 79f161f..094f91f 100644
--- a/libgrust/libproc_macro/rust/bridge/token_stream.rs
+++ b/libgrust/libproc_macro/rust/bridge/token_stream.rs
@@ -1,6 +1,5 @@
-use bridge::{group::Group, ident::Ident, literal::Literal, punct::Punct};
+use bridge::{ffistring::FFIString, group::Group, ident::Ident, literal::Literal, punct::Punct};
use std::convert::TryInto;
-use std::ffi::c_uchar;
use std::fmt;
use std::slice;
use std::str::FromStr;
@@ -13,7 +12,7 @@ extern "C" {
fn TokenStream__new() -> TokenStream;
fn TokenStream__with_capacity(capacity: u64) -> TokenStream;
fn TokenStream__push(stream: *mut TokenStream, tree: TokenTree);
- fn TokenStream__from_string(str: *const c_uchar, len: u64, ts: *mut TokenStream) -> bool;
+ fn TokenStream__from_string(str: FFIString, ts: *mut TokenStream) -> bool;
fn TokenStream__clone(ts: *const TokenStream) -> TokenStream;
fn TokenStream__drop(stream: *mut TokenStream);
}
@@ -136,13 +135,7 @@ impl FromStr for TokenStream {
type Err = LexError;
fn from_str(string: &str) -> Result<Self, LexError> {
let mut ts = TokenStream::new();
- if unsafe {
- TokenStream__from_string(
- string.as_ptr(),
- string.len().try_into().unwrap(),
- &mut ts as *mut TokenStream,
- )
- } {
+ if unsafe { TokenStream__from_string(string.into(), &mut ts as *mut TokenStream) } {
Err(LexError)
} else {
Ok(ts)