Skip to content

Commit 2b164e3

Browse files
committed
Hoist fwrite/fread args to avoid borrow errors
1 parent 942d599 commit 2b164e3

6 files changed

Lines changed: 74 additions & 48 deletions

File tree

rules/stdio/ir_refcount.json

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@
431431
"f5": {
432432
"body": [
433433
{
434-
"text": "libcc2rs::fread_refcount("
434+
"text": "let __a0 = "
435435
},
436436
{
437437
"placeholder": {
@@ -440,7 +440,7 @@
440440
}
441441
},
442442
{
443-
"text": ", "
443+
"text": ";\n let __a1 = "
444444
},
445445
{
446446
"placeholder": {
@@ -449,7 +449,7 @@
449449
}
450450
},
451451
{
452-
"text": ", "
452+
"text": ";\n let __a2 = "
453453
},
454454
{
455455
"placeholder": {
@@ -458,7 +458,7 @@
458458
}
459459
},
460460
{
461-
"text": ", "
461+
"text": ";\n let __a3 = "
462462
},
463463
{
464464
"method_call": {
@@ -478,9 +478,10 @@
478478
}
479479
},
480480
{
481-
"text": ")"
481+
"text": ";\n libcc2rs::fread_refcount(__a0, __a1, __a2, __a3)"
482482
}
483483
],
484+
"multi_statement": true,
484485
"params": {
485486
"a0": {
486487
"type": "AnyPtr"
@@ -503,7 +504,7 @@
503504
"f6": {
504505
"body": [
505506
{
506-
"text": "libcc2rs::fwrite_refcount("
507+
"text": "let __a0 = "
507508
},
508509
{
509510
"placeholder": {
@@ -512,7 +513,7 @@
512513
}
513514
},
514515
{
515-
"text": ", "
516+
"text": ";\n let __a1 = "
516517
},
517518
{
518519
"placeholder": {
@@ -521,7 +522,7 @@
521522
}
522523
},
523524
{
524-
"text": ", "
525+
"text": ";\n let __a2 = "
525526
},
526527
{
527528
"placeholder": {
@@ -530,7 +531,7 @@
530531
}
531532
},
532533
{
533-
"text": ", "
534+
"text": ";\n let __a3 = "
534535
},
535536
{
536537
"method_call": {
@@ -550,9 +551,10 @@
550551
}
551552
},
552553
{
553-
"text": ")"
554+
"text": ";\n libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3)"
554555
}
555556
],
557+
"multi_statement": true,
556558
"params": {
557559
"a0": {
558560
"type": "AnyPtr"

rules/stdio/ir_unsafe.json

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@
488488
"f5": {
489489
"body": [
490490
{
491-
"text": "libcc2rs::fread_unsafe("
491+
"text": "let __a0 = "
492492
},
493493
{
494494
"placeholder": {
@@ -497,7 +497,7 @@
497497
}
498498
},
499499
{
500-
"text": " as *mut ::std::ffi::c_void, "
500+
"text": " as *mut ::std::ffi::c_void;\n let __a1 = "
501501
},
502502
{
503503
"placeholder": {
@@ -506,7 +506,7 @@
506506
}
507507
},
508508
{
509-
"text": ", "
509+
"text": ";\n let __a2 = "
510510
},
511511
{
512512
"placeholder": {
@@ -515,7 +515,7 @@
515515
}
516516
},
517517
{
518-
"text": ", "
518+
"text": ";\n let __a3 = "
519519
},
520520
{
521521
"placeholder": {
@@ -524,9 +524,10 @@
524524
}
525525
},
526526
{
527-
"text": ")"
527+
"text": ";\n libcc2rs::fread_unsafe(__a0, __a1, __a2, __a3)"
528528
}
529529
],
530+
"multi_statement": true,
530531
"params": {
531532
"a0": {
532533
"type": "*mut ::libc::c_void",
@@ -550,7 +551,7 @@
550551
"f6": {
551552
"body": [
552553
{
553-
"text": "libcc2rs::fwrite_unsafe("
554+
"text": "let __a0 = "
554555
},
555556
{
556557
"placeholder": {
@@ -559,7 +560,7 @@
559560
}
560561
},
561562
{
562-
"text": " as *const ::std::ffi::c_void, "
563+
"text": " as *const ::std::ffi::c_void;\n let __a1 = "
563564
},
564565
{
565566
"placeholder": {
@@ -568,7 +569,7 @@
568569
}
569570
},
570571
{
571-
"text": ", "
572+
"text": ";\n let __a2 = "
572573
},
573574
{
574575
"placeholder": {
@@ -577,7 +578,7 @@
577578
}
578579
},
579580
{
580-
"text": ", "
581+
"text": ";\n let __a3 = "
581582
},
582583
{
583584
"placeholder": {
@@ -586,9 +587,10 @@
586587
}
587588
},
588589
{
589-
"text": ")"
590+
"text": ";\n libcc2rs::fwrite_unsafe(__a0, __a1, __a2, __a3)"
590591
}
591592
],
593+
"multi_statement": true,
592594
"params": {
593595
"a0": {
594596
"type": "*const ::libc::c_void",

rules/stdio/tgt_refcount.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,19 @@ fn f4(a0: &mut ::std::fs::File, a1: i64, a2: i32) -> i32 {
5656
}
5757

5858
fn f5(a0: AnyPtr, a1: u64, a2: u64, a3: Ptr<::std::fs::File>) -> u64 {
59-
libcc2rs::fread_refcount(a0, a1, a2, a3.clone())
59+
let __a0 = a0;
60+
let __a1 = a1;
61+
let __a2 = a2;
62+
let __a3 = a3.clone();
63+
libcc2rs::fread_refcount(__a0, __a1, __a2, __a3)
6064
}
6165

6266
fn f6(a0: AnyPtr, a1: u64, a2: u64, a3: Ptr<::std::fs::File>) -> u64 {
63-
libcc2rs::fwrite_refcount(a0, a1, a2, a3.clone())
67+
let __a0 = a0;
68+
let __a1 = a1;
69+
let __a2 = a2;
70+
let __a3 = a3.clone();
71+
libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3)
6472
}
6573

6674
fn f7(a0: Ptr<::std::fs::File>) -> i32 {

rules/stdio/tgt_unsafe.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,19 @@ unsafe fn f4(a0: *mut ::std::fs::File, a1: i64, a2: i32) -> i32 {
6666
}
6767

6868
unsafe fn f5(a0: *mut ::libc::c_void, a1: u64, a2: u64, a3: *mut ::std::fs::File) -> u64 {
69-
libcc2rs::fread_unsafe(a0 as *mut ::std::ffi::c_void, a1, a2, a3)
69+
let __a0 = a0 as *mut ::std::ffi::c_void;
70+
let __a1 = a1;
71+
let __a2 = a2;
72+
let __a3 = a3;
73+
libcc2rs::fread_unsafe(__a0, __a1, __a2, __a3)
7074
}
7175

7276
unsafe fn f6(a0: *const ::libc::c_void, a1: u64, a2: u64, a3: *mut ::std::fs::File) -> u64 {
73-
libcc2rs::fwrite_unsafe(a0 as *const ::std::ffi::c_void, a1, a2, a3)
77+
let __a0 = a0 as *const ::std::ffi::c_void;
78+
let __a1 = a1;
79+
let __a2 = a2;
80+
let __a3 = a3;
81+
libcc2rs::fwrite_unsafe(__a0, __a1, __a2, __a3)
7482
}
7583

7684
unsafe fn f7(a0: *mut ::std::fs::File) -> i32 {

tests/unit/out/refcount/fn_ptr_stdlib_compare.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,13 @@ fn main_0() -> i32 {
102102
);
103103
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any().clone()
104104
};
105-
let n: Value<u64> = Rc::new(RefCell::new(libcc2rs::fread_refcount(
106-
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any(),
107-
1_u64,
108-
10_u64,
109-
(*stream.borrow()).clone(),
110-
)));
105+
let n: Value<u64> = Rc::new(RefCell::new({
106+
let __a0 = ((buf.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any();
107+
let __a1 = 1_u64;
108+
let __a2 = 10_u64;
109+
let __a3 = (*stream.borrow()).clone();
110+
libcc2rs::fread_refcount(__a0, __a1, __a2, __a3)
111+
}));
111112
assert!(((*n.borrow()) == 10_u64));
112113
let i: Value<i32> = Rc::new(RefCell::new(0));
113114
'loop_: while ((*i.borrow()) < 10) {
@@ -267,12 +268,13 @@ fn main_0() -> i32 {
267268
);
268269
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any().clone()
269270
};
270-
let n: Value<u64> = Rc::new(RefCell::new(libcc2rs::fwrite_refcount(
271-
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any(),
272-
1_u64,
273-
10_u64,
274-
(*stream.borrow()).clone(),
275-
)));
271+
let n: Value<u64> = Rc::new(RefCell::new({
272+
let __a0 = ((buf.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any();
273+
let __a1 = 1_u64;
274+
let __a2 = 10_u64;
275+
let __a3 = (*stream.borrow()).clone();
276+
libcc2rs::fwrite_refcount(__a0, __a1, __a2, __a3)
277+
}));
276278
assert!(((*n.borrow()) == 10_u64));
277279
{
278280
(*stream.borrow()).delete();

tests/unit/out/unsafe/fn_ptr_stdlib_compare.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,14 @@ unsafe fn main_0() -> i32 {
9595
}
9696
(buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void)
9797
};
98-
let mut n: u64 = libcc2rs::fread_unsafe(
99-
(buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void) as *mut ::std::ffi::c_void,
100-
1_u64,
101-
10_u64,
102-
stream,
103-
);
98+
let mut n: u64 = {
99+
let __a0 =
100+
(buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void) as *mut ::std::ffi::c_void;
101+
let __a1 = 1_u64;
102+
let __a2 = 10_u64;
103+
let __a3 = stream;
104+
libcc2rs::fread_unsafe(__a0, __a1, __a2, __a3)
105+
};
104106
assert!(((n) == (10_u64)));
105107
let mut i: i32 = 0;
106108
'loop_: while ((i) < (10)) {
@@ -250,12 +252,14 @@ unsafe fn main_0() -> i32 {
250252
}
251253
(buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void)
252254
};
253-
let mut n: u64 = libcc2rs::fwrite_unsafe(
254-
(buf.as_mut_ptr() as *const u8 as *const ::libc::c_void) as *const ::std::ffi::c_void,
255-
1_u64,
256-
10_u64,
257-
stream,
258-
);
255+
let mut n: u64 = {
256+
let __a0 = (buf.as_mut_ptr() as *const u8 as *const ::libc::c_void)
257+
as *const ::std::ffi::c_void;
258+
let __a1 = 1_u64;
259+
let __a2 = 10_u64;
260+
let __a3 = stream;
261+
libcc2rs::fwrite_unsafe(__a0, __a1, __a2, __a3)
262+
};
259263
assert!(((n) == (10_u64)));
260264
{
261265
Box::from_raw(stream);

0 commit comments

Comments
 (0)