diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index aa0a46aa..d88d0b30 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -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); diff --git a/tests/unit/out/refcount/ptr_cast_init.rs b/tests/unit/out/refcount/ptr_cast_init.rs new file mode 100644 index 00000000..1929e905 --- /dev/null +++ b/tests/unit/out/refcount/ptr_cast_init.rs @@ -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, + pub size: Value, +} +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(::from_bytes(&buf[0..4]))), + size: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} +#[derive(Default)] +pub struct view { + pub tag: Value, +} +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(::from_bytes(&buf[0..4]))), + } + } +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let text: Value> = Rc::new(RefCell::new(Box::from(*b"hi\0"))); + let cp: Value> = Rc::new(RefCell::new((text.as_pointer() as Ptr))); + let u: Value> = Rc::new(RefCell::new( + ((*cp.borrow()).reinterpret_cast::()).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
= Rc::new(RefCell::new(header { + tag: Rc::new(RefCell::new(7)), + size: Rc::new(RefCell::new(32)), + })); + let hp: Value> = Rc::new(RefCell::new((h.as_pointer()))); + let v: Value> = Rc::new(RefCell::new( + ((*hp.borrow()).reinterpret_cast::()).clone(), + )); + assert!(((((*(*(*v.borrow()).upgrade().deref()).tag.borrow()) == 7) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/unsafe/ptr_cast_init.rs b/tests/unit/out/unsafe/ptr_cast_init.rs new file mode 100644 index 00000000..f61bd869 --- /dev/null +++ b/tests/unit/out/unsafe/ptr_cast_init.rs @@ -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; +} diff --git a/tests/unit/ptr_cast_init.c b/tests/unit/ptr_cast_init.c new file mode 100644 index 00000000..78543c61 --- /dev/null +++ b/tests/unit/ptr_cast_init.c @@ -0,0 +1,24 @@ +#include + +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; +}