diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index c0e7f693..c0801fcb 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1924,6 +1924,8 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) { auto type = expr->getTypeAsWritten(); auto *sub_expr = expr->getSubExpr(); if (type->isVoidType()) { + StrCat(token::kRef); + PushParen paren(*this); PushExprKind push(*this, ExprKind::Void); Convert(expr->getSubExpr()); return false; diff --git a/tests/unit/out/refcount/void_cast.rs b/tests/unit/out/refcount/void_cast.rs index e372fef3..f8c55b9d 100644 --- a/tests/unit/out/refcount/void_cast.rs +++ b/tests/unit/out/refcount/void_cast.rs @@ -10,10 +10,30 @@ pub fn unused_param_0(x: i32) { let x: Value = Rc::new(RefCell::new(x)); (*x.borrow_mut()); } +#[derive(Default)] +pub struct NonTrivial { + pub data: Value>, +} +impl Clone for NonTrivial { + fn clone(&self) -> Self { + let mut this = Self { + data: Rc::new(RefCell::new((*self.data.borrow()).clone())), + }; + this + } +} +impl ByteRepr for NonTrivial {} +pub fn unused_ref_param_1(x: Ptr) { + (*x.upgrade().deref()).clone(); +} +pub fn unused_ptr_param_2(p: Ptr) { + let p: Value> = Rc::new(RefCell::new(p)); + (*(*p.borrow()).upgrade().deref()).clone(); +} thread_local!( pub static side_effect_counter: Value = Rc::new(RefCell::new(0)); ); -pub fn bump_and_return_1() -> i32 { +pub fn bump_and_return_3() -> i32 { (*side_effect_counter.with(Value::clone).borrow_mut()).prefix_inc(); return (*side_effect_counter.with(Value::clone).borrow()); } @@ -53,10 +73,10 @@ fn main_0() -> i32 { })); assert!(((*w.borrow()) == 3)); assert!(((*counter.borrow()) == 3)); - ({ bump_and_return_1() }); + ({ bump_and_return_3() }); assert!(((*side_effect_counter.with(Value::clone).borrow()) == 1)); let v: Value = Rc::new(RefCell::new({ - ({ bump_and_return_1() }); + ({ bump_and_return_3() }); 99 })); assert!(((*side_effect_counter.with(Value::clone).borrow()) == 2)); @@ -75,11 +95,11 @@ fn main_0() -> i32 { })); assert!(((*err.borrow()) == 7)); assert!(((*chosen.borrow()) == 123)); - bump_and_return_1; + bump_and_return_3; assert!(((*side_effect_counter.with(Value::clone).borrow()) == 2)); - (FnPtr:: i32>::new(bump_and_return_1)); + (FnPtr:: i32>::new(bump_and_return_3)); assert!(((*side_effect_counter.with(Value::clone).borrow()) == 2)); - ((FnPtr:: i32>::new(bump_and_return_1)).cast:: i32>(None)); + ((FnPtr:: i32>::new(bump_and_return_3)).cast:: i32>(None)); assert!(((*side_effect_counter.with(Value::clone).borrow()) == 2)); let storage: Value = Rc::new(RefCell::new(11)); let p: Value> = Rc::new(RefCell::new((storage.as_pointer()))); @@ -93,5 +113,14 @@ fn main_0() -> i32 { (*(*h.borrow()).field.borrow_mut()); let hp: Value> = Rc::new(RefCell::new((h.as_pointer()))); (*(*(*hp.borrow()).upgrade().deref()).field.borrow_mut()); + let nt: Value = Rc::new(RefCell::new(::default())); + ({ + let _x: Ptr = nt.as_pointer(); + unused_ref_param_1(_x) + }); + ({ + let _p: Ptr = (nt.as_pointer()); + unused_ptr_param_2(_p) + }); return 0; } diff --git a/tests/unit/out/unsafe/reinterpret_cast_oob_read.rs b/tests/unit/out/unsafe/reinterpret_cast_oob_read.rs index 754cca82..2c1192e3 100644 --- a/tests/unit/out/unsafe/reinterpret_cast_oob_read.rs +++ b/tests/unit/out/unsafe/reinterpret_cast_oob_read.rs @@ -15,6 +15,6 @@ unsafe fn main_0() -> i32 { let mut val: u32 = 67305985_u32; let mut bytes: *mut u8 = ((&mut val as *mut u32) as *mut u8); let mut x: u8 = (*bytes.offset((4) as isize)); - x; + &(x); return 0; } diff --git a/tests/unit/out/unsafe/reinterpret_cast_undersize.rs b/tests/unit/out/unsafe/reinterpret_cast_undersize.rs index 7bbed02e..4504ce2f 100644 --- a/tests/unit/out/unsafe/reinterpret_cast_undersize.rs +++ b/tests/unit/out/unsafe/reinterpret_cast_undersize.rs @@ -15,6 +15,6 @@ unsafe fn main_0() -> i32 { let mut b: u8 = 66_u8; let mut p: *mut u32 = ((&mut b as *mut u8) as *mut u32); let mut val: u32 = (*p); - val; + &(val); return 0; } diff --git a/tests/unit/out/unsafe/va_arg_printf.rs b/tests/unit/out/unsafe/va_arg_printf.rs index b6095c14..be3cd82f 100644 --- a/tests/unit/out/unsafe/va_arg_printf.rs +++ b/tests/unit/out/unsafe/va_arg_printf.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub unsafe fn logf_impl_0(mut fmt: *const u8, mut ap: VaList) -> i32 { - fmt; + &(fmt); return ((ap.arg::()) + (ap.arg::())); } pub unsafe fn logf_1(mut fmt: *const u8, __args: &[VaArg]) -> i32 { diff --git a/tests/unit/out/unsafe/void_cast.rs b/tests/unit/out/unsafe/void_cast.rs index af4df89d..d3a68f95 100644 --- a/tests/unit/out/unsafe/void_cast.rs +++ b/tests/unit/out/unsafe/void_cast.rs @@ -7,10 +7,21 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub unsafe fn unused_param_0(mut x: i32) { - x; + &(x); +} +#[repr(C)] +#[derive(Clone, Default)] +pub struct NonTrivial { + pub data: Vec, +} +pub unsafe fn unused_ref_param_1(x: *const NonTrivial) { + &(*x); +} +pub unsafe fn unused_ptr_param_2(mut p: *const NonTrivial) { + &(*p); } pub static mut side_effect_counter: i32 = 0; -pub unsafe fn bump_and_return_1() -> i32 { +pub unsafe fn bump_and_return_3() -> i32 { side_effect_counter.prefix_inc(); return side_effect_counter; } @@ -30,59 +41,68 @@ unsafe fn main_0() -> i32 { unused_param_0(_x) }); let mut y: i32 = 5; - y; + &(y); let mut z: i32 = { - y; + &(y); 7 }; assert!(((z) == (7))); let mut counter: i32 = 0; let mut w: i32 = { - counter; + &(counter); counter = 3; counter }; assert!(((w) == (3))); assert!(((counter) == (3))); - (unsafe { bump_and_return_1() }); + &(unsafe { bump_and_return_3() }); assert!(((side_effect_counter) == (1))); let mut v: i32 = { - (unsafe { bump_and_return_1() }); + &(unsafe { bump_and_return_3() }); 99 }; assert!(((side_effect_counter) == (2))); assert!(((v) == (99))); - 0; - (0); - (y); - (0); - (y); + &(0); + &(0); + &(y); + (&(0)); + (&(y)); let mut err: i32 = 0; - (err = 42); + (&(err = 42)); assert!(((err) == (42))); let mut chosen: i32 = { - (err = 7); + &(err = 7); 123 }; assert!(((err) == (7))); assert!(((chosen) == (123))); - bump_and_return_1; + &(bump_and_return_3); assert!(((side_effect_counter) == (2))); - (Some(bump_and_return_1)); + &(Some(bump_and_return_3)); assert!(((side_effect_counter) == (2))); - (std::mem::transmute:: i32>, Option i32>>( - (Some(bump_and_return_1)), + &(std::mem::transmute:: i32>, Option i32>>( + (Some(bump_and_return_3)), )); assert!(((side_effect_counter) == (2))); let mut storage: i32 = 11; let mut p: *mut i32 = (&mut storage as *mut i32); - (*p); - (p); + &(*p); + &(p); let mut arr: [i32; 3] = [1, 2, 3]; - (arr[(1) as usize]); + &(arr[(1) as usize]); let mut h: Holder = Holder { field: 17 }; - (h.field); + &(h.field); let mut hp: *mut Holder = (&mut h as *mut Holder); - ((*hp).field); + &((*hp).field); + let mut nt: NonTrivial = ::default(); + (unsafe { + let _x: *const NonTrivial = &nt as *const NonTrivial; + unused_ref_param_1(_x) + }); + (unsafe { + let _p: *const NonTrivial = (&mut nt as *mut NonTrivial).cast_const(); + unused_ptr_param_2(_p) + }); return 0; } diff --git a/tests/unit/void_cast.cpp b/tests/unit/void_cast.cpp index 78796979..194e599d 100644 --- a/tests/unit/void_cast.cpp +++ b/tests/unit/void_cast.cpp @@ -1,7 +1,16 @@ #include +#include void unused_param(int x) { (void)x; } +struct NonTrivial { + std::vector data; +}; + +void unused_ref_param(const NonTrivial &x) { (void)x; } + +void unused_ptr_param(const NonTrivial *p) { (void)(*p); } + int side_effect_counter = 0; int bump_and_return() { ++side_effect_counter; @@ -71,5 +80,9 @@ int main() { Holder *hp = &h; (void)(hp->field); + NonTrivial nt; + unused_ref_param(nt); + unused_ptr_param(&nt); + return 0; }