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
1 change: 1 addition & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2459,6 +2459,7 @@ std::string ConverterRefCount::ConvertMappedMethodCall(

std::string ConverterRefCount::ConvertPointeeType(clang::QualType ptr_type) {
assert(!ptr_type.isNull() && ptr_type->isPointerType());
PushConversionKind push(*this, ConversionKind::Unboxed);
auto pointee = ptr_type->getPointeeType();
if (!pointee->isRecordType()) {
return ToString(pointee);
Expand Down
82 changes: 82 additions & 0 deletions tests/unit/out/refcount/ptr_cast_init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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};
#[derive(Default)]
pub struct header {
pub tag: Value<i32>,
pub size: Value<i32>,
}
impl Clone for header {
fn clone(&self) -> Self {
Self {
tag: Rc::new(RefCell::new((*self.tag.borrow()).clone())),
size: Rc::new(RefCell::new((*self.size.borrow()).clone())),
}
}
}
impl ByteRepr for header {
fn byte_size() -> usize {
8
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.tag.borrow()).to_bytes(&mut buf[0..4]);
(*self.size.borrow()).to_bytes(&mut buf[4..8]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
tag: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
size: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))),
}
}
}
#[derive(Default)]
pub struct view {
pub tag: Value<i32>,
}
impl Clone for view {
fn clone(&self) -> Self {
Self {
tag: Rc::new(RefCell::new((*self.tag.borrow()).clone())),
}
}
}
impl ByteRepr for view {
fn byte_size() -> usize {
4
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.tag.borrow()).to_bytes(&mut buf[0..4]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
tag: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
}
}
}
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let text: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::from(*b"hi\0")));
let cp: Value<Ptr<u8>> = Rc::new(RefCell::new((text.as_pointer() as Ptr<u8>)));
let u: Value<Ptr<u8>> = Rc::new(RefCell::new(
((*cp.borrow()).reinterpret_cast::<u8>()).clone(),
));
assert!(((((((*u.borrow()).offset((0) as isize).read()) as i32) == ('h' as i32)) as i32) != 0));
assert!(((((((*u.borrow()).offset((1) as isize).read()) as i32) == ('i' as i32)) as i32) != 0));
let h: Value<header> = Rc::new(RefCell::new(header {
tag: Rc::new(RefCell::new(7)),
size: Rc::new(RefCell::new(32)),
}));
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>()).clone(),
));
assert!(((((*(*(*v.borrow()).upgrade().deref()).tag.borrow()) == 7) as i32) != 0));
return 0;
}
36 changes: 36 additions & 0 deletions tests/unit/out/unsafe/ptr_cast_init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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;
#[repr(C)]
#[derive(Copy, Clone, Default)]
pub struct header {
pub tag: i32,
pub size: i32,
}
#[repr(C)]
#[derive(Copy, Clone, Default)]
pub struct view {
pub tag: i32,
}
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let text: [libc::c_char; 3] = std::mem::transmute(*b"hi\0");
let mut cp: *const libc::c_char = text.as_ptr();
let mut u: *mut u8 = (cp as *mut u8);
assert!((((((*u.offset((0) as isize)) as i32) == ('h' as i32)) as i32) != 0));
assert!((((((*u.offset((1) as isize)) as i32) == ('i' as i32)) as i32) != 0));
let mut h: header = header { tag: 7, size: 32 };
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));
return 0;
}
24 changes: 24 additions & 0 deletions tests/unit/ptr_cast_init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <assert.h>

struct header {
int tag;
int size;
};

struct view {
int tag;
};

int main(void) {
const char text[] = "hi";
const char *cp = text;
unsigned char *u = (unsigned char *)cp;
assert(u[0] == 'h');
assert(u[1] == 'i');

struct header h = {7, 32};
struct header *hp = &h;
struct view *v = (struct view *)hp;
assert(v->tag == 7);
return 0;
}
Loading