Skip to content

Commit 8fc603c

Browse files
committed
Translate explicit bool cast
1 parent 4c52021 commit 8fc603c

7 files changed

Lines changed: 108 additions & 0 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,6 +1944,13 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
19441944
ConvertIntegerToEnumeralCast(expr, sub_expr);
19451945
return false;
19461946
}
1947+
if (type->isBooleanType() && sub_expr->getType()->isIntegerType() &&
1948+
!sub_expr->getType()->isBooleanType()) {
1949+
PushParen paren(*this);
1950+
Convert(sub_expr);
1951+
StrCat(token::kDiff, token::kZero);
1952+
return false;
1953+
}
19471954
{
19481955
PushParen paren(*this);
19491956
Convert(sub_expr);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <assert.h>
2+
3+
int main() {
4+
unsigned int flag = 7;
5+
bool b1 = (bool)flag;
6+
bool b2 = (bool)0u;
7+
assert(b1);
8+
assert(!b2);
9+
return 0;
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <assert.h>
2+
#include <stdbool.h>
3+
4+
int main() {
5+
unsigned int flag = 7;
6+
bool b1 = (bool)flag;
7+
bool b2 = (bool)0u;
8+
assert(b1);
9+
assert(!b2);
10+
return 0;
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
let flag: Value<u32> = Rc::new(RefCell::new(7_u32));
14+
let b1: Value<bool> = Rc::new(RefCell::new(((*flag.borrow()) != 0)));
15+
let b2: Value<bool> = Rc::new(RefCell::new((0_u32 != 0)));
16+
assert!((*b1.borrow()));
17+
assert!(!(*b2.borrow()));
18+
return 0;
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
let flag: Value<u32> = Rc::new(RefCell::new(7_u32));
14+
let b1: Value<bool> = Rc::new(RefCell::new(((*flag.borrow()) != 0)));
15+
let b2: Value<bool> = Rc::new(RefCell::new((0_u32 != 0)));
16+
assert!((*b1.borrow()));
17+
assert!(((!(*b2.borrow()) as i32) != 0));
18+
return 0;
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub fn main() {
10+
unsafe {
11+
std::process::exit(main_0() as i32);
12+
}
13+
}
14+
unsafe fn main_0() -> i32 {
15+
let mut flag: u32 = 7_u32;
16+
let mut b1: bool = (flag != 0);
17+
let mut b2: bool = (0_u32 != 0);
18+
assert!(b1);
19+
assert!(!b2);
20+
return 0;
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub fn main() {
10+
unsafe {
11+
std::process::exit(main_0() as i32);
12+
}
13+
}
14+
unsafe fn main_0() -> i32 {
15+
let mut flag: u32 = 7_u32;
16+
let mut b1: bool = (flag != 0);
17+
let mut b2: bool = (0_u32 != 0);
18+
assert!(b1);
19+
assert!(((!b2 as i32) != 0));
20+
return 0;
21+
}

0 commit comments

Comments
 (0)