diff --git a/libc/src/stdio/printf_core/CMakeLists.txt b/libc/src/stdio/printf_core/CMakeLists.txt --- a/libc/src/stdio/printf_core/CMakeLists.txt +++ b/libc/src/stdio/printf_core/CMakeLists.txt @@ -28,6 +28,7 @@ string_writer.h DEPENDS libc.src.string.memory_utils.memcpy_implementation + .core_structs ) add_object_library( @@ -38,6 +39,7 @@ file_writer.h DEPENDS libc.src.__support.File.file + .core_structs ) add_object_library( diff --git a/libc/src/stdio/printf_core/char_converter.h b/libc/src/stdio/printf_core/char_converter.h --- a/libc/src/stdio/printf_core/char_converter.h +++ b/libc/src/stdio/printf_core/char_converter.h @@ -31,7 +31,7 @@ } else { RET_IF_RESULT_NEGATIVE(writer->write(&c, 1)); } - return 0; + return WRITE_OK; } } // namespace printf_core diff --git a/libc/src/stdio/printf_core/core_structs.h b/libc/src/stdio/printf_core/core_structs.h --- a/libc/src/stdio/printf_core/core_structs.h +++ b/libc/src/stdio/printf_core/core_structs.h @@ -78,6 +78,15 @@ return true; } }; + +// This is the value to be returned by conversions when no error has occurred. +constexpr int WRITE_OK = 0; +// 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; + } // namespace printf_core } // namespace __llvm_libc diff --git a/libc/src/stdio/printf_core/file_writer.cpp b/libc/src/stdio/printf_core/file_writer.cpp --- a/libc/src/stdio/printf_core/file_writer.cpp +++ b/libc/src/stdio/printf_core/file_writer.cpp @@ -8,6 +8,7 @@ #include "src/stdio/printf_core/file_writer.h" #include "src/__support/File/file.h" +#include "src/stdio/printf_core/core_structs.h" #include namespace __llvm_libc { @@ -16,9 +17,9 @@ int FileWriter::write(const char *__restrict to_write, size_t len) { int written = file->write_unlocked(to_write, len); if (written != static_cast(len)) - written = -1; + written = FILE_WRITE_ERROR; if (file->error_unlocked()) - written = -2; + written = FILE_STATUS_ERROR; return written; } diff --git a/libc/src/stdio/printf_core/float_hex_converter.h b/libc/src/stdio/printf_core/float_hex_converter.h --- a/libc/src/stdio/printf_core/float_hex_converter.h +++ b/libc/src/stdio/printf_core/float_hex_converter.h @@ -89,7 +89,7 @@ FormatFlags::LEFT_JUSTIFIED)) RET_IF_RESULT_NEGATIVE(writer->write_chars(' ', padding)); - return 0; + return WRITE_OK; } // Handle the exponent for numbers with a 0 exponent @@ -264,7 +264,7 @@ RET_IF_RESULT_NEGATIVE( writer->write(exp_buffer + exp_cur, EXP_LEN - exp_cur)); } - return 0; + return WRITE_OK; } } // namespace printf_core diff --git a/libc/src/stdio/printf_core/hex_converter.h b/libc/src/stdio/printf_core/hex_converter.h --- a/libc/src/stdio/printf_core/hex_converter.h +++ b/libc/src/stdio/printf_core/hex_converter.h @@ -124,7 +124,7 @@ if (digits_written > 0) RET_IF_RESULT_NEGATIVE(writer->write(buffer + buff_cur, digits_written)); } - return 0; + return WRITE_OK; } } // namespace printf_core diff --git a/libc/src/stdio/printf_core/int_converter.h b/libc/src/stdio/printf_core/int_converter.h --- a/libc/src/stdio/printf_core/int_converter.h +++ b/libc/src/stdio/printf_core/int_converter.h @@ -136,7 +136,7 @@ if (digits_written > 0) RET_IF_RESULT_NEGATIVE(writer->write(buffer + buff_cur, digits_written)); } - return 0; + return WRITE_OK; } } // namespace printf_core diff --git a/libc/src/stdio/printf_core/oct_converter.h b/libc/src/stdio/printf_core/oct_converter.h --- a/libc/src/stdio/printf_core/oct_converter.h +++ b/libc/src/stdio/printf_core/oct_converter.h @@ -102,7 +102,7 @@ if (num_digits > 0) RET_IF_RESULT_NEGATIVE(writer->write(buffer + buff_cur, num_digits)); } - return 0; + return WRITE_OK; } } // namespace printf_core diff --git a/libc/src/stdio/printf_core/ptr_converter.h b/libc/src/stdio/printf_core/ptr_converter.h --- a/libc/src/stdio/printf_core/ptr_converter.h +++ b/libc/src/stdio/printf_core/ptr_converter.h @@ -30,7 +30,7 @@ hex_conv.conv_val_raw = reinterpret_cast(to_conv.conv_val_ptr); return convert_hex(writer, hex_conv); } - return 0; + return WRITE_OK; } } // namespace printf_core diff --git a/libc/src/stdio/printf_core/string_converter.h b/libc/src/stdio/printf_core/string_converter.h --- a/libc/src/stdio/printf_core/string_converter.h +++ b/libc/src/stdio/printf_core/string_converter.h @@ -47,7 +47,7 @@ RET_IF_RESULT_NEGATIVE(writer->write( reinterpret_cast(to_conv.conv_val_ptr), string_len)); } - return 0; + return WRITE_OK; } } // namespace printf_core diff --git a/libc/src/stdio/printf_core/string_writer.cpp b/libc/src/stdio/printf_core/string_writer.cpp --- a/libc/src/stdio/printf_core/string_writer.cpp +++ b/libc/src/stdio/printf_core/string_writer.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/stdio/printf_core/string_writer.h" +#include "src/stdio/printf_core/core_structs.h" #include "src/string/memory_utils/memcpy_implementations.h" #include @@ -28,7 +29,7 @@ size_t len) { StringWriter *string_writer = reinterpret_cast(raw_pointer); string_writer->write(to_write, len); - return 0; + return WRITE_OK; } } // namespace printf_core diff --git a/libc/src/stdio/printf_core/write_int_converter.h b/libc/src/stdio/printf_core/write_int_converter.h --- a/libc/src/stdio/printf_core/write_int_converter.h +++ b/libc/src/stdio/printf_core/write_int_converter.h @@ -25,7 +25,7 @@ // because printf uses negative return values for errors, and -1 and -2 are // already in use by the file_writer class for file errors. if (to_conv.conv_val_ptr == nullptr) - return -3; + return NULLPTR_WRITE_ERROR; int written = writer->get_chars_written(); @@ -56,7 +56,7 @@ *reinterpret_cast(to_conv.conv_val_ptr) = written; break; } - return 0; + return WRITE_OK; } } // namespace printf_core