Skip to content

Commit 31f110a

Browse files
Copilotnunoplopes
andauthored
Convert standalone integer i++/++i to i += 1 in converter and golden files
Agent-Logs-Url: https://github.com/Cpp2Rust/cpp2rust/sessions/a93c5839-6535-4cd5-9e98-6988635319df Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
1 parent 701899a commit 31f110a

121 files changed

Lines changed: 373 additions & 351 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cpp2rust/converter/converter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,8 +2045,16 @@ bool Converter::IsReferenceType(const clang::Expr *expr) const {
20452045
bool Converter::ConvertIncAndDec(clang::UnaryOperator *expr) {
20462046
auto opcode = expr->getOpcode();
20472047
auto *sub_expr = expr->getSubExpr();
2048+
bool is_int = sub_expr->getType().getNonReferenceType()->isIntegerType();
20482049
switch (opcode) {
20492050
case clang::UO_PostInc: {
2051+
if (isVoid() && is_int) {
2052+
PushExprKind push(*this, ExprKind::LValue);
2053+
Convert(sub_expr);
2054+
StrCat(" += 1");
2055+
SetFresh();
2056+
return true;
2057+
}
20502058
PushExprKind push(*this, ExprKind::RValue);
20512059
Convert(sub_expr);
20522060
StrCat(".postfix_inc()");
@@ -2061,6 +2069,13 @@ bool Converter::ConvertIncAndDec(clang::UnaryOperator *expr) {
20612069
return true;
20622070
}
20632071
case clang::UO_PreInc: {
2072+
if (isVoid() && is_int) {
2073+
PushExprKind push(*this, ExprKind::LValue);
2074+
Convert(sub_expr);
2075+
StrCat(" += 1");
2076+
SetFresh();
2077+
return true;
2078+
}
20642079
PushExprKind push(*this, ExprKind::RValue);
20652080
Convert(sub_expr);
20662081
StrCat(".prefix_inc()");

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,10 @@ bool ConverterRefCount::ConvertIncAndDec(clang::UnaryOperator *expr) {
586586
auto opcode = expr->getOpcode();
587587
auto *sub_expr = expr->getSubExpr();
588588

589+
bool is_inc = (opcode == clang::UO_PostInc || opcode == clang::UO_PreInc);
590+
bool use_addassign = isVoid() && is_inc &&
591+
sub_expr->getType().getNonReferenceType()->isIntegerType();
592+
589593
const char *method = nullptr;
590594
switch (opcode) {
591595
case clang::UO_PostInc:
@@ -606,9 +610,17 @@ bool ConverterRefCount::ConvertIncAndDec(clang::UnaryOperator *expr) {
606610

607611
auto str = ConvertLValue(sub_expr);
608612
if (!pending_deref_.empty()) {
609-
StrCat(pending_deref_.take(), ".with_mut(|__v| __v.", method, "())");
613+
if (use_addassign) {
614+
StrCat(pending_deref_.take(), ".with_mut(|__v| *__v += 1)");
615+
} else {
616+
StrCat(pending_deref_.take(), ".with_mut(|__v| __v.", method, "())");
617+
}
610618
} else {
611-
StrCat(str, ".", method, "()");
619+
if (use_addassign) {
620+
StrCat(str, " += 1");
621+
} else {
622+
StrCat(str, ".", method, "()");
623+
}
612624
}
613625
return true;
614626
}

tests/benchmarks/out/refcount/array_sum.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ fn main_0() -> i32 {
2525
let __rhs = (*i.borrow());
2626
(*array.borrow()).as_ref().unwrap().borrow_mut()[((*i.borrow()) as u64) as usize] =
2727
__rhs;
28-
(*i.borrow_mut()).prefix_inc();
28+
*i.borrow_mut() += 1;
2929
}
3030
let i: Value<i32> = Rc::new(RefCell::new(0));
3131
'loop_: while ((*i.borrow()) < (*N.borrow())) {
3232
(*sum.borrow_mut()) += ((*array.borrow()).as_ref().unwrap().borrow()
3333
[((*i.borrow()) as u64) as usize] as i64);
34-
(*i.borrow_mut()).prefix_inc();
34+
*i.borrow_mut() += 1;
3535
}
36-
(*k.borrow_mut()).prefix_inc();
36+
*k.borrow_mut() += 1;
3737
}
3838
return ((*sum.borrow()) as i32);
3939
}

tests/benchmarks/out/refcount/bfs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub fn BFS_0(graph: Ptr<Graph>, start_vertex: u32) -> Ptr<u32> {
143143
.write(false);
144144
let __rhs = (*i.borrow());
145145
(*pred.borrow()).offset((*i.borrow()) as isize).write(__rhs);
146-
(*i.borrow_mut()).prefix_inc();
146+
*i.borrow_mut() += 1;
147147
}
148148
(*visited.borrow())
149149
.offset((*start_vertex.borrow()) as isize)
@@ -207,7 +207,7 @@ fn main_0() -> i32 {
207207
(*(*graph.borrow()).adj.borrow())
208208
.offset((*i.borrow()) as isize)
209209
.write(Default::default());
210-
(*i.borrow_mut()).prefix_inc();
210+
*i.borrow_mut() += 1;
211211
}
212212
let i: Value<u32> = Rc::new(RefCell::new(0_u32));
213213
'loop_: while (((*i.borrow()) as u64) < (*V.borrow())) {
@@ -218,9 +218,9 @@ fn main_0() -> i32 {
218218
let _dst: u32 = (*j.borrow());
219219
(*graph.borrow()).push(_src, _dst)
220220
});
221-
(*j.borrow_mut()).prefix_inc();
221+
*j.borrow_mut() += 1;
222222
}
223-
(*i.borrow_mut()).prefix_inc();
223+
*i.borrow_mut() += 1;
224224
}
225225
let pred: Value<Ptr<u32>> = Rc::new(RefCell::new(
226226
({
@@ -244,7 +244,7 @@ fn main_0() -> i32 {
244244
(*head.borrow()).delete();
245245
(*head.borrow_mut()) = (*next.borrow()).clone();
246246
}
247-
(*i.borrow_mut()).prefix_inc();
247+
*i.borrow_mut() += 1;
248248
}
249249
(*(*graph.borrow()).adj.borrow()).delete_array();
250250
(*pred.borrow()).delete_array();

tests/benchmarks/out/refcount/bst.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn main_0() -> i32 {
110110
});
111111
insert_1(_node, _new_node)
112112
});
113-
(*i.borrow_mut()).prefix_inc();
113+
*i.borrow_mut() += 1;
114114
}
115115
let i: Value<i32> = Rc::new(RefCell::new(0));
116116
'loop_: while ((*i.borrow()) < (*N.borrow())) {
@@ -119,7 +119,7 @@ fn main_0() -> i32 {
119119
let _value: i32 = (*i.borrow());
120120
find_0(_node, _value)
121121
});
122-
(*i.borrow_mut()).prefix_inc();
122+
*i.borrow_mut() += 1;
123123
}
124124
return 0;
125125
}

