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
22 changes: 22 additions & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1307,8 +1307,30 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
return false;
case clang::Stmt::CStyleCastExprClass:
case clang::Stmt::CXXStaticCastExprClass:
if (expr->getCastKind() == clang::CastKind::CK_PointerToIntegral ||
expr->getCastKind() == clang::CastKind::CK_IntegralToPointer) {
std::string dst_type;
{
PushConversionKind push(*this, ConversionKind::Unboxed);
dst_type = ToString(expr->getType());
}
if (expr->getCastKind() == clang::CastKind::CK_PointerToIntegral) {
StrCat(std::format("{}.to_int::<{}>()", ToString(expr->getSubExpr()),
dst_type));
computed_expr_type_ = ComputedExprType::FreshValue;
} else {
StrCat(std::format("<{}>::from_int({})", dst_type,
ToString(expr->getSubExpr())));
computed_expr_type_ = ComputedExprType::FreshPointer;
}
return false;
}

if (!VisitFunctionPointerCast(expr)) {
return false;
} else if (expr->getSubExpr()->getType()->isVoidPointerType() &&
expr->getType()->isVoidPointerType()) {
return Convert(expr->getSubExpr());
} else if (expr->getSubExpr()->getType()->isVoidPointerType() &&
expr->getType()->isPointerType()) {
Convert(expr->getSubExpr());
Expand Down
24 changes: 24 additions & 0 deletions libcc2rs/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,30 @@ impl<T: ?Sized> AsPointerDyn<T> for Rc<RefCell<T>> {
impl<T: 'static> ByteRepr for Ptr<T> {}
impl ByteRepr for AnyPtr {}

impl<T: 'static> Ptr<T> {
pub fn to_int<U: ByteRepr>(&self) -> U {
let mut buf = vec![0u8; Self::byte_size()];
self.to_bytes(&mut buf);
U::from_bytes(&buf[..U::byte_size()])
}

pub fn from_int<U: ByteRepr>(value: U) -> Self {
let mut buf = vec![0u8; Self::byte_size()];
value.to_bytes(&mut buf[..U::byte_size()]);
Self::from_bytes(&buf)
}
}

impl AnyPtr {
pub fn to_int<U: ByteRepr>(&self) -> U {
self.reinterpret_cast::<u8>().to_int()
}

pub fn from_int<U: ByteRepr>(value: U) -> Self {
Ptr::<u8>::from_int(value).to_any()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/out/refcount/unconst.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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};
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let a: Value<i32> = Rc::new(RefCell::new(1));
let p: Value<Ptr<i32>> = Rc::new(RefCell::new((a.as_pointer())));
let q: Value<Ptr<i32>> = Rc::new(RefCell::new(
(<AnyPtr>::from_int(((*p.borrow()).clone() as Ptr<i32>).to_any().to_int::<u64>()))
.reinterpret_cast::<i32>(),
));
assert!({
let _lhs = (*p.borrow()).clone();
_lhs == (*q.borrow()).clone()
});
return 0;
}
21 changes: 21 additions & 0 deletions tests/unit/out/unsafe/unconst.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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;
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let a: i32 = 1;
let mut p: *const i32 = (&a as *const i32);
let mut q: *mut i32 = (((((p) as *const i32 as *const ::libc::c_void) as u64)
as *mut ::libc::c_void) as *mut i32);
assert!(((p) == ((q).cast_const())));
return 0;
}
13 changes: 13 additions & 0 deletions tests/unit/unconst.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// panic: refcount
#include <assert.h>
#include <stdint.h>

#define UNCONST(p) ((void *)(uintptr_t)(const void *)(p))

int main() {
const int a = 1;
const int *p = &a;
auto q = static_cast<int *>(UNCONST(p));
assert(p == q);
return 0;
}
Loading