From 006d714af9fbef02ecbfd432e409dd5b2521bcbf Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 18 May 2026 13:42:43 +0100 Subject: [PATCH 1/2] Receive &Ptr instead of Ptr in PtrValueIter::new --- libcc2rs/src/rc.rs | 4 +- rules/algorithm/ir_refcount.json | 23 ++----- rules/algorithm/tgt_refcount.rs | 6 +- rules/vector/ir_refcount.json | 61 +++++++------------ rules/vector/tgt_refcount.rs | 11 ++-- tests/unit/out/refcount/push_emplace_back.rs | 4 +- tests/unit/out/refcount/stdcopy.rs | 2 +- .../out/refcount/vector_iter_range_ctor.rs | 24 ++++++-- .../unit/out/unsafe/vector_iter_range_ctor.rs | 10 +++ tests/unit/vector_iter_range_ctor.cpp | 7 +++ 10 files changed, 77 insertions(+), 75 deletions(-) diff --git a/libcc2rs/src/rc.rs b/libcc2rs/src/rc.rs index f88e0e68..d980d0cd 100644 --- a/libcc2rs/src/rc.rs +++ b/libcc2rs/src/rc.rs @@ -639,8 +639,8 @@ pub struct PtrValueIter { } impl PtrValueIter { - pub fn new(ptr: Ptr, n: usize) -> Self { - Self { ptr, n } + pub fn new(ptr: &Ptr, n: usize) -> Self { + Self { ptr: ptr.clone() , n } } } diff --git a/rules/algorithm/ir_refcount.json b/rules/algorithm/ir_refcount.json index 42fbf7a8..e1c45e35 100644 --- a/rules/algorithm/ir_refcount.json +++ b/rules/algorithm/ir_refcount.json @@ -127,7 +127,7 @@ } }, { - "text": ";\n let mut iter = PtrValueIter::new(" + "text": ";\n let mut iter = PtrValueIter::new(&" }, { "placeholder": { @@ -975,7 +975,7 @@ } }, { - "text": ";\n for value in PtrValueIter::new(" + "text": ";\n for value in PtrValueIter::new(&" }, { "placeholder": { @@ -1430,23 +1430,12 @@ "method_call": { "receiver": [ { - "text": "PtrValueIter::new(" + "text": "PtrValueIter::new(&" }, { - "method_call": { - "receiver": [ - { - "placeholder": { - "arg": 0, - "access": "read" - } - } - ], - "body": [ - { - "text": ".clone()" - } - ] + "placeholder": { + "arg": 0, + "access": "read" } }, { diff --git a/rules/algorithm/tgt_refcount.rs b/rules/algorithm/tgt_refcount.rs index 7791c971..bfd796db 100644 --- a/rules/algorithm/tgt_refcount.rs +++ b/rules/algorithm/tgt_refcount.rs @@ -17,7 +17,7 @@ fn f2 + ByteRepr>( ) -> Ptr { let count = a1.get_offset() - a0.get_offset(); let mut outptr = a2.clone(); - for value in PtrValueIter::new(a0, count) { + for value in PtrValueIter::new(&a0, count) { outptr.write(value.into()); outptr += 1; } @@ -52,7 +52,7 @@ where fn f8(a0: Ptr, a1: Ptr) -> Ptr { let count = a1.get_offset() - a0.get_offset(); - let max_index = PtrValueIter::new(a0.clone(), count) + let max_index = PtrValueIter::new(&a0, count) .enumerate() .max_by(|(_, val_a), (_, val_b)| { val_a @@ -76,7 +76,7 @@ fn f10(a0: Ptr, a1: Ptr) -> Ptr { a1 } else { let mut write_ptr = a0.clone(); - let mut iter = PtrValueIter::new(a0, count); + let mut iter = PtrValueIter::new(&a0, count); let mut last_unique = iter.next().unwrap(); // the first unique value is already in place diff --git a/rules/vector/ir_refcount.json b/rules/vector/ir_refcount.json index a905e7cd..771e1e47 100644 --- a/rules/vector/ir_refcount.json +++ b/rules/vector/ir_refcount.json @@ -1071,7 +1071,7 @@ "method_call": { "receiver": [ { - "text": "PtrValueIter::new(" + "text": "PtrValueIter::new(&" }, { "placeholder": { @@ -1164,23 +1164,12 @@ "method_call": { "receiver": [ { - "text": "PtrValueIter::new(" + "text": "PtrValueIter::new(&" }, { - "method_call": { - "receiver": [ - { - "placeholder": { - "arg": 0, - "access": "read" - } - } - ], - "body": [ - { - "text": ".clone()" - } - ] + "placeholder": { + "arg": 0, + "access": "read" } }, { @@ -1318,27 +1307,7 @@ "f42": { "body": [ { - "text": "let __a0 = " - }, - { - "method_call": { - "receiver": [ - { - "placeholder": { - "arg": 0, - "access": "read" - } - } - ], - "body": [ - { - "text": ".clone()" - } - ] - } - }, - { - "text": ";\n let __count = " + "text": "let __count = " }, { "method_call": { @@ -1364,7 +1333,10 @@ "method_call": { "receiver": [ { - "text": "__a0" + "placeholder": { + "arg": 0, + "access": "read" + } } ], "body": [ @@ -1390,7 +1362,16 @@ "method_call": { "receiver": [ { - "text": "PtrValueIter::new(__a0, __count)" + "text": "PtrValueIter::new(&" + }, + { + "placeholder": { + "arg": 0, + "access": "read" + } + }, + { + "text": ", __count)" } ], "body": [ @@ -1809,7 +1790,7 @@ "method_call": { "receiver": [ { - "text": "PtrValueIter::new(" + "text": "PtrValueIter::new(&" }, { "placeholder": { diff --git a/rules/vector/tgt_refcount.rs b/rules/vector/tgt_refcount.rs index f85ab327..8563aafd 100644 --- a/rules/vector/tgt_refcount.rs +++ b/rules/vector/tgt_refcount.rs @@ -114,12 +114,12 @@ fn f34(a0: &mut Ptr) -> Ptr { fn f35(a0: Ptr, a1: Ptr) -> Vec { let __count = a1.get_offset() - a0.get_offset(); - PtrValueIter::new(a0, __count).collect::>() + PtrValueIter::new(&a0, __count).collect::>() } fn f37, T2: Clone + ByteRepr>(a0: Ptr, a1: Ptr) -> Vec { let __count = a1.get_offset() - a0.get_offset(); - PtrValueIter::new(a0.clone(), __count) + PtrValueIter::new(&a0, __count) .map(|item| T1::try_from(item).ok().unwrap()) .collect::>() } @@ -133,9 +133,8 @@ fn f41(a0: Ptr) -> Ptr { } fn f42(a0: Ptr, a1: Ptr) -> Ptr { - let __a0 = a0.clone(); - let __count = a1.get_offset() - __a0.get_offset(); - let max_index = PtrValueIter::new(__a0, __count) + let __count = a1.get_offset() - a0.get_offset(); + let max_index = PtrValueIter::new(&a0, __count) .enumerate() .max_by_key(|&(_, val)| val) .map(|(idx, _)| idx) @@ -176,7 +175,7 @@ fn f53( ) -> Ptr> { let start_idx = a1.get_offset(); let count = a3.get_offset() - a2.get_offset(); - let temp_vec: Vec = PtrValueIter::new(a2, count).collect(); + let temp_vec: Vec = PtrValueIter::new(&a2, count).collect(); a0.with_mut(|v: &mut Vec| { v.splice(start_idx..start_idx, temp_vec); }); diff --git a/tests/unit/out/refcount/push_emplace_back.rs b/tests/unit/out/refcount/push_emplace_back.rs index 55868cc3..d1af0aaf 100644 --- a/tests/unit/out/refcount/push_emplace_back.rs +++ b/tests/unit/out/refcount/push_emplace_back.rs @@ -84,7 +84,7 @@ pub fn push_local_from_field_1(jpg: Ptr, cond: bool) { .offset((3) as isize) .get_offset() - (head.as_pointer() as Ptr).get_offset(); - PtrValueIter::new((head.as_pointer() as Ptr).clone(), __count) + PtrValueIter::new(&(head.as_pointer() as Ptr), __count) .map(|item| u8::try_from(item).ok().unwrap()) .collect::>() } @@ -125,7 +125,7 @@ pub fn emplace_local_from_field_4(jpg: Ptr, cond: bool) { .offset((3) as isize) .get_offset() - (head.as_pointer() as Ptr).get_offset(); - PtrValueIter::new((head.as_pointer() as Ptr).clone(), __count) + PtrValueIter::new(&(head.as_pointer() as Ptr), __count) .map(|item| u8::try_from(item).ok().unwrap()) .collect::>() }))) diff --git a/tests/unit/out/refcount/stdcopy.rs b/tests/unit/out/refcount/stdcopy.rs index 281a9e4e..9f4fabe2 100644 --- a/tests/unit/out/refcount/stdcopy.rs +++ b/tests/unit/out/refcount/stdcopy.rs @@ -20,7 +20,7 @@ fn main_0() -> i32 { .get_offset() - (input.as_pointer() as Ptr).get_offset(); let mut outptr = (output.as_pointer() as Ptr).clone(); - for value in PtrValueIter::new((input.as_pointer() as Ptr), count) { + for value in PtrValueIter::new(&(input.as_pointer() as Ptr), count) { outptr.write(value.into()); outptr += 1; } diff --git a/tests/unit/out/refcount/vector_iter_range_ctor.rs b/tests/unit/out/refcount/vector_iter_range_ctor.rs index 3121db8b..6debd56c 100644 --- a/tests/unit/out/refcount/vector_iter_range_ctor.rs +++ b/tests/unit/out/refcount/vector_iter_range_ctor.rs @@ -16,7 +16,7 @@ fn main_0() -> i32 { .offset((3) as isize) .get_offset() - (src.as_pointer() as Ptr).get_offset(); - PtrValueIter::new((src.as_pointer() as Ptr).clone(), __count) + PtrValueIter::new(&(src.as_pointer() as Ptr), __count) .map(|item| u32::try_from(item).ok().unwrap()) .collect::>() })); @@ -31,7 +31,7 @@ fn main_0() -> i32 { .offset((3) as isize) .get_offset() - (src.as_pointer() as Ptr).get_offset(); - PtrValueIter::new((src.as_pointer() as Ptr).clone(), __count) + PtrValueIter::new(&(src.as_pointer() as Ptr), __count) .map(|item| u64::try_from(item).ok().unwrap()) .collect::>() })); @@ -46,7 +46,7 @@ fn main_0() -> i32 { .offset((3) as isize) .get_offset() - (src.as_pointer() as Ptr).get_offset(); - PtrValueIter::new((src.as_pointer() as Ptr).clone(), __count) + PtrValueIter::new(&(src.as_pointer() as Ptr), __count) .map(|item| i32::try_from(item).ok().unwrap()) .collect::>() })); @@ -60,7 +60,7 @@ fn main_0() -> i32 { let v4: Value> = Rc::new(RefCell::new({ let __count = (src1.as_pointer() as Ptr).to_end().get_offset() - (src1.as_pointer() as Ptr).get_offset(); - PtrValueIter::new((src1.as_pointer() as Ptr), __count).collect::>() + PtrValueIter::new(&(src1.as_pointer() as Ptr), __count).collect::>() })); assert!(((*v4.borrow()).len() as u64 == 3_u64)); assert!( @@ -68,5 +68,21 @@ fn main_0() -> i32 { && (((v4.as_pointer() as Ptr).offset(1_u64 as isize).read()) == 2_u32)) && (((v4.as_pointer() as Ptr).offset(2_u64 as isize).read()) == 3_u32) ); + let buf: Value> = + Rc::new(RefCell::new(Box::new([10_u8, 20_u8, 30_u8, 40_u8, 50_u8]))); + let start: Value> = Rc::new(RefCell::new((buf.as_pointer() as Ptr))); + let len: Value = Rc::new(RefCell::new(5_u64)); + let v5: Value> = Rc::new(RefCell::new({ + let __count = (*start.borrow()) + .offset((*len.borrow()) as isize) + .get_offset() + - (*start.borrow()).get_offset(); + PtrValueIter::new(&(*start.borrow()), __count).collect::>() + })); + assert!(((*v5.borrow()).len() as u64 == 5_u64)); + assert!( + ((((v5.as_pointer() as Ptr).offset(0_u64 as isize).read()) as i32) == 10) + && ((((v5.as_pointer() as Ptr).offset(4_u64 as isize).read()) as i32) == 50) + ); return 0; } diff --git a/tests/unit/out/unsafe/vector_iter_range_ctor.rs b/tests/unit/out/unsafe/vector_iter_range_ctor.rs index b38238fb..d45854e8 100644 --- a/tests/unit/out/unsafe/vector_iter_range_ctor.rs +++ b/tests/unit/out/unsafe/vector_iter_range_ctor.rs @@ -60,5 +60,15 @@ unsafe fn main_0() -> i32 { (((v4[(0_u64) as usize]) == (1_u32)) && ((v4[(1_u64) as usize]) == (2_u32))) && ((v4[(2_u64) as usize]) == (3_u32)) ); + let mut buf: [u8; 5] = [10_u8, 20_u8, 30_u8, 40_u8, 50_u8]; + let mut start: *const u8 = buf.as_mut_ptr().cast_const(); + let mut len: u64 = 5_u64; + let mut v5: Vec = core::slice::from_raw_parts( + start, + (start.offset((len) as isize)).offset_from(start) as usize, + ) + .to_vec(); + assert!(((v5.len() as u64) == (5_u64))); + assert!(((v5[(0_u64) as usize] as i32) == (10)) && ((v5[(4_u64) as usize] as i32) == (50))); return 0; } diff --git a/tests/unit/vector_iter_range_ctor.cpp b/tests/unit/vector_iter_range_ctor.cpp index da6f8818..84d113c1 100644 --- a/tests/unit/vector_iter_range_ctor.cpp +++ b/tests/unit/vector_iter_range_ctor.cpp @@ -26,5 +26,12 @@ int main() { assert(v4.size() == 3); assert(v4[0] == 1 && v4[1] == 2 && v4[2] == 3); + uint8_t buf[5] = {10, 20, 30, 40, 50}; + const uint8_t *start = buf; + size_t len = 5; + std::vector v5(start, start + len); + assert(v5.size() == 5); + assert(v5[0] == 10 && v5[4] == 50); + return 0; } From aafbbcaa9c73277032dbd7cd0190420af8c2cf42 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 18 May 2026 13:52:31 +0100 Subject: [PATCH 2/2] format --- libcc2rs/src/rc.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libcc2rs/src/rc.rs b/libcc2rs/src/rc.rs index d980d0cd..631e49c5 100644 --- a/libcc2rs/src/rc.rs +++ b/libcc2rs/src/rc.rs @@ -640,7 +640,10 @@ pub struct PtrValueIter { impl PtrValueIter { pub fn new(ptr: &Ptr, n: usize) -> Self { - Self { ptr: ptr.clone() , n } + Self { + ptr: ptr.clone(), + n, + } } }