Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions tests/lit/lit/formats/Cpp2RustTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __init__(self):
self.regex_xfail = re.compile(r"//\s*XFAIL:\s*(.*)")
self.regex_panic = re.compile(r"//\s*panic\s*(?::\s*(.*))?$", re.MULTILINE)
self.regex_nocompile = re.compile(r"//\s*no-compile\s*(?::\s*(.*))?$", re.MULTILINE)
self.regex_translation_fail = re.compile(r"//\s*translation-fail\s*(?::\s*(.*))?$", re.MULTILINE)
self.regex_nondet_result = re.compile(r"//\s*nondet-result\s*(?::\s*(.*))?$", re.MULTILINE)
self.rust_version = read_rust_version()
os.environ['RUSTFLAGS'] = '-Awarnings -A dangerous-implicit-autorefs'
Expand Down Expand Up @@ -83,6 +84,7 @@ def matches_model(match, model):

should_panic = matches_model(self.regex_panic.search(text), model)
should_not_compile = matches_model(self.regex_nocompile.search(text), model)
should_not_translate = matches_model(self.regex_translation_fail.search(text), model)
is_nondet_result = matches_model(self.regex_nondet_result.search(text), model)

tmp_dir = "tmp/" + fname + "-" + model + "_" + format(random.getrandbits(64), "x")
Expand All @@ -94,10 +96,6 @@ def fail(str, code = fail_code):
shutil.rmtree(tmp_dir, True)
return code, str

expected_file = self.getExpectedFile(filepath, model, fname)
if not os.path.exists(expected_file) and not replace_expected:
return fail('no expected file')

cmd = ['./cpp2rust/cpp2rust', '-file', cc_input, '-model', model,
'-o', rs_file]

Expand All @@ -113,8 +111,14 @@ def fail(str, code = fail_code):
generated = f.read()

if returncode != 0:
if should_not_translate:
return lit.Test.XFAIL, ''
return fail('cpp2rust failed\n' + err)

expected_file = self.getExpectedFile(filepath, model, fname)
if not os.path.exists(expected_file) and not replace_expected:
return fail('no expected file')

if replace_expected:
self.updateExpected(generated, expected_file)

