[libc-commits] [libc] 402c376 - [libc] Change default behaviour of baremetal/printf to use stdout (#143703)
via libc-commits
libc-commits at lists.llvm.org
Mon Jun 16 12:23:01 PDT 2025
Author: William Huynh
Date: 2025-06-16T12:22:58-07:00
New Revision: 402c376daa659c0c3a477ad038a415079ffa0a48
URL: https://github.com/llvm/llvm-project/commit/402c376daa659c0c3a477ad038a415079ffa0a48
DIFF: https://github.com/llvm/llvm-project/commit/402c376daa659c0c3a477ad038a415079ffa0a48.diff
LOG: [libc] Change default behaviour of baremetal/printf to use stdout (#143703)
In #94078, `write_to_stdout` had not been fully implemented. However,
now that it has been implemented, to conform with the C standard
(7.23.6.3. The printf function, specifically point 2), we use `stdout`.
This issue is tracked in #94685.
- Also prefer `static constexpr`
- Made it explicit that we are writing to `stdout`
Added:
Modified:
libc/src/stdio/baremetal/printf.cpp
libc/src/stdio/baremetal/putchar.cpp
libc/src/stdio/baremetal/puts.cpp
libc/src/stdio/baremetal/vprintf.cpp
Removed:
################################################################################
diff --git a/libc/src/stdio/baremetal/printf.cpp b/libc/src/stdio/baremetal/printf.cpp
index c94698ec02953..7253c6549a4e4 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 stdout_write_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<printf_core::WriteMode::FLUSH_TO_STREAM> wb(
- buffer, BUFF_SIZE, &raw_write_hook, nullptr);
+ buffer, BUFF_SIZE, &stdout_write_hook, nullptr);
printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> 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<char>(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..ab02533f14911 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 stdout_write_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<printf_core::WriteMode::FLUSH_TO_STREAM> wb(
- buffer, BUFF_SIZE, &raw_write_hook, nullptr);
+ buffer, BUFF_SIZE, &stdout_write_hook, nullptr);
printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> writer(wb);
int retval = printf_core::printf_main(&writer, format, args);
More information about the libc-commits
mailing list