diff --git a/cpp2rust/converter/models/boxed_vars.cpp b/cpp2rust/converter/models/boxed_vars.cpp index 429584bca..d8fb506d8 100644 --- a/cpp2rust/converter/models/boxed_vars.cpp +++ b/cpp2rust/converter/models/boxed_vars.cpp @@ -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()); @@ -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(expr)) { diff --git a/tests/unit/out/refcount/lambda_to_fn_ptr.rs b/tests/unit/out/refcount/lambda_to_fn_ptr.rs index b91484eb5..5eabeb4a4 100644 --- a/tests/unit/out/refcount/lambda_to_fn_ptr.rs +++ b/tests/unit/out/refcount/lambda_to_fn_ptr.rs @@ -79,13 +79,13 @@ fn main_0() -> i32 { let n: Value = Rc::new(RefCell::new(n)); let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); (*ap.borrow_mut()) = __args.as_pointer(); - let s: Value = 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::(); + s += (*ap.borrow_mut()).arg::(); i.postfix_inc(); } - return (*s.borrow()); + return s; } }, ))); diff --git a/tests/unit/out/refcount/string_repeat_append.rs b/tests/unit/out/refcount/string_repeat_append.rs index f19eeb24f..669870fe9 100644 --- a/tests/unit/out/refcount/string_repeat_append.rs +++ b/tests/unit/out/refcount/string_repeat_append.rs @@ -71,7 +71,7 @@ fn main_0() -> i32 { (characters.as_pointer() as Ptr), (characters.as_pointer() as Ptr).offset((20_i64) as isize), ) { - let character: Value = Rc::new(RefCell::new((character.read()))); + let mut character: i8 = (character.read()); let mut shrink: i32 = 0; 'loop_: while (shrink < 2) { let s: Value> = Rc::new(RefCell::new( @@ -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() }; @@ -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(); diff --git a/tests/unit/out/refcount/unboxed_conditional_iterator.rs b/tests/unit/out/refcount/unboxed_conditional_iterator.rs index 2f5e3b1b1..b54ea1037 100644 --- a/tests/unit/out/refcount/unboxed_conditional_iterator.rs +++ b/tests/unit/out/refcount/unboxed_conditional_iterator.rs @@ -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 = Rc::new(RefCell::new(1)); let b: Value = Rc::new(RefCell::new(2)); if c { @@ -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, mut len: u32, mut code: Ptr) { +pub fn lehmer_3(mut sigma: Ptr, mut len: u32, mut code: Ptr) { let items: Value> = Rc::new(RefCell::new( (0..(len as usize) as usize) .map(|_| 0_u32) @@ -75,7 +79,7 @@ pub fn lehmer_2(mut sigma: Ptr, mut len: u32, mut code: Ptr) { i.prefix_inc(); } } -pub fn sum_3(v: Ptr>) -> i32 { +pub fn sum_4(v: Ptr>) -> i32 { let mut s: i32 = 0; let it: Value> = Rc::new(RefCell::new(v.decay().begin())); 'loop_: while (*it.borrow()) != v.decay().end() { @@ -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> = Rc::new(RefCell::new(Box::new([2_u32, 0_u32, 3_u32, 1_u32]))); let code: Value> = Rc::new(RefCell::new((0..4).map(|_| 0_u32).collect::>())); ({ - lehmer_2( + lehmer_3( (sigma.as_pointer() as Ptr), 4_u32, (code.as_pointer() as Ptr), @@ -113,7 +119,7 @@ fn main_0() -> i32 { }) ); let v: Value> = 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() {} diff --git a/tests/unit/out/refcount/unboxed_locals.rs b/tests/unit/out/refcount/unboxed_locals.rs index 4c43ba355..1b4897f80 100644 --- a/tests/unit/out/refcount/unboxed_locals.rs +++ b/tests/unit/out/refcount/unboxed_locals.rs @@ -70,6 +70,8 @@ fn main_0() -> i32 { plain.postfix_inc(); assert!((plain == 4)); &(::std::mem::size_of::()); + let mut typed: i32 = (plain + 1); + assert!((typed == 5)); let addr: Value = Rc::new(RefCell::new(0)); ({ set_0((addr.as_pointer()), 5) }); assert!(({ ((*addr.borrow()) == 5) })); diff --git a/tests/unit/out/refcount/wide_char.rs b/tests/unit/out/refcount/wide_char.rs index 74ac016d6..160c479e0 100644 --- a/tests/unit/out/refcount/wide_char.rs +++ b/tests/unit/out/refcount/wide_char.rs @@ -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) -> Ptr { - let s: Value> = Rc::new(RefCell::new(s)); - return (*s.borrow()).clone(); +pub fn get_0(mut s: Ptr) -> Ptr { + return (s).clone(); } -pub fn get_1(s: Ptr) -> Ptr { - let s: Value> = Rc::new(RefCell::new(s)); - return (*s.borrow()).clone(); +pub fn get_1(mut s: Ptr) -> Ptr { + return (s).clone(); } -pub fn get_2(s: Ptr) -> Ptr { - let s: Value> = Rc::new(RefCell::new(s)); - return (*s.borrow()).clone(); +pub fn get_2(mut s: Ptr) -> Ptr { + return (s).clone(); } -pub fn get_3(s: Ptr) -> Ptr { - let s: Value> = Rc::new(RefCell::new(s)); - return (*s.borrow()).clone(); +pub fn get_3(mut s: Ptr) -> Ptr { + return (s).clone(); } -pub fn get_4(s: Ptr) -> Ptr { - let s: Value> = Rc::new(RefCell::new(s)); - return (*s.borrow()).clone(); +pub fn get_4(mut s: Ptr) -> Ptr { + return (s).clone(); } pub fn second_5(s: Ptr) -> i8 { return (elem!((s), 1).read()); diff --git a/tests/unit/out/unsafe/unboxed_conditional_iterator.rs b/tests/unit/out/unsafe/unboxed_conditional_iterator.rs index fe0c17ad8..28373c724 100644 --- a/tests/unit/out/unsafe/unboxed_conditional_iterator.rs +++ b/tests/unit/out/unsafe/unboxed_conditional_iterator.rs @@ -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 { @@ -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 = (0..(len as usize) as usize) .map(|_| 0_u32) .collect::>(); @@ -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 { +pub unsafe fn sum_4(v: *mut Vec) -> i32 { let mut s: i32 = 0; let mut it: UnsafeRandomAccessIterator = (*v).begin(); 'loop_: while it != (*v).end() { @@ -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 = 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() {} diff --git a/tests/unit/out/unsafe/unboxed_locals.rs b/tests/unit/out/unsafe/unboxed_locals.rs index c92d1b168..614fadd92 100644 --- a/tests/unit/out/unsafe/unboxed_locals.rs +++ b/tests/unit/out/unsafe/unboxed_locals.rs @@ -69,6 +69,8 @@ unsafe fn main_0() -> i32 { plain.postfix_inc(); assert!(((plain) == (4))); &(::std::mem::size_of::()); + 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))); diff --git a/tests/unit/unboxed_conditional_iterator.cpp b/tests/unit/unboxed_conditional_iterator.cpp index 521b0add6..d5f848259 100644 --- a/tests/unit/unboxed_conditional_iterator.cpp +++ b/tests/unit/unboxed_conditional_iterator.cpp @@ -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; @@ -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}; diff --git a/tests/unit/unboxed_locals.cpp b/tests/unit/unboxed_locals.cpp index cea557161..f52be8043 100644 --- a/tests/unit/unboxed_locals.cpp +++ b/tests/unit/unboxed_locals.cpp @@ -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;