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
12 changes: 7 additions & 5 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions cpp2rust/converter/models/boxed_vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
18 changes: 18 additions & 0 deletions cpp2rust/converter/rules/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<clang::Expr *>(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<clang::VarDecl>(decl)->getType()->getPointeeType();
auto proto = type->getAs<clang::FunctionProtoType>();
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) {
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/rules/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 10 additions & 5 deletions cpp2rust/converter/translation_rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,22 +332,27 @@ 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,
const clang::Expr *expr, unsigned i,
const clang::Expr *arg) const {
auto *call = clang::dyn_cast<clang::CallExpr>(expr);
unsigned first_arg = clang::isa<clang::CXXMemberCallExpr>(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();
}

Expand Down
6 changes: 3 additions & 3 deletions cpp2rust/converter/translation_rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ struct ExprRule {
std::vector<bool> argsToBind(clang::ASTContext &ctx, const clang::Expr *expr,
llvm::ArrayRef<clang::Expr *> 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
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int *, int> 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;
}
8 changes: 8 additions & 0 deletions tests/unit/min_max_ptr2ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
34 changes: 34 additions & 0 deletions tests/unit/out/refcount/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,40 @@ fn main_0() -> i32 {
== 21_usize)
})
);
let pk1: Value<i32> = Rc::new(RefCell::new(0));
let pk2: Value<i32> = Rc::new(RefCell::new(0));
let ptr_counts: Value<Map<Ptr<i32>, (Value<Ptr<i32>>, Value<i32>)>> =
Rc::new(RefCell::new(Map::new()));
let ptr_key: Value<Ptr<i32>> = 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() {}
51 changes: 51 additions & 0 deletions tests/unit/out/refcount/min_max_ptr2ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,57 @@ fn main_0() -> i32 {
}
.read());
assert!(((r1 + r2) == 30));
let values: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([2, 1])));
let low: Value<Ptr<i32>> = Rc::new(RefCell::new((values.decay().offset(0))));
let high: Value<Ptr<i32>> = Rc::new(RefCell::new((values.decay().offset(1))));
assert!(
({
({
(if CppLess::less(&high.as_pointer(), &low.as_pointer()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not correct. It should be comparing the pointers themselves, not the pointer to the pointer.
should be less(*high.borrow(), *low.borrow())

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is exactly what the PR fixes. CppLess receives reference (Ptr), low and high are themselves Ptr, so the input is Ptr<Ptr<T>>. Inside CppLess the pointer is dereferenced: lhs.with(|a| rhs.with(|b| a < b))

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() {}
20 changes: 20 additions & 0 deletions tests/unit/out/refcount/swap_malloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ fn main_0() -> i32 {
&& ((*words.borrow())[(1) as usize] == 50595592_u32)
})
);
let s1: Value<i32> = Rc::new(RefCell::new(1));
let s2: Value<i32> = Rc::new(RefCell::new(2));
let sp: Value<Ptr<i32>> = Rc::new(RefCell::new((s1.as_pointer())));
let sq: Value<Ptr<i32>> = 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<Ptr<i32>> = Rc::new(RefCell::new(Ptr::<i32>::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;
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/out/unsafe/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
31 changes: 31 additions & 0 deletions tests/unit/out/unsafe/min_max_ptr2ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
10 changes: 10 additions & 0 deletions tests/unit/out/unsafe/swap_malloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/swap_malloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading