From 5616dad33b76a7eb0ca5dcaa9d1eca5e14a2f6b2 Mon Sep 17 00:00:00 2001 From: William Huynh Date: Wed, 11 Jun 2025 14:35:11 +0100 Subject: [PATCH 1/2] Change default behaviour of baremetal/printf to use stdout --- libc/src/stdio/baremetal/printf.cpp | 8 ++++---- libc/src/stdio/baremetal/putchar.cpp | 2 +- libc/src/stdio/baremetal/puts.cpp | 4 ++-- libc/src/stdio/baremetal/vprintf.cpp | 8 ++++---- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/libc/src/stdio/baremetal/printf.cpp b/libc/src/stdio/baremetal/printf.cpp index c94698ec02953..9814edb556faf 100644 --- a/libc/src/stdio/baremetal/printf.cpp +++ b/libc/src/stdio/baremetal/printf.cpp @@ -21,8 +21,8 @@ namespace LIBC_NAMESPACE_DECL { namespace { -LIBC_INLINE int raw_write_hook(cpp::string_view new_str, void *) { - write_to_stderr(new_str); +LIBC_INLINE int raw_write_stdout_hook(cpp::string_view new_str, void *) { + write_to_stdout(new_str); return printf_core::WRITE_OK; } @@ -35,11 +35,11 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) { // and pointer semantics, as well as handling // destruction automatically. va_end(vlist); - constexpr size_t BUFF_SIZE = 1024; + static constexpr size_t BUFF_SIZE = 1024; char buffer[BUFF_SIZE]; printf_core::WriteBuffer wb( - buffer, BUFF_SIZE, &raw_write_hook, nullptr); + buffer, BUFF_SIZE, &raw_write_stdout_hook, nullptr); printf_core::Writer writer(wb); int retval = printf_core::printf_main(&writer, format, args); diff --git a/libc/src/stdio/baremetal/putchar.cpp b/libc/src/stdio/baremetal/putchar.cpp index 0ba46a5ade6c9..ac21e6e783b01 100644 --- a/libc/src/stdio/baremetal/putchar.cpp +++ b/libc/src/stdio/baremetal/putchar.cpp @@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(int, putchar, (int c)) { char uc = static_cast(c); - write_to_stderr(cpp::string_view(&uc, 1)); + write_to_stdout(cpp::string_view(&uc, 1)); return 0; } diff --git a/libc/src/stdio/baremetal/puts.cpp b/libc/src/stdio/baremetal/puts.cpp index 5062efda1c0dc..fcd3aa086b2bf 100644 --- a/libc/src/stdio/baremetal/puts.cpp +++ b/libc/src/stdio/baremetal/puts.cpp @@ -17,8 +17,8 @@ LLVM_LIBC_FUNCTION(int, puts, (const char *__restrict str)) { cpp::string_view str_view(str); // TODO: Can we combine these to avoid needing two writes? - write_to_stderr(str_view); - write_to_stderr("\n"); + write_to_stdout(str_view); + write_to_stdout("\n"); return 0; } diff --git a/libc/src/stdio/baremetal/vprintf.cpp b/libc/src/stdio/baremetal/vprintf.cpp index 3e8631abd90d9..4fa1ae00dd61c 100644 --- a/libc/src/stdio/baremetal/vprintf.cpp +++ b/libc/src/stdio/baremetal/vprintf.cpp @@ -21,8 +21,8 @@ namespace LIBC_NAMESPACE_DECL { namespace { -LIBC_INLINE int raw_write_hook(cpp::string_view new_str, void *) { - write_to_stderr(new_str); +LIBC_INLINE int raw_write_stdout_hook(cpp::string_view new_str, void *) { + write_to_stdout(new_str); return printf_core::WRITE_OK; } @@ -33,11 +33,11 @@ LLVM_LIBC_FUNCTION(int, vprintf, internal::ArgList args(vlist); // This holder class allows for easier copying // and pointer semantics, as well as handling // destruction automatically. - constexpr size_t BUFF_SIZE = 1024; + static constexpr size_t BUFF_SIZE = 1024; char buffer[BUFF_SIZE]; printf_core::WriteBuffer wb( - buffer, BUFF_SIZE, &raw_write_hook, nullptr); + buffer, BUFF_SIZE, &raw_write_stdout_hook, nullptr); printf_core::Writer writer(wb); int retval = printf_core::printf_main(&writer, format, args); From cde8ed1e0ae27ef55d1752acaeef6beab4847432 Mon Sep 17 00:00:00 2001 From: William Huynh Date: Wed, 11 Jun 2025 20:22:27 +0100 Subject: [PATCH 2/2] fixup! Change default behaviour of baremetal/printf to use stdout --- libc/src/stdio/baremetal/printf.cpp | 4 ++-- libc/src/stdio/baremetal/vprintf.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libc/src/stdio/baremetal/printf.cpp b/libc/src/stdio/baremetal/printf.cpp index 9814edb556faf..7253c6549a4e4 100644 --- a/libc/src/stdio/baremetal/printf.cpp +++ b/libc/src/stdio/baremetal/printf.cpp @@ -21,7 +21,7 @@ namespace LIBC_NAMESPACE_DECL { namespace { -LIBC_INLINE int raw_write_stdout_hook(cpp::string_view new_str, void *) { +LIBC_INLINE int stdout_write_hook(cpp::string_view new_str, void *) { write_to_stdout(new_str); return printf_core::WRITE_OK; } @@ -39,7 +39,7 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) { char buffer[BUFF_SIZE]; printf_core::WriteBuffer wb( - buffer, BUFF_SIZE, &raw_write_stdout_hook, nullptr); + buffer, BUFF_SIZE, &stdout_write_hook, nullptr); printf_core::Writer writer(wb); int retval = printf_core::printf_main(&writer, format, args); diff --git a/libc/src/stdio/baremetal/vprintf.cpp b/libc/src/stdio/baremetal/vprintf.cpp index 4fa1ae00dd61c..ab02533f14911 100644 --- a/libc/src/stdio/baremetal/vprintf.cpp +++ b/libc/src/stdio/baremetal/vprintf.cpp @@ -21,7 +21,7 @@ namespace LIBC_NAMESPACE_DECL { namespace { -LIBC_INLINE int raw_write_stdout_hook(cpp::string_view new_str, void *) { +LIBC_INLINE int stdout_write_hook(cpp::string_view new_str, void *) { write_to_stdout(new_str); return printf_core::WRITE_OK; } @@ -37,7 +37,7 @@ LLVM_LIBC_FUNCTION(int, vprintf, char buffer[BUFF_SIZE]; printf_core::WriteBuffer wb( - buffer, BUFF_SIZE, &raw_write_stdout_hook, nullptr); + buffer, BUFF_SIZE, &stdout_write_hook, nullptr); printf_core::Writer writer(wb); int retval = printf_core::printf_main(&writer, format, args);