Skip to content

Commit 0872412

Browse files
committed
Fix boxing in ConvertPointeeType
1 parent 6b0264a commit 0872412

4 files changed

Lines changed: 143 additions & 0 deletions

File tree

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,7 @@ std::string ConverterRefCount::ConvertMappedMethodCall(
24592459

24602460
std::string ConverterRefCount::ConvertPointeeType(clang::QualType ptr_type) {
24612461
assert(!ptr_type.isNull() && ptr_type->isPointerType());
2462+
PushConversionKind push(*this, ConversionKind::Unboxed);
24622463
auto pointee = ptr_type->getPointeeType();
24632464
if (!pointee->isRecordType()) {
24642465
return ToString(pointee);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
#[derive(Default)]
10+
pub struct header {
11+
pub tag: Value<i32>,
12+
pub size: Value<i32>,
13+
}
14+
impl Clone for header {
15+
fn clone(&self) -> Self {
16+
Self {
17+
tag: Rc::new(RefCell::new((*self.tag.borrow()).clone())),
18+
size: Rc::new(RefCell::new((*self.size.borrow()).clone())),
19+
}
20+
}
21+
}
22+
impl ByteRepr for header {
23+
fn byte_size() -> usize {
24+
8
25+
}
26+
fn to_bytes(&self, buf: &mut [u8]) {
27+
(*self.tag.borrow()).to_bytes(&mut buf[0..4]);
28+
(*self.size.borrow()).to_bytes(&mut buf[4..8]);
29+
}
30+
fn from_bytes(buf: &[u8]) -> Self {
31+
Self {
32+
tag: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
33+
size: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))),
34+
}
35+
}
36+
}
37+
#[derive(Default)]
38+
pub struct view {
39+
pub tag: Value<i32>,
40+
}
41+
impl Clone for view {
42+
fn clone(&self) -> Self {
43+
Self {
44+
tag: Rc::new(RefCell::new((*self.tag.borrow()).clone())),
45+
}
46+
}
47+
}
48+
impl ByteRepr for view {
49+
fn byte_size() -> usize {
50+
4
51+
}
52+
fn to_bytes(&self, buf: &mut [u8]) {
53+
(*self.tag.borrow()).to_bytes(&mut buf[0..4]);
54+
}
55+
fn from_bytes(buf: &[u8]) -> Self {
56+
Self {
57+
tag: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
58+
}
59+
}
60+
}
61+
pub fn main() {
62+
std::process::exit(main_0());
63+
}
64+
fn main_0() -> i32 {
65+
let text: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::from(*b"hi\0")));
66+
let cp: Value<Ptr<u8>> = Rc::new(RefCell::new((text.as_pointer() as Ptr<u8>)));
67+
let u: Value<Ptr<u8>> = Rc::new(RefCell::new(
68+
((*cp.borrow()).reinterpret_cast::<u8>()).clone(),
69+
));
70+
assert!(((((((*u.borrow()).offset((0) as isize).read()) as i32) == ('h' as i32)) as i32) != 0));
71+
assert!(((((((*u.borrow()).offset((1) as isize).read()) as i32) == ('i' as i32)) as i32) != 0));
72+
let h: Value<header> = Rc::new(RefCell::new(header {
73+
tag: Rc::new(RefCell::new(7)),
74+
size: Rc::new(RefCell::new(32)),
75+
}));
76+
let hp: Value<Ptr<header>> = Rc::new(RefCell::new((h.as_pointer())));
77+
let v: Value<Ptr<view>> = Rc::new(RefCell::new(
78+
((*hp.borrow()).reinterpret_cast::<view>()).clone(),
79+
));
80+
assert!(((((*(*(*v.borrow()).upgrade().deref()).tag.borrow()) == 7) as i32) != 0));
81+
return 0;
82+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
#[repr(C)]
10+
#[derive(Copy, Clone, Default)]
11+
pub struct header {
12+
pub tag: i32,
13+
pub size: i32,
14+
}
15+
#[repr(C)]
16+
#[derive(Copy, Clone, Default)]
17+
pub struct view {
18+
pub tag: i32,
19+
}
20+
pub fn main() {
21+
unsafe {
22+
std::process::exit(main_0() as i32);
23+
}
24+
}
25+
unsafe fn main_0() -> i32 {
26+
let text: [libc::c_char; 3] = std::mem::transmute(*b"hi\0");
27+
let mut cp: *const libc::c_char = text.as_ptr();
28+
let mut u: *mut u8 = (cp as *mut u8);
29+
assert!((((((*u.offset((0) as isize)) as i32) == ('h' as i32)) as i32) != 0));
30+
assert!((((((*u.offset((1) as isize)) as i32) == ('i' as i32)) as i32) != 0));
31+
let mut h: header = header { tag: 7, size: 32 };
32+
let mut hp: *mut header = (&mut h as *mut header);
33+
let mut v: *mut view = (hp as *mut view);
34+
assert!((((((*v).tag) == (7)) as i32) != 0));
35+
return 0;
36+
}

tests/unit/ptr_cast_init.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <assert.h>
2+
3+
struct header {
4+
int tag;
5+
int size;
6+
};
7+
8+
struct view {
9+
int tag;
10+
};
11+
12+
int main(void) {
13+
const char text[] = "hi";
14+
const char *cp = text;
15+
unsigned char *u = (unsigned char *)cp;
16+
assert(u[0] == 'h');
17+
assert(u[1] == 'i');
18+
19+
struct header h = {7, 32};
20+
struct header *hp = &h;
21+
struct view *v = (struct view *)hp;
22+
assert(v->tag == 7);
23+
return 0;
24+
}

0 commit comments

Comments
 (0)