[libc-commits] [libc] 9e421a1 - [libc] clean up printf error codes

Michael Jones via libc-commits libc-commits at lists.llvm.org
Mon Jul 11 16:49:52 PDT 2022


Author: Michael Jones
Date: 2022-07-11T16:49:47-07:00
New Revision: 9e421a1633247d5e113a55159f5ee3dde009d82d

URL: https://github.com/llvm/llvm-project/commit/9e421a1633247d5e113a55159f5ee3dde009d82d
DIFF: https://github.com/llvm/llvm-project/commit/9e421a1633247d5e113a55159f5ee3dde009d82d.diff

LOG: [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

Added: 
    

Modified: 
    libc/src/stdio/printf_core/CMakeLists.txt
    libc/src/stdio/printf_core/char_converter.h
    libc/src/stdio/printf_core/core_structs.h
    libc/src/stdio/printf_core/file_writer.cpp
    libc/src/stdio/printf_core/float_hex_converter.h
    libc/src/stdio/printf_core/hex_converter.h
    libc/src/stdio/printf_core/int_converter.h
    libc/src/stdio/printf_core/oct_converter.h
    libc/src/stdio/printf_core/ptr_converter.h
    libc/src/stdio/printf_core/string_converter.h
    libc/src/stdio/printf_core/string_writer.cpp
    libc/src/stdio/printf_core/write_int_converter.h

Removed: 
    


################################################################################
diff  --git a/libc/src/stdio/printf_core/CMakeLists.txt b/libc/src/stdio/printf_core/CMakeLists.txt
index 565b968657028..ab94d84725396 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 73af2a471be0b..b32a39c419ff4 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 9ecc25bb3ea43..0c1d30157dc0e 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 b87ae626a1df6..888c5ecbb60ff 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 b4840b2faa9e0..c8c01d07824d1 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 4c8239cefb63a..ad30e5be3d67f 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 33234c0f372ac..59cd089bda453 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 873464911ced1..f127f7fe80b2e 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 6b1f3a30e8ba5..7362c5c697a6e 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 267310701e6c8..eaf9b9987620c 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 44160d821b8fc..1e95ec805dcd4 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 e8ed9496aa978..5baebd96b79c1 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


        


More information about the libc-commits mailing list