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
6 changes: 6 additions & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,12 @@ bool ConverterRefCount::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
ToString(sub_expr->getType())));
}
computed_expr_type_ = ComputedExprType::FreshPointer;
} else if (sub_expr->getType()->isVoidPointerType() &&
expr->getType()->isPointerType()) {
Convert(sub_expr);
PushConversionKind push(*this, ConversionKind::Unboxed);
StrCat(std::format(".reinterpret_cast::<{}>()",
ConvertPointeeType(expr->getType())));
} else {
Convert(sub_expr);
}
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/out/refcount/void_ptr_implicit_conversion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 bump_0(arg: AnyPtr) -> i32 {
let arg: Value<AnyPtr> = Rc::new(RefCell::new(arg));
let value: Value<Ptr<i32>> = Rc::new(RefCell::new(
((*arg.borrow()).reinterpret_cast::<i32>()).clone(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this clone appearing here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It comes from ConvertVarInit. Initializers of variables are cloned in order to avoid moves

));
{
let _ptr = (*value.borrow()).clone();
_ptr.write(_ptr.read() + 1)
};
return ((*value.borrow()).read());
}
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let value: Value<i32> = Rc::new(RefCell::new(41));
let opaque: Value<AnyPtr> = Rc::new(RefCell::new(((value.as_pointer()) as Ptr<i32>).to_any()));
let typed: Value<Ptr<i32>> = Rc::new(RefCell::new(
((*opaque.borrow()).reinterpret_cast::<i32>()).clone(),
));
assert!((((({ bump_0((*opaque.borrow()).clone(),) }) == 42) as i32) != 0));
assert!((((((*typed.borrow()).read()) == 42) as i32) != 0));
(*typed.borrow()).write(7);
assert!(((((*value.borrow()) == 7) as i32) != 0));
return 0;
}
29 changes: 29 additions & 0 deletions tests/unit/out/unsafe/void_ptr_implicit_conversion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 unsafe fn bump_0(mut arg: *mut ::libc::c_void) -> i32 {
let mut value: *mut i32 = (arg as *mut i32);
(*value) += 1;
return (*value);
}
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut value: i32 = 41;
let mut opaque: *mut ::libc::c_void =
((&mut value as *mut i32) as *mut i32 as *mut ::libc::c_void);
let mut typed: *mut i32 = (opaque as *mut i32);
assert!(((((unsafe { bump_0(opaque,) }) == (42)) as i32) != 0));
assert!(((((*typed) == (42)) as i32) != 0));
(*typed) = 7;
assert!(((((value) == (7)) as i32) != 0));
return 0;
}
19 changes: 19 additions & 0 deletions tests/unit/void_ptr_implicit_conversion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <assert.h>
#include <stdint.h>

static int32_t bump(void *arg) {
int32_t *value = arg;
*value += 1;
return *value;
}

int main() {
int32_t value = 41;
void *opaque = &value;
int32_t *typed = opaque;
assert(bump(opaque) == 42);
assert(*typed == 42);
*typed = 7;
assert(value == 7);
return 0;
}
Loading