diff options
author | Michael Jones <michaelrj@google.com> | 2022-06-28 15:08:00 -0700 |
---|---|---|
committer | Michael Jones <michaelrj@google.com> | 2022-07-11 16:49:47 -0700 |
commit | 9e421a1633247d5e113a55159f5ee3dde009d82d (patch) | |
tree | aa79b6884735478022c94c9484d55f02e372fa01 /libc | |
parent | 9bc34636a50ffd9d417d30af021798b6294a1725 (diff) | |
download | llvm-9e421a1633247d5e113a55159f5ee3dde009d82d.zip llvm-9e421a1633247d5e113a55159f5ee3dde009d82d.tar.gz llvm-9e421a1633247d5e113a55159f5ee3dde009d82d.tar.bz2 |
[libc] clean up printf error codes
Move the constants for printf's return values into core_structs, and
update the converters to match.
Reviewed By: lntue
Differential Revision: https://reviews.llvm.org/D128767
Diffstat (limited to 'libc')
-rw-r--r-- | libc/src/stdio/printf_core/CMakeLists.txt | 2 | ||||
-rw-r--r-- | libc/src/stdio/printf_core/char_converter.h | 2 | ||||
-rw-r--r-- | libc/src/stdio/printf_core/core_structs.h | 9 | ||||
-rw-r--r-- | libc/src/stdio/printf_core/file_writer.cpp | 5 | ||||
-rw-r--r-- | libc/src/stdio/printf_core/float_hex_converter.h | 4 | ||||
-rw-r--r-- | libc/src/stdio/printf_core/hex_converter.h | 2 | ||||
-rw-r--r-- | libc/src/stdio/printf_core/int_converter.h | 2 | ||||
-rw-r--r-- | libc/src/stdio/printf_core/oct_converter.h | 2 | ||||
-rw-r--r-- | libc/src/stdio/printf_core/ptr_converter.h | 2 | ||||
-rw-r--r-- | libc/src/stdio/printf_core/string_converter.h | 2 | ||||
-rw-r--r-- | libc/src/stdio/printf_core/string_writer.cpp | 3 | ||||
-rw-r--r-- | libc/src/stdio/printf_core/write_int_converter.h | 4 |
12 files changed, 26 insertions, 13 deletions
diff --git a/libc/src/stdio/printf_core/CMakeLists.txt b/libc/src/stdio/printf_core/CMakeLists.txt index 565b968..ab94d84 100644 --- a/libc/src/stdio/printf_core/CMakeLists.txt +++ b/libc/src/stdio/printf_core/CMakeLists.txt @@ -28,6 +28,7 @@ add_object_library( string_writer.h DEPENDS libc.src.string.memory_utils.memcpy_implementation + .core_structs ) add_object_library( @@ -38,6 +39,7 @@ add_object_library( 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 index 73af2a4..b32a39c 100644 --- a/libc/src/stdio/printf_core/char_converter.h +++ b/libc/src/stdio/printf_core/char_converter.h @@ -31,7 +31,7 @@ int inline convert_char(Writer *writer, const FormatSection &to_conv) { } 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 index 9ecc25b..0c1d301 100644 --- a/libc/src/stdio/printf_core/core_structs.h +++ b/libc/src/stdio/printf_core/core_structs.h @@ -78,6 +78,15 @@ struct FormatSection { 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 index b87ae62..888c5ec 100644 --- 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 <stddef.h> namespace __llvm_libc { @@ -16,9 +17,9 @@ namespace printf_core { int FileWriter::write(const char *__restrict to_write, size_t len) { int written = file->write_unlocked(to_write, len); if (written != static_cast<int>(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 index b4840b2..c8c01d0 100644 --- a/libc/src/stdio/printf_core/float_hex_converter.h +++ b/libc/src/stdio/printf_core/float_hex_converter.h @@ -89,7 +89,7 @@ int inline convert_float_hex_exp(Writer *writer, const FormatSection &to_conv) { 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 @@ int inline convert_float_hex_exp(Writer *writer, const FormatSection &to_conv) { 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 index 4c8239c..ad30e5b 100644 --- a/libc/src/stdio/printf_core/hex_converter.h +++ b/libc/src/stdio/printf_core/hex_converter.h @@ -124,7 +124,7 @@ int convert_hex(Writer *writer, const FormatSection &to_conv) { 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 index 33234c0..59cd089 100644 --- a/libc/src/stdio/printf_core/int_converter.h +++ b/libc/src/stdio/printf_core/int_converter.h @@ -136,7 +136,7 @@ int inline convert_int(Writer *writer, const FormatSection &to_conv) { 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 index 8734649..f127f7f 100644 --- a/libc/src/stdio/printf_core/oct_converter.h +++ b/libc/src/stdio/printf_core/oct_converter.h @@ -102,7 +102,7 @@ int inline convert_oct(Writer *writer, const FormatSection &to_conv) { 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 index 6b1f3a3..7362c5c 100644 --- a/libc/src/stdio/printf_core/ptr_converter.h +++ b/libc/src/stdio/printf_core/ptr_converter.h @@ -30,7 +30,7 @@ int inline convert_pointer(Writer *writer, const FormatSection &to_conv) { hex_conv.conv_val_raw = reinterpret_cast<uintptr_t>(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 index 2673107..eaf9b99 100644 --- a/libc/src/stdio/printf_core/string_converter.h +++ b/libc/src/stdio/printf_core/string_converter.h @@ -47,7 +47,7 @@ int inline convert_string(Writer *writer, const FormatSection &to_conv) { RET_IF_RESULT_NEGATIVE(writer->write( reinterpret_cast<const char *>(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 index 44160d8..1e95ec8 100644 --- 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 <stddef.h> @@ -28,7 +29,7 @@ int write_to_string(void *raw_pointer, const char *__restrict to_write, size_t len) { StringWriter *string_writer = reinterpret_cast<StringWriter *>(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 index e8ed949..5baebd9 100644 --- a/libc/src/stdio/printf_core/write_int_converter.h +++ b/libc/src/stdio/printf_core/write_int_converter.h @@ -25,7 +25,7 @@ int inline convert_write_int(Writer *writer, const FormatSection &to_conv) { // 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 @@ int inline convert_write_int(Writer *writer, const FormatSection &to_conv) { *reinterpret_cast<uintmax_t *>(to_conv.conv_val_ptr) = written; break; } - return 0; + return WRITE_OK; } } // namespace printf_core |