Skip to content
Open
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
24 changes: 24 additions & 0 deletions libc/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ add_entrypoint_object(
DEPENDS
libc.src.stdio.printf_core.printf_main
libc.src.stdio.printf_core.writer
libc.src.stdio.printf_core.core_structs
libc.src.stdio.printf_core.error_mapper
libc.src.__support.libc_errno
libc.src.__support.CPP.limits
)

add_entrypoint_object(
Expand All @@ -136,6 +140,10 @@ add_entrypoint_object(
DEPENDS
libc.src.stdio.printf_core.printf_main
libc.src.stdio.printf_core.writer
libc.src.stdio.printf_core.core_structs
libc.src.stdio.printf_core.error_mapper
libc.src.__support.libc_errno
libc.src.__support.CPP.limits
)

add_entrypoint_object(
Expand All @@ -146,6 +154,10 @@ add_entrypoint_object(
asprintf.h
DEPENDS
libc.src.stdio.printf_core.vasprintf_internal
libc.src.stdio.printf_core.core_structs
libc.src.stdio.printf_core.error_mapper
libc.src.__support.libc_errno
libc.src.__support.CPP.limits
)

add_entrypoint_object(
Expand All @@ -157,6 +169,10 @@ add_entrypoint_object(
DEPENDS
libc.src.stdio.printf_core.printf_main
libc.src.stdio.printf_core.writer
libc.src.stdio.printf_core.core_structs
libc.src.stdio.printf_core.error_mapper
libc.src.__support.libc_errno
libc.src.__support.CPP.limits
)

add_entrypoint_object(
Expand All @@ -168,6 +184,10 @@ add_entrypoint_object(
DEPENDS
libc.src.stdio.printf_core.printf_main
libc.src.stdio.printf_core.writer
libc.src.stdio.printf_core.core_structs
libc.src.stdio.printf_core.error_mapper
libc.src.__support.libc_errno
libc.src.__support.CPP.limits
)

add_entrypoint_object(
Expand All @@ -178,6 +198,10 @@ add_entrypoint_object(
vasprintf.h
DEPENDS
libc.src.stdio.printf_core.vasprintf_internal
libc.src.stdio.printf_core.core_structs
libc.src.stdio.printf_core.error_mapper
libc.src.__support.libc_errno
libc.src.__support.CPP.limits
)

add_subdirectory(printf_core)
Expand Down
18 changes: 16 additions & 2 deletions libc/src/stdio/asprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
//===----------------------------------------------------------------------===//

#include "src/stdio/asprintf.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/arg_list.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/error_mapper.h"
#include "src/stdio/printf_core/vasprintf_internal.h"

namespace LIBC_NAMESPACE_DECL {
Expand All @@ -22,8 +26,18 @@ LLVM_LIBC_FUNCTION(int, asprintf,
// and pointer semantics, as well as handling
// destruction automatically.
va_end(vlist);
int ret = printf_core::vasprintf_internal(buffer, format, args);
return ret;
auto ret_val = printf_core::vasprintf_internal(buffer, format, args);
if (!ret_val.has_value()) {
libc_errno = printf_core::internal_error_to_errno(ret_val.error());
return -1;
}
if (ret_val.value() > static_cast<size_t>(cpp::numeric_limits<int>::max())) {
libc_errno =
printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);
return -1;
}

return static_cast<int>(ret_val.value());
}

} // namespace LIBC_NAMESPACE_DECL
8 changes: 8 additions & 0 deletions libc/src/stdio/baremetal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ add_entrypoint_object(
DEPENDS
libc.src.stdio.printf_core.printf_main
libc.src.stdio.printf_core.writer
libc.src.stdio.printf_core.error_mapper
libc.src.stdio.printf_core.core_structs
libc.src.__support.arg_list
libc.src.__support.OSUtil.osutil
libc.src.__support.libc_errno
libc.src.__support.CPP.limits
)

add_entrypoint_object(
Expand Down Expand Up @@ -87,8 +91,12 @@ add_entrypoint_object(
DEPENDS
libc.src.stdio.printf_core.printf_main
libc.src.stdio.printf_core.writer
libc.src.stdio.printf_core.error_mapper
libc.src.stdio.printf_core.core_structs
libc.src.__support.arg_list
libc.src.__support.OSUtil.osutil
libc.src.__support.libc_errno
libc.src.__support.CPP.limits
)

add_entrypoint_object(
Expand Down
23 changes: 19 additions & 4 deletions libc/src/stdio/baremetal/printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
//===----------------------------------------------------------------------===//

#include "src/stdio/printf.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/OSUtil/io.h"
#include "src/__support/arg_list.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/error_mapper.h"
#include "src/stdio/printf_core/printf_main.h"
#include "src/stdio/printf_core/writer.h"

Expand Down Expand Up @@ -42,13 +45,25 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
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);
auto retval = printf_core::printf_main(&writer, format, args);
if (!retval.has_value()) {
libc_errno = printf_core::internal_error_to_errno(retval.error());
return -1;
}

int flushval = wb.overflow_write("");
if (flushval != printf_core::WRITE_OK)
retval = flushval;
if (flushval != printf_core::WRITE_OK) {
libc_errno = printf_core::internal_error_to_errno(-flushval);
return -1;
}

return retval;
if (retval.value() > static_cast<size_t>(cpp::numeric_limits<int>::max())) {
libc_errno =
printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);
return -1;
}

return static_cast<int>(retval.value());
}

} // namespace LIBC_NAMESPACE_DECL
23 changes: 19 additions & 4 deletions libc/src/stdio/baremetal/vprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
//===----------------------------------------------------------------------===//

#include "src/stdio/vprintf.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/OSUtil/io.h"
#include "src/__support/arg_list.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/error_mapper.h"
#include "src/stdio/printf_core/printf_main.h"
#include "src/stdio/printf_core/writer.h"

Expand Down Expand Up @@ -40,13 +43,25 @@ LLVM_LIBC_FUNCTION(int, vprintf,
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);
auto retval = printf_core::printf_main(&writer, format, args);
if (!retval.has_value()) {
libc_errno = printf_core::internal_error_to_errno(retval.error());
return -1;
}

int flushval = wb.overflow_write("");
if (flushval != printf_core::WRITE_OK)
retval = flushval;
if (flushval != printf_core::WRITE_OK) {
libc_errno = printf_core::internal_error_to_errno(-flushval);
return -1;
}

return retval;
if (retval.value() > static_cast<size_t>(cpp::numeric_limits<int>::max())) {
libc_errno =
printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);
return -1;
}

return static_cast<int>(retval.value());
}

} // namespace LIBC_NAMESPACE_DECL
4 changes: 4 additions & 0 deletions libc/src/stdio/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,11 @@ add_generic_entrypoint_object(
list(APPEND fprintf_deps
libc.hdr.types.FILE
libc.src.__support.arg_list
libc.src.__support.CPP.limits
libc.src.__support.libc_errno
libc.src.stdio.printf_core.vfprintf_internal
libc.src.stdio.printf_core.core_structs
libc.src.stdio.printf_core.error_mapper
)

if(LLVM_LIBC_FULL_BUILD)
Expand Down
17 changes: 15 additions & 2 deletions libc/src/stdio/generic/fprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

#include "src/stdio/fprintf.h"

#include "src/__support/CPP/limits.h"
#include "src/__support/File/file.h"
#include "src/__support/arg_list.h"
#include "src/__support/macros/config.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/error_mapper.h"
#include "src/stdio/printf_core/vfprintf_internal.h"

#include "hdr/types/FILE.h"
Expand All @@ -27,8 +30,18 @@ LLVM_LIBC_FUNCTION(int, fprintf,
// and pointer semantics, as well as handling
// destruction automatically.
va_end(vlist);
int ret_val = printf_core::vfprintf_internal(stream, format, args);
return ret_val;
auto ret_val = printf_core::vfprintf_internal(stream, format, args);
if (!ret_val.has_value()) {
libc_errno = printf_core::internal_error_to_errno(ret_val.error());
return -1;
}
if (ret_val.value() > static_cast<size_t>(cpp::numeric_limits<int>::max())) {
libc_errno =
printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);
return -1;
}

return static_cast<int>(ret_val.value());
}

} // namespace LIBC_NAMESPACE_DECL
17 changes: 15 additions & 2 deletions libc/src/stdio/generic/printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

