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
7 changes: 7 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,13 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
ConvertIntegerToEnumeralCast(expr, sub_expr);
return false;
}
if (type->isBooleanType() && sub_expr->getType()->isIntegerType() &&
!sub_expr->getType()->isBooleanType()) {
PushParen paren(*this);
Convert(sub_expr);
StrCat(token::kDiff, token::kZero);
return false;
}
{
PushParen paren(*this);
Convert(sub_expr);
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/int_to_bool_explicit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <assert.h>

int main() {
unsigned int flag = 7;
bool b1 = (bool)flag;
bool b2 = (bool)0u;
assert(b1);
assert(!b2);
return 0;
}
11 changes: 11 additions & 0 deletions tests/unit/int_to_bool_explicit_c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <assert.h>
#include <stdbool.h>

int main() {
unsigned int flag = 7;
bool b1 = (bool)flag;
bool b2 = (bool)0u;
assert(b1);
assert(!b2);
return 0;
}
19 changes: 19 additions & 0 deletions tests/unit/out/refcount/int_to_bool_explicit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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 main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let flag: Value<u32> = Rc::new(RefCell::new(7_u32));
let b1: Value<bool> = Rc::new(RefCell::new(((*flag.borrow()) != 0)));
let b2: Value<bool> = Rc::new(RefCell::new((0_u32 != 0)));
assert!((*b1.borrow()));
assert!(!(*b2.borrow()));
return 0;
}
19 changes: 19 additions & 0 deletions tests/unit/out/refcount/int_to_bool_explicit_c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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 main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let flag: Value<u32> = Rc::new(RefCell::new(7_u32));
let b1: Value<bool> = Rc::new(RefCell::new(((*flag.borrow()) != 0)));
let b2: Value<bool> = Rc::new(RefCell::new((0_u32 != 0)));
assert!((*b1.borrow()));
assert!(((!(*b2.borrow()) as i32) != 0));
return 0;
}
21 changes: 21 additions & 0 deletions tests/unit/out/unsafe/int_to_bool_explicit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut flag: u32 = 7_u32;
let mut b1: bool = (flag != 0);
let mut b2: bool = (0_u32 != 0);
assert!(b1);
assert!(!b2);
return 0;
}
21 changes: 21 additions & 0 deletions tests/unit/out/unsafe/int_to_bool_explicit_c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut flag: u32 = 7_u32;
let mut b1: bool = (flag != 0);
let mut b2: bool = (0_u32 != 0);
assert!(b1);
assert!(((!b2 as i32) != 0));
return 0;
}
Loading