summaryrefslogtreecommitdiff
path: root/libgrust/libformat_parser
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2024-02-16 18:27:22 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2024-08-01 13:11:40 +0200
commit68cb878c1dab0636da4f686d6c8f5d4fa3b66e3b (patch)
tree789b4ca35931e44ef51751b66f4b4919be338ce9 /libgrust/libformat_parser
parentd9fa4153c83d3a4e4099530a450c7e511b1b24ec (diff)
gccrs: format-args: Fix Rust interface and add input parsing.
gcc/rust/ChangeLog: * ast/rust-ast.cc: Make FormatArgs inherit from AST::Expr * ast/rust-builtin-ast-nodes.h: Improve FormatArg* nodes and helpers. * ast/rust-fmt.cc (Pieces::collect): Fix interface to match FFI function. * ast/rust-fmt.h (collect_pieces): Likewise. (struct Pieces): Add append_newline parameter. * expand/rust-macro-builtins.cc: Add proper parsing of format_args input. * hir/rust-ast-lower-base.cc: Include diagnostics header. libgrust/ChangeLog: * libformat_parser/src/lib.rs: Switch interface to use more parser parameters. * libformat_parser/src/bin.rs: Use new interface.
Diffstat (limited to 'libgrust/libformat_parser')
-rw-r--r--libgrust/libformat_parser/src/bin.rs5
-rw-r--r--libgrust/libformat_parser/src/lib.rs63
2 files changed, 48 insertions, 20 deletions
diff --git a/libgrust/libformat_parser/src/bin.rs b/libgrust/libformat_parser/src/bin.rs
index 4b1f903ad5f..5f46497c946 100644
--- a/libgrust/libformat_parser/src/bin.rs
+++ b/libgrust/libformat_parser/src/bin.rs
@@ -2,6 +2,9 @@ use libformat_parser::rust;
fn main() {
dbg!(rust::collect_pieces(
- std::env::args().nth(1).unwrap().as_str()
+ std::env::args().nth(1).unwrap().as_str(),
+ None,
+ None,
+ false
));
}
diff --git a/libgrust/libformat_parser/src/lib.rs b/libgrust/libformat_parser/src/lib.rs
index c164578a103..6bf78c4f2a8 100644
--- a/libgrust/libformat_parser/src/lib.rs
+++ b/libgrust/libformat_parser/src/lib.rs
@@ -77,20 +77,15 @@ mod ffi {
/// A piece is a portion of the format string which represents the next part
/// to emit. These are emitted as a stream by the `Parser` class.
- #[derive(Clone, Debug, PartialEq)]
+ #[derive(Debug, Clone, PartialEq)]
#[repr(C)]
pub enum Piece<'a> {
/// A literal string which should directly be emitted
String(&'a str),
/// This describes that formatting should process the next argument (as
/// specified inside) for emission.
- NextArgument(*const Argument<'a>),
- }
-
- impl<'a> Drop for Piece<'a> {
- fn drop(&mut self) {
- println!("dropping Piece: {:?}", self)
- }
+ // do we need a pointer here? we're doing big cloning anyway
+ NextArgument(Argument<'a>),
}
/// Representation of an argument specification.
@@ -216,7 +211,7 @@ mod ffi {
let ptr = Box::leak(x);
let dst = Into::<Argument>::into(*ptr);
- Piece::NextArgument(&dst as *const Argument)
+ Piece::NextArgument(dst)
}
}
}
@@ -321,8 +316,13 @@ mod ffi {
pub mod rust {
use generic_format_parser::{ParseMode, Parser, Piece};
- pub fn collect_pieces(input: &str) -> Vec<Piece<'_>> {
- let parser = Parser::new(input, None, None, true, ParseMode::Format);
+ pub fn collect_pieces(
+ input: &str,
+ style: Option<usize>,
+ snippet: Option<String>,
+ append_newline: bool,
+ ) -> Vec<Piece<'_>> {
+ let parser = Parser::new(input, style, snippet, append_newline, ParseMode::Format);
parser.into_iter().collect()
}
@@ -337,16 +337,18 @@ pub struct PieceSlice {
}
#[no_mangle]
-pub extern "C" fn collect_pieces(input: *const libc::c_char) -> PieceSlice {
+pub extern "C" fn collect_pieces(input: *const libc::c_char, append_newline: bool) -> PieceSlice {
+ dbg!(input);
+
// FIXME: Add comment
let str = unsafe { CStr::from_ptr(input) };
- dbg!(str);
// FIXME: No unwrap
- let pieces: Vec<ffi::Piece<'_>> = rust::collect_pieces(str.to_str().unwrap())
- .into_iter()
- .map(Into::into)
- .collect();
+ let pieces: Vec<ffi::Piece<'_>> =
+ rust::collect_pieces(str.to_str().unwrap(), None, None, append_newline)
+ .into_iter()
+ .map(Into::into)
+ .collect();
println!("[ARTHUR]: debug: {:?}, {:?}", pieces.as_ptr(), pieces.len());
@@ -358,6 +360,29 @@ pub extern "C" fn collect_pieces(input: *const libc::c_char) -> PieceSlice {
}
#[no_mangle]
-pub extern "C" fn destroy_pieces(PieceSlice { base_ptr, len, cap }: PieceSlice) {
- let _ = unsafe { Vec::from_raw_parts(base_ptr, len, cap) };
+pub unsafe extern "C" fn destroy_pieces(PieceSlice { base_ptr, len, cap }: PieceSlice) {
+ eprintln!("[ARTHUR] destroying pieces: {base_ptr:?} {len} {cap}");
+ drop(Vec::from_raw_parts(base_ptr, len, cap));
+}
+
+#[no_mangle]
+pub extern "C" fn clone_pieces(
+ base_ptr: *mut ffi::Piece<'static>,
+ len: usize,
+ cap: usize,
+) -> PieceSlice {
+ eprintln!("[ARTHUR] cloning pieces: {base_ptr:?} {len} {cap}");
+
+ let v = unsafe { Vec::from_raw_parts(base_ptr, len, cap) };
+
+ let cloned_v = v.clone();
+
+ // FIXME: Add documentation
+ v.leak();
+
+ PieceSlice {
+ len: cloned_v.len(),
+ cap: cloned_v.capacity(),
+ base_ptr: dbg!(cloned_v.leak().as_mut_ptr()),
+ }
}