Skip to content

Commit 9977866

Browse files
authored
Emit clang-computed sizeof values in refcount (#490)
1 parent 2717e9d commit 9977866

4 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ bool RustSizeDivergesFromC(clang::QualType qt) {
347347
if (auto *arr = qt->getAsArrayTypeUnsafe()) {
348348
return RustSizeDivergesFromC(arr->getElementType());
349349
}
350+
if (qt->isPointerType()) {
351+
return true;
352+
}
350353
return false;
351354
}
352355

‎tests/unit/out/refcount/types.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ use std::io::prelude::*;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
9+
const _: () = assert!(
10+
(::std::mem::size_of::<usize>() == 8usize),
11+
"sizeof(std::size_t) == sizeof(void *)"
12+
);
13+
const _: () = assert!((8usize == 8_usize), "sizeof(int *) == 8");
914
pub fn main() {
1015
__cpp2rust_init_globals();
1116
std::process::exit(main_0());
@@ -42,6 +47,8 @@ fn main_0() -> i32 {
4247
assert!(((xld * 2_f64) == 3_f64));
4348
assert!(((((xwc + (xc8 as i32)) + (xc16 as i32)) as u32).wrapping_add(xc32) == 266_u32));
4449
assert!((AnyPtr::default()).is_null());
50+
let mut ip: Ptr<i32> = Ptr::<i32>::null();
51+
assert!((8usize == 8usize));
4552
return 0;
4653
}
4754
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/types.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ use std::collections::BTreeMap;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
9+
const _: () = assert!(
10+
((::std::mem::size_of::<usize>()) == (::std::mem::size_of::<*mut ::libc::c_void>())),
11+
"sizeof(std::size_t) == sizeof(void *)"
12+
);
13+
const _: () = assert!(
14+
((::std::mem::size_of::<*mut i32>()) == (8_usize)),
15+
"sizeof(int *) == 8"
16+
);
917
pub fn main() {
1018
unsafe {
1119
__cpp2rust_init_globals();
@@ -44,6 +52,10 @@ unsafe fn main_0() -> i32 {
4452
assert!((((xld) * (2_f64)) == (3_f64)));
4553
assert!(((((((xwc) + (xc8 as i32)) + (xc16 as i32)) as u32).wrapping_add(xc32)) == (266_u32)));
4654
assert!((std::ptr::null_mut::<::libc::c_void>()).is_null());
55+
let mut ip: *mut i32 = std::ptr::null_mut();
56+
assert!(
57+
((::std::mem::size_of::<*mut i32>()) == (::std::mem::size_of::<*mut ::libc::c_void>()))
58+
);
4759
return 0;
4860
}
4961
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/types.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <cstdint>
66
#include <inttypes.h>
77

8+
static_assert(sizeof(std::size_t) == sizeof(void *), "");
9+
static_assert(sizeof(int *) == 8, "");
10+
811
int main() {
912
const uint8_t xu8 = 8;
1013
const uint16_t xu16 = 16;
@@ -27,5 +30,7 @@ int main() {
2730
assert(xld * 2 == 3);
2831
assert(xwc + xc8 + xc16 + xc32 == 266);
2932
assert(xnp == nullptr);
33+
int *ip = nullptr;
34+
assert(sizeof(ip) == sizeof(void *));
3035
return 0;
3136
}

0 commit comments

Comments
 (0)