tests/benchmarks/out/refcount/prime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn is_prime_0(x: i32) -> bool {
1313
if (((*x.borrow()) % (*i.borrow())) == 0) {
1414
return false;
1515
}
16-
(*i.borrow_mut()).prefix_inc();
16+
*i.borrow_mut() += 1;
1717
}
1818
return true;
1919
}
@@ -28,7 +28,7 @@ pub fn largest_prime_1(n: i32) -> i32 {
2828
}) {
2929
(*max.borrow_mut()) = (*i.borrow());
3030
}
31-
(*i.borrow_mut()).prefix_inc();
31+
*i.borrow_mut() += 1;
3232
}
3333
return (*max.borrow());
3434
}

tests/benchmarks/out/refcount/ref_array_sum.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn initialize_0(array: Ptr<Option<Value<Box<[i32]>>>>, N: i32) {
1313
let __rhs = (*i.borrow());
1414
(*array.upgrade().deref()).as_ref().unwrap().borrow_mut()
1515
[((*i.borrow()) as u64) as usize] = __rhs;
16-
(*i.borrow_mut()).prefix_inc();
16+
*i.borrow_mut() += 1;
1717
}
1818
}
1919
pub fn sum_1(array: Ptr<Option<Value<Box<[i32]>>>>, N: i32) -> i64 {
@@ -26,7 +26,7 @@ pub fn sum_1(array: Ptr<Option<Value<Box<[i32]>>>>, N: i32) -> i64 {
2626
.as_ref()
2727
.unwrap()
2828
.borrow()[((*i.borrow()) as u64) as usize] as i64);
29-
(*i.borrow_mut()).prefix_inc();
29+
*i.borrow_mut() += 1;
3030
}
3131
return (*sum.borrow());
3232
}
@@ -54,7 +54,7 @@ fn main_0() -> i32 {
5454
let _N: i32 = (*N.borrow());
5555
sum_1(_array, _N)
5656
});
57-
(*k.borrow_mut()).prefix_inc();
57+
*k.borrow_mut() += 1;
5858
}
5959
return ((*out.borrow()) as i32);
6060
}

tests/benchmarks/out/refcount/ref_prime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn is_prime_0(x: Ptr<i32>) -> bool {
1919
{
2020
return false;
2121
}
22-
(*i.borrow_mut()).prefix_inc();
22+
*i.borrow_mut() += 1;
2323
}
2424
return true;
2525
}
@@ -36,7 +36,7 @@ pub fn largest_prime_1(n: Ptr<i32>) -> i32 {
3636
}) {
3737
(*max.borrow_mut()) = (*i.borrow());
3838
}
39-
(*i.borrow_mut()).prefix_inc();
39+
*i.borrow_mut() += 1;
4040
}
4141
return (*max.borrow());
4242
}

tests/benchmarks/out/refcount/sum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main_0() -> i32 {
1616
let j: Value<i64> = Rc::new(RefCell::new((*N.borrow())));
1717
'loop_: while ((*i.borrow()) < (*j.borrow())) {
1818
(*sum.borrow_mut()) += ((*i.borrow()) + (*j.borrow()));
19-
(*i.borrow_mut()).prefix_inc();
19+
*i.borrow_mut() += 1;
2020
(*j.borrow_mut()).prefix_dec();
2121
}
2222
return ((*sum.borrow()) as i32);

tests/benchmarks/out/unsafe/array_sum.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ unsafe fn main_0() -> i32 {
2424
let mut i: i32 = 0;
2525
'loop_: while ((i) < (N)) {
2626
array.as_mut().unwrap()[(i as u64) as usize] = i;
27-
i.prefix_inc();
27+
i += 1;
2828
}
2929
let mut i: i32 = 0;
3030
'loop_: while ((i) < (N)) {
3131
sum += (array.as_mut().unwrap()[(i as u64) as usize] as i64);
32-
i.prefix_inc();
32+
i += 1;
3333
}
34-
k.prefix_inc();
34+
k += 1;
3535
}
3636
return (sum as i32);
3737
}

0 commit comments

Comments
 (0)