[libc-commits] [libc] e9bdf4a - [libc][NFC] clean up type warnings in printf

Michael Jones via libc-commits libc-commits at lists.llvm.org
Mon Jul 31 13:19:39 PDT 2023


Author: Michael Jones
Date: 2023-07-31T13:19:34-07:00
New Revision: e9bdf4afa0be3b364571a7cc3db9510367972bec

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

LOG: [libc][NFC] clean up type warnings in printf

In preparation for https://reviews.llvm.org/D156630 this patch cleans up
all integer size and sign conversion warnings in printf.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D156723

Added: 
    

Modified: 
    libc/src/__support/float_to_string.h
    libc/src/stdio/printf_core/float_dec_converter.h
    libc/src/stdio/printf_core/float_hex_converter.h
    libc/src/stdio/printf_core/int_converter.h
    libc/src/stdio/printf_core/vfprintf_internal.h
    libc/src/stdio/printf_core/write_int_converter.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/float_to_string.h b/libc/src/__support/float_to_string.h
index a3d1b08f3964f4..a729cbf6c49e99 100644
--- a/libc/src/__support/float_to_string.h
+++ b/libc/src/__support/float_to_string.h
@@ -178,7 +178,8 @@ LIBC_INLINE constexpr cpp::UInt<MID_INT_SIZE> get_table_positive(int exponent,
   // ~1000 for double and ~16000 for long double. Be warned that the time
   // complexity of exponentiation is O(n^2 * log_2(m)) where n is the number of
   // bits in the number being exponentiated and m is the exponent.
-  const int shift_amount = exponent + CALC_SHIFT_CONST - (BLOCK_SIZE * i);
+  const int shift_amount =
+      static_cast<int>(exponent + CALC_SHIFT_CONST - (BLOCK_SIZE * i));
   if (shift_amount < 0) {
     return 1;
   }
@@ -221,7 +222,8 @@ LIBC_INLINE cpp::UInt<MID_INT_SIZE> get_table_positive_df(int exponent,
   // doubles are only accurate to ~35 digits, the 50 digits of accuracy are
   // enough for these floats to be converted back and forth safely. This is
   // ideal for avoiding the size of the long double table.
-  const int shift_amount = exponent + CALC_SHIFT_CONST - (9 * i);
+  const int shift_amount =
+      static_cast<int>(exponent + CALC_SHIFT_CONST - (9 * i));
   if (shift_amount < 0) {
     return 1;
   }
@@ -288,7 +290,7 @@ LIBC_INLINE cpp::UInt<MID_INT_SIZE> get_table_negative(int exponent, size_t i) {
     } else {
       ten_blocks = 0;
       five_blocks = i;
-      shift_amount = shift_amount + (i * BLOCK_SIZE);
+      shift_amount = static_cast<int>(shift_amount + (i * BLOCK_SIZE));
     }
   }
 
@@ -375,8 +377,9 @@ LIBC_INLINE uint32_t fast_uint_mod_1e9(const cpp::UInt<MID_INT_SIZE> &val) {
       {0x31680A88F8953031u, 0x89705F4136B4A597u, 0});
   const auto middle = (mult_const * val);
   const uint64_t result = static_cast<uint64_t>(middle[2]);
-  const uint32_t shifted = result >> 29;
-  return static_cast<uint32_t>(val) - (1000000000 * shifted);
+  const uint64_t shifted = result >> 29;
+  return static_cast<uint32_t>(static_cast<uint32_t>(val) -
+                               (1000000000 * shifted));
 }
 
 LIBC_INLINE uint32_t mul_shift_mod_1e9(const MantissaInt mantissa,
@@ -390,7 +393,8 @@ LIBC_INLINE uint32_t mul_shift_mod_1e9(const MantissaInt mantissa,
   wide_mant[1] = static_cast<size_t>(mantissa >> 64);
   val = (val * wide_mant) >> shift_amount;
 
-  return val.div_uint32_times_pow_2(1000000000, 0).value()[0];
+  return static_cast<uint32_t>(
+      val.div_uint32_times_pow_2(1000000000, 0).value());
   // return fast_uint_mod_1e9(val);
 }
 
@@ -596,7 +600,7 @@ class FloatToString {
     return false;
 #else
     const int32_t idx = -exponent / IDX_SIZE;
-    const uint32_t p = POW10_OFFSET_2[idx] + block_index - MIN_BLOCK_2[idx];
+    const size_t p = POW10_OFFSET_2[idx] + block_index - MIN_BLOCK_2[idx];
     // If the remaining digits are all 0, then this is the lowest block.
     return p >= POW10_OFFSET_2[idx + 1];
 #endif

diff  --git a/libc/src/stdio/printf_core/float_dec_converter.h b/libc/src/stdio/printf_core/float_dec_converter.h
index 35bbb823e83834..a0c891fb866c5a 100644
--- a/libc/src/stdio/printf_core/float_dec_converter.h
+++ b/libc/src/stdio/printf_core/float_dec_converter.h
@@ -67,7 +67,8 @@ class PaddingWriter {
   int write_left_padding(Writer *writer, size_t total_digits) {
     // The pattern is (spaces) (sign) (zeroes), but only one of spaces and
     // zeroes can be written, and only if the padding amount is positive.
-    int padding_amount = min_width - total_digits - (sign_char > 0 ? 1 : 0);
+    int padding_amount =
+        static_cast<int>(min_width - total_digits - (sign_char > 0 ? 1 : 0));
     if (left_justified || padding_amount < 0) {
       if (sign_char > 0) {
         RET_IF_RESULT_NEGATIVE(writer->write(sign_char));
@@ -89,7 +90,8 @@ class PaddingWriter {
   int write_right_padding(Writer *writer, size_t total_digits) {
     // If and only if the conversion is left justified, there may be trailing
     // spaces.
-    int padding_amount = min_width - total_digits - (sign_char > 0 ? 1 : 0);
+    int padding_amount =
+        static_cast<int>(min_width - total_digits - (sign_char > 0 ? 1 : 0));
     if (left_justified && padding_amount > 0) {
       RET_IF_RESULT_NEGATIVE(writer->write(' ', padding_amount));
     }
@@ -288,7 +290,7 @@ class FloatWriter {
 
     // copy the last block_digits characters into the start of end_buff.
     // TODO: Replace with memcpy
-    for (int count = block_digits - 1; count >= 0; --count) {
+    for (size_t count = 0; count < block_digits; ++count) {
       end_buff[count] = int_to_str[count + 1 + (BLOCK_SIZE - block_digits)];
     }
 
@@ -306,7 +308,8 @@ class FloatWriter {
         (round == RoundDirection::Even && low_digit % 2 != 0)) {
       bool has_carry = true;
       // handle the low block that we're adding
-      for (int count = block_digits - 1; count >= 0 && has_carry; --count) {
+      for (int count = static_cast<int>(block_digits) - 1;
+           count >= 0 && has_carry; --count) {
         if (end_buff[count] == '9') {
           end_buff[count] = '0';
         } else {
@@ -315,7 +318,8 @@ class FloatWriter {
         }
       }
       // handle the high block that's buffered
-      for (int count = buffered_digits - 1; count >= 0 && has_carry; --count) {
+      for (int count = static_cast<int>(buffered_digits) - 1;
+           count >= 0 && has_carry; --count) {
         if (block_buffer[count] == '9') {
           block_buffer[count] = '0';
         } else {
@@ -374,7 +378,7 @@ class FloatWriter {
 
       // copy the last block_digits characters into the start of end_buff.
       // TODO: Replace with memcpy
-      for (int count = block_digits - 1; count >= 0; --count) {
+      for (size_t count = 0; count < block_digits; ++count) {
         end_buff[count] = int_to_str[count + 1 + (BLOCK_SIZE - block_digits)];
       }
     }
@@ -393,7 +397,8 @@ class FloatWriter {
         (round == RoundDirection::Even && low_digit % 2 != 0)) {
       bool has_carry = true;
       // handle the low block that we're adding
-      for (int count = block_digits - 1; count >= 0 && has_carry; --count) {
+      for (int count = static_cast<int>(block_digits) - 1;
+           count >= 0 && has_carry; --count) {
         if (end_buff[count] == '9') {
           end_buff[count] = '0';
         } else {
@@ -402,7 +407,8 @@ class FloatWriter {
         }
       }
       // handle the high block that's buffered
-      for (int count = buffered_digits - 1; count >= 0 && has_carry; --count) {
+      for (int count = static_cast<int>(buffered_digits) - 1;
+           count >= 0 && has_carry; --count) {
         if (block_buffer[count] == '9') {
           block_buffer[count] = '0';
         } else {
@@ -519,7 +525,7 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
     sign_char = ' ';
 
   // If to_conv doesn't specify a precision, the precision defaults to 6.
-  const size_t precision = to_conv.precision < 0 ? 6 : to_conv.precision;
+  const unsigned int precision = to_conv.precision < 0 ? 6 : to_conv.precision;
   bool has_decimal_point =
       (precision > 0) || ((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0);
 
@@ -532,7 +538,7 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
   FloatWriter float_writer(writer, has_decimal_point, padding_writer);
   FloatToString<T> float_converter(static_cast<T>(float_bits));
 
-  const uint32_t positive_blocks = float_converter.get_positive_blocks();
+  const size_t positive_blocks = float_converter.get_positive_blocks();
 
   if (positive_blocks >= 0) {
     // This loop iterates through the number a block at a time until it finds a
@@ -571,7 +577,7 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
       RET_IF_RESULT_NEGATIVE(float_writer.write_zeroes(precision));
     } else if (i < float_converter.zero_blocks_after_point()) {
       // else if there are some blocks that are zeroes
-      i = float_converter.zero_blocks_after_point();
+      i = static_cast<uint32_t>(float_converter.zero_blocks_after_point());
       // write those blocks as zeroes.
       RET_IF_RESULT_NEGATIVE(float_writer.write_zeroes(9 * i));
     }
@@ -665,7 +671,7 @@ LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
     sign_char = ' ';
 
   // If to_conv doesn't specify a precision, the precision defaults to 6.
-  const size_t precision = to_conv.precision < 0 ? 6 : to_conv.precision;
+  const unsigned int precision = to_conv.precision < 0 ? 6 : to_conv.precision;
   bool has_decimal_point =
       (precision > 0) || ((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0);
 
@@ -682,9 +688,9 @@ LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
   int cur_block;
 
   if (exponent < 0) {
-    cur_block = -float_converter.zero_blocks_after_point();
+    cur_block = -static_cast<int>(float_converter.zero_blocks_after_point());
   } else {
-    cur_block = float_converter.get_positive_blocks();
+    cur_block = static_cast<int>(float_converter.get_positive_blocks());
   }
 
   BlockInt digits = 0;
@@ -707,7 +713,7 @@ LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
   auto int_to_str = *IntegerToString::dec(digits, buf);
   size_t block_width = int_to_str.size();
 
-  final_exponent = (cur_block * BLOCK_SIZE) + (block_width - 1);
+  final_exponent = (cur_block * BLOCK_SIZE) + static_cast<int>(block_width - 1);
   int positive_exponent = final_exponent < 0 ? -final_exponent : final_exponent;
 
   int_to_str = *IntegerToString::dec(positive_exponent, buf);
@@ -753,7 +759,7 @@ LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
   }
 
   // This is the last block.
-  const uint32_t maximum = precision + 1 - digits_written;
+  const size_t maximum = precision + 1 - digits_written;
   uint32_t last_digit = 0;
   for (uint32_t k = 0; k < last_block_size - maximum; ++k) {
     last_digit = digits % 10;
@@ -823,9 +829,9 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
 
   // From the standard: Let P (init_precision) equal the precision if nonzero, 6
   // if the precision is omitted, or 1 if the precision is zero.
-  const size_t init_precision = to_conv.precision <= 0
-                                    ? (to_conv.precision == 0 ? 1 : 6)
-                                    : to_conv.precision;
+  const unsigned int init_precision = to_conv.precision <= 0
+                                          ? (to_conv.precision == 0 ? 1 : 6)
+                                          : to_conv.precision;
 
   //  Then, if a conversion with style E would have an exponent of X
   //  (base_10_exp):
@@ -835,7 +841,7 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
 
   // For calculating the base 10 exponent, we need to process the number as if
   // it has style E, so here we calculate the precision we'll use in that case.
-  const size_t exp_precision = init_precision - 1;
+  const unsigned int exp_precision = init_precision - 1;
 
   FloatToString<T> float_converter(static_cast<T>(float_bits));
 
@@ -845,9 +851,9 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
   int cur_block;
 
   if (exponent < 0) {
-    cur_block = -float_converter.zero_blocks_after_point();
+    cur_block = -static_cast<int>(float_converter.zero_blocks_after_point());
   } else {
-    cur_block = float_converter.get_positive_blocks();
+    cur_block = static_cast<int>(float_converter.get_positive_blocks());
   }
 
   BlockInt digits = 0;
@@ -890,7 +896,7 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
   size_t trailing_zeroes = 0;
   size_t trailing_nines = 0;
 
-  base_10_exp = (cur_block * BLOCK_SIZE) + (block_width - 1);
+  base_10_exp = (cur_block * BLOCK_SIZE) + static_cast<int>(block_width - 1);
 
   // If the first block is not also the last block
   if (block_width <= exp_precision + 1) {
@@ -959,7 +965,7 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
   char buf[IntegerToString::dec_bufsize<intmax_t>()];
   auto int_to_str = *IntegerToString::dec(digits, buf);
 
-  int implicit_leading_zeroes = BLOCK_SIZE - int_to_str.size();
+  size_t implicit_leading_zeroes = BLOCK_SIZE - int_to_str.size();
 
   // if the last block is also the first block, then ignore leading zeroes.
   if (digits_checked == 0) {
@@ -967,9 +973,11 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
     implicit_leading_zeroes = 0;
   }
 
-  int digits_requested = (exp_precision + 1) - digits_checked;
+  unsigned int digits_requested =
+      (exp_precision + 1) - static_cast<unsigned int>(digits_checked);
 
-  int digits_to_check = digits_requested - implicit_leading_zeroes;
+  int digits_to_check =
+      digits_requested - static_cast<int>(implicit_leading_zeroes);
   if (digits_to_check < 0) {
     digits_to_check = 0;
   }
@@ -1003,7 +1011,8 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
 
   // Find the digit after the lowest digit that we'll actually print to
   // determine the rounding.
-  const uint32_t maximum = exp_precision + 1 - digits_checked;
+  const uint32_t maximum =
+      exp_precision + 1 - static_cast<uint32_t>(digits_checked);
   uint32_t last_digit = 0;
   for (uint32_t k = 0; k < last_block_size - maximum; ++k) {
     last_digit = digits % 10;
@@ -1100,7 +1109,7 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
   //  P - (X + 1).
   if (static_cast<int>(init_precision) > base_10_exp && base_10_exp >= -4) {
     FormatSection new_conv = to_conv;
-    const size_t conv_precision = init_precision - (base_10_exp + 1);
+    const int conv_precision = init_precision - (base_10_exp + 1);
 
     if ((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0) {
       new_conv.precision = conv_precision;
@@ -1125,36 +1134,35 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
                              |   |
        base_10_exp + 1 = 2 --+   +--- trailing_zeroes = 11
       */
-      int trimmed_precision =
-          digits_checked - (base_10_exp + 1) - trailing_zeroes;
+      int trimmed_precision = static_cast<int>(
+          digits_checked - (base_10_exp + 1) - trailing_zeroes);
       if (trimmed_precision < 0) {
         trimmed_precision = 0;
       }
-      new_conv.precision =
-          (static_cast<size_t>(trimmed_precision) > conv_precision)
-              ? conv_precision
-              : trimmed_precision;
+      new_conv.precision = (trimmed_precision > conv_precision)
+                               ? conv_precision
+                               : trimmed_precision;
     }
 
     return convert_float_decimal_typed<T>(writer, new_conv, float_bits);
   } else {
     // otherwise, the conversion is with style e (or E) and precision equals
     // P - 1
-    const size_t conv_precision = init_precision - 1;
+    const int conv_precision = init_precision - 1;
     FormatSection new_conv = to_conv;
     if ((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0) {
       new_conv.precision = conv_precision;
     } else {
       // If alt form isn't set, then we need to determine the number of trailing
       // zeroes and set the precision such that they are removed.
-      int trimmed_precision = digits_checked - 1 - trailing_zeroes;
+      int trimmed_precision =
+          static_cast<int>(digits_checked - 1 - trailing_zeroes);
       if (trimmed_precision < 0) {
         trimmed_precision = 0;
       }
-      new_conv.precision =
-          (static_cast<size_t>(trimmed_precision) > conv_precision)
-              ? conv_precision
-              : trimmed_precision;
+      new_conv.precision = (trimmed_precision > conv_precision)
+                               ? conv_precision
+                               : trimmed_precision;
     }
     return convert_float_dec_exp_typed<T>(writer, new_conv, float_bits);
   }

diff  --git a/libc/src/stdio/printf_core/float_hex_converter.h b/libc/src/stdio/printf_core/float_hex_converter.h
index 6bb83a2a0c0ff7..6f350ce8d6765d 100644
--- a/libc/src/stdio/printf_core/float_hex_converter.h
+++ b/libc/src/stdio/printf_core/float_hex_converter.h
@@ -191,7 +191,7 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,
 
   // these are signed to prevent underflow due to negative values. The eventual
   // values will always be non-negative.
-  int trailing_zeroes = 0;
+  size_t trailing_zeroes = 0;
   int padding;
 
   // prefix is "0x", and always appears.
@@ -214,9 +214,10 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,
   const char exp_seperator = a + ('p' - 'a');
   constexpr int EXP_SEPERATOR_LEN = 1;
 
-  padding = to_conv.min_width - (sign_char > 0 ? 1 : 0) - PREFIX_LEN -
-            mant_digits - (has_hexadecimal_point ? 1 : 0) - EXP_SEPERATOR_LEN -
-            (EXP_LEN - exp_cur);
+  padding = static_cast<int>(to_conv.min_width - (sign_char > 0 ? 1 : 0) -
+                             PREFIX_LEN - mant_digits -
+                             static_cast<int>(has_hexadecimal_point) -
+                             EXP_SEPERATOR_LEN - (EXP_LEN - exp_cur));
   if (padding < 0)
     padding = 0;
 

diff  --git a/libc/src/stdio/printf_core/int_converter.h b/libc/src/stdio/printf_core/int_converter.h
index 448fe8f8bafef2..aa91e34486ba48 100644
--- a/libc/src/stdio/printf_core/int_converter.h
+++ b/libc/src/stdio/printf_core/int_converter.h
@@ -108,13 +108,15 @@ LIBC_INLINE int convert_int(Writer *writer, const FormatSection &to_conv) {
       // If this conv has flag 0 but not - and no specified precision, it's
       // padded with 0's instead of spaces identically to if precision =
       // min_width - (1 if sign_char). For example: ("%+04d", 1) -> "+001"
-      zeroes = to_conv.min_width - digits_written - prefix_len;
+      zeroes =
+          static_cast<int>(to_conv.min_width - digits_written - prefix_len);
       spaces = 0;
     } else {
       // If there are enough digits to pass over the precision, just write the
       // number, padded by spaces.
       zeroes = 0;
-      spaces = to_conv.min_width - digits_written - prefix_len;
+      spaces =
+          static_cast<int>(to_conv.min_width - digits_written - prefix_len);
     }
   } else {
     // If precision was specified, possibly write zeroes, and possibly write
@@ -127,10 +129,12 @@ LIBC_INLINE int convert_int(Writer *writer, const FormatSection &to_conv) {
     // that special case first.
     if (num == 0 && to_conv.precision == 0)
       digits_written = 0;
-    zeroes = to_conv.precision - digits_written; // a negative value means 0
+    zeroes = static_cast<int>(to_conv.precision -
+                              digits_written); // a negative value means 0
     if (zeroes < 0)
       zeroes = 0;
-    spaces = to_conv.min_width - zeroes - digits_written - prefix_len;
+    spaces = static_cast<int>(to_conv.min_width - zeroes - digits_written -
+                              prefix_len);
   }
 
   if ((to_conv.conv_name == 'o') &&

diff  --git a/libc/src/stdio/printf_core/vfprintf_internal.h b/libc/src/stdio/printf_core/vfprintf_internal.h
index e45189d05c88c1..2e19d2a927bd77 100644
--- a/libc/src/stdio/printf_core/vfprintf_internal.h
+++ b/libc/src/stdio/printf_core/vfprintf_internal.h
@@ -36,8 +36,9 @@ LIBC_INLINE void funlockfile(FILE *f) {
 
 LIBC_INLINE int fwrite_unlocked(const void *ptr, size_t size, size_t nmemb,
                                 FILE *f) {
-  return reinterpret_cast<__llvm_libc::File *>(f)->write_unlocked(ptr,
-                                                                  size * nmemb);
+  return static_cast<int>(
+      reinterpret_cast<__llvm_libc::File *>(f)->write_unlocked(ptr,
+                                                               size * nmemb));
 }
 #else  // defined(LIBC_COPT_PRINTF_USE_SYSTEM_FILE)
 LIBC_INLINE int ferror_unlocked(::FILE *f) { return ::ferror_unlocked(f); }

diff  --git a/libc/src/stdio/printf_core/write_int_converter.h b/libc/src/stdio/printf_core/write_int_converter.h
index a88665c517ab5e..6b26c9bf394af4 100644
--- a/libc/src/stdio/printf_core/write_int_converter.h
+++ b/libc/src/stdio/printf_core/write_int_converter.h
@@ -25,6 +25,8 @@ LIBC_INLINE int convert_write_int(Writer *writer,
   // This is an additional check added by LLVM-libc. The reason it returns -3 is
   // because printf uses negative return values for errors, and -1 and -2 are
   // already in use by the file_writer class for file errors.
+  // TODO: Remove this. It's better to crash than to provide an incorrect
+  // result.
   if (to_conv.conv_val_ptr == nullptr)
     return NULLPTR_WRITE_ERROR;
 
@@ -42,10 +44,12 @@ LIBC_INLINE int convert_write_int(Writer *writer,
     *reinterpret_cast<long long *>(to_conv.conv_val_ptr) = written;
     break;
   case LengthModifier::h:
-    *reinterpret_cast<short *>(to_conv.conv_val_ptr) = written;
+    *reinterpret_cast<short *>(to_conv.conv_val_ptr) =
+        static_cast<short>(written);
     break;
   case LengthModifier::hh:
-    *reinterpret_cast<signed char *>(to_conv.conv_val_ptr) = written;
+    *reinterpret_cast<signed char *>(to_conv.conv_val_ptr) =
+        static_cast<signed char>(written);
     break;
   case LengthModifier::z:
     *reinterpret_cast<size_t *>(to_conv.conv_val_ptr) = written;


        


More information about the libc-commits mailing list