Skip to content
Merged
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
8 changes: 7 additions & 1 deletion cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,13 @@ bool ConverterRefCount::VisitImplicitValueInitExpr(
return Converter::VisitImplicitValueInitExpr(expr);
}

void ConverterRefCount::ConvertVariadicArg(clang::Expr *arg) { Convert(arg); }
void ConverterRefCount::ConvertVariadicArg(clang::Expr *arg) {
if (arg->getType()->isPointerType()) {
StrCat(ConvertFreshPointer(arg));
return;
}
Convert(arg);
}

bool ConverterRefCount::VisitVAArgExpr(clang::VAArgExpr *expr) {
auto va_list_expr = expr->getSubExpr();
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ fn main_0() -> i32 {
);
assert!((((!((*s.borrow()).is_null())) as i32) != 0));
assert!(
(((({ dispatch_0((opt::OPT_FILE as i32), &[(libcc2rs::cout()).into(),]) }) == 1) as i32)
(((({
dispatch_0(
(opt::OPT_FILE as i32),
&[((libcc2rs::cout()).clone()).into()],
)
}) == 1) as i32)
!= 0)
);
assert!(
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/out/refcount/va_arg_printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ pub fn logf_1(fmt: Ptr<u8>, __args: &[VaArg]) -> i32 {
));
return (*result.borrow());
}
pub fn lenf_2(fmt: Ptr<u8>, __args: &[VaArg]) -> i32 {
let fmt: Value<Ptr<u8>> = Rc::new(RefCell::new(fmt));
let ap: Value<VaList> = Rc::new(RefCell::new(VaList::default()));
(*ap.borrow_mut()) = VaList::new(__args);
let s: Value<Ptr<u8>> = Rc::new(RefCell::new(((*ap.borrow_mut()).arg::<Ptr<u8>>()).clone()));
let result: Value<i32> = Rc::new(RefCell::new(
((*s.borrow()).to_string_iterator().count() as i32),
));
return (*result.borrow());
}
pub fn main() {
std::process::exit(main_0());
}
Expand All @@ -50,5 +60,30 @@ fn main_0() -> i32 {
}) == 3) as i32)
!= 0)
);
assert!(
(((({
lenf_2(
Ptr::from_string_literal(b"%s"),
&[((*dummy.borrow()).clone()).into()],
)
}) == 5) as i32)
!= 0)
);
assert!(
(((({
lenf_2(
Ptr::from_string_literal(b"%s"),
&[
(if ((((*dummy.borrow()).offset((0) as isize).read()) as i32) != 0) {
(*dummy.borrow()).clone()
} else {
Ptr::from_string_literal(b"")
})
.into(),
],
)
}) == 5) as i32)
!= 0)
);
return 0;
}
26 changes: 26 additions & 0 deletions tests/unit/out/unsafe/va_arg_printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ pub unsafe fn logf_1(mut fmt: *const libc::c_char, __args: &[VaArg]) -> i32 {
let mut result: i32 = (unsafe { logf_impl_0(fmt, ap) });
return result;
}
pub unsafe fn lenf_2(mut fmt: *const libc::c_char, __args: &[VaArg]) -> i32 {
let mut ap: VaList = VaList::default();
ap = VaList::new(__args);
let mut s: *const libc::c_char = ap.arg::<*const libc::c_char>();
let mut result: i32 = (libc::strlen(s) as i32);
return result;
}
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
Expand All @@ -41,5 +48,24 @@ unsafe fn main_0() -> i32 {
}) == (3)) as i32)
!= 0)
);
assert!(
((((unsafe { lenf_2((c"%s".as_ptr().cast_mut()).cast_const(), &[(dummy).into(),]) }) == (5))
as i32)
!= 0)
);
assert!(
((((unsafe {
lenf_2(
(c"%s".as_ptr().cast_mut()).cast_const(),
&[(if (((*dummy.offset((0) as isize)) as i32) != 0) {
dummy
} else {
(c"".as_ptr().cast_mut() as *const libc::c_char)
})
.into()],
)
}) == (5)) as i32)
!= 0)
);
return 0;
}
11 changes: 11 additions & 0 deletions tests/unit/va_arg_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@ int logf(const char *fmt, ...) {
return result;
}

int lenf(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
const char *s = va_arg(ap, const char *);
int result = (int)strlen(s);
va_end(ap);
return result;
}

int main() {
const char *dummy = "dummy";
assert(logf("hello %d %d", 10, strlen(dummy)) == 15);
assert(logf("x %d %d", 1, 2) == 3);
assert(lenf("%s", dummy) == 5);
assert(lenf("%s", dummy[0] ? dummy : "") == 5);
return 0;
}
Loading