#include "src/stdio/printf.h"

#include "src/__support/CPP/limits.h"
#include "src/__support/File/file.h"
#include "src/__support/arg_list.h"
#include "src/__support/macros/config.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/error_mapper.h"
#include "src/stdio/printf_core/vfprintf_internal.h"

#include "hdr/types/FILE.h"
Expand All @@ -31,9 +34,19 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
// and pointer semantics, as well as handling
// destruction automatically.
va_end(vlist);
int ret_val = printf_core::vfprintf_internal(
auto ret_val = printf_core::vfprintf_internal(
reinterpret_cast<::FILE *>(PRINTF_STDOUT), format, args);
return ret_val;
if (!ret_val.has_value()) {
libc_errno = printf_core::internal_error_to_errno(ret_val.error());
return -1;
}
if (ret_val.value() > static_cast<size_t>(cpp::numeric_limits<int>::max())) {
libc_errno =
printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);
return -1;
}

return static_cast<int>(ret_val.value());
}

} // namespace LIBC_NAMESPACE_DECL
17 changes: 15 additions & 2 deletions libc/src/stdio/generic/vfprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

#include "src/stdio/vfprintf.h"

#include "src/__support/CPP/limits.h"
#include "src/__support/File/file.h"
#include "src/__support/arg_list.h"
#include "src/__support/macros/config.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/error_mapper.h"
#include "src/stdio/printf_core/vfprintf_internal.h"

