Skip to content
Open
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
20 changes: 9 additions & 11 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1050,22 +1050,20 @@ bool Converter::VisitWhileStmt(clang::WhileStmt *stmt) {

bool Converter::VisitDoStmt(clang::DoStmt *stmt) {
PushBreakTarget push(break_target_, BreakTarget::Loop);
StrCat("'loop_:");
StrCat(keyword::kLoop);
const char *control_var = "__do_while";
StrCat(keyword::kLet, "mut", control_var, token::kAssign, keyword::kTrue,
token::kSemiColon);
StrCat("'loop_:", keyword::kWhile, control_var, "||");
{
PushParen paren(*this);
ConvertCondition(stmt->getCond());
}
{
PushBrace loop_brace(*this);
StrCat(control_var, token::kAssign, keyword::kFalse, token::kSemiColon);
curr_for_inc_.emplace_back(nullptr);
Convert(stmt->getBody());
curr_for_inc_.pop_back();
StrCat(keyword::kIf, token::kNot);
{
PushParen paren(*this);
ConvertCondition(stmt->getCond());
}
{
PushBrace if_brace(*this);
StrCat(keyword::kBreak, token::kSemiColon);
}
}
return false;
}
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/do_while_continue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <assert.h>

static int run(void) {
int i = 0;
int runs = 0;
do {
runs += 1;
i += 1;
if (i == 4) {
continue;
}
} while (i < 4);
return runs;
}

static int nested(void) {
int oi = 0;
int runs = 0;
do {
oi += 1;
int ii = 0;
do {
runs += 1;
ii += 1;
if (ii == 3) {
continue;
}
} while (ii < 3);
} while (oi < 2);
return runs;
}

int main(void) {
assert(run() == 4);
assert(nested() == 6);
return 0;
}
50 changes: 50 additions & 0 deletions tests/unit/out/refcount/do_while_continue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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 run_0() -> i32 {
let i: Value<i32> = Rc::new(RefCell::new(0));
let runs: Value<i32> = Rc::new(RefCell::new(0));
let mut __do_while = true;
'loop_: while __do_while || ((((*i.borrow()) < 4) as i32) != 0) {
__do_while = false;
(*runs.borrow_mut()) += 1;
(*i.borrow_mut()) += 1;
if ((((*i.borrow()) == 4) as i32) != 0) {
continue 'loop_;
}
}
return (*runs.borrow());
}
pub fn nested_1() -> i32 {
let oi: Value<i32> = Rc::new(RefCell::new(0));
let runs: Value<i32> = Rc::new(RefCell::new(0));
let mut __do_while = true;
'loop_: while __do_while || ((((*oi.borrow()) < 2) as i32) != 0) {
__do_while = false;
(*oi.borrow_mut()) += 1;
let ii: Value<i32> = Rc::new(RefCell::new(0));
let mut __do_while = true;
'loop_: while __do_while || ((((*ii.borrow()) < 3) as i32) != 0) {
__do_while = false;
(*runs.borrow_mut()) += 1;
(*ii.borrow_mut()) += 1;
if ((((*ii.borrow()) == 3) as i32) != 0) {
continue 'loop_;
}
}
}
return (*runs.borrow());
}
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
assert!((((({ run_0() }) == 4) as i32) != 0));
assert!((((({ nested_1() }) == 6) as i32) != 0));
return 0;
}
14 changes: 6 additions & 8 deletions tests/unit/out/refcount/dowhile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn dowhile_0(x: i32) -> i32 {
let x: Value<i32> = Rc::new(RefCell::new(x));
'loop_: loop {
let mut __do_while = true;
'loop_: while __do_while || ((*x.borrow()) <= 200) {
__do_while = false;
(*x.borrow_mut()) += 1;
'loop_: loop {
let mut __do_while = true;
'loop_: while __do_while || ((*x.borrow()) <= 100) {
__do_while = false;
(*x.borrow_mut()) += 1;
(*x.borrow_mut()) += 1;
if !((*x.borrow()) <= 100) {
break;
}
}
(*x.borrow_mut()) += 1;
if !((*x.borrow()) <= 200) {
break;
}
}
return (*x.borrow());
}
Expand Down
28 changes: 12 additions & 16 deletions tests/unit/out/refcount/fn_ptr_stdlib_compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ fn main_0() -> i32 {
(*(*f3.borrow()))(_arg0, _arg1, _arg2, _arg3)
}) == 22_u64)
);
'loop_: loop {
let mut __do_while = true;
'loop_: while __do_while || (0 != 0) {
__do_while = false;
let stream: Value<Ptr<::std::fs::File>> = Rc::new(RefCell::new(
match Ptr::from_string_literal("rb").to_rust_string() {
v if v == "rb" => std::fs::OpenOptions::new()
Expand Down Expand Up @@ -124,11 +126,10 @@ fn main_0() -> i32 {
(*stream.borrow()).delete();
0
};
if !(0 != 0) {
break;
}
}
'loop_: loop {
let mut __do_while = true;
'loop_: while __do_while || (0 != 0) {
__do_while = false;
let stream: Value<Ptr<::std::fs::File>> = Rc::new(RefCell::new(
match Ptr::from_string_literal("rb").to_rust_string() {
v if v == "rb" => std::fs::OpenOptions::new()
Expand Down Expand Up @@ -181,9 +182,6 @@ fn main_0() -> i32 {
(*stream.borrow()).delete();
0
};
if !(0 != 0) {
break;
}
}
let gn1: Value<FnPtr<fn(AnyPtr, u64, u64, Ptr<::std::fs::File>) -> u64>> =
Rc::new(RefCell::new(FnPtr::<
Expand Down Expand Up @@ -239,7 +237,9 @@ fn main_0() -> i32 {
(*(*g3.borrow()))(_arg0, _arg1, _arg2, _arg3)
}) == 33_u64)
);
'loop_: loop {
let mut __do_while = true;
'loop_: while __do_while || (0 != 0) {
__do_while = false;
let stream: Value<Ptr<::std::fs::File>> = Rc::new(RefCell::new(
match Ptr::from_string_literal("wb").to_rust_string() {
v if v == "rb" => std::fs::OpenOptions::new()
Expand Down Expand Up @@ -280,11 +280,10 @@ fn main_0() -> i32 {
(*stream.borrow()).delete();
0
};
if !(0 != 0) {
break;
}
}
'loop_: loop {
let mut __do_while = true;
'loop_: while __do_while || (0 != 0) {
__do_while = false;
let stream: Value<Ptr<::std::fs::File>> = Rc::new(RefCell::new(
match Ptr::from_string_literal("wb").to_rust_string() {
v if v == "rb" => std::fs::OpenOptions::new()
Expand Down Expand Up @@ -327,9 +326,6 @@ fn main_0() -> i32 {
(*stream.borrow()).delete();
0
};
if !(0 != 0) {
break;
}
}
return 0;
}
7 changes: 3 additions & 4 deletions tests/unit/out/refcount/switch_in_dowhile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ pub fn switch_in_dowhile_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_: loop {
let mut __do_while = true;
'loop_: while __do_while || ((*i.borrow()) < (*n.borrow())) {
__do_while = false;
'switch: {
let __match_cond = (*i.borrow());
match __match_cond {
Expand All @@ -29,9 +31,6 @@ pub fn switch_in_dowhile_0(n: i32) -> i32 {
}
};
(*i.borrow_mut()).prefix_inc();
if !((*i.borrow()) < (*n.borrow())) {
break;
}
}
return (*r.borrow());
}
Expand Down
52 changes: 52 additions & 0 deletions tests/unit/out/unsafe/do_while_continue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub unsafe fn run_0() -> i32 {
let mut i: i32 = 0;
let mut runs: i32 = 0;
let mut __do_while = true;
'loop_: while __do_while || ((((i) < (4)) as i32) != 0) {
__do_while = false;
runs += 1;
i += 1;
if ((((i) == (4)) as i32) != 0) {
continue 'loop_;
}
}
return runs;
}
pub unsafe fn nested_1() -> i32 {
let mut oi: i32 = 0;
let mut runs: i32 = 0;
let mut __do_while = true;
'loop_: while __do_while || ((((oi) < (2)) as i32) != 0) {
__do_while = false;
oi += 1;
let mut ii: i32 = 0;
let mut __do_while = true;
'loop_: while __do_while || ((((ii) < (3)) as i32) != 0) {
__do_while = false;
runs += 1;
ii += 1;
if ((((ii) == (3)) as i32) != 0) {
continue 'loop_;
}
}
}
return runs;
}
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
assert!(((((unsafe { run_0() }) == (4)) as i32) != 0));
assert!(((((unsafe { nested_1() }) == (6)) as i32) != 0));
return 0;
}
14 changes: 6 additions & 8 deletions tests/unit/out/unsafe/dowhile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub unsafe fn dowhile_0(mut x: i32) -> i32 {
'loop_: loop {
let mut __do_while = true;
'loop_: while __do_while || ((x) <= (200)) {
__do_while = false;
x += 1;
'loop_: loop {
let mut __do_while = true;
'loop_: while __do_while || ((x) <= (100)) {
__do_while = false;
x += 1;
x += 1;
if !((x) <= (100)) {
break;
}
}
x += 1;
if !((x) <= (200)) {
break;
}
}
return x;
}
Expand Down
28 changes: 12 additions & 16 deletions tests/unit/out/unsafe/fn_ptr_stdlib_compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ unsafe fn main_0() -> i32 {
(f3).unwrap()(_arg0, _arg1, _arg2, _arg3)
}) == (22_u64))
);
'loop_: loop {
let mut __do_while = true;
'loop_: while __do_while || (0 != 0) {
__do_while = false;
let mut stream: *mut ::libc::FILE = libc::fopen(
b"/dev/zero\0".as_ptr() as *const i8,
b"rb\0".as_ptr() as *const i8,
Expand Down Expand Up @@ -90,11 +92,10 @@ unsafe fn main_0() -> i32 {
i.prefix_inc();
}
libc::fclose(stream);
if !(0 != 0) {
break;
}
}
'loop_: loop {
let mut __do_while = true;
'loop_: while __do_while || (0 != 0) {
__do_while = false;
let mut stream: *mut ::libc::FILE = libc::fopen(
b"/dev/zero\0".as_ptr() as *const i8,
b"rb\0".as_ptr() as *const i8,
Expand Down Expand Up @@ -127,9 +128,6 @@ unsafe fn main_0() -> i32 {
i.prefix_inc();
}
libc::fclose(stream);
if !(0 != 0) {
break;
}
}
let mut gn1: Option<unsafe fn(*const ::libc::c_void, u64, u64, *mut ::libc::FILE) -> u64> =
Some(libcc2rs::fwrite_unsafe);
Expand Down Expand Up @@ -161,7 +159,9 @@ unsafe fn main_0() -> i32 {
(g3).unwrap()(_arg0, _arg1, _arg2, _arg3)
}) == (33_u64))
);
'loop_: loop {
let mut __do_while = true;
'loop_: while __do_while || (0 != 0) {
__do_while = false;
let mut stream: *mut ::libc::FILE = libc::fopen(
b"/dev/null\0".as_ptr() as *const i8,
b"wb\0".as_ptr() as *const i8,
Expand All @@ -183,11 +183,10 @@ unsafe fn main_0() -> i32 {
);
assert!(((n) == (10_u64)));
libc::fclose(stream);
if !(0 != 0) {
break;
}
}
'loop_: loop {
let mut __do_while = true;
'loop_: while __do_while || (0 != 0) {
__do_while = false;
let mut stream: *mut ::libc::FILE = libc::fopen(
b"/dev/null\0".as_ptr() as *const i8,
b"wb\0".as_ptr() as *const i8,
Expand All @@ -211,9 +210,6 @@ unsafe fn main_0() -> i32 {
});
assert!(((n) == (10_u64)));
libc::fclose(stream);
if !(0 != 0) {
break;
}
}
return 0;
}
Loading
Loading