mirror of
https://github.com/gcc-mirror/gcc.git
synced 2024-11-21 13:40:47 +00:00
gccrs: 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. libgrust/ChangeLog: * libproc_macro/rust/bridge/literal.rs: Change extern C function argument from raw string pointer and length to FFIString. * libproc_macro/rust/bridge/token_stream.rs: Likewise. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
This commit is contained in:
parent
dd15fff385
commit
f00f3837ef
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user