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
9 changes: 7 additions & 2 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2279,8 +2279,13 @@ std::optional<Converter::TempMaterializationCtx>
Converter::ConvertCallExpr(clang::CallExpr *expr) {
auto *callee = expr->getCallee();

if (auto fn = Printer::ToString(ctx_, callee);
fn.starts_with("int printf") || fn.starts_with("int fprintf")) {
auto fn = Printer::ToString(ctx_, callee);
bool is_printf = fn.starts_with("int printf");
bool is_fprintf = fn.starts_with("int fprintf");
unsigned format_arg = is_fprintf ? 1 : 0;
if ((is_printf || is_fprintf) &&
clang::isa<clang::StringLiteral>(
expr->getArg(format_arg)->IgnoreImplicit())) {
ConvertPrintf(expr);
} else if (IsTransparentStdCall(expr)) {
Convert(expr->getArg(0));
Expand Down
12 changes: 2 additions & 10 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1252,16 +1252,8 @@ static std::vector<const char *> printf2fmt(std::string &format) {
void ConverterRefCount::ConvertPrintf(clang::CallExpr *expr) {
bool is_fprintf =
Printer::ToString(ctx_, expr->getCallee()).starts_with("int fprintf");
std::string format;
if (auto *str = clang::dyn_cast<clang::StringLiteral>(
expr->getArg(is_fprintf)->IgnoreImplicit())) {
format = GetEscapedStringLiteral(str);
} else {
llvm::errs() << "Unknown fprintf format: ";
expr->getArg(1)->dump();
llvm::errs() << '\n';
exit(1);
}
auto format = GetEscapedStringLiteral(clang::cast<clang::StringLiteral>(
expr->getArg(is_fprintf)->IgnoreImplicit()));
bool ends_newline = format.ends_with("\\n\"");

auto fd = is_fprintf ? Printer::ToString(ctx_, expr->getArg(0)) : "stdout";
Expand Down
4 changes: 4 additions & 0 deletions rules/stdio/src.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ int f25(void) { return SEEK_SET; }
int f26(void) { return SEEK_CUR; }

int f27(void) { return SEEK_END; }

int (*f28)(FILE *, const char *, ...) = fprintf;

int (*f29)(const char *, ...) = printf;
10 changes: 10 additions & 0 deletions rules/stdio/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,13 @@ fn f23(a0: Ptr<CFile>) -> i32 {
fn f24(a0: Ptr<CFile>, a1: Ptr<i8>, a2: i32, a3: usize) -> i32 {
0
}

fn f28(a0: Ptr<CFile>, a1: Ptr<i8>, va: &[VaArg]) -> i32 {
let __s = libcc2rs::format_c(&a1.to_rust_string(), va);
a0.with_mut(|__f| __f.write(__s.as_bytes())) as i32
}

fn f29(a0: Ptr<i8>, va: &[VaArg]) -> i32 {
let __s = libcc2rs::format_c(&a0.to_rust_string(), va);
libcc2rs::c_stdout().with_mut(|__f| __f.write(__s.as_bytes())) as i32
}
8 changes: 8 additions & 0 deletions rules/stdio/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,11 @@ unsafe fn f26() -> i32 {
unsafe fn f27() -> i32 {
::libc::SEEK_END
}

unsafe extern "C" {
fn f28(a0: *mut ::libc::FILE, a1: *const libc::c_char, ...) -> i32;
}

unsafe extern "C" {
fn f29(a0: *const libc::c_char, ...) -> i32;
}
37 changes: 37 additions & 0 deletions tests/unit/out/refcount/printfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ pub fn fn_0(mut v: Vec<i8>) -> Vec<i8> {
pub fn fn2_1(v: Ptr<Vec<i8>>) -> Ptr<Vec<i8>> {
return (v).clone();
}
pub fn log_to_2(mut out: Ptr<CFile>, mut fmt: Ptr<i8>, mut args_2: Ptr<i8>, mut args_3: i32) {
{
let __s = libcc2rs::format_c(
&fmt.to_rust_string(),
&[((args_2).clone()).into(), (args_3).into()],
);
out.with_mut(|__f| __f.write(__s.as_bytes())) as i32
};
}
pub fn log_to_3(mut out: Ptr<CFile>, mut fmt: Ptr<i8>, mut args: i32) {
{
let __s = libcc2rs::format_c(&fmt.to_rust_string(), &[(args).into()]);
out.with_mut(|__f| __f.write(__s.as_bytes())) as i32
};
}
pub fn log_fmt_4(mut fmt: Ptr<i8>, mut v: i32) {
{
let __s = libcc2rs::format_c(&fmt.to_rust_string(), &[(v).into()]);
libcc2rs::c_stdout().with_mut(|__f| __f.write(__s.as_bytes())) as i32
};
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand Down Expand Up @@ -53,6 +74,22 @@ fn main_0() -> i32 {
"{}",
(Ptr::<Vec<i8>>::decay(&({ fn2_1(s.as_pointer(),) })) as Ptr<i8>)
);
({
log_to_2(
libcc2rs::c_stdout(),
Ptr::<i8>::from_string_literal(b"%s %d\n"),
Ptr::<i8>::from_string_literal(b"runtime"),
4,
)
});
({
log_to_3(
libcc2rs::c_stderr(),
Ptr::<i8>::from_string_literal(b"%d\n"),
5,
)
});
({ log_fmt_4(Ptr::<i8>::from_string_literal(b"%d\n"), 6) });
println!("{{}} {} {}", 5_u64, (-6_i32 as i64));
return 0;
}
Expand Down
32 changes: 17 additions & 15 deletions tests/unit/out/unsafe/c_string_embedded_nul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@ unsafe fn main_0() -> i32 {
let s = (buf.as_mut_ptr()).cast_const();
std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1).to_vec()
};
printf(c"%zu\n".as_ptr() as *const i8, (s.len() - 1));
printf(
c"%zu %zu\n".as_ptr() as *const i8,
libc::strlen((buf.as_mut_ptr()).cast_const()),
match s.iter().rposition(|&c| {
::std::ffi::CStr::from_ptr((buf.as_mut_ptr()).cast_const())
.to_str()
.unwrap()
.contains(c as u8 as char)
}) {
Some(idx) => idx,
None => usize::MAX,
},
);
(unsafe { libc::printf(c"%zu\n".as_ptr() as *const libc::c_char, (s.len() - 1)) });
(unsafe {
libc::printf(
c"%zu %zu\n".as_ptr() as *const libc::c_char,
(libc::strlen((buf.as_mut_ptr()).cast_const())),
(match s.iter().rposition(|&c| {
::std::ffi::CStr::from_ptr((buf.as_mut_ptr()).cast_const())
.to_str()
.unwrap()
.contains(c as u8 as char)
}) {
Some(idx) => idx,
None => usize::MAX,
}),
)
});
let mut lit: *const libc::c_char = (&[
(120 as libc::c_char),
(121 as libc::c_char),
Expand All @@ -55,7 +57,7 @@ unsafe fn main_0() -> i32 {
let s = lit;
std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1).to_vec()
};
printf(c"%zu\n".as_ptr() as *const i8, (t.len() - 1));
(unsafe { libc::printf(c"%zu\n".as_ptr() as *const libc::c_char, (t.len() - 1)) });
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
89 changes: 54 additions & 35 deletions tests/unit/out/unsafe/char_signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,69 @@ pub fn main() {
unsafe fn main_0() -> i32 {
let mut c: libc::c_char = (-56 as libc::c_char);
let mut widened: i32 = (c as i32);
printf(
c"%d %d\n".as_ptr() as *const i8,
widened,
(unsafe { to_int_0((b'\xff' as libc::c_char)) }),
);
printf(c"%d\n".as_ptr() as *const i8, (((c as i32) < (0)) as i32));
(unsafe {
libc::printf(
c"%d %d\n".as_ptr() as *const libc::c_char,
(widened),
(unsafe { to_int_0((b'\xff' as libc::c_char)) }),
)
});
(unsafe {
libc::printf(
c"%d\n".as_ptr() as *const libc::c_char,
(((c as i32) < (0)) as i32),
)
});
let mut lit: [libc::c_char; 4] = std::mem::transmute(*b"\xe9t\xe9\0");
let mut ulit: [u8; 4] = std::mem::transmute(*b"\xe9t\xe9\0");
printf(
c"%d %d\n".as_ptr() as *const i8,
(lit[(0) as usize] as i32),
(ulit[(0) as usize] as i32),
);
printf(
c"%d\n".as_ptr() as *const i8,
((unsafe { is_negative_1((lit.as_mut_ptr()).cast_const()) }) as i32),
);
(unsafe {
libc::printf(
c"%d %d\n".as_ptr() as *const libc::c_char,
(lit[(0) as usize] as i32),
(ulit[(0) as usize] as i32),
)
});
(unsafe {
libc::printf(
c"%d\n".as_ptr() as *const libc::c_char,
((unsafe { is_negative_1((lit.as_mut_ptr()).cast_const()) }) as i32),
)
});
let mut u8s: *const libc::c_char = c"\xc3\xa9".as_ptr();
printf(
c"%d %d\n".as_ptr() as *const i8,
((*u8s.offset((0) as isize)) as i32),
((*u8s.offset((1) as isize)) as i32),
);
(unsafe {
libc::printf(
c"%d %d\n".as_ptr() as *const libc::c_char,
((*u8s.offset((0) as isize)) as i32),
((*u8s.offset((1) as isize)) as i32),
)
});
let mut p: *const libc::c_char = c"\x80".as_ptr();
printf(
c"%d %d\n".as_ptr() as *const i8,
((*p.offset((0) as isize)) as i32),
(((*p.offset((0) as isize)) as u8) as i32),
);
printf(
c"%d\n".as_ptr() as *const i8,
(((libc::strcmp(c"\x80".as_ptr(), c"a".as_ptr())) > (0)) as i32),
);
(unsafe {
libc::printf(
c"%d %d\n".as_ptr() as *const libc::c_char,
((*p.offset((0) as isize)) as i32),
(((*p.offset((0) as isize)) as u8) as i32),
)
});
(unsafe {
libc::printf(
c"%d\n".as_ptr() as *const libc::c_char,
(((libc::strcmp(c"\x80".as_ptr(), c"a".as_ptr())) > (0)) as i32),
)
});
let mut s: Vec<libc::c_char> = {
let s = c"\xfe!".as_ptr();
std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1).to_vec()
};
printf(
c"%d %d\n".as_ptr() as *const i8,
(s[(0_usize)] as i32),
(s[(1_usize)] as i32),
);
(unsafe {
libc::printf(
c"%d %d\n".as_ptr() as *const libc::c_char,
(s[(0_usize)] as i32),
(s[(1_usize)] as i32),
)
});
let mut sum: libc::c_char = (((c as i32) + (c as i32)) as libc::c_char);
printf(c"%d\n".as_ptr() as *const i8, (sum as i32));
(unsafe { libc::printf(c"%d\n".as_ptr() as *const libc::c_char, (sum as i32)) });
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
6 changes: 3 additions & 3 deletions tests/unit/out/unsafe/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ unsafe fn main_0() -> i32 {
}
'loop_: for c in 0..(x.len() - 1) {
let mut c = x.as_mut_ptr().add(c);
printf(c"%c\n".as_ptr() as *const i8, ((*c) as i32));
(unsafe { libc::printf(c"%c\n".as_ptr() as *const libc::c_char, ((*c) as i32)) });
}
'loop_: for c in 0..(x.len() - 1) {
let mut c = x[c].clone();
printf(c"%c\n".as_ptr() as *const i8, (c as i32));
(unsafe { libc::printf(c"%c\n".as_ptr() as *const libc::c_char, (c as i32)) });
}
let mut v: Vec<*mut i32> = Vec::new();
{
Expand All @@ -40,7 +40,7 @@ unsafe fn main_0() -> i32 {
};
'loop_: for p in 0..(v.len()) {
let mut p = v[p].clone();
printf(c"%d\n".as_ptr() as *const i8, (*p));
(unsafe { libc::printf(c"%d\n".as_ptr() as *const libc::c_char, (*p)) });
}
return 0;
}
Expand Down
30 changes: 22 additions & 8 deletions tests/unit/out/unsafe/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ pub unsafe fn base_name_0(mut path: *const libc::c_char) -> *const libc::c_char
};
}
pub unsafe fn log_1(mut file: *const libc::c_char, mut line: i32, mut func: *const libc::c_char) {
printf(c"%s %d %s\n".as_ptr() as *const i8, file, line, func);
(unsafe {
libc::printf(
c"%s %d %s\n".as_ptr() as *const libc::c_char,
(file),
(line),
(func),
)
});
}
pub unsafe fn line_2() -> i32 {
return (line!() as u32 as i32);
Expand All @@ -31,12 +38,14 @@ pub fn main() {
}
}
unsafe fn main_0() -> i32 {
printf(
c"%s %d %s\n".as_ptr() as *const i8,
c"macros.cpp".as_ptr(),
19,
c"main".as_ptr(),
);
(unsafe {
libc::printf(
c"%s %d %s\n".as_ptr() as *const libc::c_char,
(c"macros.cpp".as_ptr()),
(19),
(c"main".as_ptr()),
)
});
(unsafe { log_1(c"macros.cpp".as_ptr(), 20, c"main".as_ptr()) });
assert!(((line!() as u32) > (0_u32)));
assert!(
Expand All @@ -45,7 +54,12 @@ unsafe fn main_0() -> i32 {
!= (('\0' as libc::c_char) as i32))
);
assert!(((unsafe { line_2() }) > (0)));
printf(c"%s\n".as_ptr() as *const i8, (unsafe { function_3() }));
(unsafe {
libc::printf(
c"%s\n".as_ptr() as *const libc::c_char,
(unsafe { function_3() }),
)
});
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/pointer_printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn main() {
unsafe fn main_0() -> i32 {
let mut a: i32 = 1;
let mut p: *mut i32 = (&mut a as *mut i32);
printf(c"%p %d".as_ptr() as *const i8, p, (*p));
(unsafe { libc::printf(c"%p %d".as_ptr() as *const libc::c_char, (p), (*p)) });
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
Loading
Loading