Skip to content

Commit 8492b53

Browse files
committed
Remove clones on fresh values
1 parent ac52c1f commit 8492b53

30 files changed

Lines changed: 93 additions & 67 deletions

‎cpp2rust/converter/converter.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,17 +2005,20 @@ bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
20052005
uint64_t arr_size = arr_ty->getSize().getZExtValue();
20062006
if (expr->getString().empty()) {
20072007
StrCat(std::format("[0 as libc::c_char; {}]", arr_size));
2008+
computed_expr_type_ = ComputedExprType::FreshValue;
20082009
return false;
20092010
}
20102011
uint64_t pad = arr_size > expr->getString().size()
20112012
? arr_size - expr->getString().size()
20122013
: 0;
20132014
StrCat(std::format("std::mem::transmute(*b{})",
20142015
GetEscapedStringLiteral(expr, pad)));
2016+
computed_expr_type_ = ComputedExprType::FreshValue;
20152017
return false;
20162018
}
20172019
StrCat(std::format("std::mem::transmute(*b{})",
20182020
GetEscapedStringLiteral(expr, 1)));
2021+
computed_expr_type_ = ComputedExprType::FreshValue;
20192022
return false;
20202023
}
20212024
if (expr->getString().contains('\0')) {
@@ -2025,9 +2028,11 @@ bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
20252028
}
20262029
out += getTypedLiteral("0", CharRustType()) + "])";
20272030
StrCat(out);
2031+
computed_expr_type_ = ComputedExprType::FreshValue;
20282032
return false;
20292033
}
20302034
StrCat(std::format("c{}", GetEscapedStringLiteral(expr, 0)));
2035+
computed_expr_type_ = ComputedExprType::FreshValue;
20312036
return false;
20322037
}
20332038

@@ -2349,6 +2354,9 @@ void Converter::ConvertBinaryOperator(clang::BinaryOperator *expr) {
23492354
ConvertUnsignedArithOperand(lhs, type);
23502355
}
23512356
ConvertUnsignedArithBinaryOperator(expr, rhs);
2357+
if (!expr->isCompoundAssignmentOp()) {
2358+
computed_expr_type_ = ComputedExprType::FreshValue;
2359+
}
23522360
} else if (expr->isAssignmentOp()) {
23532361
if (expr->isCompoundAssignmentOp() &&
23542362
expr->getLHS()->getType()->isPointerType() &&
@@ -2408,6 +2416,7 @@ void Converter::ConvertBinaryOperator(clang::BinaryOperator *expr) {
24082416
PushParen paren(*this);
24092417
ConvertCondition(expr->getRHS());
24102418
}
2419+
computed_expr_type_ = ComputedExprType::FreshValue;
24112420
} else {
24122421
ConvertGenericBinaryOperator(expr);
24132422
}
@@ -2429,6 +2438,7 @@ void Converter::ConvertGenericBinaryOperator(clang::BinaryOperator *expr) {
24292438
PushParen rhs_paren(*this);
24302439
Convert(rhs, GetOperandImplicitConversionTarget(expr, rhs, lhs));
24312440
}
2441+
computed_expr_type_ = ComputedExprType::FreshValue;
24322442
}
24332443

24342444
bool Converter::IsReferenceType(const clang::Expr *expr) const {
@@ -2991,12 +3001,14 @@ bool Converter::VisitVAArgExpr(clang::VAArgExpr *expr) {
29913001
Convert(va_list_expr);
29923002
}
29933003
StrCat(".arg::<*mut ::libc::c_void>()");
3004+
SetFreshType(expr->getType());
29943005
return false;
29953006
}
29963007
Convert(va_list_expr);
29973008
StrCat(".arg::<");
29983009
Convert(expr->getType());
29993010
StrCat(">()");
3011+
SetFreshType(expr->getType());
30003012
return false;
30013013
}
30023014

