Skip to content

Commit aac5b9e

Browse files
committed
Address taken analysis: dig into initializer lists
1 parent 76bd8b2 commit aac5b9e

13 files changed

Lines changed: 142 additions & 107 deletions

File tree

‎cpp2rust/converter/models/boxed_vars.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ class AddressTakenVisitor
4141

4242
bool shouldVisitTemplateInstantiations() const { return true; }
4343

44+
// Only the semantic form of an initializer list is converted, in which the
45+
// values of variables are read through lvalue-to-rvalue conversions.
46+
bool TraverseInitListExpr(clang::InitListExpr *expr,
47+
DataRecursionQueue *queue = nullptr) {
48+
return TraverseSynOrSemInitListExpr(
49+
expr->isSemanticForm() ? expr : expr->getSemanticForm(), queue);
50+
}
51+
4452
bool VisitDeclRefExpr(clang::DeclRefExpr *expr) {
4553
if (!value_uses_.contains(expr)) {
4654
AddressTaken(expr->getDecl());

‎tests/unit/out/refcount/10_struct.rs‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,17 @@ pub trait GraphImpl {
8787
fn push(&self, src: u32, dst: u32);
8888
}
8989
impl GraphImpl for Ptr<Graph> {
90-
fn push(&self, src: u32, dst: u32) {
91-
let src: Value<u32> = Rc::new(RefCell::new(src));
92-
let dst: Value<u32> = Rc::new(RefCell::new(dst));
90+
fn push(&self, mut src: u32, mut dst: u32) {
9391
let __rhs = Ptr::alloc(GraphNode {
94-
dst: (*dst.borrow()),
95-
next: (elem!((*self).with(|__s| __s.adj.clone()), (*src.borrow())).read()).clone(),
92+
dst: dst,
93+
next: (elem!((*self).with(|__s| __s.adj.clone()), src).read()).clone(),
9694
});
97-
elem!((*self).with(|__s| __s.adj.clone()), (*src.borrow())).write(__rhs);
95+
elem!((*self).with(|__s| __s.adj.clone()), src).write(__rhs);
9896
let __rhs = Ptr::alloc(GraphNode {
99-
dst: (*src.borrow()),
100-
next: (elem!((*self).with(|__s| __s.adj.clone()), (*dst.borrow())).read()).clone(),
97+
dst: src,
98+
next: (elem!((*self).with(|__s| __s.adj.clone()), dst).read()).clone(),
10199
});
102-
elem!((*self).with(|__s| __s.adj.clone()), (*dst.borrow())).write(__rhs);
100+
elem!((*self).with(|__s| __s.adj.clone()), dst).write(__rhs);
103101
}
104102
}
105103
pub trait PartialImpl {

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,10 @@ pub trait Outer_int_Impl {
278278
fn with(&self, n: i32) -> Outer_int__Inner_int_;
279279
}
280280
impl Outer_int_Impl for Ptr<Outer_int_> {
281-
fn with(&self, n: i32) -> Outer_int__Inner_int_ {
282-
let n: Value<i32> = Rc::new(RefCell::new(n));
281+
fn with(&self, mut n: i32) -> Outer_int__Inner_int_ {
283282
return Outer_int__Inner_int_ {
284283
t: (*self).with(|__s| __s.v),
285-
u: (*n.borrow()),
284+
u: n,
286285
};
287286
}
288287
}
@@ -298,11 +297,10 @@ pub trait Outer_long_Impl {
298297
fn with(&self, n: i32) -> Outer_long__Inner_int_;
299298
}
300299
impl Outer_long_Impl for Ptr<Outer_long_> {
301-
fn with(&self, n: i32) -> Outer_long__Inner_int_ {
302-
let n: Value<i32> = Rc::new(RefCell::new(n));
300+
fn with(&self, mut n: i32) -> Outer_long__Inner_int_ {
303301
return Outer_long__Inner_int_ {
304302
t: (*self).with(|__s| __s.v),
305-
u: (*n.borrow()),
303+
u: n,
306304
};
307305
}
308306
}

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@ pub struct Explicit {
2525
pub arr: Value<Box<[i32]>>,
2626
}
2727
impl Explicit {
28-
pub fn new(v: i32) -> Self {
29-
let v: Value<i32> = Rc::new(RefCell::new(v));
28+
pub fn new(mut v: i32) -> Self {
3029
Self {
31-
v: (*v.borrow()),
32-
inner: Inner {
33-
x: ((*v.borrow()) * 10),
34-
},
35-
arr: Rc::new(RefCell::new(Box::new([(*v.borrow()), ((*v.borrow()) + 1)]))),
30+
v: v,
31+
inner: Inner { x: (v * 10) },
32+
arr: Rc::new(RefCell::new(Box::new([v, (v + 1)]))),
3633
}
3734
}
3835
}
@@ -227,11 +224,10 @@ pub struct Holder {
227224
pub p: Option<Value<i32>>,
228225
}
229226
impl Holder {
230-
pub fn new(v: i32) -> Self {
231-
let v: Value<i32> = Rc::new(RefCell::new(v));
227+
pub fn new(mut v: i32) -> Self {
232228
Self {
233-
inner: Inner { x: (*v.borrow()) },
234-
e: Explicit::new({ (*v.borrow()) }),
229+
inner: Inner { x: v },
230+
e: Explicit::new({ v }),
235231
p: None,
236232
}
237233
}

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

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@ pub fn Product_0(mut z1: Complex, mut z2: Complex) -> Complex {
2525
};
2626
}
2727
pub fn Sum_1(mut z1: Complex, mut z2: Complex) -> Complex {
28-
let ac: Value<f64> = Rc::new(RefCell::new((z1.re + z2.re)));
29-
let bd: Value<f64> = Rc::new(RefCell::new((z1.img + z2.img)));
30-
return Complex {
31-
re: (*ac.borrow()),
32-
img: (*bd.borrow()),
33-
};
28+
let mut ac: f64 = (z1.re + z2.re);
29+
let mut bd: f64 = (z1.img + z2.img);
30+
return Complex { re: ac, img: bd };
3431
}
3532
pub fn Neg_2(mut z1: Complex) -> Complex {
3633
return Complex {
@@ -111,47 +108,43 @@ pub fn fft_3(a: Ptr<Option<Value<Box<[Complex]>>>>, mut N: i32) -> Option<Value<
111108
Rc::new(RefCell::new(({ fft_3(A1.as_pointer(), (N / 2)) })));
112109
let mut k: i32 = 0;
113110
'loop_: while (k < (N / 2)) {
114-
let yk: Value<Complex> = Rc::new(RefCell::new(
115-
({
111+
let mut yk: Complex = ({
112+
let _z1: Complex =
113+
((*y0.borrow()).as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
114+
let _z2: Complex = ({
116115
let _z1: Complex =
117-
((*y0.borrow()).as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
118-
let _z2: Complex = ({
119-
let _z1: Complex =
120-
((*w.borrow()).as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
121-
let _z2: Complex =
122-
((*y1.borrow()).as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
123-
Product_0(_z1, _z2)
124-
});
125-
Sum_1(_z1, _z2)
126-
}),
127-
));
116+
((*w.borrow()).as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
117+
let _z2: Complex =
118+
((*y1.borrow()).as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
119+
Product_0(_z1, _z2)
120+
});
121+
Sum_1(_z1, _z2)
122+
});
128123
(*y.borrow()).as_ref().unwrap().borrow_mut()[(k as usize) as usize] = Complex {
129-
re: { (*yk.borrow()).re },
130-
img: { (*yk.borrow()).img },
124+
re: yk.re,
125+
img: yk.img,
131126
};
132-
let yk_n2: Value<Complex> = Rc::new(RefCell::new(
133-
({
134-
let _z1: Complex =
135-
((*y0.borrow()).as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
136-
let _z2: Complex = ({
137-
Neg_2(
138-
({
139-
let _z1: Complex = ((*w.borrow()).as_ref().unwrap().borrow()
140-
[(k as usize) as usize])
141-
.clone();
142-
let _z2: Complex = ((*y1.borrow()).as_ref().unwrap().borrow()
143-
[(k as usize) as usize])
144-
.clone();
145-
Product_0(_z1, _z2)
146-
}),
147-
)
148-
});
149-
Sum_1(_z1, _z2)
150-
}),
151-
));
127+
let mut yk_n2: Complex = ({
128+
let _z1: Complex =
129+
((*y0.borrow()).as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
130+
let _z2: Complex = ({
131+
Neg_2(
132+
({
133+
let _z1: Complex = ((*w.borrow()).as_ref().unwrap().borrow()
134+
[(k as usize) as usize])
135+
.clone();
136+
let _z2: Complex = ((*y1.borrow()).as_ref().unwrap().borrow()
137+
[(k as usize) as usize])
138+
.clone();
139+
Product_0(_z1, _z2)
140+
}),
141+
)
142+
});
143+
Sum_1(_z1, _z2)
144+
});
152145
(*y.borrow()).as_ref().unwrap().borrow_mut()[((k + (N / 2)) as usize) as usize] = Complex {
153-
re: { (*yk_n2.borrow()).re },
154-
img: { (*yk_n2.borrow()).img },
146+
re: yk_n2.re,
147+
img: yk_n2.img,
155148
};
156149
k.postfix_inc();
157150
}

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ pub struct MinHeapNode {
2121
pub right: Ptr<MinHeapNode>,
2222
}
2323
pub fn Swap_0(a: Ptr<MinHeapNode>, b: Ptr<MinHeapNode>) {
24-
let t: Value<MinHeapNode> = Rc::new(RefCell::new(MinHeapNode {
24+
let mut t: MinHeapNode = MinHeapNode {
2525
data: a.with(|__s| __s.data),
2626
freq: a.with(|__s| __s.freq),
2727
left: a.with(|__s| __s.left.clone()),
2828
right: a.with(|__s| __s.right.clone()),
29-
}));
29+
};
3030
a.write({
3131
MinHeapNode {
3232
data: b.with(|__s| __s.data),
@@ -37,10 +37,10 @@ pub fn Swap_0(a: Ptr<MinHeapNode>, b: Ptr<MinHeapNode>) {
3737
});
3838
b.write({
3939
MinHeapNode {
40-
data: { (*t.borrow()).data },
41-
freq: { (*t.borrow()).freq },
42-
left: { (*t.borrow()).left.clone() },
43-
right: { (*t.borrow()).right.clone() },
40+
data: t.data,
41+
freq: t.freq,
42+
left: (t.left).clone(),
43+
right: (t.right).clone(),
4444
}
4545
});
4646
}
@@ -73,15 +73,14 @@ impl MinHeap {
7373
}
7474
}
7575
}
76-
pub fn AllocMinHeap_1(capacity: i32) -> Option<Value<MinHeap>> {
77-
let capacity: Value<i32> = Rc::new(RefCell::new(capacity));
76+
pub fn AllocMinHeap_1(mut capacity: i32) -> Option<Value<MinHeap>> {
7877
let minHeap: Value<Option<Value<MinHeap>>> =
7978
Rc::new(RefCell::new(Some(Rc::new(RefCell::new({
8079
let __tmp_0: Value<MinHeap> = Rc::new(RefCell::new(MinHeap {
8180
size: 0,
82-
capacity: (*capacity.borrow()),
81+
capacity: capacity,
8382
arr: Some(Rc::new(RefCell::new(
84-
(0..((*capacity.borrow()) as usize))
83+
(0..(capacity as usize))
8584
.map(|_| <Ptr<MinHeapNode>>::default())
8685
.collect::<Box<[_]>>(),
8786
))),
@@ -289,16 +288,14 @@ pub trait MinHeapImpl {
289288
fn move_assign(&self, _a0: Ptr<MinHeap>) -> Ptr<MinHeap>;
290289
}
291290
impl MinHeapImpl for Ptr<MinHeap> {
292-
fn Alloc(&self, data: i8, freq: i32) -> Ptr<MinHeapNode> {
293-
let data: Value<i8> = Rc::new(RefCell::new(data));
294-
let freq: Value<i32> = Rc::new(RefCell::new(freq));
291+
fn Alloc(&self, mut data: i8, mut freq: i32) -> Ptr<MinHeapNode> {
295292
(*self)
296293
.with(|__s| __s.alloc.clone())
297294
.as_ref()
298295
.unwrap()
299296
.borrow_mut()[((*self).with(|__s| __s.next) as usize) as usize] = MinHeapNode {
300-
data: (*data.borrow()),
301-
freq: (*freq.borrow()),
297+
data: data,
298+
freq: freq,
302299
left: Ptr::<MinHeapNode>::null(),
303300
right: Ptr::<MinHeapNode>::null(),
304301
};

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,16 @@ pub fn main() {
225225
std::process::exit(main_0());
226226
}
227227
fn main_0() -> i32 {
228-
let V: Value<i32> = Rc::new(RefCell::new(4));
229-
let E: Value<i32> = Rc::new(RefCell::new(5));
228+
let mut V: i32 = 4;
229+
let mut E: i32 = 5;
230230
let graph: Value<Graph> = Rc::new(RefCell::new(Graph {
231231
edges: Some(Rc::new(RefCell::new(
232-
(0..((*E.borrow()) as usize))
232+
(0..(E as usize))
233233
.map(|_| <Edge>::default())
234234
.collect::<Box<[_]>>(),
235235
))),
236-
V: (*V.borrow()),
237-
E: (*E.borrow()),
236+
V: V,
237+
E: E,
238238
}));
239239
{ (*graph.borrow()).edges.clone() }
240240
.as_ref()

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ pub fn get_6(mut t: Local_7) -> i32 {
2121
pub fn twice_8(mut t: Local_3) -> i32 {
2222
return (t.x * 2);
2323
}
24-
pub fn wrap_9(v: i32) -> i32 {
25-
let v: Value<i32> = Rc::new(RefCell::new(v));
26-
let l: Value<Local_5> = Rc::new(RefCell::new(Local_5 { x: (*v.borrow()) }));
24+
pub fn wrap_9(mut v: i32) -> i32 {
25+
let l: Value<Local_5> = Rc::new(RefCell::new(Local_5 { x: v }));
2726
return ({ get_4((*l.borrow()).clone()) });
2827
}
29-
pub fn wrap_10(v: i64) -> i32 {
30-
let v: Value<i64> = Rc::new(RefCell::new(v));
31-
let l: Value<Local_7> = Rc::new(RefCell::new(Local_7 { x: (*v.borrow()) }));
28+
pub fn wrap_10(mut v: i64) -> i32 {
29+
let l: Value<Local_7> = Rc::new(RefCell::new(Local_7 { x: v }));
3230
return ({ get_6((*l.borrow()).clone()) });
3331
}
3432
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,19 @@ pub fn find_0(mut node: Ptr<node_t>, mut value: i32) -> Ptr<node_t> {
3232
}
3333
return Ptr::<node_t>::null();
3434
}
35-
pub fn insert_1(mut node: Ptr<node_t>, value: i32) -> Ptr<node_t> {
36-
let value: Value<i32> = Rc::new(RefCell::new(value));
35+
pub fn insert_1(mut node: Ptr<node_t>, mut value: i32) -> Ptr<node_t> {
3736
if (node).is_null() {
3837
return Ptr::alloc(node_t {
3938
left: Ptr::<node_t>::null(),
4039
right: Ptr::<node_t>::null(),
41-
value: (*value.borrow()),
40+
value: value,
4241
});
4342
}
44-
if ({ (*value.borrow()) } < { node.with(|__s| __s.value) }) {
45-
let __rhs = ({ insert_1(node.with(|__s| __s.left.clone()), (*value.borrow())) });
43+
if ({ value } < { node.with(|__s| __s.value) }) {
44+
let __rhs = ({ insert_1(node.with(|__s| __s.left.clone()), value) });
4645
field!(node, left).write(__rhs);
47-
} else if ({ (*value.borrow()) } > { node.with(|__s| __s.value) }) {
48-
let __rhs = ({ insert_1(node.with(|__s| __s.right.clone()), (*value.borrow())) });
46+
} else if ({ value } > { node.with(|__s| __s.value) }) {
47+
let __rhs = ({ insert_1(node.with(|__s| __s.right.clone()), value) });
4948
field!(node, right).write(__rhs);
5049
}
5150
return (node).clone();

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ pub fn main() {
3232
std::process::exit(main_0());
3333
}
3434
fn main_0() -> i32 {
35-
let p1: Value<POD> = Rc::new(RefCell::new(POD {
35+
let mut p1: POD = POD {
3636
x1: 10,
3737
x2: 11,
3838
x3: 12,
39-
}));
39+
};
4040
let p2: Value<POD> = Rc::new(RefCell::new(POD {
41-
x1: { (*p1.borrow()).x1 },
42-
x2: { (*p1.borrow()).x2 },
43-
x3: { (*p1.borrow()).x3 },
41+
x1: p1.x1,
42+
x2: p1.x2,
43+
x3: p1.x3,
4444
}));
4545
({ PODIncrement_0(p2.as_pointer()) });
4646
assert!(((({ (*p2.borrow()).x1 } + { (*p2.borrow()).x2 }) + { (*p2.borrow()).x3 }) == 39));

0 commit comments

Comments
 (0)