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
7 changes: 6 additions & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,12 @@ std::string Converter::ConvertFreshObject(clang::Expr *expr) {

std::string Converter::ConvertPointerReceiver(clang::Expr *arg,
std::string_view param_type) {
return std::format("({} as {})", ConvertFreshObject(arg), param_type);
auto *unwrapped = clang::IgnoreExprNodes(
arg, clang::IgnoreParensSingleStep, clang::IgnoreImplicitCastsSingleStep);
auto receiver = clang::isa<clang::MaterializeTemporaryExpr>(unwrapped)
? ConvertTemporaryPointer(arg, std::nullopt)
: ConvertFreshObject(arg);
return std::format("({} as {})", receiver, param_type);
}

std::string Converter::ConvertTemporaryPointer(
Expand Down
15 changes: 10 additions & 5 deletions rules/string/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ fn t7() -> i8 {
0_i8
}

fn f1(a0: Vec<i8>, a1: usize, a2: usize) -> Vec<i8> {
let mut __tmp1 =
a0[(a1) as usize..::std::cmp::min((a1 + a2) as usize, a0.len().saturating_sub(1))].to_vec();
__tmp1.push(0);
__tmp1
// Keep temporary receivers alive, but borrow only after evaluating the arguments.
fn f1(a0: Ptr<Vec<i8>>, a1: usize, a2: usize) -> Vec<i8> {
match (a0, a1, a2) {
(__source, __pos, __count) => __source.with(|__s: &Vec<i8>| {
let mut __tmp1 =
__s[__pos..::std::cmp::min(__pos + __count, __s.len().saturating_sub(1))].to_vec();
__tmp1.push(0);
__tmp1
}),
}
}

fn f3(a0: Vec<i8>, a1: Ptr<i8>) -> Vec<i8> {
Expand Down
14 changes: 10 additions & 4 deletions rules/string/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ fn t7() -> libc::c_char {
0 as libc::c_char
}

unsafe fn f1(a0: Vec<libc::c_char>, a1: usize, a2: usize) -> Vec<libc::c_char> {
let mut __tmp1 = a0[(a1) as usize..::std::cmp::min((a1 + a2) as usize, a0.len() - 1)].to_vec();
__tmp1.push(0);
__tmp1
// Keep temporary receivers alive, but borrow only after evaluating the arguments.
unsafe fn f1(a0: *const Vec<libc::c_char>, a1: usize, a2: usize) -> Vec<libc::c_char> {
match (a0, a1, a2) {
(__source, __pos, __count) => {
let __s = &*__source;
let mut __tmp1 = __s[__pos..::std::cmp::min(__pos + __count, __s.len() - 1)].to_vec();
__tmp1.push(0);
__tmp1
}
}
}
unsafe fn f2(a0: Vec<libc::c_char>) -> usize {
(a0.len() - 1)
Expand Down
194 changes: 125 additions & 69 deletions tests/unit/out/refcount/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn counted_string_0(calls: Ptr<i32>) -> Vec<i8> {
calls.with_mut(|__v| __v.prefix_inc());
return {
let mut __bytes = Ptr::<i8>::from_string_literal(b"hello").to_c_bytes();
__bytes.push(0);
__bytes
};
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand Down Expand Up @@ -73,15 +81,13 @@ fn main_0() -> i32 {
);
i.prefix_inc();
}
let s3: Value<Vec<i8>> = Rc::new(RefCell::new({
let mut __tmp1 = (*s2.borrow())[(2_usize) as usize
..::std::cmp::min(
(2_usize + 5_usize) as usize,
(*s2.borrow()).len().saturating_sub(1),
)]
.to_vec();
__tmp1.push(0);
__tmp1
let s3: Value<Vec<i8>> = Rc::new(RefCell::new(match (s2.as_pointer(), 2_usize, 5_usize) {
(__source, __pos, __count) => __source.with(|__s: &Vec<i8>| {
let mut __tmp1 =
__s[__pos..::std::cmp::min(__pos + __count, __s.len().saturating_sub(1))].to_vec();
__tmp1.push(0);
__tmp1
}),
}));
assert!((((*s3.borrow()).len() - 1) == 5_usize));
assert!((((*s3.borrow()).len() - 1) == ((*s3.borrow()).len() - 1)));
Expand All @@ -93,23 +99,27 @@ fn main_0() -> i32 {
);
i.prefix_inc();
}
let s4: Value<Vec<i8>> = Rc::new(RefCell::new({
let mut __tmp1 = (*s1.borrow())[(1_usize) as usize
..::std::cmp::min(
(1_usize
+ Ptr::<i8>::from_string_literal(b"l").with_c_str(|__lookup| {
(*s1.borrow())
.iter()
.take((*s1.borrow()).len().saturating_sub(1))
.rposition(|&x| __lookup.contains(&x))
.unwrap_or(usize::MAX)
})) as usize,
(*s1.borrow()).len().saturating_sub(1),
)]
.to_vec();
__tmp1.push(0);
__tmp1
}));
let s4: Value<Vec<i8>> = Rc::new(RefCell::new(
match (
s1.as_pointer(),
1_usize,
Ptr::<i8>::from_string_literal(b"l").with_c_str(|__lookup| {
(*s1.borrow())
.iter()
.take((*s1.borrow()).len().saturating_sub(1))
.rposition(|&x| __lookup.contains(&x))
.unwrap_or(usize::MAX)
}),
) {
(__source, __pos, __count) => __source.with(|__s: &Vec<i8>| {
let mut __tmp1 = __s
[__pos..::std::cmp::min(__pos + __count, __s.len().saturating_sub(1))]
.to_vec();
__tmp1.push(0);
__tmp1
}),
},
));
assert!((((*s4.borrow()).len() - 1) == 3_usize));
assert!((((*s4.borrow()).len() - 1) == ((*s4.borrow()).len() - 1)));
let mut p4: Ptr<i8> = s4.decay();
Expand Down Expand Up @@ -201,55 +211,55 @@ fn main_0() -> i32 {
assert!(((string[1_usize] as i32) == (('a' as i8) as i32)));
assert!(((string[2_usize] as i32) == (('r' as i8) as i32)));
assert!(((string[3_usize] as i32) == 0));
let mut result: Vec<i8> = {
let result: Value<Vec<i8>> = Rc::new(RefCell::new({
let mut r = (string).clone();
r.pop();
Ptr::<i8>::from_string_literal(b" foo").with_c_str(|__s| r.extend_from_slice(__s));
r.push(0);
r
};
assert!(((result.len() - 1) == 8_usize));
assert!(((result[0_usize] as i32) == (('b' as i8) as i32)));
assert!(((result[1_usize] as i32) == (('a' as i8) as i32)));
assert!(((result[2_usize] as i32) == (('r' as i8) as i32)));
assert!(((result[3_usize] as i32) == 0));
assert!(((result[4_usize] as i32) == ((' ' as i8) as i32)));
assert!(((result[5_usize] as i32) == (('f' as i8) as i32)));
assert!(((result[6_usize] as i32) == (('o' as i8) as i32)));
assert!(((result[7_usize] as i32) == (('o' as i8) as i32)));
let mut substr_0: Vec<i8> = {
let mut __tmp1 = result[(5_usize) as usize
..::std::cmp::min((5_usize + 3_usize) as usize, result.len().saturating_sub(1))]
.to_vec();
__tmp1.push(0);
__tmp1
}));
assert!((((*result.borrow()).len() - 1) == 8_usize));
assert!((({ (*result.borrow())[0_usize] } as i32) == (('b' as i8) as i32)));
assert!((({ (*result.borrow())[1_usize] } as i32) == (('a' as i8) as i32)));
assert!((({ (*result.borrow())[2_usize] } as i32) == (('r' as i8) as i32)));
assert!((({ (*result.borrow())[3_usize] } as i32) == 0));
assert!((({ (*result.borrow())[4_usize] } as i32) == ((' ' as i8) as i32)));
assert!((({ (*result.borrow())[5_usize] } as i32) == (('f' as i8) as i32)));
assert!((({ (*result.borrow())[6_usize] } as i32) == (('o' as i8) as i32)));
assert!((({ (*result.borrow())[7_usize] } as i32) == (('o' as i8) as i32)));
let mut substr_0: Vec<i8> = match (result.as_pointer(), 5_usize, 3_usize) {
(__source, __pos, __count) => __source.with(|__s: &Vec<i8>| {
let mut __tmp1 =
__s[__pos..::std::cmp::min(__pos + __count, __s.len().saturating_sub(1))].to_vec();
__tmp1.push(0);
__tmp1
}),
};
assert!(((substr_0.len() - 1) == 3_usize));
assert!(((substr_0[0_usize] as i32) == (('f' as i8) as i32)));
assert!(((substr_0[1_usize] as i32) == (('o' as i8) as i32)));
assert!(((substr_0[2_usize] as i32) == (('o' as i8) as i32)));
let mut substr_1: Vec<i8> = {
let mut __tmp1 = result[(0_usize) as usize
..::std::cmp::min((0_usize + 5_usize) as usize, result.len().saturating_sub(1))]
.to_vec();
__tmp1.push(0);
__tmp1
let mut substr_1: Vec<i8> = match (result.as_pointer(), 0_usize, 5_usize) {
(__source, __pos, __count) => __source.with(|__s: &Vec<i8>| {
let mut __tmp1 =
__s[__pos..::std::cmp::min(__pos + __count, __s.len().saturating_sub(1))].to_vec();
__tmp1.push(0);
__tmp1
}),
};
assert!(((substr_1.len() - 1) == 5_usize));
assert!(((substr_1[0_usize] as i32) == (('b' as i8) as i32)));
assert!(((substr_1[1_usize] as i32) == (('a' as i8) as i32)));
assert!(((substr_1[2_usize] as i32) == (('r' as i8) as i32)));
assert!(((substr_1[3_usize] as i32) == 0));
assert!(((substr_1[4_usize] as i32) == ((' ' as i8) as i32)));
let mut substr_2: Vec<i8> = {
let mut __tmp1 = result[(0_usize) as usize
..::std::cmp::min(
(0_usize + 15_usize) as usize,
result.len().saturating_sub(1),
)]
.to_vec();
__tmp1.push(0);
__tmp1
let mut substr_2: Vec<i8> = match (result.as_pointer(), 0_usize, 15_usize) {
(__source, __pos, __count) => __source.with(|__s: &Vec<i8>| {
let mut __tmp1 =
__s[__pos..::std::cmp::min(__pos + __count, __s.len().saturating_sub(1))].to_vec();
__tmp1.push(0);
__tmp1
}),
};
assert!(((substr_2.len() - 1) == 8_usize));
assert!(((substr_2[0_usize] as i32) == (('b' as i8) as i32)));
Expand All @@ -261,39 +271,39 @@ fn main_0() -> i32 {
assert!(((substr_2[6_usize] as i32) == (('o' as i8) as i32)));
assert!(((substr_2[7_usize] as i32) == (('o' as i8) as i32)));
let mut pos: usize = Ptr::<i8>::from_string_literal(b"b").with_c_str(|__lookup| {
result
(*result.borrow())
.iter()
.take(result.len().saturating_sub(1))
.take((*result.borrow()).len().saturating_sub(1))
.rposition(|&x| __lookup.contains(&x))
.unwrap_or(usize::MAX)
});
assert!((pos == 0_usize));
pos = Ptr::<i8>::from_string_literal(b"f").with_c_str(|__lookup| {
result
(*result.borrow())
.iter()
.take(result.len().saturating_sub(1))
.take((*result.borrow()).len().saturating_sub(1))
.rposition(|&x| __lookup.contains(&x))
.unwrap_or(usize::MAX)
});
assert!((pos == 5_usize));
pos = Ptr::<i8>::from_string_literal(b"o").with_c_str(|__lookup| {
result
(*result.borrow())
.iter()
.take(result.len().saturating_sub(1))
.take((*result.borrow()).len().saturating_sub(1))
.rposition(|&x| __lookup.contains(&x))
.unwrap_or(usize::MAX)
});
assert!((pos == 7_usize));
pos = Ptr::<i8>::from_string_literal(b"x").with_c_str(|__lookup| {
result
(*result.borrow())
.iter()
.take(result.len().saturating_sub(1))
.take((*result.borrow()).len().saturating_sub(1))
.rposition(|&x| __lookup.contains(&x))
.unwrap_or(usize::MAX)
});
assert!((pos == ((-1_i64 as u64) as usize)));
pos = {
let __hay = &result[..result.len().saturating_sub(1)];
let __hay = &(*result.borrow())[..(*result.borrow()).len().saturating_sub(1)];
Ptr::<i8>::from_string_literal(b"f").with_c_str(|__needle| {
(0_usize..=__hay.len().saturating_sub(__needle.len()))
.find(|&__i| __hay[__i..].starts_with(__needle))
Expand All @@ -302,7 +312,7 @@ fn main_0() -> i32 {
};
assert!((pos == 5_usize));
pos = {
let __hay = &result[..result.len().saturating_sub(1)];
let __hay = &(*result.borrow())[..(*result.borrow()).len().saturating_sub(1)];
Ptr::<i8>::from_string_literal(b"oo").with_c_str(|__needle| {
(0_usize..=__hay.len().saturating_sub(__needle.len()))
.find(|&__i| __hay[__i..].starts_with(__needle))
Expand All @@ -311,7 +321,7 @@ fn main_0() -> i32 {
};
assert!((pos == 6_usize));
pos = {
let __hay = &result[..result.len().saturating_sub(1)];
let __hay = &(*result.borrow())[..(*result.borrow()).len().saturating_sub(1)];
Ptr::<i8>::from_string_literal(b"x").with_c_str(|__needle| {
(0_usize..=__hay.len().saturating_sub(__needle.len()))
.find(|&__i| __hay[__i..].starts_with(__needle))
Expand All @@ -334,6 +344,52 @@ fn main_0() -> i32 {
assert!((diff < 0_i64));
diff += 6_i64;
assert!((diff == 3_i64));
let mut substr_pos: usize = 1_usize;
let once: Value<Vec<i8>> = Rc::new(RefCell::new(
match (s1.as_pointer(), substr_pos.postfix_inc(), 2_usize) {
(__source, __pos, __count) => __source.with(|__s: &Vec<i8>| {
let mut __tmp1 = __s
[__pos..::std::cmp::min(__pos + __count, __s.len().saturating_sub(1))]
.to_vec();
__tmp1.push(0);
__tmp1
}),
},
));
println!("position: {} {}", once.decay(), substr_pos);
let mut substr_count: usize = 2_usize;
once.as_pointer().write(
match (s1.as_pointer(), 1_usize, substr_count.postfix_inc()) {
(__source, __pos, __count) => __source.with(|__s: &Vec<i8>| {
let mut __tmp1 = __s
[__pos..::std::cmp::min(__pos + __count, __s.len().saturating_sub(1))]
.to_vec();
__tmp1.push(0);
__tmp1
}),
},
);
println!("count: {} {}", once.decay(), substr_count);
let receiver_calls: Value<i32> = Rc::new(RefCell::new(0));
once.as_pointer().write(
match (
Rc::new(RefCell::new(
({ counted_string_0(receiver_calls.as_pointer()) }),
))
.as_pointer(),
1_usize,
2_usize,
) {
(__source, __pos, __count) => __source.with(|__s: &Vec<i8>| {
let mut __tmp1 = __s
[__pos..::std::cmp::min(__pos + __count, __s.len().saturating_sub(1))]
.to_vec();
__tmp1.push(0);
__tmp1
}),
},
);
println!("receiver: {} {}", once.decay(), (*receiver_calls.borrow()));
let mut t0: usize = ((*s1.borrow()).len() - 1);
let mut t1: usize = (t0).wrapping_add(((p1.read()) as usize));
let mut t2: usize = (t1).wrapping_add(((*s2.borrow()).len() - 1));
Expand Down
Loading
Loading