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/models/boxed_vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ class AddressTakenVisitor
expr->isSemanticForm() ? expr : expr->getSemanticForm(), queue);
}

bool TraverseDecltypeTypeLoc(clang::DecltypeTypeLoc, bool) { return true; }

bool TraverseDecltypeType(clang::DecltypeType *, bool) { return true; }

bool VisitDeclRefExpr(clang::DeclRefExpr *expr) {
if (!value_uses_.contains(expr)) {
AddressTaken(expr->getDecl());
Expand Down Expand Up @@ -259,7 +263,7 @@ class AddressTakenVisitor
// An element of an array or vector, or a field of a struct, is accessed
// without making a pointer to it.
void AddValueUse(const clang::Expr *expr, bool write = false) {
expr = expr->IgnoreParens();
expr = IgnoreNoOpCasts(expr->IgnoreParens());
// The value of a conditional lvalue is the value of either operand; it is
// written through a pointer to the operand.
if (auto *cond = clang::dyn_cast<clang::ConditionalOperator>(expr)) {
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/out/refcount/lambda_to_fn_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ fn main_0() -> i32 {
let n: Value<i32> = Rc::new(RefCell::new(n));
let ap: Value<Ptr<VaArg>> = Rc::new(RefCell::new(Ptr::<VaArg>::default()));
(*ap.borrow_mut()) = __args.as_pointer();
let s: Value<i32> = Rc::new(RefCell::new(0));
let mut s: i32 = 0;
let mut i: i32 = 0;
'loop_: while (i < (*n.borrow())) {
(*s.borrow_mut()) += (*ap.borrow_mut()).arg::<i32>();
s += (*ap.borrow_mut()).arg::<i32>();
i.postfix_inc();
}
return (*s.borrow());
return s;
}
},
)));
Expand Down
22 changes: 10 additions & 12 deletions tests/unit/out/refcount/string_repeat_append.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn main_0() -> i32 {
(characters.as_pointer() as Ptr<i8>),
(characters.as_pointer() as Ptr<i8>).offset((20_i64) as isize),
) {
let character: Value<i8> = Rc::new(RefCell::new((character.read())));
let mut character: i8 = (character.read());
let mut shrink: i32 = 0;
'loop_: while (shrink < 2) {
let s: Value<Vec<i8>> = Rc::new(RefCell::new(
Expand All @@ -95,7 +95,7 @@ fn main_0() -> i32 {
{
(*s.borrow_mut()).pop();
let __len = (*s.borrow()).len() + (count) as usize;
(*s.borrow_mut()).resize(__len, ({ (*character.borrow()) }));
(*s.borrow_mut()).resize(__len, character);
(*s.borrow_mut()).push(0);
(*s.borrow()).clone()
};
Expand All @@ -104,16 +104,14 @@ fn main_0() -> i32 {
let mut i: usize = 0_usize;
'loop_: while (i < (length).wrapping_add(count)) {
assert!(
({
({ ((elem!(data, i).read()) as i32) } == {
((if (i < length) {
initial[((i).wrapping_rem(
(::std::mem::size_of::<[i8; 5]>() as usize),
)) as usize]
} else {
(*character.borrow())
}) as i32)
})
({ ((elem!(data, i).read()) as i32) } == {
((if (i < length) {
initial[((i)
.wrapping_rem((::std::mem::size_of::<[i8; 5]>() as usize)))
as usize]
} else {
character
}) as i32)
})
);
i.prefix_inc();
Expand Down
20 changes: 13 additions & 7 deletions tests/unit/out/refcount/unboxed_conditional_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ pub fn median_0(mut w: i32, mut n: i32, mut nw: i32) -> i32 {
}
return ((n + w) - nw);
}
pub fn assign_1(mut c: bool, mut v: i32) -> i32 {
pub fn at_least_1(mut w: i32, mut lo: i32) -> i32 {
let mut bound: i32 = lo;
return if (w > bound) { w } else { bound };
}
pub fn assign_2(mut c: bool, mut v: i32) -> i32 {
let a: Value<i32> = Rc::new(RefCell::new(1));
let b: Value<i32> = Rc::new(RefCell::new(2));
if c {
Expand All @@ -33,7 +37,7 @@ pub fn assign_1(mut c: bool, mut v: i32) -> i32 {
.with_mut(|__v| __v.prefix_inc());
return (((*a.borrow()) * 10) + (*b.borrow()));
}
pub fn lehmer_2(mut sigma: Ptr<u32>, mut len: u32, mut code: Ptr<u32>) {
pub fn lehmer_3(mut sigma: Ptr<u32>, mut len: u32, mut code: Ptr<u32>) {
let items: Value<Vec<u32>> = Rc::new(RefCell::new(
(0..(len as usize) as usize)
.map(|_| 0_u32)
Expand Down Expand Up @@ -75,7 +79,7 @@ pub fn lehmer_2(mut sigma: Ptr<u32>, mut len: u32, mut code: Ptr<u32>) {
i.prefix_inc();
}
}
pub fn sum_3(v: Ptr<Vec<i32>>) -> i32 {
pub fn sum_4(v: Ptr<Vec<i32>>) -> i32 {
let mut s: i32 = 0;
let it: Value<RandomAccessIterator<i32>> = Rc::new(RefCell::new(v.decay().begin()));
'loop_: while (*it.borrow()) != v.decay().end() {
Expand All @@ -92,13 +96,15 @@ fn main_0() -> i32 {
assert!((({ median_0(1, 5, 7,) }) == 1));
assert!((({ median_0(1, 5, 0,) }) == 5));
assert!((({ median_0(1, 5, 3,) }) == 3));
assert!((({ assign_1(true, 5,) }) == 62));
assert!((({ assign_1(false, 5,) }) == 16));
assert!((({ at_least_1(1, 3,) }) == 3));
assert!((({ at_least_1(4, 3,) }) == 4));
assert!((({ assign_2(true, 5,) }) == 62));
assert!((({ assign_2(false, 5,) }) == 16));
let sigma: Value<Box<[u32]>> = Rc::new(RefCell::new(Box::new([2_u32, 0_u32, 3_u32, 1_u32])));
let code: Value<Box<[u32]>> =
Rc::new(RefCell::new((0..4).map(|_| 0_u32).collect::<Box<[u32]>>()));
({
lehmer_2(
lehmer_3(
(sigma.as_pointer() as Ptr<u32>),
4_u32,
(code.as_pointer() as Ptr<u32>),
Expand All @@ -113,7 +119,7 @@ fn main_0() -> i32 {
})
);
let v: Value<Vec<i32>> = Rc::new(RefCell::new(Vec::from([1, 2, 3])));
assert!((({ sum_3(v.as_pointer(),) }) == 6));
assert!((({ sum_4(v.as_pointer(),) }) == 6));
return 0;
}
pub fn __cpp2rust_init_globals() {}
2 changes: 2 additions & 0 deletions tests/unit/out/refcount/unboxed_locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ fn main_0() -> i32 {
plain.postfix_inc();
assert!((plain == 4));
&(::std::mem::size_of::<i32>());
let mut typed: i32 = (plain + 1);
assert!((typed == 5));
let addr: Value<i32> = Rc::new(RefCell::new(0));
({ set_0((addr.as_pointer()), 5) });
assert!(({ ((*addr.borrow()) == 5) }));
Expand Down
25 changes: 10 additions & 15 deletions tests/unit/out/refcount/wide_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,20 @@ use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn get_0(s: Ptr<i8>) -> Ptr<i8> {
let s: Value<Ptr<i8>> = Rc::new(RefCell::new(s));
return (*s.borrow()).clone();
pub fn get_0(mut s: Ptr<i8>) -> Ptr<i8> {
return (s).clone();
}
pub fn get_1(s: Ptr<i32>) -> Ptr<i32> {
let s: Value<Ptr<i32>> = Rc::new(RefCell::new(s));
return (*s.borrow()).clone();
pub fn get_1(mut s: Ptr<i32>) -> Ptr<i32> {
return (s).clone();
}
pub fn get_2(s: Ptr<u8>) -> Ptr<u8> {
let s: Value<Ptr<u8>> = Rc::new(RefCell::new(s));
return (*s.borrow()).clone();
pub fn get_2(mut s: Ptr<u8>) -> Ptr<u8> {
return (s).clone();
}
pub fn get_3(s: Ptr<u16>) -> Ptr<u16> {
let s: Value<Ptr<u16>> = Rc::new(RefCell::new(s));
return (*s.borrow()).clone();
pub fn get_3(mut s: Ptr<u16>) -> Ptr<u16> {
return (s).clone();
}
pub fn get_4(s: Ptr<u32>) -> Ptr<u32> {
let s: Value<Ptr<u32>> = Rc::new(RefCell::new(s));
return (*s.borrow()).clone();
pub fn get_4(mut s: Ptr<u32>) -> Ptr<u32> {
return (s).clone();
}
pub fn second_5(s: Ptr<i8>) -> i8 {
return (elem!((s), 1).read());
Expand Down
20 changes: 13 additions & 7 deletions tests/unit/out/unsafe/unboxed_conditional_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ pub unsafe fn median_0(mut w: i32, mut n: i32, mut nw: i32) -> i32 {
}
return (((n) + (w)) - (nw));
}
pub unsafe fn assign_1(mut c: bool, mut v: i32) -> i32 {
pub unsafe fn at_least_1(mut w: i32, mut lo: i32) -> i32 {
let bound: i32 = lo;
return if ((w) > (bound)) { w } else { bound };
}
pub unsafe fn assign_2(mut c: bool, mut v: i32) -> i32 {
let mut a: i32 = 1;
let mut b: i32 = 2;
(*if c {
Expand All @@ -32,7 +36,7 @@ pub unsafe fn assign_1(mut c: bool, mut v: i32) -> i32 {
.prefix_inc();
return (((a) * (10)) + (b));
}
pub unsafe fn lehmer_2(mut sigma: *const u32, mut len: u32, mut code: *mut u32) {
pub unsafe fn lehmer_3(mut sigma: *const u32, mut len: u32, mut code: *mut u32) {
let mut items: Vec<u32> = (0..(len as usize) as usize)
.map(|_| 0_u32)
.collect::<Vec<_>>();
Expand Down Expand Up @@ -66,7 +70,7 @@ pub unsafe fn lehmer_2(mut sigma: *const u32, mut len: u32, mut code: *mut u32)
i.prefix_inc();
}
}
pub unsafe fn sum_3(v: *mut Vec<i32>) -> i32 {
pub unsafe fn sum_4(v: *mut Vec<i32>) -> i32 {
let mut s: i32 = 0;
let mut it: UnsafeRandomAccessIterator<i32> = (*v).begin();
'loop_: while it != (*v).end() {
Expand All @@ -85,18 +89,20 @@ unsafe fn main_0() -> i32 {
assert!(((unsafe { median_0(1, 5, 7,) }) == (1)));
assert!(((unsafe { median_0(1, 5, 0,) }) == (5)));
assert!(((unsafe { median_0(1, 5, 3,) }) == (3)));
assert!(((unsafe { assign_1(true, 5,) }) == (62)));
assert!(((unsafe { assign_1(false, 5,) }) == (16)));
assert!(((unsafe { at_least_1(1, 3,) }) == (3)));
assert!(((unsafe { at_least_1(4, 3,) }) == (4)));
assert!(((unsafe { assign_2(true, 5,) }) == (62)));
assert!(((unsafe { assign_2(false, 5,) }) == (16)));
let mut sigma: [u32; 4] = [2_u32, 0_u32, 3_u32, 1_u32];
let mut code: [u32; 4] = [0_u32; 4];
(unsafe { lehmer_2((sigma.as_mut_ptr()).cast_const(), 4_u32, code.as_mut_ptr()) });
(unsafe { lehmer_3((sigma.as_mut_ptr()).cast_const(), 4_u32, code.as_mut_ptr()) });
assert!(
((((code[(0) as usize]) == (2_u32)) && ((code[(1) as usize]) == (0_u32)))
&& ((code[(2) as usize]) == (1_u32)))
&& ((code[(3) as usize]) == (0_u32))
);
let mut v: Vec<i32> = Vec::from([1, 2, 3]);
assert!(((unsafe { sum_3(&mut v,) }) == (6)));
assert!(((unsafe { sum_4(&mut v,) }) == (6)));
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
2 changes: 2 additions & 0 deletions tests/unit/out/unsafe/unboxed_locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ unsafe fn main_0() -> i32 {
plain.postfix_inc();
assert!(((plain) == (4)));
&(::std::mem::size_of::<i32>());
let mut typed: i32 = ((plain) + (1));
assert!(((typed) == (5)));
let mut addr: i32 = 0;
(unsafe { set_0((&mut addr as *mut i32), 5) });
assert!(((addr) == (5)));
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/unboxed_conditional_iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ int median(int w, int n, int nw) {
return n + w - nw;
}

int at_least(int w, int lo) {
const int bound = lo;
return w > bound ? w : bound;
}

// a and b are written through the conditional.
int assign(bool c, int v) {
int a = 1;
Expand Down Expand Up @@ -48,6 +53,8 @@ int main() {
assert(median(1, 5, 7) == 1);
assert(median(1, 5, 0) == 5);
assert(median(1, 5, 3) == 3);
assert(at_least(1, 3) == 3);
assert(at_least(4, 3) == 4);
assert(assign(true, 5) == 62);
assert(assign(false, 5) == 16);
unsigned sigma[] = {2, 0, 3, 1};
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/unboxed_locals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ int main() {
plain++;
assert(plain == 4);
(void)sizeof(plain);
decltype(plain) typed = plain + 1;
assert(typed == 5);

// Boxed: its address is taken.
int addr = 0;
Expand Down
Loading