diff options
Diffstat (limited to 'libc/src/stdio')
27 files changed, 67 insertions, 456 deletions
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt index c75c8b11be2b..b0a6ef1e291b 100644 --- a/libc/src/stdio/CMakeLists.txt +++ b/libc/src/stdio/CMakeLists.txt @@ -125,10 +125,6 @@ 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( @@ -140,10 +136,6 @@ 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( @@ -154,10 +146,6 @@ 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( @@ -169,10 +157,6 @@ 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( @@ -184,10 +168,6 @@ 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( @@ -198,10 +178,6 @@ 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) diff --git a/libc/src/stdio/asprintf.cpp b/libc/src/stdio/asprintf.cpp index 0991dfca6a05..f8cfb74ce48e 100644 --- a/libc/src/stdio/asprintf.cpp +++ b/libc/src/stdio/asprintf.cpp @@ -7,12 +7,8 @@ //===----------------------------------------------------------------------===// #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 { @@ -26,18 +22,8 @@ LLVM_LIBC_FUNCTION(int, asprintf, // and pointer semantics, as well as handling // destruction automatically. va_end(vlist); - 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()); + int ret = printf_core::vasprintf_internal(buffer, format, args); + return ret; } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/baremetal/CMakeLists.txt b/libc/src/stdio/baremetal/CMakeLists.txt index bfeff0e2b588..548938f885c9 100644 --- a/libc/src/stdio/baremetal/CMakeLists.txt +++ b/libc/src/stdio/baremetal/CMakeLists.txt @@ -29,12 +29,8 @@ 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( @@ -91,12 +87,8 @@ 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( diff --git a/libc/src/stdio/baremetal/printf.cpp b/libc/src/stdio/baremetal/printf.cpp index 5a9b19ff2047..7253c6549a4e 100644 --- a/libc/src/stdio/baremetal/printf.cpp +++ b/libc/src/stdio/baremetal/printf.cpp @@ -7,13 +7,10 @@ //===----------------------------------------------------------------------===// #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" @@ -45,25 +42,13 @@ 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); - 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 retval = printf_core::printf_main(&writer, format, args); int flushval = wb.overflow_write(""); - if (flushval != printf_core::WRITE_OK) { - libc_errno = printf_core::internal_error_to_errno(-flushval); - return -1; - } + if (flushval != printf_core::WRITE_OK) + retval = flushval; - 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()); + return retval; } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/baremetal/vprintf.cpp b/libc/src/stdio/baremetal/vprintf.cpp index c172b368d15f..ab02533f1491 100644 --- a/libc/src/stdio/baremetal/vprintf.cpp +++ b/libc/src/stdio/baremetal/vprintf.cpp @@ -7,13 +7,10 @@ //===----------------------------------------------------------------------===// #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" @@ -43,25 +40,13 @@ LLVM_LIBC_FUNCTION(int, vprintf, buffer, BUFF_SIZE, &stdout_write_hook, nullptr); printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> writer(wb); - 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 retval = printf_core::printf_main(&writer, format, args); int flushval = wb.overflow_write(""); - if (flushval != printf_core::WRITE_OK) { - libc_errno = printf_core::internal_error_to_errno(-flushval); - return -1; - } + if (flushval != printf_core::WRITE_OK) + retval = flushval; - 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()); + return retval; } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/generic/CMakeLists.txt b/libc/src/stdio/generic/CMakeLists.txt index 71055edea3d9..6361822b6199 100644 --- a/libc/src/stdio/generic/CMakeLists.txt +++ b/libc/src/stdio/generic/CMakeLists.txt @@ -393,11 +393,7 @@ 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) diff --git a/libc/src/stdio/generic/fprintf.cpp b/libc/src/stdio/generic/fprintf.cpp index b2033901557a..087aeadfc52c 100644 --- a/libc/src/stdio/generic/fprintf.cpp +++ b/libc/src/stdio/generic/fprintf.cpp @@ -8,12 +8,9 @@ #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" @@ -30,18 +27,8 @@ LLVM_LIBC_FUNCTION(int, fprintf, // and pointer semantics, as well as handling // destruction automatically. va_end(vlist); - 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()); + int ret_val = printf_core::vfprintf_internal(stream, format, args); + return ret_val; } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/generic/printf.cpp b/libc/src/stdio/generic/printf.cpp index 8d159d5c7087..bb7c7c86f843 100644 --- a/libc/src/stdio/generic/printf.cpp +++ b/libc/src/stdio/generic/printf.cpp @@ -8,12 +8,9 @@ #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" @@ -34,19 +31,9 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) { // and pointer semantics, as well as handling // destruction automatically. va_end(vlist); - auto ret_val = printf_core::vfprintf_internal( + int ret_val = printf_core::vfprintf_internal( reinterpret_cast<::FILE *>(PRINTF_STDOUT), 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()); + return ret_val; } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/generic/vfprintf.cpp b/libc/src/stdio/generic/vfprintf.cpp index a26f082ed934..01f4265f118a 100644 --- a/libc/src/stdio/generic/vfprintf.cpp +++ b/libc/src/stdio/generic/vfprintf.cpp @@ -8,12 +8,9 @@ #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" @@ -27,18 +24,8 @@ 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. - 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()); + int ret_val = printf_core::vfprintf_internal(stream, format, args); + return ret_val; } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/generic/vprintf.cpp b/libc/src/stdio/generic/vprintf.cpp index ae2160219f2b..08d71515646e 100644 --- a/libc/src/stdio/generic/vprintf.cpp +++ b/libc/src/stdio/generic/vprintf.cpp @@ -8,12 +8,9 @@ #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" @@ -32,19 +29,9 @@ 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. - auto ret_val = printf_core::vfprintf_internal( + int ret_val = printf_core::vfprintf_internal( reinterpret_cast<::FILE *>(PRINTF_STDOUT), 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()); + return ret_val; } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/printf_core/CMakeLists.txt b/libc/src/stdio/printf_core/CMakeLists.txt index 624129b2b36e..ee66145e6015 100644 --- a/libc/src/stdio/printf_core/CMakeLists.txt +++ b/libc/src/stdio/printf_core/CMakeLists.txt @@ -32,17 +32,6 @@ if(printf_config_copts) list(PREPEND printf_config_copts "COMPILE_OPTIONS") endif() -if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) - add_subdirectory(${LIBC_TARGET_OS}) -else() - add_subdirectory(generic) -endif() - -set(target_error_mapper libc.src.stdio.printf_core.${LIBC_TARGET_OS}.error_mapper) -if(NOT TARGET ${target_error_mapper}) - set(target_error_mapper libc.src.stdio.printf_core.generic.error_mapper) -endif() - add_header_library( printf_config HDRS @@ -58,7 +47,6 @@ add_header_library( libc.include.inttypes libc.src.__support.CPP.string_view libc.src.__support.FPUtil.fp_bits - libc.hdr.errno_macros ) add_header_library( @@ -137,7 +125,6 @@ add_header_library( .writer .core_structs libc.src.__support.arg_list - libc.src.__support.error_or ) add_header_library( @@ -149,20 +136,10 @@ add_header_library( libc.hdr.func.free libc.hdr.func.realloc libc.src.__support.arg_list - libc.src.__support.error_or libc.src.stdio.printf_core.printf_main libc.src.stdio.printf_core.writer ) -add_header_library( - error_mapper - HDRS - error_mapper.h - DEPENDS - ${target_error_mapper} - libc.src.__support.macros.properties.architectures -) - if(NOT (TARGET libc.src.__support.File.file) AND LLVM_LIBC_FULL_BUILD) # Not all platforms have a file implementation. If file is unvailable, and a # full build is requested, then we must skip all file based printf sections. @@ -175,10 +152,8 @@ add_header_library( vfprintf_internal.h DEPENDS libc.src.__support.File.file - libc.src.__support.error_or libc.src.__support.arg_list libc.src.stdio.printf_core.printf_main libc.src.stdio.printf_core.writer ${use_system_file} ) - diff --git a/libc/src/stdio/printf_core/core_structs.h b/libc/src/stdio/printf_core/core_structs.h index 0d41f2244d8d..e27f77b6b594 100644 --- a/libc/src/stdio/printf_core/core_structs.h +++ b/libc/src/stdio/printf_core/core_structs.h @@ -132,17 +132,14 @@ template <typename T> LIBC_INLINE constexpr TypeDesc type_desc_from_type() { // This is the value to be returned by conversions when no error has occurred. constexpr int WRITE_OK = 0; -// These are the error return values used by the printf engine when an -// error has occurred. They are all large negative, distinct values starting -// from -1000 to not overlap with system errors. -constexpr int FILE_WRITE_ERROR = -1001; -constexpr int FILE_STATUS_ERROR = -1002; -constexpr int NULLPTR_WRITE_ERROR = -1003; -constexpr int INT_CONVERSION_ERROR = -1004; -constexpr int FIXED_POINT_CONVERSION_ERROR = -1005; -constexpr int ALLOCATION_ERROR = -1006; -constexpr int OVERFLOW_ERROR = -1007; - +// These are the printf return values for when an error has occurred. They are +// all negative, and should be distinct. +constexpr int FILE_WRITE_ERROR = -1; +constexpr int FILE_STATUS_ERROR = -2; +constexpr int NULLPTR_WRITE_ERROR = -3; +constexpr int INT_CONVERSION_ERROR = -4; +constexpr int FIXED_POINT_CONVERSION_ERROR = -5; +constexpr int ALLOCATION_ERROR = -6; } // namespace printf_core } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/printf_core/error_mapper.h b/libc/src/stdio/printf_core/error_mapper.h deleted file mode 100644 index 23030930133a..000000000000 --- a/libc/src/stdio/printf_core/error_mapper.h +++ /dev/null @@ -1,21 +0,0 @@ -//===-- Error mapper for printf ---------------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_ERROR_MAPPER_H -#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_ERROR_MAPPER_H - -#include "src/__support/macros/properties/architectures.h" - -// Maps internal errors to the available errnos on the platform. -#if defined(__linux__) -#include "linux/error_mapper.h" -#else -#include "generic/error_mapper.h" -#endif - -#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_ERROR_MAPPER_H diff --git a/libc/src/stdio/printf_core/generic/CMakeLists.txt b/libc/src/stdio/printf_core/generic/CMakeLists.txt deleted file mode 100644 index 2f0143d992e3..000000000000 --- a/libc/src/stdio/printf_core/generic/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -add_header_library( - error_mapper - HDRS - error_mapper.h - DEPENDS - libc.src.stdio.printf_core.core_structs - libc.hdr.errno_macros -) diff --git a/libc/src/stdio/printf_core/generic/error_mapper.h b/libc/src/stdio/printf_core/generic/error_mapper.h deleted file mode 100644 index d8cdd2cc2dba..000000000000 --- a/libc/src/stdio/printf_core/generic/error_mapper.h +++ /dev/null @@ -1,49 +0,0 @@ -//===-- Generic implementation of error mapper ------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_GENERIC_ERROR_MAPPER_H -#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_GENERIC_ERROR_MAPPER_H - -#include "hdr/errno_macros.h" -#include "src/stdio/printf_core/core_structs.h" -#include "src/stdio/printf_core/error_mapper.h" - -namespace LIBC_NAMESPACE_DECL { -namespace printf_core { - -LIBC_INLINE static int internal_error_to_errno(int internal_error) { - // System error occured, return error as is. - if (internal_error < 1001 && internal_error > 0) { - return internal_error; - } - - // Map internal error to the available C standard errnos. - switch (-internal_error) { - case WRITE_OK: - return 0; - case FILE_WRITE_ERROR: - case FILE_STATUS_ERROR: - case NULLPTR_WRITE_ERROR: - case ALLOCATION_ERROR: - return EDOM; - case INT_CONVERSION_ERROR: - case FIXED_POINT_CONVERSION_ERROR: - case OVERFLOW_ERROR: - return ERANGE; - default: - LIBC_ASSERT( - false && - "Invalid internal printf error code passed to internal_error_to_errno"); - return EDOM; - } -} - -} // namespace printf_core -} // namespace LIBC_NAMESPACE_DECL - -#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_GENERIC_ERROR_MAPPER_H diff --git a/libc/src/stdio/printf_core/linux/CMakeLists.txt b/libc/src/stdio/printf_core/linux/CMakeLists.txt deleted file mode 100644 index 2f0143d992e3..000000000000 --- a/libc/src/stdio/printf_core/linux/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -add_header_library( - error_mapper - HDRS - error_mapper.h - DEPENDS - libc.src.stdio.printf_core.core_structs - libc.hdr.errno_macros -) diff --git a/libc/src/stdio/printf_core/linux/error_mapper.h b/libc/src/stdio/printf_core/linux/error_mapper.h deleted file mode 100644 index 3c2fe663072d..000000000000 --- a/libc/src/stdio/printf_core/linux/error_mapper.h +++ /dev/null @@ -1,54 +0,0 @@ -//===-- Linux implementation of error mapper --------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_LINUX_ERROR_MAPPER_H -#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_LINUX_ERROR_MAPPER_H - -#include "hdr/errno_macros.h" -#include "src/stdio/printf_core/core_structs.h" -#include "src/stdio/printf_core/error_mapper.h" - -namespace LIBC_NAMESPACE_DECL { -namespace printf_core { - -LIBC_INLINE static int internal_error_to_errno(int internal_error) { - // System error occured, return error as is. - if (internal_error < 1001 && internal_error > 0) { - return internal_error; - } - - // Map internal error to POSIX errnos. - switch (-internal_error) { - case WRITE_OK: - return 0; - case FILE_WRITE_ERROR: - return EIO; - case FILE_STATUS_ERROR: - return EIO; - case NULLPTR_WRITE_ERROR: - return EINVAL; - case INT_CONVERSION_ERROR: - return ERANGE; - case FIXED_POINT_CONVERSION_ERROR: - return EINVAL; - case ALLOCATION_ERROR: - return ENOMEM; - case OVERFLOW_ERROR: - return EOVERFLOW; - default: - LIBC_ASSERT( - false && - "Invalid internal printf error code passed to internal_error_to_errno"); - return EINVAL; - } -} - -} // namespace printf_core -} // namespace LIBC_NAMESPACE_DECL - -#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_LINUX_ERROR_MAPPER_H diff --git a/libc/src/stdio/printf_core/printf_main.h b/libc/src/stdio/printf_core/printf_main.h index 1c7a7237c097..57f29858d529 100644 --- a/libc/src/stdio/printf_core/printf_main.h +++ b/libc/src/stdio/printf_core/printf_main.h @@ -10,7 +10,6 @@ #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_MAIN_H #include "src/__support/arg_list.h" -#include "src/__support/error_or.h" #include "src/__support/macros/config.h" #include "src/stdio/printf_core/converter.h" #include "src/stdio/printf_core/core_structs.h" @@ -23,9 +22,8 @@ namespace LIBC_NAMESPACE_DECL { namespace printf_core { template <WriteMode write_mode> -ErrorOr<size_t> printf_main(Writer<write_mode> *writer, - const char *__restrict str, - internal::ArgList &args) { +int printf_main(Writer<write_mode> *writer, const char *__restrict str, + internal::ArgList &args) { Parser<internal::ArgList> parser(str, args); int result = 0; for (FormatSection cur_section = parser.get_next_section(); @@ -35,8 +33,9 @@ ErrorOr<size_t> printf_main(Writer<write_mode> *writer, result = convert(writer, cur_section); else result = writer->write(cur_section.raw_string); + if (result < 0) - return Error(-result); + return result; } return writer->get_chars_written(); diff --git a/libc/src/stdio/printf_core/vasprintf_internal.h b/libc/src/stdio/printf_core/vasprintf_internal.h index 41df17b67f35..283d8df2810f 100644 --- a/libc/src/stdio/printf_core/vasprintf_internal.h +++ b/libc/src/stdio/printf_core/vasprintf_internal.h @@ -10,7 +10,6 @@ #include "hdr/func/malloc.h" #include "hdr/func/realloc.h" #include "src/__support/arg_list.h" -#include "src/__support/error_or.h" #include "src/stdio/printf_core/core_structs.h" #include "src/stdio/printf_core/printf_main.h" #include "src/stdio/printf_core/writer.h" @@ -30,7 +29,7 @@ LIBC_INLINE int resize_overflow_hook(cpp::string_view new_str, void *target) { if (new_buff == nullptr) { if (wb->buff != wb->init_buff) free(wb->buff); - return ALLOCATION_ERROR; + return printf_core::ALLOCATION_ERROR; } if (isBuffOnStack) inline_memcpy(new_buff, wb->buff, wb->buff_cur); @@ -43,28 +42,27 @@ LIBC_INLINE int resize_overflow_hook(cpp::string_view new_str, void *target) { constexpr size_t DEFAULT_BUFFER_SIZE = 200; -LIBC_INLINE ErrorOr<size_t> vasprintf_internal(char **ret, - const char *__restrict format, - internal::ArgList args) { +LIBC_INLINE int vasprintf_internal(char **ret, const char *__restrict format, + internal::ArgList args) { char init_buff_on_stack[DEFAULT_BUFFER_SIZE]; printf_core::WriteBuffer<Mode<WriteMode::RESIZE_AND_FILL_BUFF>::value> wb( init_buff_on_stack, DEFAULT_BUFFER_SIZE, resize_overflow_hook); printf_core::Writer writer(wb); auto ret_val = printf_core::printf_main(&writer, format, args); - if (!ret_val.has_value()) { + if (ret_val < 0) { *ret = nullptr; - return ret_val; + return -1; } if (wb.buff == init_buff_on_stack) { - *ret = static_cast<char *>(malloc(ret_val.value() + 1)); + *ret = static_cast<char *>(malloc(ret_val + 1)); if (ret == nullptr) - return Error(ALLOCATION_ERROR); - inline_memcpy(*ret, wb.buff, ret_val.value()); + return printf_core::ALLOCATION_ERROR; + inline_memcpy(*ret, wb.buff, ret_val); } else { *ret = wb.buff; } - (*ret)[ret_val.value()] = '\0'; + (*ret)[ret_val] = '\0'; return ret_val; } } // namespace printf_core diff --git a/libc/src/stdio/printf_core/vfprintf_internal.h b/libc/src/stdio/printf_core/vfprintf_internal.h index 564441d3bf51..630de9d9d43d 100644 --- a/libc/src/stdio/printf_core/vfprintf_internal.h +++ b/libc/src/stdio/printf_core/vfprintf_internal.h @@ -11,7 +11,6 @@ #include "src/__support/File/file.h" #include "src/__support/arg_list.h" -#include "src/__support/error_or.h" #include "src/__support/macros/attributes.h" // For LIBC_INLINE #include "src/__support/macros/config.h" #include "src/stdio/printf_core/core_structs.h" @@ -36,8 +35,8 @@ LIBC_INLINE void funlockfile(FILE *f) { reinterpret_cast<LIBC_NAMESPACE::File *>(f)->unlock(); } -LIBC_INLINE FileIOResult fwrite_unlocked(const void *ptr, size_t size, - size_t nmemb, FILE *f) { +LIBC_INLINE size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb, + FILE *f) { return reinterpret_cast<LIBC_NAMESPACE::File *>(f)->write_unlocked( ptr, size * nmemb); } @@ -48,11 +47,9 @@ LIBC_INLINE void flockfile(::FILE *f) { ::flockfile(f); } LIBC_INLINE void funlockfile(::FILE *f) { ::funlockfile(f); } -LIBC_INLINE FileIOResult fwrite_unlocked(const void *ptr, size_t size, - size_t nmemb, ::FILE *f) { - // Need to use system errno in this case, as system write will set this errno - // which we need to propagate back into our code. - return {::fwrite_unlocked(ptr, size, nmemb, f), errno}; +LIBC_INLINE size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb, + ::FILE *f) { + return ::fwrite_unlocked(ptr, size, nmemb, f); } #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE } // namespace internal @@ -63,38 +60,26 @@ LIBC_INLINE int file_write_hook(cpp::string_view new_str, void *fp) { ::FILE *target_file = reinterpret_cast<::FILE *>(fp); // Write new_str to the target file. The logic preventing a zero-length write // is in the writer, so we don't check here. - auto write_result = internal::fwrite_unlocked(new_str.data(), sizeof(char), - new_str.size(), target_file); - // Propagate actual system error in FileIOResult. - if (write_result.has_error()) - return -write_result.error; - - // In case short write occured or error was not set on FileIOResult for some - // reason. - if (write_result.value != new_str.size() || - internal::ferror_unlocked(target_file)) + size_t written = internal::fwrite_unlocked(new_str.data(), sizeof(char), + new_str.size(), target_file); + if (written != new_str.size() || internal::ferror_unlocked(target_file)) return FILE_WRITE_ERROR; - return WRITE_OK; } -LIBC_INLINE ErrorOr<size_t> vfprintf_internal(::FILE *__restrict stream, - const char *__restrict format, - internal::ArgList &args) { +LIBC_INLINE int vfprintf_internal(::FILE *__restrict stream, + const char *__restrict format, + internal::ArgList &args) { constexpr size_t BUFF_SIZE = 1024; char buffer[BUFF_SIZE]; printf_core::WriteBuffer<Mode<WriteMode::FLUSH_TO_STREAM>::value> wb( buffer, BUFF_SIZE, &file_write_hook, reinterpret_cast<void *>(stream)); Writer writer(wb); internal::flockfile(stream); - auto retval = printf_main(&writer, format, args); - if (!retval.has_value()) { - internal::funlockfile(stream); - return retval; - } + int retval = printf_main(&writer, format, args); int flushval = wb.overflow_write(""); if (flushval != WRITE_OK) - retval = Error(-flushval); + retval = flushval; internal::funlockfile(stream); return retval; } diff --git a/libc/src/stdio/printf_core/write_int_converter.h b/libc/src/stdio/printf_core/write_int_converter.h index 04b2bef05bc7..efcff278bd28 100644 --- a/libc/src/stdio/printf_core/write_int_converter.h +++ b/libc/src/stdio/printf_core/write_int_converter.h @@ -29,11 +29,11 @@ LIBC_INLINE int convert_write_int(Writer<write_mode> *writer, return NULLPTR_WRITE_ERROR; #endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS - size_t written = writer->get_chars_written(); + int written = writer->get_chars_written(); switch (to_conv.length_modifier) { case LengthModifier::none: - *reinterpret_cast<int *>(to_conv.conv_val_ptr) = static_cast<int>(written); + *reinterpret_cast<int *>(to_conv.conv_val_ptr) = written; break; case LengthModifier::l: *reinterpret_cast<long *>(to_conv.conv_val_ptr) = written; diff --git a/libc/src/stdio/printf_core/writer.h b/libc/src/stdio/printf_core/writer.h index 9de108ece510..1d4734a51b9b 100644 --- a/libc/src/stdio/printf_core/writer.h +++ b/libc/src/stdio/printf_core/writer.h @@ -127,7 +127,7 @@ template <WriteMode write_mode> struct WriteBuffer { template <WriteMode write_mode> class Writer final { WriteBuffer<write_mode> &wb; - size_t chars_written = 0; + int chars_written = 0; LIBC_INLINE int pad(char new_char, size_t length) { // First, fill as much of the buffer as possible with the padding char. @@ -161,7 +161,7 @@ public: // Takes a string, copies it into the buffer if there is space, else passes it // to the overflow mechanism to be handled separately. LIBC_INLINE int write(cpp::string_view new_string) { - chars_written += new_string.size(); + chars_written += static_cast<int>(new_string.size()); if (LIBC_LIKELY(wb.buff_cur + new_string.size() <= wb.buff_len)) { inline_memcpy(wb.buff + wb.buff_cur, new_string.data(), new_string.size()); @@ -175,7 +175,7 @@ public: // if there is space, else calls pad which will loop and call the overflow // mechanism on a secondary buffer. LIBC_INLINE int write(char new_char, size_t length) { - chars_written += length; + chars_written += static_cast<int>(length); if (LIBC_LIKELY(wb.buff_cur + length <= wb.buff_len)) { inline_memset(wb.buff + wb.buff_cur, static_cast<unsigned char>(new_char), @@ -199,7 +199,7 @@ public: return wb.overflow_write(char_string_view); } - LIBC_INLINE size_t get_chars_written() { return chars_written; } + LIBC_INLINE int get_chars_written() { return chars_written; } }; // Class-template auto deduction helpers. diff --git a/libc/src/stdio/snprintf.cpp b/libc/src/stdio/snprintf.cpp index d95195f6f485..c8940862f711 100644 --- a/libc/src/stdio/snprintf.cpp +++ b/libc/src/stdio/snprintf.cpp @@ -8,12 +8,8 @@ #include "src/stdio/snprintf.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/printf_main.h" #include "src/stdio/printf_core/writer.h" @@ -36,21 +32,10 @@ LLVM_LIBC_FUNCTION(int, snprintf, wb(buffer, (buffsz > 0 ? buffsz - 1 : 0)); printf_core::Writer writer(wb); - auto ret_val = printf_core::printf_main(&writer, format, args); - if (!ret_val.has_value()) { - libc_errno = printf_core::internal_error_to_errno(ret_val.error()); - return -1; - } + int ret_val = printf_core::printf_main(&writer, format, args); if (buffsz > 0) // if the buffsz is 0 the buffer may be a null pointer. wb.buff[wb.buff_cur] = '\0'; - - 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()); + return ret_val; } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/sprintf.cpp b/libc/src/stdio/sprintf.cpp index 2a9b6ea7c5e5..7be97d3591aa 100644 --- a/libc/src/stdio/sprintf.cpp +++ b/libc/src/stdio/sprintf.cpp @@ -10,10 +10,7 @@ #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/printf_main.h" #include "src/stdio/printf_core/writer.h" @@ -36,20 +33,9 @@ LLVM_LIBC_FUNCTION(int, sprintf, wb(buffer, cpp::numeric_limits<size_t>::max()); printf_core::Writer writer(wb); - auto ret_val = printf_core::printf_main(&writer, format, args); - if (!ret_val.has_value()) { - libc_errno = printf_core::internal_error_to_errno(ret_val.error()); - return -1; - } + int ret_val = printf_core::printf_main(&writer, format, args); wb.buff[wb.buff_cur] = '\0'; - - 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()); + return ret_val; } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/vasprintf.cpp b/libc/src/stdio/vasprintf.cpp index bd77cd886431..4a44d4a0f884 100644 --- a/libc/src/stdio/vasprintf.cpp +++ b/libc/src/stdio/vasprintf.cpp @@ -7,11 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/stdio/vasprintf.h" -#include "src/__support/CPP/limits.h" #include "src/__support/arg_list.h" -#include "src/__support/libc_errno.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 { @@ -22,17 +18,7 @@ LLVM_LIBC_FUNCTION(int, vasprintf, internal::ArgList args(vlist); // This holder class allows for easier copying // and pointer semantics, as well as handling // destruction automatically. - auto ret_val = printf_core::vasprintf_internal(ret, 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()); + return printf_core::vasprintf_internal(ret, format, args); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/vsnprintf.cpp b/libc/src/stdio/vsnprintf.cpp index 5d936360c085..b07a2499a0dd 100644 --- a/libc/src/stdio/vsnprintf.cpp +++ b/libc/src/stdio/vsnprintf.cpp @@ -8,12 +8,8 @@ #include "src/stdio/vsnprintf.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/printf_main.h" #include "src/stdio/printf_core/writer.h" @@ -33,21 +29,10 @@ LLVM_LIBC_FUNCTION(int, vsnprintf, wb(buffer, (buffsz > 0 ? buffsz - 1 : 0)); printf_core::Writer writer(wb); - auto ret_val = printf_core::printf_main(&writer, format, args); - if (!ret_val.has_value()) { - libc_errno = printf_core::internal_error_to_errno(ret_val.error()); - return -1; - } + int ret_val = printf_core::printf_main(&writer, format, args); if (buffsz > 0) // if the buffsz is 0 the buffer may be a null pointer. wb.buff[wb.buff_cur] = '\0'; - - 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()); + return ret_val; } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdio/vsprintf.cpp b/libc/src/stdio/vsprintf.cpp index f9cf8118534f..26d497be4212 100644 --- a/libc/src/stdio/vsprintf.cpp +++ b/libc/src/stdio/vsprintf.cpp @@ -10,10 +10,7 @@ #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/printf_main.h" #include "src/stdio/printf_core/writer.h" @@ -33,19 +30,9 @@ LLVM_LIBC_FUNCTION(int, vsprintf, wb(buffer, cpp::numeric_limits<size_t>::max()); printf_core::Writer writer(wb); - auto ret_val = printf_core::printf_main(&writer, format, args); - if (!ret_val.has_value()) { - libc_errno = printf_core::internal_error_to_errno(ret_val.error()); - return -1; - } + int ret_val = printf_core::printf_main(&writer, format, args); wb.buff[wb.buff_cur] = '\0'; - - 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()); + return ret_val; } } // namespace LIBC_NAMESPACE_DECL |
