Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3440,12 +3440,16 @@ bool Converter::VisitCXXNewExpr(clang::CXXNewExpr *expr) {
if (!curr_init_type_.empty() && curr_init_type_.back()->isPointerType()) {
StrCat(".as_mut_ptr()");
}
SetFreshType(expr->getType());
} else {
auto initializer_as_string = ToString(expr->getInitializer());
auto initializer_as_string =
expr->getInitializer() ? ToString(expr->getInitializer())
: GetDefaultAsString(expr->getAllocatedType());
auto new_as_string =
std::format("(Box::leak(Box::new({})) as {})", initializer_as_string,
ToString(expr->getType()));
StrCat(new_as_string);
SetFreshType(expr->getType());
}
return false;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/new.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
#include <cassert>

struct Pair {
int x, y;
};

int main() {
int *x = new int(5);
int out = *x;
delete x;
assert(out == 5);

int *y = new int;
*y = 9;
assert(*y == 9);
delete y;

Pair *p = new Pair;
p->x = 1;
p->y = 2;
assert(p->x + p->y == 3);
delete p;
return 0;
}
17 changes: 17 additions & 0 deletions tests/unit/new_array.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
static int sum(int *p, int n) {
int total = 0;
for (int i = 0; i < n; ++i) {
total += p[i];
}
return total;
}

int main() {
int *array = new int[100];
delete[] array;

int *filled = new int[4];
for (int i = 0; i < 4; ++i) {
filled[i] = i + 1;
}
if (sum(filled, 4) != 10) {
return 1;
}
delete[] filled;
return 0;
}
44 changes: 44 additions & 0 deletions tests/unit/out/refcount/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@ use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
#[derive(Default)]
pub struct Pair {
pub x: Value<i32>,
pub y: Value<i32>,
}
impl Clone for Pair {
fn clone(&self) -> Self {
let __this: Value<Pair> = Rc::new(RefCell::new(Self {
x: Rc::new(RefCell::new((*self.x.borrow()))),
y: Rc::new(RefCell::new((*self.y.borrow()))),
}));
let this: Ptr<Pair> = __this.as_pointer();
Rc::try_unwrap(__this).ok().unwrap().into_inner()
}
}
impl ByteRepr for Pair {
fn byte_size() -> usize {
8
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.x.borrow()).to_bytes(&mut buf[0..4]);
(*self.y.borrow()).to_bytes(&mut buf[4..8]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
y: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))),
}
}
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand All @@ -15,6 +45,20 @@ fn main_0() -> i32 {
let out: Value<i32> = Rc::new(RefCell::new(((*x.borrow()).read())));
(*x.borrow()).delete();
assert!(((*out.borrow()) == 5));
let y: Value<Ptr<i32>> = Rc::new(RefCell::new(Ptr::alloc(Default::default())));
(*y.borrow()).write(9);
assert!((((*y.borrow()).read()) == 9));
(*y.borrow()).delete();
let p: Value<Ptr<Pair>> = Rc::new(RefCell::new(Ptr::alloc(<Pair>::default())));
(*(*(*p.borrow()).upgrade().deref()).x.borrow_mut()) = 1;
(*(*(*p.borrow()).upgrade().deref()).y.borrow_mut()) = 2;
assert!(
({
let _lhs = (*(*(*p.borrow()).upgrade().deref()).x.borrow());
_lhs + (*(*(*p.borrow()).upgrade().deref()).y.borrow())
} == 3)
);
(*p.borrow()).delete();
return 0;
}
pub fn __cpp2rust_init_globals() {}
33 changes: 33 additions & 0 deletions tests/unit/out/refcount/new_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn sum_0(p: Ptr<i32>, n: i32) -> i32 {
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(p));
let n: Value<i32> = Rc::new(RefCell::new(n));
let total: Value<i32> = Rc::new(RefCell::new(0));
let i: Value<i32> = Rc::new(RefCell::new(0));
'loop_: while ((*i.borrow()) < (*n.borrow())) {
let __rhs = ((*p.borrow()).offset((*i.borrow()) as isize).read());
(*total.borrow_mut()) += __rhs;
(*i.borrow_mut()).prefix_inc();
}
return (*total.borrow());
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand All @@ -17,6 +29,27 @@ fn main_0() -> i32 {
.collect::<Box<[i32]>>(),
)));
(*array.borrow()).delete_array();
let filled: Value<Ptr<i32>> = Rc::new(RefCell::new(Ptr::alloc_array(
(0..4_usize)
.map(|_| <i32>::default())
.collect::<Box<[i32]>>(),
)));
let i: Value<i32> = Rc::new(RefCell::new(0));
'loop_: while ((*i.borrow()) < 4) {
let __rhs = ((*i.borrow()) + 1);
(*filled.borrow())
.offset((*i.borrow()) as isize)
.write(__rhs);
(*i.borrow_mut()).prefix_inc();
}
if (({
let _p: Ptr<i32> = (*filled.borrow()).clone();
sum_0(_p, 4)
}) != 10)
{
return 1;
}
(*filled.borrow()).delete_array();
return 0;
}
pub fn __cpp2rust_init_globals() {}
15 changes: 15 additions & 0 deletions tests/unit/out/unsafe/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
#[repr(C)]
#[derive(Copy, Clone, Default)]
pub struct Pair {
pub x: i32,
pub y: i32,
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand All @@ -17,6 +23,15 @@ unsafe fn main_0() -> i32 {
let mut out: i32 = (*x);
::std::mem::drop(Box::from_raw(x));
assert!(((out) == (5)));
let mut y: *mut i32 = (Box::leak(Box::new(0_i32)) as *mut i32);
(*y) = 9;
assert!(((*y) == (9)));
::std::mem::drop(Box::from_raw(y));
let mut p: *mut Pair = (Box::leak(Box::new(<Pair>::default())) as *mut Pair);
(*p).x = 1;
(*p).y = 2;
assert!(((((*p).x) + ((*p).y)) == (3)));
::std::mem::drop(Box::from_raw(p));
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
27 changes: 27 additions & 0 deletions tests/unit/out/unsafe/new_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub unsafe fn sum_0(mut p: *mut i32, mut n: i32) -> i32 {
let mut total: i32 = 0;
let mut i: i32 = 0;
'loop_: while ((i) < (n)) {
total += (*p.offset((i) as isize));
i.prefix_inc();
}
return total;
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand All @@ -20,6 +29,24 @@ unsafe fn main_0() -> i32 {
array,
libcc2rs::malloc_usable_size(array as *mut ::libc::c_void) / ::std::mem::size_of::<i32>(),
)));
let mut filled: *mut i32 =
Box::leak((0..4_usize).map(|_| 0_i32).collect::<Box<[i32]>>()).as_mut_ptr();
let mut i: i32 = 0;
'loop_: while ((i) < (4)) {
(*filled.offset((i) as isize)) = ((i) + (1));
i.prefix_inc();
}
if ((unsafe {
let _p: *mut i32 = filled;
sum_0(_p, 4)
}) != (10))
{
return 1;
}
::std::mem::drop(Box::from_raw(::std::slice::from_raw_parts_mut(
filled,
libcc2rs::malloc_usable_size(filled as *mut ::libc::c_void) / ::std::mem::size_of::<i32>(),
)));
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
Loading