Skip to content

Commit 402c376

Browse files
authored
[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`
1 parent a0662ce commit 402c376

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

libc/src/stdio/baremetal/printf.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace LIBC_NAMESPACE_DECL {
2121

2222
namespace {
2323

24-
LIBC_INLINE int raw_write_hook(cpp::string_view new_str, void *) {
25-
write_to_stderr(new_str);
24+
LIBC_INLINE int stdout_write_hook(cpp::string_view new_str, void *) {
25+
write_to_stdout(new_str);
2626
return printf_core::WRITE_OK;
2727
}
2828

@@ -35,11 +35,11 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
3535
// and pointer semantics, as well as handling
3636
// destruction automatically.
3737
va_end(vlist);
38-
constexpr size_t BUFF_SIZE = 1024;
38+
static constexpr size_t BUFF_SIZE = 1024;
3939
char buffer[BUFF_SIZE];
4040

4141
printf_core::WriteBuffer<printf_core::WriteMode::FLUSH_TO_STREAM> wb(
42-
buffer, BUFF_SIZE, &raw_write_hook, nullptr);
42+
buffer, BUFF_SIZE, &stdout_write_hook, nullptr);
4343
printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> writer(wb);
4444

4545
int retval = printf_core::printf_main(&writer, format, args);

libc/src/stdio/baremetal/putchar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
1616
LLVM_LIBC_FUNCTION(int, putchar, (int c)) {
1717
char uc = static_cast<char>(c);
1818

19-
write_to_stderr(cpp::string_view(&uc, 1));
19+
write_to_stdout(cpp::string_view(&uc, 1));
2020

2121
return 0;
2222
}

libc/src/stdio/baremetal/puts.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ LLVM_LIBC_FUNCTION(int, puts, (const char *__restrict str)) {
1717
cpp::string_view str_view(str);
1818

1919
// TODO: Can we combine these to avoid needing two writes?
20-
write_to_stderr(str_view);
21-
write_to_stderr("\n");
20+
write_to_stdout(str_view);
21+
write_to_stdout("\n");
2222

2323
return 0;
2424
}

libc/src/stdio/baremetal/vprintf.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace LIBC_NAMESPACE_DECL {
2121

2222
namespace {
2323

24-
LIBC_INLINE int raw_write_hook(cpp::string_view new_str, void *) {
25-
write_to_stderr(new_str);
24+
LIBC_INLINE int stdout_write_hook(cpp::string_view new_str, void *) {
25+
write_to_stdout(new_str);
2626
return printf_core::WRITE_OK;
2727
}
2828

@@ -33,11 +33,11 @@ LLVM_LIBC_FUNCTION(int, vprintf,
3333
internal::ArgList args(vlist); // This holder class allows for easier copying
3434
// and pointer semantics, as well as handling
3535
// destruction automatically.
36-
constexpr size_t BUFF_SIZE = 1024;
36+
static constexpr size_t BUFF_SIZE = 1024;
3737
char buffer[BUFF_SIZE];
3838

3939
printf_core::WriteBuffer<printf_core::WriteMode::FLUSH_TO_STREAM> wb(
40-
buffer, BUFF_SIZE, &raw_write_hook, nullptr);
40+
buffer, BUFF_SIZE, &stdout_write_hook, nullptr);
4141
printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> writer(wb);
4242

4343
int retval = printf_core::printf_main(&writer, format, args);

0 commit comments

Comments
 (0)