@@ -3246,6 +3258,7 @@ void Converter::AddIncDecImpls(clang::EnumDecl *decl) {
32463258
bool Converter::VisitCXXDefaultArgExpr(clang::CXXDefaultArgExpr *expr) {
32473259
if (expr->getType()->isPointerType()) {
32483260
StrCat(token::kDefault);
3261+
computed_expr_type_ = ComputedExprType::FreshPointer;
32493262
}
32503263
return false;
32513264
}
@@ -3278,11 +3291,13 @@ bool Converter::VisitImplicitValueInitExpr(clang::ImplicitValueInitExpr *expr) {
32783291
auto elem_ty = const_arr_ty->getElementType();
32793292
if (elem_ty->isIntegerType() && !elem_ty->isEnumeralType()) {
32803293
StrCat(std::format("[0; {}]", const_arr_ty->getSize().getZExtValue()));
3294+
computed_expr_type_ = ComputedExprType::FreshValue;
32813295
return false;
32823296
}
32833297
StrCat(
32843298
std::format("std::array::from_fn::<_, {}, _>(|_| Default::default())",
32853299
const_arr_ty->getSize().getZExtValue()));
3300+
computed_expr_type_ = ComputedExprType::FreshValue;
32863301
return false;
32873302
}
32883303
}
@@ -3681,6 +3696,7 @@ void Converter::ConvertEqualsNullPtr(clang::Expr *expr) {
36813696
} else {
36823697
StrCat(").is_null()");
36833698
}
3699+
computed_expr_type_ = ComputedExprType::FreshValue;
36843700
}
36853701

36863702
void Converter::ConvertPointerSubscript(clang::ArraySubscriptExpr *expr) {

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ bool ConverterRefCount::VisitArraySubscriptExpr(
402402
StrCat(GetPointerDerefPrefix(expr->getType()));
403403
ConvertArraySubscript(base, expr->getIdx(), expr->getType());
404404
StrCat(GetPointerDerefSuffix(expr->getType()));
405+
SetValueFreshness(expr->getType());
405406
} else {
406407
ConvertArraySubscript(base, expr->getIdx(), expr->getType());
407408
}
@@ -788,6 +789,7 @@ bool ConverterRefCount::ConvertIncAndDec(clang::UnaryOperator *expr) {
788789
} else {
789790
StrCat(str, '.', method, "()");
790791
}
792+
SetFreshType(expr->getType());
791793
return true;
792794
}
793795

@@ -1133,16 +1135,19 @@ bool ConverterRefCount::VisitStringLiteral(clang::StringLiteral *expr) {
11331135
uint64_t arr_size = arr_ty->getSize().getZExtValue();
11341136
if (expr->getString().empty()) {
11351137
StrCat(std::format("vec![0u8; {}].into_boxed_slice()", arr_size));
1138+
computed_expr_type_ = ComputedExprType::FreshValue;
11361139
return false;
11371140
}
11381141
pad = arr_size > expr->getString().size()
11391142
? arr_size - expr->getString().size()
11401143
: 0;
11411144
}
11421145
StrCat(std::format("Box::from(*b{})", GetEscapedStringLiteral(expr, pad)));
1146+
computed_expr_type_ = ComputedExprType::FreshValue;
11431147
return false;
11441148
}
11451149
StrCat(std::format("b{}", GetEscapedStringLiteral(expr, 0)));
1150+
computed_expr_type_ = ComputedExprType::FreshValue;
11461151
return false;
11471152
}
11481153

@@ -1270,6 +1275,7 @@ void ConverterRefCount::ConvertEqualsNullPtr(clang::Expr *expr) {
12701275
StrCat('(');
12711276
Convert(expr);
12721277
StrCat(").is_null()");
1278+
computed_expr_type_ = ComputedExprType::FreshValue;
12731279
}
12741280