Expand Down
58 changes: 58 additions & 0 deletions tests/unit/out/refcount/switch_basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn basic_0(x: i32) -> i32 {
let x: Value<i32> = Rc::new(RefCell::new(x));
let r: Value<i32> = Rc::new(RefCell::new(0));
'switch: {
let __match_cond = (*x.borrow());
match __match_cond {
v if v == 0 => {
(*r.borrow_mut()) = 10;
break 'switch;
}
v if v == 1 => {
(*r.borrow_mut()) = 20;
break 'switch;
}
v if v == 2 => {
(*r.borrow_mut()) = 30;
break 'switch;
}
_ => {
(*r.borrow_mut()) = 40;
break 'switch;
}
}
};
return (*r.borrow());
}
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
assert!(
(({
let _x: i32 = 0;
basic_0(_x)
}) == 10)
);
assert!(
(({
let _x: i32 = 2;
basic_0(_x)
}) == 30)
);
assert!(
(({
let _x: i32 = 99;
basic_0(_x)
}) == 40)
);
return 0;
}
39 changes: 39 additions & 0 deletions tests/unit/out/refcount/switch_borrow_in_condition_and_in_body.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn borrow_in_condition_and_in_body_0(x: i32) -> i32 {
let x: Value<i32> = Rc::new(RefCell::new(x));
'switch: {
let __match_cond = (*x.borrow());
match __match_cond {
v if v == 0 => {}
_ => {
return ((*x.borrow()) + 1);
}
}
};
panic!("ub: non-void function does not return a value")
}
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
assert!(
(({
let _x: i32 = 0;
borrow_in_condition_and_in_body_0(_x)
}) == 1)
);
assert!(
(({
let _x: i32 = 1;
borrow_in_condition_and_in_body_0(_x)
}) == 2)
);
return 0;
}
75 changes: 75 additions & 0 deletions tests/unit/out/refcount/switch_char.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn switch_char_0(c: u8) -> i32 {
let c: Value<u8> = Rc::new(RefCell::new(c));
'switch: {
let __match_cond = ((*c.borrow()) as i32);
match __match_cond {
v if v == (('a' as u8) as i32) => {
return 1;
}
v if v == (('b' as u8) as i32) => {
return 2;
}
v if v == (('\n' as u8) as i32) => {
return 3;
}
v if v == (('\0' as u8) as i32) => {
return 4;
}
_ => {
return 0;
}
}
};
panic!("ub: non-void function does not return a value")
}
#[derive(Clone, Copy, PartialEq, Debug, Default)]
enum Color {
#[default]
kRed = 0,
kGreen = 1,
kBlue = 2,
}
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
assert!(
(({
let _c: u8 = ('a' as u8);
switch_char_0(_c)
}) == 1)
);
assert!(
(({
let _c: u8 = ('b' as u8);
switch_char_0(_c)
}) == 2)
);
assert!(
(({
let _c: u8 = ('\n' as u8);
switch_char_0(_c)
}) == 3)
);
assert!(
(({
let _c: u8 = ('\0' as u8);
switch_char_0(_c)
}) == 4)
);
assert!(
(({
let _c: u8 = ('z' as u8);
switch_char_0(_c)
}) == 0)
);
return 0;
}
68 changes: 68 additions & 0 deletions tests/unit/out/refcount/switch_complex_cond.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn switch_complex_cond_0(p: Ptr<i32>, bias: i32) -> i32 {
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(p));
let bias: Value<i32> = Rc::new(RefCell::new(bias));
'switch: {
let __match_cond = {
let _lhs = ((*p.borrow()).read());
_lhs + (*bias.borrow())
};
match __match_cond {
v if v == 0 => {
return 1;
}
v if v == 5 => {
return 2;
}
v if v == 10 => {
return 3;
}
_ => {
return 0;
}
}
};
panic!("ub: non-void function does not return a value")
}
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let p_val: Value<i32> = Rc::new(RefCell::new(5));
assert!(
(({
let _p: Ptr<i32> = (p_val.as_pointer());
let _bias: i32 = 0;
switch_complex_cond_0(_p, _bias)
}) == 2)
);
assert!(
(({
let _p: Ptr<i32> = (p_val.as_pointer());
let _bias: i32 = 5;
switch_complex_cond_0(_p, _bias)
}) == 3)
);
assert!(
(({
let _p: Ptr<i32> = (p_val.as_pointer());
let _bias: i32 = -5_i32;
switch_complex_cond_0(_p, _bias)
}) == 1)
);
assert!(
(({
let _p: Ptr<i32> = (p_val.as_pointer());
let _bias: i32 = 99;
switch_complex_cond_0(_p, _bias)
}) == 0)
);
return 0;
}
57 changes: 57 additions & 0 deletions tests/unit/out/refcount/switch_compound_case_body.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn compound_case_body_0(x: i32) -> i32 {
let x: Value<i32> = Rc::new(RefCell::new(x));
let r: Value<i32> = Rc::new(RefCell::new(0));
'switch: {
let __match_cond = (*x.borrow());
match __match_cond {
v if v == 1 => {
let y: Value<i32> = Rc::new(RefCell::new(10));
let z: Value<i32> = Rc::new(RefCell::new(20));
(*r.borrow_mut()) = ((*y.borrow()) + (*z.borrow()));
break 'switch;
}
v if v == 2 => {
let y: Value<i32> = Rc::new(RefCell::new(100));
(*r.borrow_mut()) = ((*y.borrow()) - 1);
break 'switch;
}
_ => {
(*r.borrow_mut()) = -1_i32;
break 'switch;
}
}
};
return (*r.borrow());
}
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
assert!(
(({
let _x: i32 = 1;
compound_case_body_0(_x)
}) == 30)
);
assert!(
(({
let _x: i32 = 2;
compound_case_body_0(_x)
}) == 99)
);
assert!(
(({
let _x: i32 = 9;
compound_case_body_0(_x)
}) == -1_i32)
);
return 0;
}
43 changes: 43 additions & 0 deletions tests/unit/out/refcount/switch_continue_inside_switch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn continue_inside_switch_0(n: i32) -> i32 {
let n: Value<i32> = Rc::new(RefCell::new(n));
let r: Value<i32> = Rc::new(RefCell::new(0));
let i: Value<i32> = Rc::new(RefCell::new(0));
'loop_: while ((*i.borrow()) < (*n.borrow())) {
'switch: {
let __match_cond = (*i.borrow());
match __match_cond {
v if v == 0 || v == 2 || v == 4 => {
(*i.borrow_mut()).prefix_inc();
continue 'loop_;
}
_ => {
(*r.borrow_mut()) += (*i.borrow());
break 'switch;
}
}
};
(*r.borrow_mut()) += 1000;
(*i.borrow_mut()).prefix_inc();
}
return (*r.borrow());
}
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
assert!(
(({
let _n: i32 = 6;
continue_inside_switch_0(_n)
}) == ((((1 + 3) + 5) as i32) + (3 * 1000)))
);
return 0;
}
Loading
Loading