diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index dc59f0d05..a6c49a61e 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -2279,8 +2279,13 @@ std::optional 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( + expr->getArg(format_arg)->IgnoreImplicit())) { ConvertPrintf(expr); } else if (IsTransparentStdCall(expr)) { Convert(expr->getArg(0)); diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index a9172bc13..d47322762 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1252,16 +1252,8 @@ static std::vector 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( - 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( + expr->getArg(is_fprintf)->IgnoreImplicit())); bool ends_newline = format.ends_with("\\n\""); auto fd = is_fprintf ? Printer::ToString(ctx_, expr->getArg(0)) : "stdout"; diff --git a/rules/stdio/src.c b/rules/stdio/src.c index 2381b28e6..09abc461c 100644 --- a/rules/stdio/src.c +++ b/rules/stdio/src.c @@ -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; diff --git a/rules/stdio/tgt_refcount.rs b/rules/stdio/tgt_refcount.rs index 4acd6fce7..316d5e672 100644 --- a/rules/stdio/tgt_refcount.rs +++ b/rules/stdio/tgt_refcount.rs @@ -189,3 +189,13 @@ fn f23(a0: Ptr) -> i32 { fn f24(a0: Ptr, a1: Ptr, a2: i32, a3: usize) -> i32 { 0 } + +fn f28(a0: Ptr, a1: Ptr, 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, 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 +} diff --git a/rules/stdio/tgt_unsafe.rs b/rules/stdio/tgt_unsafe.rs index e2dafd935..0be970ce4 100644 --- a/rules/stdio/tgt_unsafe.rs +++ b/rules/stdio/tgt_unsafe.rs @@ -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; +} diff --git a/tests/unit/out/refcount/printfs.rs b/tests/unit/out/refcount/printfs.rs index 6b19aabc1..564243d3e 100644 --- a/tests/unit/out/refcount/printfs.rs +++ b/tests/unit/out/refcount/printfs.rs @@ -18,6 +18,27 @@ pub fn fn_0(mut v: Vec) -> Vec { pub fn fn2_1(v: Ptr>) -> Ptr> { return (v).clone(); } +pub fn log_to_2(mut out: Ptr, mut fmt: Ptr, mut args_2: Ptr, 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, mut fmt: Ptr, 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, 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()); @@ -53,6 +74,22 @@ fn main_0() -> i32 { "{}", (Ptr::>::decay(&({ fn2_1(s.as_pointer(),) })) as Ptr) ); + ({ + log_to_2( + libcc2rs::c_stdout(), + Ptr::::from_string_literal(b"%s %d\n"), + Ptr::::from_string_literal(b"runtime"), + 4, + ) + }); + ({ + log_to_3( + libcc2rs::c_stderr(), + Ptr::::from_string_literal(b"%d\n"), + 5, + ) + }); + ({ log_fmt_4(Ptr::::from_string_literal(b"%d\n"), 6) }); println!("{{}} {} {}", 5_u64, (-6_i32 as i64)); return 0; } diff --git a/tests/unit/out/unsafe/c_string_embedded_nul.rs b/tests/unit/out/unsafe/c_string_embedded_nul.rs index fce7675ac..69af75fbc 100644 --- a/tests/unit/out/unsafe/c_string_embedded_nul.rs +++ b/tests/unit/out/unsafe/c_string_embedded_nul.rs @@ -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), @@ -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() {} diff --git a/tests/unit/out/unsafe/char_signed.rs b/tests/unit/out/unsafe/char_signed.rs index f47585e55..82274d546 100644 --- a/tests/unit/out/unsafe/char_signed.rs +++ b/tests/unit/out/unsafe/char_signed.rs @@ -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 = { 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() {} diff --git a/tests/unit/out/unsafe/iterators.rs b/tests/unit/out/unsafe/iterators.rs index c2c58b810..66d08982d 100644 --- a/tests/unit/out/unsafe/iterators.rs +++ b/tests/unit/out/unsafe/iterators.rs @@ -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(); { @@ -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; } diff --git a/tests/unit/out/unsafe/macros.rs b/tests/unit/out/unsafe/macros.rs index af9a53cfd..b3b0f4425 100644 --- a/tests/unit/out/unsafe/macros.rs +++ b/tests/unit/out/unsafe/macros.rs @@ -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); @@ -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!( @@ -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() {} diff --git a/tests/unit/out/unsafe/pointer_printf.rs b/tests/unit/out/unsafe/pointer_printf.rs index a43ba89ff..5b51d7c6c 100644 --- a/tests/unit/out/unsafe/pointer_printf.rs +++ b/tests/unit/out/unsafe/pointer_printf.rs @@ -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() {} diff --git a/tests/unit/out/unsafe/printf_char.rs b/tests/unit/out/unsafe/printf_char.rs index cb08e5b60..266593c05 100644 --- a/tests/unit/out/unsafe/printf_char.rs +++ b/tests/unit/out/unsafe/printf_char.rs @@ -15,25 +15,33 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut c: libc::c_char = (('a' as i32) as libc::c_char); let mut n: i32 = 3; - printf( - (c"%c\n".as_ptr().cast_mut()).cast_const() as *const i8, - (c as i32), - ); - printf( - (c"%d %c\n".as_ptr().cast_mut()).cast_const() as *const i8, - n, - (c as i32), - ); - printf( - (c"100%% %c\n".as_ptr().cast_mut()).cast_const() as *const i8, - (c as i32), - ); - printf( - (c"%c%c%d\n".as_ptr().cast_mut()).cast_const() as *const i8, - (c as i32), - ((c as i32) + (1)), - n, - ); + (unsafe { + libc::printf( + (c"%c\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (c as i32), + ) + }); + (unsafe { + libc::printf( + (c"%d %c\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (n), + (c as i32), + ) + }); + (unsafe { + libc::printf( + (c"100%% %c\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (c as i32), + ) + }); + (unsafe { + libc::printf( + (c"%c%c%d\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (c as i32), + ((c as i32) + (1)), + (n), + ) + }); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/printfs.rs b/tests/unit/out/unsafe/printfs.rs index b9fe7e476..44bd40f07 100644 --- a/tests/unit/out/unsafe/printfs.rs +++ b/tests/unit/out/unsafe/printfs.rs @@ -22,6 +22,27 @@ pub unsafe fn fn_0(mut v: Vec) -> Vec { pub unsafe fn fn2_1(v: *const Vec) -> *const Vec { return v; } +pub unsafe fn log_to_2( + mut out: *mut ::libc::FILE, + mut fmt: *const libc::c_char, + mut args_2: *const libc::c_char, + mut args_3: i32, +) { + (unsafe { + libc::fprintf( + out as *mut ::libc::FILE, + fmt as *const libc::c_char, + (args_2), + (args_3), + ) + }); +} +pub unsafe fn log_to_3(mut out: *mut ::libc::FILE, mut fmt: *const libc::c_char, mut args: i32) { + (unsafe { libc::fprintf(out as *mut ::libc::FILE, fmt as *const libc::c_char, (args)) }); +} +pub unsafe fn log_fmt_4(mut fmt: *const libc::c_char, mut v: i32) { + (unsafe { libc::printf(fmt as *const libc::c_char, (v)) }); +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -29,38 +50,78 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - printf(c"%s\n".as_ptr() as *const i8, c"fprintf stdout".as_ptr()); - printf(c"%d %u %ld\n".as_ptr() as *const i8, 1, 2_u32, 3_i64); - printf(c"hello world".as_ptr() as *const i8); + (unsafe { + libc::fprintf( + libcc2rs::stdout_unsafe() as *mut ::libc::FILE, + c"%s\n".as_ptr() as *const libc::c_char, + (c"fprintf stdout".as_ptr()), + ) + }); + (unsafe { + libc::fprintf( + libcc2rs::stdout_unsafe() as *mut ::libc::FILE, + c"%d %u %ld\n".as_ptr() as *const libc::c_char, + (1), + (2_u32), + (3_i64), + ) + }); + (unsafe { + libc::fprintf( + libcc2rs::stdout_unsafe() as *mut ::libc::FILE, + c"hello world".as_ptr() as *const libc::c_char, + ) + }); let mut in_: *mut ::libc::FILE = libcc2rs::stdin_unsafe(); assert!(!((in_).is_null())); - printf(c"%s\n".as_ptr() as *const i8, c"printf".as_ptr()); - printf(c"hello world".as_ptr() as *const i8); + (unsafe { + libc::printf( + c"%s\n".as_ptr() as *const libc::c_char, + (c"printf".as_ptr()), + ) + }); + (unsafe { libc::printf(c"hello world".as_ptr() as *const libc::c_char) }); let mut s: Vec = { let s = c"a string".as_ptr(); std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1).to_vec() }; - printf(c"%s\n".as_ptr() as *const i8, s.as_mut_ptr()); - printf( - c"%s\n".as_ptr() as *const i8, - (unsafe { - fn_0({ - let s = c"foo".as_ptr(); - std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1) - .to_vec() + (unsafe { libc::printf(c"%s\n".as_ptr() as *const libc::c_char, (s.as_mut_ptr())) }); + (unsafe { + libc::printf( + c"%s\n".as_ptr() as *const libc::c_char, + ((unsafe { + fn_0({ + let s = c"foo".as_ptr(); + std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1) + .to_vec() + }) }) - }) - .as_ptr(), - ); - printf( - c"%s\n".as_ptr() as *const i8, - (*(unsafe { fn2_1(&s) })).as_ptr(), - ); - printf( - c"{} %ju %jd\n".as_ptr() as *const i8, - 5_u64, - (-6_i32 as i64), - ); + .as_ptr()), + ) + }); + (unsafe { + libc::printf( + c"%s\n".as_ptr() as *const libc::c_char, + ((*(unsafe { fn2_1(&s) })).as_ptr()), + ) + }); + (unsafe { + log_to_2( + libcc2rs::stdout_unsafe(), + c"%s %d\n".as_ptr(), + c"runtime".as_ptr(), + 4, + ) + }); + (unsafe { log_to_3(libcc2rs::stderr_unsafe(), c"%d\n".as_ptr(), 5) }); + (unsafe { log_fmt_4(c"%d\n".as_ptr(), 6) }); + (unsafe { + libc::printf( + c"{} %ju %jd\n".as_ptr() as *const libc::c_char, + (5_u64), + (-6_i32 as i64), + ) + }); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/pwd.rs b/tests/unit/out/unsafe/pwd.rs index 88cb2172c..dfd607f14 100644 --- a/tests/unit/out/unsafe/pwd.rs +++ b/tests/unit/out/unsafe/pwd.rs @@ -12,10 +12,12 @@ pub unsafe fn test_getpwuid_0() { assert!((((((*pw).pw_uid) == (libc::geteuid())) as i32) != 0)); assert!(((((libc::strlen(((*pw).pw_name).cast_const())) > (0_usize)) as i32) != 0)); assert!((((!(((*pw).pw_dir).is_null())) as i32) != 0)); - printf( - (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, - (*pw).pw_name, - ); + (unsafe { + libc::printf( + (c"%s\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + ((*pw).pw_name), + ) + }); } pub unsafe fn test_getpwuid_missing_1() { (*libcc2rs::cpp2rust_errno_unsafe()) = 0; @@ -47,10 +49,12 @@ pub unsafe fn test_getpwuid_r_2() { as i32) != 0) ); - printf( - (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, - pw.pw_name, - ); + (unsafe { + libc::printf( + (c"%s\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (pw.pw_name), + ) + }); } pub unsafe fn test_getpwuid_r_erange_3() { let mut pw: ::libc::passwd = unsafe { std::mem::zeroed() }; diff --git a/tests/unit/out/unsafe/sys_time.rs b/tests/unit/out/unsafe/sys_time.rs index ee553db9f..19c2c0445 100644 --- a/tests/unit/out/unsafe/sys_time.rs +++ b/tests/unit/out/unsafe/sys_time.rs @@ -24,23 +24,25 @@ pub unsafe fn print_tm_1(mut t: i64) { .is_null())) as i32) != 0) ); - printf( - (c"%d-%d-%d %d:%d:%d wday=%d yday=%d %s gmtoff=%ld isdst=%d\n" - .as_ptr() - .cast_mut()) - .cast_const() as *const i8, - tm.tm_year, - tm.tm_mon, - tm.tm_mday, - tm.tm_hour, - tm.tm_min, - tm.tm_sec, - tm.tm_wday, - tm.tm_yday, - tm.tm_zone, - tm.tm_gmtoff, - tm.tm_isdst, - ); + (unsafe { + libc::printf( + (c"%d-%d-%d %d:%d:%d wday=%d yday=%d %s gmtoff=%ld isdst=%d\n" + .as_ptr() + .cast_mut()) + .cast_const() as *const libc::c_char, + (tm.tm_year), + (tm.tm_mon), + (tm.tm_mday), + (tm.tm_hour), + (tm.tm_min), + (tm.tm_sec), + (tm.tm_wday), + (tm.tm_yday), + (tm.tm_zone), + (tm.tm_gmtoff), + (tm.tm_isdst), + ) + }); } pub unsafe fn test_gmtime_r_2() { (unsafe { print_tm_1(0_i64) }); @@ -64,23 +66,25 @@ pub unsafe fn print_local_tm_3(mut t: i64) { .is_null())) as i32) != 0) ); - printf( - (c"%d-%d-%d %d:%d:%d wday=%d yday=%d %s gmtoff=%ld isdst=%d\n" - .as_ptr() - .cast_mut()) - .cast_const() as *const i8, - tm.tm_year, - tm.tm_mon, - tm.tm_mday, - tm.tm_hour, - tm.tm_min, - tm.tm_sec, - tm.tm_wday, - tm.tm_yday, - tm.tm_zone, - tm.tm_gmtoff, - tm.tm_isdst, - ); + (unsafe { + libc::printf( + (c"%d-%d-%d %d:%d:%d wday=%d yday=%d %s gmtoff=%ld isdst=%d\n" + .as_ptr() + .cast_mut()) + .cast_const() as *const libc::c_char, + (tm.tm_year), + (tm.tm_mon), + (tm.tm_mday), + (tm.tm_hour), + (tm.tm_min), + (tm.tm_sec), + (tm.tm_wday), + (tm.tm_yday), + (tm.tm_zone), + (tm.tm_gmtoff), + (tm.tm_isdst), + ) + }); } pub unsafe fn test_localtime_r_4() { (unsafe { print_local_tm_3(0_i64) }); @@ -110,10 +114,12 @@ pub unsafe fn test_strftime_5() { )) > (0_usize)) as i32) != 0) ); - printf( - (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, - buf.as_mut_ptr(), - ); + (unsafe { + libc::printf( + (c"%s\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (buf.as_mut_ptr()), + ) + }); assert!( ((((libc::strftime( buf.as_mut_ptr(), @@ -123,10 +129,12 @@ pub unsafe fn test_strftime_5() { )) > (0_usize)) as i32) != 0) ); - printf( - (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, - buf.as_mut_ptr(), - ); + (unsafe { + libc::printf( + (c"%s\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (buf.as_mut_ptr()), + ) + }); assert!( ((((libc::strftime( buf.as_mut_ptr(), @@ -136,10 +144,12 @@ pub unsafe fn test_strftime_5() { )) > (0_usize)) as i32) != 0) ); - printf( - (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, - buf.as_mut_ptr(), - ); + (unsafe { + libc::printf( + (c"%s\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (buf.as_mut_ptr()), + ) + }); assert!( ((((libc::strftime( buf.as_mut_ptr(), @@ -149,10 +159,12 @@ pub unsafe fn test_strftime_5() { )) > (0_usize)) as i32) != 0) ); - printf( - (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, - buf.as_mut_ptr(), - ); + (unsafe { + libc::printf( + (c"%s\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (buf.as_mut_ptr()), + ) + }); let mut small: [libc::c_char; 4] = [(0 as libc::c_char); 4]; assert!( ((((libc::strftime( diff --git a/tests/unit/out/unsafe/unistd.rs b/tests/unit/out/unsafe/unistd.rs index 73b3dab8e..905de26ee 100644 --- a/tests/unit/out/unsafe/unistd.rs +++ b/tests/unit/out/unsafe/unistd.rs @@ -254,16 +254,20 @@ pub unsafe fn test_ioctl_8() { ); } pub unsafe fn test_isatty_9() { - printf( - (c"%d\n".as_ptr().cast_mut()).cast_const() as *const i8, - libc::isatty(0), - ); + (unsafe { + libc::printf( + (c"%d\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (libc::isatty(0)), + ) + }); } pub unsafe fn test_geteuid_10() { - printf( - (c"%u\n".as_ptr().cast_mut()).cast_const() as *const i8, - libc::geteuid(), - ); + (unsafe { + libc::printf( + (c"%u\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (libc::geteuid()), + ) + }); } pub unsafe fn test_gethostname_11() { let mut name: [libc::c_char; 256] = [(0 as libc::c_char); 256]; @@ -274,10 +278,12 @@ pub unsafe fn test_gethostname_11() { )) == (0)) as i32) != 0) ); - printf( - (c"%s\n".as_ptr().cast_mut()).cast_const() as *const i8, - name.as_mut_ptr(), - ); + (unsafe { + libc::printf( + (c"%s\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (name.as_mut_ptr()), + ) + }); } pub fn main() { unsafe { diff --git a/tests/unit/out/unsafe/vector3.rs b/tests/unit/out/unsafe/vector3.rs index e8c8a165f..116740ed0 100644 --- a/tests/unit/out/unsafe/vector3.rs +++ b/tests/unit/out/unsafe/vector3.rs @@ -37,14 +37,14 @@ unsafe fn main_0() -> i32 { let mut v2 = v.as_mut_ptr().add(v2); 'loop_: for i in 0..((*v2).len()) { let mut i = (&(*v2))[i].clone(); - printf(c"%d\n".as_ptr() as *const i8, ((i) + (3))); + (unsafe { libc::printf(c"%d\n".as_ptr() as *const libc::c_char, ((i) + (3))) }); } } 'loop_: for v2 in 0..(v.len()) { let mut v2 = v[v2].clone(); 'loop_: for i in 0..(v2.len()) { let mut i = v2[i].clone(); - printf(c"%d\n".as_ptr() as *const i8, i); + (unsafe { libc::printf(c"%d\n".as_ptr() as *const libc::c_char, (i)) }); } } return 0; diff --git a/tests/unit/printfs.cpp b/tests/unit/printfs.cpp index c36f4b6cf..087f9daac 100644 --- a/tests/unit/printfs.cpp +++ b/tests/unit/printfs.cpp @@ -7,6 +7,12 @@ std::string fn(std::string v) { return v + " str"; } const std::string &fn2(const std::string &v) { return v; } +template void log_to(FILE *out, const char *fmt, Args... args) { + fprintf(out, fmt, args...); +} + +void log_fmt(const char *fmt, int v) { printf(fmt, v); } + int main() { fprintf(stdout, "%s\n", "fprintf stdout"); fprintf(stdout, "%d %u %ld\n", 1, 2U, 3L); @@ -19,6 +25,9 @@ int main() { printf("%s\n", s.data()); printf("%s\n", fn("foo").c_str()); printf("%s\n", fn2(s).c_str()); + log_to(stdout, "%s %d\n", "runtime", 4); + log_to(stderr, "%d\n", 5); + log_fmt("%d\n", 6); printf("{} %ju %jd\n", (uintmax_t)5, (intmax_t)-6); return 0; }