diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 35e6275ee5..3fb7db3dbe 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -5575,7 +5575,8 @@ std::string Converter::GetMappedAsString(clang::Expr *expr, clang::Expr **args, // The rule takes the value of a transparent union. auto *arg = IgnoreTransparentUnion(all_args[i]); auto type = arg->getType(); - bool by_value = arg->isPRValue() && !tgt_ir->takesAddress(ctx_, i, arg); + bool by_value = + arg->isPRValue() && !tgt_ir->takesAddress(ctx_, expr, i, arg); if (!by_value && arg->isPRValue()) { arg = new (ctx_) clang::MaterializeTemporaryExpr(type, arg, false); } @@ -5670,7 +5671,9 @@ std::string Converter::ConvertIRFragment( assert(arg_idx < all_args.size()); auto *arg = all_args[arg_idx]; bool is_receiver = HasReceiver(expr) && arg_idx == 0; - bool is_cpp_ptr = arg->getType()->isPointerType(); + bool cpp_ptr = arg->getType()->isPointerType(); + bool is_cpp_ptr = + cpp_ptr && RuleRegistry::ParamTakesPointerValue(expr, arg_idx); PlaceholderCtx ph_ctx{ .arg_idx = arg_idx, @@ -5683,9 +5686,8 @@ std::string Converter::ConvertIRFragment( .is_cpp_ptr = is_cpp_ptr, // Whatever its spelling, a pointer in C++ is converted as one. .maps_to_rust_ptr = - !is_cpp_ptr && - RuleRegistry::MapsToPointer( - ctx_, arg->IgnoreParenNoopCasts(ctx_)->getType()), + !cpp_ptr && RuleRegistry::MapsToPointer( + ctx_, arg->IgnoreParenNoopCasts(ctx_)->getType()), .declared_in_rule_as_rust_ptr = RuleRegistry::ParamIsPointer(ctx_, expr, arg_idx), .is_index_base = ph->is_index_base, diff --git a/cpp2rust/converter/models/boxed_vars.cpp b/cpp2rust/converter/models/boxed_vars.cpp index 144875a883..f550a21e5b 100644 --- a/cpp2rust/converter/models/boxed_vars.cpp +++ b/cpp2rust/converter/models/boxed_vars.cpp @@ -300,7 +300,7 @@ class AddressTakenVisitor // borrowed, unless the rule takes a pointer to them, other than to a // temporary copy of them, or they are bound to references. A pointer // parameter receives the value of an argument that is itself translated to a - // pointer. + // pointer, unless the rule binds a reference to that argument. void AddRuleArgs(clang::Expr *expr, clang::Expr **args, unsigned num_args) { auto *rule = RuleRegistry::GetExprRule(ctx_, expr); if (!rule) { @@ -337,7 +337,7 @@ class AddressTakenVisitor })) { continue; } - if (!rule->takesAddress(ctx_, i, arg) || + if (!rule->takesAddress(ctx_, expr, i, arg) || rule->takesTemporaryAddress(ctx_, expr, i, arg)) { AddValueUse(arg); } diff --git a/cpp2rust/converter/rules/registry.cpp b/cpp2rust/converter/rules/registry.cpp index ef867d4b97..059acbe475 100644 --- a/cpp2rust/converter/rules/registry.cpp +++ b/cpp2rust/converter/rules/registry.cpp @@ -562,6 +562,24 @@ bool ParamIsPointer(clang::ASTContext &ctx, const clang::Expr *expr, return rule->params.at(index).is_pointer(); } +bool ParamTakesPointerValue(const clang::Expr *expr, unsigned index) { + if (HasReceiver(const_cast(expr)) && index == 0) { + return true; + } + auto match = findExpr(expr); + assert(match.rule_decl && "expression must have a translation rule"); + auto decl = match.rule_decl->decl; + auto function = decl->getAsFunction(); + auto type = + function ? function->getType() + : llvm::cast(decl)->getType()->getPointeeType(); + auto proto = type->getAs(); + assert(proto && "expression rule is neither a function nor a function " + "pointer"); + return index >= proto->getNumParams() || + !proto->getParamType(index)->isReferenceType(); +} + clang::Expr *BuildParamConversion(clang::ASTContext &ctx, const clang::Expr *expr, unsigned index, clang::Expr *arg) { diff --git a/cpp2rust/converter/rules/registry.h b/cpp2rust/converter/rules/registry.h index f25617ad4b..441080387f 100644 --- a/cpp2rust/converter/rules/registry.h +++ b/cpp2rust/converter/rules/registry.h @@ -57,6 +57,8 @@ bool ReturnsReference(const clang::Expr *expr); bool ParamIsPointer(clang::ASTContext &ctx, const clang::Expr *expr, unsigned index); +bool ParamTakesPointerValue(const clang::Expr *expr, unsigned index); + clang::Expr *BuildParamConversion(clang::ASTContext &ctx, const clang::Expr *expr, unsigned index, clang::Expr *arg); diff --git a/cpp2rust/converter/translation_rule.cpp b/cpp2rust/converter/translation_rule.cpp index bc82a38bd3..ee304a6f95 100644 --- a/cpp2rust/converter/translation_rule.cpp +++ b/cpp2rust/converter/translation_rule.cpp @@ -332,14 +332,19 @@ ExprRule::argsToBind(clang::ASTContext &ctx, const clang::Expr *expr, return bind; } -bool ExprRule::takesAddress(clang::ASTContext &ctx, unsigned i, - const clang::Expr *arg) const { +bool ExprRule::takesAddress(clang::ASTContext &ctx, const clang::Expr *expr, + unsigned i, const clang::Expr *arg) const { // An init pack forwards references to its arguments, not their values. if (i >= params.size()) { return usesInit(); } - return params[i].is_pointer() && !arg->getType()->isPointerType() && - !RuleRegistry::MapsToPointer(ctx, arg->getType()); + if (!params[i].is_pointer()) { + return false; + } + if (arg->getType()->isPointerType()) { + return !RuleRegistry::ParamTakesPointerValue(expr, i); + } + return !RuleRegistry::MapsToPointer(ctx, arg->getType()); } bool ExprRule::takesTemporaryAddress(clang::ASTContext &ctx, @@ -347,7 +352,7 @@ bool ExprRule::takesTemporaryAddress(clang::ASTContext &ctx, const clang::Expr *arg) const { auto *call = clang::dyn_cast(expr); unsigned first_arg = clang::isa(expr); - return call && i >= first_arg && takesAddress(ctx, i, arg) && + return call && i >= first_arg && takesAddress(ctx, expr, i, arg) && GetRefBindingTempType(ctx, call, i - first_arg).has_value(); } diff --git a/cpp2rust/converter/translation_rule.h b/cpp2rust/converter/translation_rule.h index d5be83aa90..99ebaae628 100644 --- a/cpp2rust/converter/translation_rule.h +++ b/cpp2rust/converter/translation_rule.h @@ -138,9 +138,9 @@ struct ExprRule { std::vector argsToBind(clang::ASTContext &ctx, const clang::Expr *expr, llvm::ArrayRef args) const; - // Whether the body takes a pointer to arg, its argument i, which is not a - // pointer. - bool takesAddress(clang::ASTContext &ctx, unsigned i, + // Whether the body takes a pointer to arg, argument i of expr, rather than + // its value. + bool takesAddress(clang::ASTContext &ctx, const clang::Expr *expr, unsigned i, const clang::Expr *arg) const; // Whether the body takes a pointer to a temporary copy of arg, argument i diff --git a/tests/unit/map.cpp b/tests/unit/map.cpp index caa288ecd8..5a330ae8c7 100644 --- a/tests/unit/map.cpp +++ b/tests/unit/map.cpp @@ -147,5 +147,20 @@ int main() { k = 0; const auto &value_0 = m.at(k); assert(m.size() + x1 + x2 + x3 + x4 + x5 + value_0 == 21); + + int pk1 = 0, pk2 = 0; + std::map ptr_counts; + int *ptr_key = &pk1; + ptr_counts[ptr_key] = 1; + ptr_key = &pk2; + ptr_counts[ptr_key] = 2; + ptr_counts[ptr_key] += 3; + assert(ptr_counts.size() == 2); + assert(ptr_counts[ptr_key] == 5); + ptr_key = &pk1; + assert(ptr_counts[ptr_key] == 1); + ptr_counts[&pk2] += 1; + assert(ptr_counts[&pk2] == 6); + assert(pk1 == 0 && pk2 == 0); return 0; } diff --git a/tests/unit/min_max_ptr2ptr.cpp b/tests/unit/min_max_ptr2ptr.cpp index fbd0523ce8..f30ebd4b93 100644 --- a/tests/unit/min_max_ptr2ptr.cpp +++ b/tests/unit/min_max_ptr2ptr.cpp @@ -9,5 +9,13 @@ int main() { int r1 = std::max(**ppa, **ppb); int r2 = std::min(**ppa, **ppb); assert(r1 + r2 == 30); + + int values[2] = {2, 1}; + int *low = &values[0]; + int *high = &values[1]; + assert(std::min(low, high) == low); + assert(std::min(high, low) == low); + assert(std::max(low, high) == high); + assert(std::max(high, low) == high); return 0; } diff --git a/tests/unit/out/refcount/map.rs b/tests/unit/out/refcount/map.rs index c4ed1ab17e..18a58fc26d 100644 --- a/tests/unit/out/refcount/map.rs +++ b/tests/unit/out/refcount/map.rs @@ -758,6 +758,40 @@ fn main_0() -> i32 { == 21_usize) }) ); + let pk1: Value = Rc::new(RefCell::new(0)); + let pk2: Value = Rc::new(RefCell::new(0)); + let ptr_counts: Value, (Value>, Value)>> = + Rc::new(RefCell::new(Map::new())); + let ptr_key: Value> = Rc::new(RefCell::new((pk1.as_pointer()))); + Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer()).write(1); + (*ptr_key.borrow_mut()) = (pk2.as_pointer()); + Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer()).write(2); + { + Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer()) + .with_mut(|__v| *__v = *__v + 3) + }; + assert!(({ ((*ptr_counts.borrow()).len() == 2_usize) })); + assert!(((Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer()).read()) == 5)); + (*ptr_key.borrow_mut()) = (pk1.as_pointer()); + assert!(((Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer()).read()) == 1)); + { + Map::subscript_move( + ptr_counts.as_pointer(), + Rc::new(RefCell::new((pk2.as_pointer()))).as_pointer(), + |__k| cpp_move!(__k), + ) + .with_mut(|__v| *__v = *__v + 1) + }; + assert!( + ((Map::subscript_move( + ptr_counts.as_pointer(), + Rc::new(RefCell::new((pk2.as_pointer()))).as_pointer(), + |__k| cpp_move!(__k) + ) + .read()) + == 6) + ); + assert!(({ ((*pk1.borrow()) == 0) && ((*pk2.borrow()) == 0) })); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/min_max_ptr2ptr.rs b/tests/unit/out/refcount/min_max_ptr2ptr.rs index 2d1445b659..0008a79e02 100644 --- a/tests/unit/out/refcount/min_max_ptr2ptr.rs +++ b/tests/unit/out/refcount/min_max_ptr2ptr.rs @@ -30,6 +30,57 @@ fn main_0() -> i32 { } .read()); assert!(((r1 + r2) == 30)); + let values: Value> = Rc::new(RefCell::new(Box::new([2, 1]))); + let low: Value> = Rc::new(RefCell::new((values.decay().offset(0)))); + let high: Value> = Rc::new(RefCell::new((values.decay().offset(1)))); + assert!( + ({ + ({ + (if CppLess::less(&high.as_pointer(), &low.as_pointer()) { + high.as_pointer() + } else { + low.as_pointer() + } + .read()) + } == { (*low.borrow()).clone() }) + }) + ); + assert!( + ({ + ({ + (if CppLess::less(&low.as_pointer(), &high.as_pointer()) { + low.as_pointer() + } else { + high.as_pointer() + } + .read()) + } == { (*low.borrow()).clone() }) + }) + ); + assert!( + ({ + ({ + (if CppLess::less(&low.as_pointer(), &high.as_pointer()) { + high.as_pointer() + } else { + low.as_pointer() + } + .read()) + } == { (*high.borrow()).clone() }) + }) + ); + assert!( + ({ + ({ + (if CppLess::less(&high.as_pointer(), &low.as_pointer()) { + low.as_pointer() + } else { + high.as_pointer() + } + .read()) + } == { (*high.borrow()).clone() }) + }) + ); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/swap_malloc.rs b/tests/unit/out/refcount/swap_malloc.rs index 5b1fa7ac6d..41675a026d 100644 --- a/tests/unit/out/refcount/swap_malloc.rs +++ b/tests/unit/out/refcount/swap_malloc.rs @@ -48,6 +48,26 @@ fn main_0() -> i32 { && ((*words.borrow())[(1) as usize] == 50595592_u32) }) ); + let s1: Value = Rc::new(RefCell::new(1)); + let s2: Value = Rc::new(RefCell::new(2)); + let sp: Value> = Rc::new(RefCell::new((s1.as_pointer()))); + let sq: Value> = Rc::new(RefCell::new((s2.as_pointer()))); + cpp_swap!(sp.as_pointer(), sq.as_pointer()); + assert!( + ({ + ({ (*sp.borrow()).clone() } == { (s2.as_pointer()) }) + && ({ (*sq.borrow()).clone() } == { (s1.as_pointer()) }) + }) + ); + assert!(({ ((*s1.borrow()) == 1) && ((*s2.borrow()) == 2) })); + let null_ptr: Value> = Rc::new(RefCell::new(Ptr::::null())); + cpp_swap!(null_ptr.as_pointer(), sp.as_pointer()); + assert!( + ({ + ({ (*null_ptr.borrow()).clone() } == { (s2.as_pointer()) }) + && ((*sp.borrow()).is_null()) + }) + ); libcc2rs::free_refcount((other).to_any()); libcc2rs::free_refcount((buf).to_any()); return 0; diff --git a/tests/unit/out/unsafe/map.rs b/tests/unit/out/unsafe/map.rs index 1d21b475ef..ab736789ea 100644 --- a/tests/unit/out/unsafe/map.rs +++ b/tests/unit/out/unsafe/map.rs @@ -239,6 +239,24 @@ unsafe fn main_0() -> i32 { .wrapping_add(((*value_0) as usize))) == (21_usize)) ); + let mut pk1: i32 = 0; + let mut pk2: i32 = 0; + let mut ptr_counts: UnsafeMap<*mut i32, (*mut i32, i32)> = UnsafeMap::new(); + let mut ptr_key: *mut i32 = (&mut pk1 as *mut i32); + (*ptr_counts.subscript(&ptr_key)) = 1; + ptr_key = (&mut pk2 as *mut i32); + (*ptr_counts.subscript(&ptr_key)) = 2; + (*ptr_counts.subscript(&ptr_key)) += 3; + assert!(((ptr_counts.len()) == (2_usize))); + assert!(((*ptr_counts.subscript(&ptr_key)) == (5))); + ptr_key = (&mut pk1 as *mut i32); + assert!(((*ptr_counts.subscript(&ptr_key)) == (1))); + (*ptr_counts.subscript_move(&mut (&mut pk2 as *mut i32), |__k| cpp_move_unsafe!(__k))) += 1; + assert!( + ((*ptr_counts.subscript_move(&mut (&mut pk2 as *mut i32), |__k| cpp_move_unsafe!(__k))) + == (6)) + ); + assert!(((pk1) == (0)) && ((pk2) == (0))); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/min_max_ptr2ptr.rs b/tests/unit/out/unsafe/min_max_ptr2ptr.rs index cb32ecdc9a..ee7b858bcd 100644 --- a/tests/unit/out/unsafe/min_max_ptr2ptr.rs +++ b/tests/unit/out/unsafe/min_max_ptr2ptr.rs @@ -30,6 +30,37 @@ unsafe fn main_0() -> i32 { if *__b < *__a { __b } else { __a } }); assert!((((r1) + (r2)) == (30))); + let mut values: [i32; 2] = [2, 1]; + let mut low: *mut i32 = (&mut values[(0) as usize] as *mut i32); + let mut high: *mut i32 = (&mut values[(1) as usize] as *mut i32); + assert!( + ((*{ + let __a: *const _ = &mut low; + let __b: *const _ = &mut high; + if *__b < *__a { __b } else { __a } + }) == (low)) + ); + assert!( + ((*{ + let __a: *const _ = &mut high; + let __b: *const _ = &mut low; + if *__b < *__a { __b } else { __a } + }) == (low)) + ); + assert!( + ((*{ + let __a: *const _ = &mut low; + let __b: *const _ = &mut high; + if *__a < *__b { __b } else { __a } + }) == (high)) + ); + assert!( + ((*{ + let __a: *const _ = &mut high; + let __b: *const _ = &mut low; + if *__a < *__b { __b } else { __a } + }) == (high)) + ); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/swap_malloc.rs b/tests/unit/out/unsafe/swap_malloc.rs index 57de449334..4a19284675 100644 --- a/tests/unit/out/unsafe/swap_malloc.rs +++ b/tests/unit/out/unsafe/swap_malloc.rs @@ -45,6 +45,16 @@ unsafe fn main_0() -> i32 { let mut halves: *mut u16 = (words.as_mut_ptr() as *mut u16); cpp_swap_unsafe!(halves.offset((0) as isize), halves.offset((3) as isize)); assert!(((words[(0) as usize]) == (16909574_u32)) && ((words[(1) as usize]) == (50595592_u32))); + let mut s1: i32 = 1; + let mut s2: i32 = 2; + let mut sp: *mut i32 = (&mut s1 as *mut i32); + let mut sq: *mut i32 = (&mut s2 as *mut i32); + cpp_swap_unsafe!(&mut sp, &mut sq); + assert!(((sp) == (&mut s2 as *mut i32)) && ((sq) == (&mut s1 as *mut i32))); + assert!(((s1) == (1)) && ((s2) == (2))); + let mut null_ptr: *mut i32 = std::ptr::null_mut(); + cpp_swap_unsafe!(&mut null_ptr, &mut sp); + assert!(((null_ptr) == (&mut s2 as *mut i32)) && ((sp).is_null())); libcc2rs::free_unsafe((other as *mut ::libc::c_void)); libcc2rs::free_unsafe((buf as *mut ::libc::c_void)); return 0; diff --git a/tests/unit/swap_malloc.cpp b/tests/unit/swap_malloc.cpp index eb6515e014..807fee5694 100644 --- a/tests/unit/swap_malloc.cpp +++ b/tests/unit/swap_malloc.cpp @@ -32,6 +32,15 @@ int main() { std::swap(halves[0], halves[3]); assert(words[0] == 0x01020506 && words[1] == 0x03040708); + int s1 = 1, s2 = 2; + int *sp = &s1, *sq = &s2; + std::swap(sp, sq); + assert(sp == &s2 && sq == &s1); + assert(s1 == 1 && s2 == 2); + int *null_ptr = nullptr; + std::swap(null_ptr, sp); + assert(null_ptr == &s2 && sp == nullptr); + free(other); free(buf); return 0;