12751281
bool ConverterRefCount::VisitFunctionPointerCast(
@@ -1421,6 +1427,7 @@ void ConverterRefCount::EmitStmtExprTail(clang::Expr *tail) {
14211427
Convert(tail);
14221428
StrCat(token::kSemiColon);
14231429
StrCat("__result");
1430+
SetFreshType(tail->getType());
14241431
}
14251432

14261433
void ConverterRefCount::ConvertBinaryOperator(clang::BinaryOperator *expr) {
@@ -1483,6 +1490,8 @@ void ConverterRefCount::ConvertBinaryOperator(clang::BinaryOperator *expr) {
14831490
if (expr->isCompoundAssignmentOp()) {
14841491
StrCat(token::kSemiColon);
14851492
EmitSetOrAssign(lhs, "rhs_0");
1493+
} else {
1494+
computed_expr_type_ = ComputedExprType::FreshValue;
14861495
}
14871496
return;
14881497
}
@@ -1608,6 +1617,7 @@ void ConverterRefCount::ConvertUnionMemberAccessor(clang::MemberExpr *expr) {
16081617
return;
16091618
}
16101619
StrCat(DerefPtrExpr(str, member->getType()));
1620+
SetValueFreshness(member->getType());
16111621
}
16121622

16131623
bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) {
@@ -1882,6 +1892,7 @@ bool ConverterRefCount::VisitImplicitValueInitExpr(
18821892
StrCat("Box::new(");
18831893
Converter::VisitImplicitValueInitExpr(expr);
18841894
StrCat(')');
1895+
computed_expr_type_ = ComputedExprType::FreshValue;
18851896
return false;
18861897
}
18871898
}
@@ -1909,6 +1920,7 @@ bool ConverterRefCount::VisitVAArgExpr(clang::VAArgExpr *expr) {
19091920
StrCat(ToString(expr->getType()));
19101921
}
19111922
StrCat(">()");
1923+
SetFreshType(expr->getType());
19121924
return false;
19131925
}
19141926

@@ -1936,10 +1948,12 @@ ConverterRefCount::GetArrayDefaultAsString(clang::QualType qual_type) {
19361948

19371949
std::string ConverterRefCount::GetDefaultAsString(clang::QualType qual_type) {
19381950
if (IsVaListType(qual_type)) {
1951+
computed_expr_type_ = ComputedExprType::FreshValue;
19391952
return BoxValue("VaList::default()");
19401953
}
19411954

19421955
if (auto arr = GetArrayDefaultAsString(qual_type); !arr.empty()) {
1956+
computed_expr_type_ = ComputedExprType::FreshValue;
19431957
return BoxValue(std::move(arr));
19441958
}
19451959

@@ -1964,6 +1978,7 @@ std::string ConverterRefCount::GetDefaultAsString(clang::QualType qual_type) {
19641978
} else {
19651979
return Converter::GetDefaultAsString(qual_type);
19661980
}
1981+
computed_expr_type_ = ComputedExprType::FreshPointer;
19671982
return BoxValue(std::move(ret));
19681983
}
19691984

@@ -2103,13 +2118,15 @@ void ConverterRefCount::ConvertGenericBinaryOperator(
21032118
opcode,
21042119
ConvertFreshRValue(
21052120
rhs, GetOperandImplicitConversionTarget(expr, rhs, lhs))));
2121+
computed_expr_type_ = ComputedExprType::FreshValue;
21062122
return;
21072123
}
21082124

21092125
PushParen outer(*this);
21102126
Convert(lhs, GetOperandImplicitConversionTarget(expr, lhs, rhs));
21112127
StrCat(opcode);
21122128
Convert(rhs, GetOperandImplicitConversionTarget(expr, rhs, lhs));
2129+
computed_expr_type_ = ComputedExprType::FreshValue;
21132130
}
21142131

