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
1 change: 1 addition & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,7 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
return false;
}
if (expr->getCastKind() == clang::CK_NullToPointer) {
PushConversionKind push(*this, ConversionKind::Unboxed);
StrCat(GetDefaultAsString(expr->getType()));
computed_expr_type_ = ComputedExprType::FreshPointer;
return false;
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/out/refcount/ptr_cast_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,30 @@ fn main_0() -> i32 {
let hp: Value<Ptr<header>> = Rc::new(RefCell::new((h.as_pointer())));
let v: Value<Ptr<view>> = Rc::new(RefCell::new((*hp.borrow()).reinterpret_cast::<view>()));
assert!(((((*(*(*v.borrow()).upgrade().deref()).tag.borrow()) == 7) as i32) != 0));
let data: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::from(*b"hi\0")));
let vp: Value<AnyPtr> = Rc::new(RefCell::new(
((data.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any(),
));
let n: Value<i32> = Rc::new(RefCell::new(2));
let sel: Value<Ptr<u8>> = Rc::new(RefCell::new(
if ((((*n.borrow()) < 100) as i32) != 0) {
(*vp.borrow()).clone()
} else {
(AnyPtr::default())
}
.reinterpret_cast::<u8>(),
));
assert!((((!((*sel.borrow()).is_null())) as i32) != 0));
assert!(
((((((*sel.borrow()).offset((0) as isize).read()) as i32) == ('h' as i32)) as i32) != 0)
);
(*n.borrow_mut()) = 200;
(*sel.borrow_mut()) = if ((((*n.borrow()) < 100) as i32) != 0) {
(*vp.borrow()).clone()
} else {
(AnyPtr::default())
}
.reinterpret_cast::<u8>();
assert!(((((*sel.borrow()).is_null()) as i32) != 0));
return 0;
}
18 changes: 18 additions & 0 deletions tests/unit/out/unsafe/ptr_cast_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,23 @@ unsafe fn main_0() -> i32 {
let mut hp: *mut header = (&mut h as *mut header);
let mut v: *mut view = (hp as *mut view);
assert!((((((*v).tag) == (7)) as i32) != 0));
let mut data: [libc::c_char; 3] = std::mem::transmute(*b"hi\0");
let mut vp: *mut ::libc::c_void =
(data.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void);
let mut n: i32 = 2;
let mut sel: *mut libc::c_char = (if ((((n) < (100)) as i32) != 0) {
vp
} else {
(0 as *mut ::libc::c_void)
} as *mut libc::c_char);
assert!((((!((sel).is_null())) as i32) != 0));
assert!((((((*sel.offset((0) as isize)) as i32) == ('h' as i32)) as i32) != 0));
n = 200;
sel = (if ((((n) < (100)) as i32) != 0) {
vp
} else {
(0 as *mut ::libc::c_void)
} as *mut libc::c_char);
assert!(((((sel).is_null()) as i32) != 0));
return 0;
}
11 changes: 11 additions & 0 deletions tests/unit/ptr_cast_init.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <assert.h>
#include <stddef.h>

struct header {
int tag;
Expand All @@ -20,5 +21,15 @@ int main(void) {
struct header *hp = &h;
struct view *v = (struct view *)hp;
assert(v->tag == 7);

char data[] = "hi";
void *vp = data;
int n = 2;
char *sel = (n < 100) ? vp : NULL;
assert(sel != 0);
assert(sel[0] == 'h');
n = 200;
sel = (n < 100) ? vp : NULL;
assert(sel == 0);
return 0;
}
Loading