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
7 changes: 7 additions & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
// Option<fn> implements Copy
virtual bool FunctionPointerImplementsCopy() const { return true; }

bool TypeIsCopyable(clang::QualType ty) const {
if (ty->isFunctionPointerType() || ty->isFunctionType()) {
return FunctionPointerImplementsCopy();
}
return ty->isIntegerType();
}

virtual void ConvertPrintf(clang::CallExpr *expr);

void ConvertVAArgCall(clang::CallExpr *expr);
Expand Down
5 changes: 0 additions & 5 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1104,9 +1104,4 @@ ConstCastType GetConstCastType(clang::QualType to, clang::QualType from) {
}
}

bool TypeIsCopyable(clang::QualType ty) {
return ty->isIntegerType() || ty->isFunctionPointerType() ||
ty->isFunctionType();
}

} // namespace cpp2rust
2 changes: 0 additions & 2 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,4 @@ enum class ConstCastType {

ConstCastType GetConstCastType(clang::QualType to, clang::QualType from);

bool TypeIsCopyable(clang::QualType ty);

} // namespace cpp2rust
18 changes: 18 additions & 0 deletions tests/unit/out/refcount/va_arg_fn_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ pub fn apply_binary_4(a: i32, b: i32, __args: &[VaArg]) -> i32 {
));
return (*result.borrow());
}
pub fn not_supported_5(ctx: AnyPtr, fn_: FnPtr<fn(i32) -> i32>, extra: AnyPtr) -> i32 {
let ctx: Value<AnyPtr> = Rc::new(RefCell::new(ctx));
let fn_: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(fn_));
let extra: Value<AnyPtr> = Rc::new(RefCell::new(extra));
(*ctx.borrow()).clone();
(*fn_.borrow()).clone();
(*extra.borrow()).clone();
return -3_i32;
}
pub fn main() {
std::process::exit(main_0());
}
Expand All @@ -61,5 +70,14 @@ fn main_0() -> i32 {
as i32)
!= 0)
);
let dummy: Value<i32> = Rc::new(RefCell::new(0));
assert!(
(((({
let _ctx: AnyPtr = ((dummy.as_pointer()) as Ptr<i32>).to_any();
let _extra: AnyPtr = ((dummy.as_pointer()) as Ptr<i32>).to_any();
not_supported_5(_ctx, FnPtr::<fn(i32) -> i32>::new(square_0), _extra)
}) == -3_i32) as i32)
!= 0)
);
return 0;
}
6 changes: 3 additions & 3 deletions tests/unit/out/refcount/void_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ fn main_0() -> i32 {
}));
assert!(((*err.borrow()) == 7));
assert!(((*chosen.borrow()) == 123));
bump_and_return_4;
bump_and_return_4.clone();
assert!(((*side_effect_counter_3.with(Value::clone).borrow()) == 2));
(FnPtr::<fn() -> i32>::new(bump_and_return_4));
(FnPtr::<fn() -> i32>::new(bump_and_return_4)).clone();
assert!(((*side_effect_counter_3.with(Value::clone).borrow()) == 2));
((FnPtr::<fn() -> i32>::new(bump_and_return_4)).cast::<fn() -> i32>(None));
((FnPtr::<fn() -> i32>::new(bump_and_return_4)).cast::<fn() -> i32>(None)).clone();
assert!(((*side_effect_counter_3.with(Value::clone).borrow()) == 2));
let storage: Value<i32> = Rc::new(RefCell::new(11));
let p: Value<Ptr<i32>> = Rc::new(RefCell::new((storage.as_pointer())));
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/out/unsafe/va_arg_fn_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ pub unsafe fn apply_binary_4(mut a: i32, mut b: i32, __args: &[VaArg]) -> i32 {
let mut result: i32 = (unsafe { (fn_).unwrap()(a, b) });
return result;
}
pub unsafe fn not_supported_5(
mut ctx: *mut ::libc::c_void,
mut fn_: Option<unsafe fn(i32) -> i32>,
mut extra: *mut ::libc::c_void,
) -> i32 {
&(ctx);
&(fn_);
&(extra);
return -3_i32;
}
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
Expand Down Expand Up @@ -78,5 +88,16 @@ unsafe fn main_0() -> i32 {
}) == (7)) as i32)
!= 0)
);
let mut dummy: i32 = 0;
assert!(
((((unsafe {
let _ctx: *mut ::libc::c_void =
((&mut dummy as *mut i32) as *mut i32 as *mut ::libc::c_void);
let _extra: *mut ::libc::c_void =
((&mut dummy as *mut i32) as *mut i32 as *mut ::libc::c_void);
not_supported_5(_ctx, Some(square_0), _extra)
}) == (-3_i32)) as i32)
!= 0)
);
return 0;
}
9 changes: 9 additions & 0 deletions tests/unit/va_arg_fn_ptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,18 @@ int apply_binary(int a, int b, ...) {
return result;
}

static int not_supported(void *ctx, unary fn, void *extra) {
(void)ctx;
(void)fn;
(void)extra;
return -3;
}

int main() {
assert(apply_unary(5, square) == 25);
assert(apply_unary(7, negate) == -7);
assert(apply_binary(3, 4, add) == 7);
int dummy = 0;
assert(not_supported(&dummy, square, &dummy) == -3);
return 0;
}
Loading