21152132
void ConverterRefCount::ConvertUniquePtrDeref(
@@ -2394,6 +2411,7 @@ void ConverterRefCount::ConvertArrow(clang::Expr *expr) {
23942411
if (!is_overloaded_arrow) {
23952412
auto ptr = ToString(expr);
23962413
StrCat(DerefPtrExpr(ptr, expr->getType()->getPointeeType()));
2414+
SetValueFreshness(expr->getType()->getPointeeType());
23972415
return;
23982416
}
23992417

‎tests/unit/out/refcount/bool_condition_ptr.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ fn main_0() -> i32 {
4040
assert!(((*t5.borrow()) == 0));
4141
let t6: Value<i32> = Rc::new(RefCell::new((!!(*np.borrow()).is_null() as i32)));
4242
assert!(((*t6.borrow()) == 1));
43-
let b2: Value<bool> = Rc::new(RefCell::new((!(*p.borrow()).is_null()).clone()));
44-
let b3: Value<bool> = Rc::new(RefCell::new((!(*np.borrow()).is_null()).clone()));
43+
let b2: Value<bool> = Rc::new(RefCell::new(!(*p.borrow()).is_null()));
44+
let b3: Value<bool> = Rc::new(RefCell::new(!(*np.borrow()).is_null()));
4545
assert!((*b2.borrow()));
4646
assert!(!(*b3.borrow()));
4747
return 0;

‎tests/unit/out/refcount/bool_condition_ptr_c.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ fn main_0() -> i32 {
4040
assert!(((((*t5.borrow()) == 0) as i32) != 0));
4141
let t6: Value<i32> = Rc::new(RefCell::new((!!(*np.borrow()).is_null() as i32)));
4242
assert!(((((*t6.borrow()) == 1) as i32) != 0));
43-
let b2: Value<bool> = Rc::new(RefCell::new((!(*p.borrow()).is_null()).clone()));
44-
let b3: Value<bool> = Rc::new(RefCell::new((!(*np.borrow()).is_null()).clone()));
43+
let b2: Value<bool> = Rc::new(RefCell::new(!(*p.borrow()).is_null()));
44+
let b3: Value<bool> = Rc::new(RefCell::new(!(*np.borrow()).is_null()));
4545
assert!((*b2.borrow()));
4646
assert!(((!(*b3.borrow()) as i32) != 0));
4747
return 0;

‎tests/unit/out/refcount/expr_as_bool_c.rs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ pub fn cmp_eq_0(rc: i32) -> i32 {
1313
pub fn cmp_or_ptr_1(p: Ptr<u8>, q: Ptr<u8>) -> i32 {
1414
let p: Value<Ptr<u8>> = Rc::new(RefCell::new(p));
1515
let q: Value<Ptr<u8>> = Rc::new(RefCell::new(q));
16-
return (((!(*p.borrow()).is_null()) || (!(*q.borrow()).is_null())) as i32).clone();
16+
return (((!(*p.borrow()).is_null()) || (!(*q.borrow()).is_null())) as i32);
1717
}
1818
pub fn both_null_2(s1: Ptr<u8>, s2: Ptr<u8>) -> i32 {
1919
let s1: Value<Ptr<u8>> = Rc::new(RefCell::new(s1));
2020
let s2: Value<Ptr<u8>> = Rc::new(RefCell::new(s2));
2121
return ((((((*s1.borrow()).is_null()) as i32) != 0)
22-
&& ((((*s2.borrow()).is_null()) as i32) != 0)) as i32)
23-
.clone();
22+
&& ((((*s2.borrow()).is_null()) as i32) != 0)) as i32);
2423
}
2524
pub fn main() {
2625
std::process::exit(main_0());
@@ -74,10 +73,10 @@ fn main_0() -> i32 {
7473
let p1: Value<Ptr<u8>> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hi")));
7574
let p2: Value<Ptr<u8>> = Rc::new(RefCell::new(Ptr::<u8>::null()));
7675
let either: Value<i32> = Rc::new(RefCell::new(
77-
(((!(*p1.borrow()).is_null()) || (!(*p2.borrow()).is_null())) as i32).clone(),
76+
(((!(*p1.borrow()).is_null()) || (!(*p2.borrow()).is_null())) as i32),
7877
));
7978
let both: Value<i32> = Rc::new(RefCell::new(
80-
(((!(*p1.borrow()).is_null()) && (!(*p2.borrow()).is_null())) as i32).clone(),
79+
(((!(*p1.borrow()).is_null()) && (!(*p2.borrow()).is_null())) as i32),
8180
));
8281
assert!(((((*either.borrow()) == 1) as i32) != 0));
8382
assert!(((((*both.borrow()) == 0) as i32) != 0));

‎tests/unit/out/refcount/expr_as_bool_cpp.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ pub fn cmp_eq_0(rc: i32) -> i32 {
1313
pub fn cmp_or_ptr_1(p: Ptr<u8>, q: Ptr<u8>) -> i32 {
1414
let p: Value<Ptr<u8>> = Rc::new(RefCell::new(p));
1515
let q: Value<Ptr<u8>> = Rc::new(RefCell::new(q));
16-
return (((!(*p.borrow()).is_null()) || (!(*q.borrow()).is_null())) as i32).clone();
16+
return (((!(*p.borrow()).is_null()) || (!(*q.borrow()).is_null())) as i32);
1717
}
1818
pub fn both_null_2(s1: Ptr<u8>, s2: Ptr<u8>) -> i32 {
1919
let s1: Value<Ptr<u8>> = Rc::new(RefCell::new(s1));
2020
let s2: Value<Ptr<u8>> = Rc::new(RefCell::new(s2));
21-
return ((((*s1.borrow()).is_null()) && ((*s2.borrow()).is_null())) as i32).clone();
21+
return ((((*s1.borrow()).is_null()) && ((*s2.borrow()).is_null())) as i32);
2222
}
2323
pub fn main() {
2424
std::process::exit(main_0());
@@ -69,10 +69,10 @@ fn main_0() -> i32 {
6969
let p1: Value<Ptr<u8>> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hi")));
7070
let p2: Value<Ptr<u8>> = Rc::new(RefCell::new(Ptr::<u8>::null()));
7171
let either: Value<i32> = Rc::new(RefCell::new(
72-
(((!(*p1.borrow()).is_null()) || (!(*p2.borrow()).is_null())) as i32).clone(),
72+
(((!(*p1.borrow()).is_null()) || (!(*p2.borrow()).is_null())) as i32),
7373
));
7474
let both: Value<i32> = Rc::new(RefCell::new(
75-
(((!(*p1.borrow()).is_null()) && (!(*p2.borrow()).is_null())) as i32).clone(),
75+
(((!(*p1.borrow()).is_null()) && (!(*p2.borrow()).is_null())) as i32),
7676
));
7777
assert!(((*either.borrow()) == 1));
7878
assert!(((*both.borrow()) == 0));

‎tests/unit/out/refcount/huffman.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct MinHeapNode {
1515
}
1616
impl MinHeapNode {
1717
pub fn IsLeaf(&self) -> bool {
18-
return (((*self.left.borrow()).is_null()) && ((*self.right.borrow()).is_null())).clone();
18+
return ((*self.left.borrow()).is_null()) && ((*self.right.borrow()).is_null());
1919
}
2020
}
2121
impl Clone for MinHeapNode {

‎tests/unit/out/refcount/tag_vs_identifier_collision.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ impl ByteRepr for Inner_struct {
263263
pub fn is_active_0(w: Ptr<widget>) -> i32 {
264264
let w: Value<Ptr<widget>> = Rc::new(RefCell::new(w));
265265
return ((((*(*(*w.borrow()).upgrade().deref()).mode.borrow()) as u32)
266-
== ((widget_enum::MODE_ACTIVE as i32) as u32)) as i32)
267-
.clone();
266+
== ((widget_enum::MODE_ACTIVE as i32) as u32)) as i32);
268267
}
269268
pub fn main() {
270269
std::process::exit(main_0());

‎tests/unit/out/refcount/union_tagged_many_arms.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn main_0() -> i32 {
135135
(*(*c.borrow()).tag.borrow_mut()) = Tag_enum::T_TEXT;
136136
(*(*c.borrow()).payload.borrow_mut())
137137
.text()
138-
.write((Ptr::from_string_literal(b"hello")).clone());
138+
.write(Ptr::from_string_literal(b"hello"));
139139
assert!(
140140
(((((((*(*c.borrow()).payload.borrow()).text().read())
141141
.offset((0) as isize)

‎tests/unit/out/refcount/va_arg_chain.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn extract_nth_0(n: i32, ap: VaList) -> i32 {
1414
(*ap.borrow_mut()).arg::<i32>();
1515
(*i.borrow_mut()).postfix_inc();
1616
}
17-
return ((*ap.borrow_mut()).arg::<i32>()).clone();
17+
return (*ap.borrow_mut()).arg::<i32>();
1818
}
1919
pub fn middle_layer_1(n: i32, ap: VaList) -> i32 {
2020
let n: Value<i32> = Rc::new(RefCell::new(n));

0 commit comments

Comments
 (0)