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
5 changes: 5 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3804,6 +3804,11 @@ void Converter::ConvertCXXConstructExprArgs(clang::CXXConstructExpr *expr) {
bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
PushSuppressIteratorClone push(*this, expr);

if (expr->isElidable()) {
Convert(expr->getArg(0));
return false;
}

if (auto str = GetMappedAsString(expr, expr->getArgs(), expr->getNumArgs());
!str.empty()) {
StrCat(str);
Expand Down
5 changes: 5 additions & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2372,6 +2372,11 @@ std::string ConverterRefCount::ConvertStream(clang::Expr *expr) {
}

bool ConverterRefCount::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
if (expr->isElidable()) {
Convert(expr->getArg(0));
return false;
}

// An array is boxed unless it initializes an unboxed variable.
bool boxed_array = expr->getType()->isArrayType() &&
getConversionKind() != ConversionKind::Unboxed;
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/elidable_iterator_copy_cxx11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++11
#include <algorithm>
#include <cassert>
#include <vector>

int main() {
std::vector<int> v;
v.push_back(4);
v.push_back(7);
std::vector<int>::iterator it = std::find(v.begin(), v.end(), 7);
assert(it != v.end());
assert(*it == 7);
return 0;
}
39 changes: 39 additions & 0 deletions tests/unit/out/refcount/elidable_iterator_copy_cxx11.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
}
fn main_0() -> i32 {
let v: Value<Vec<i32>> = Rc::new(RefCell::new(Vec::new()));
{
let __a1 = 4;
(*v.borrow_mut()).push(__a1)
};
{
let __a1 = 7;
(*v.borrow_mut()).push(__a1)
};
let it: Value<Ptr<i32>> = Rc::new(RefCell::new({
let count = ((v.as_pointer() as Ptr<i32>).to_end().get_offset()
- (v.as_pointer() as Ptr<i32>).get_offset()) as usize;
(v.as_pointer() as Ptr<i32>).offset(
(v.as_pointer() as Ptr<i32>)
.clone()
.into_iter()
.take(count)
.position(|value_0| value_0.read() == 7)
.unwrap_or(count) as isize,
)
}));
assert!((*it.borrow()) != (v.as_pointer() as Ptr<i32>).to_end());
assert!((((*it.borrow()).read()) == 7));
return 0;
}
pub fn __cpp2rust_init_globals() {}
31 changes: 14 additions & 17 deletions tests/unit/out/refcount/huffman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,20 @@ impl MinHeap {
}
}
pub fn AllocMinHeap_1(mut capacity: i32) -> Option<Value<MinHeap>> {
let mut minHeap: Option<Value<MinHeap>> = Some(Rc::new(RefCell::new({
let __tmp_0: Value<MinHeap> = Rc::new(RefCell::new(MinHeap {
size: 0,
capacity: capacity,
arr: Some(Rc::new(RefCell::new(
(0..(capacity as usize))
.map(|_| <Ptr<MinHeapNode>>::default())
.collect::<Box<[_]>>(),
))),
next: 0,
alloc: Some(Rc::new(RefCell::new(
(0..10000_usize)
.map(|_| <MinHeapNode>::default())
.collect::<Box<[_]>>(),
))),
}));
MinHeap::move_from({ __tmp_0.as_pointer() })
let mut minHeap: Option<Value<MinHeap>> = Some(Rc::new(RefCell::new(MinHeap {
size: 0,
capacity: capacity,
arr: Some(Rc::new(RefCell::new(
(0..(capacity as usize))
.map(|_| <Ptr<MinHeapNode>>::default())
.collect::<Box<[_]>>(),
))),
next: 0,
alloc: Some(Rc::new(RefCell::new(
(0..10000_usize)
.map(|_| <MinHeapNode>::default())
.collect::<Box<[_]>>(),
))),
})));
return minHeap.take();
}
Expand Down
7 changes: 2 additions & 5 deletions tests/unit/out/refcount/unique_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,8 @@ pub fn main() {
fn main_0() -> i32 {
let x: Value<Option<Value<i32>>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(0)))));
let safe_ptr: Value<Option<Value<SafePointer>>> =
Rc::new(RefCell::new(Some(Rc::new(RefCell::new({
let __tmp_0: Value<SafePointer> = Rc::new(RefCell::new(SafePointer {
ptr: (*x.borrow_mut()).take(),
}));
SafePointer::move_from({ __tmp_0.as_pointer() })
Rc::new(RefCell::new(Some(Rc::new(RefCell::new(SafePointer {
ptr: (*x.borrow_mut()).take(),
})))));
({ DoStuffWithSafePointer_0(safe_ptr.as_pointer()) });
assert!((({ Consume_1((*safe_ptr.borrow_mut()).take(),) }) == 60));
Expand Down
7 changes: 2 additions & 5 deletions tests/unit/out/refcount/unique_ptr_nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let mut o: Option<Value<Outer>> = Some(Rc::new(RefCell::new({
let __tmp_0: Value<Outer> = Rc::new(RefCell::new(Outer {
inner: Some(Rc::new(RefCell::new(Inner { x: 10, y: 20 }))),
}));
Outer::move_from({ __tmp_0.as_pointer() })
let mut o: Option<Value<Outer>> = Some(Rc::new(RefCell::new(Outer {
inner: Some(Rc::new(RefCell::new(Inner { x: 10, y: 20 }))),
})));
(*{ (*o.as_ref().unwrap().borrow()).inner.clone() }
.as_ref()
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/out/unsafe/elidable_iterator_copy_cxx11.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut v: Vec<i32> = Vec::new();
{
let __a1 = 4;
v.push(__a1)
};
{
let __a1 = 7;
v.push(__a1)
};
let mut it: *mut i32 = {
let mut it = v.as_mut_ptr();
while it != v.as_mut_ptr().add(v.len()) && *it != 7 {
it = it.add(1);
}
it
};
assert!(it != v.as_mut_ptr().add(v.len()));
assert!(((*it) == (7)));
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
31 changes: 14 additions & 17 deletions tests/unit/out/unsafe/huffman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,20 @@ impl MinHeap {
}
}
pub unsafe fn AllocMinHeap_1(mut capacity: i32) -> Option<Box<MinHeap>> {
let mut minHeap: Option<Box<MinHeap>> = Some(Box::new({
let mut __tmp_0: MinHeap = MinHeap {
size: 0,
capacity: capacity,
arr: Some(
(0..(capacity as usize))
.map(|_| <*mut MinHeapNode>::default())
.collect::<Box<[_]>>(),
),
next: 0,
alloc: Some(
(0..10000_usize)
.map(|_| <MinHeapNode>::default())
.collect::<Box<[_]>>(),
),
};
MinHeap::move_from({ &mut __tmp_0 })
let mut minHeap: Option<Box<MinHeap>> = Some(Box::new(MinHeap {
size: 0,
capacity: capacity,
arr: Some(
(0..(capacity as usize))
.map(|_| <*mut MinHeapNode>::default())
.collect::<Box<[_]>>(),
),
next: 0,
alloc: Some(
(0..10000_usize)
.map(|_| <MinHeapNode>::default())
.collect::<Box<[_]>>(),
),
}));
return minHeap.take();
}
Expand Down
5 changes: 1 addition & 4 deletions tests/unit/out/unsafe/unique_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,7 @@ pub fn main() {
}
unsafe fn main_0() -> i32 {
let mut x: Option<Box<i32>> = Some(Box::new(0));
let mut safe_ptr: Option<Box<SafePointer>> = Some(Box::new({
let mut __tmp_0: SafePointer = SafePointer { ptr: x.take() };
SafePointer::move_from({ &mut __tmp_0 })
}));
let mut safe_ptr: Option<Box<SafePointer>> = Some(Box::new(SafePointer { ptr: x.take() }));
(unsafe { DoStuffWithSafePointer_0(&mut safe_ptr) });
assert!(((unsafe { Consume_1(safe_ptr.take(),) }) == (60)));
return 0;
Expand Down
7 changes: 2 additions & 5 deletions tests/unit/out/unsafe/unique_ptr_nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ pub fn main() {
}
}
unsafe fn main_0() -> i32 {
let mut o: Option<Box<Outer>> = Some(Box::new({
let mut __tmp_0: Outer = Outer {
inner: Some(Box::new(Inner { x: 10, y: 20 })),
};
Outer::move_from({ &mut __tmp_0 })
let mut o: Option<Box<Outer>> = Some(Box::new(Outer {
inner: Some(Box::new(Inner { x: 10, y: 20 })),
}));
(*(*o.as_deref_mut().unwrap()).inner.as_deref_mut().unwrap()).x += 5;
let mut sum: i32 = (((*(*o.as_deref_mut().unwrap()).inner.as_deref_mut().unwrap()).x)
Expand Down
Loading