#include "hdr/types/FILE.h"
Expand All @@ -24,8 +27,18 @@ LLVM_LIBC_FUNCTION(int, vfprintf,
internal::ArgList args(vlist); // This holder class allows for easier copying
// and pointer semantics, as well as handling
// destruction automatically.
int ret_val = printf_core::vfprintf_internal(stream, format, args);
return ret_val;
auto ret_val = printf_core::vfprintf_internal(stream, format, args);
if (!ret_val.has_value()) {
libc_errno = printf_core::internal_error_to_errno(ret_val.error());
return -1;
}
if (ret_val.value() > static_cast<size_t>(cpp::numeric_limits<int>::max())) {
libc_errno =
printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);
return -1;
}

return static_cast<int>(ret_val.value());
}

} // namespace LIBC_NAMESPACE_DECL
17 changes: 15 additions & 2 deletions libc/src/stdio/generic/vprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

#include "src/stdio/vprintf.h"

#include "src/__support/CPP/limits.h"
#include "src/__support/File/file.h"
#include "src/__support/arg_list.h"
#include "src/__support/macros/config.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/error_mapper.h"
#include "src/stdio/printf_core/vfprintf_internal.h"

#include "hdr/types/FILE.h"
Expand All @@ -29,9 +32,19 @@ 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.
int ret_val = printf_core::vfprintf_internal(
auto ret_val = printf_core::vfprintf_internal(
reinterpret_cast<::FILE *>(PRINTF_STDOUT), format, args);
return ret_val;
if (!ret_val.has_value()) {
libc_errno = printf_core::internal_error_to_errno(ret_val.error());
return -1;
}
if (ret_val.value() > static_cast<size_t>(cpp::numeric_limits<int>::max())) {
libc_errno =
printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);
return -1;
}

return static_cast<int>(ret_val.value());
}

} // namespace LIBC_NAMESPACE_DECL
Loading
Loading