[libc-commits] [libc] ba177c7 - [libc] Add a few missing casts (#70850)

via libc-commits libc-commits at lists.llvm.org
Tue Oct 31 12:37:13 PDT 2023


Author: Roland McGrath
Date: 2023-10-31T12:37:09-07:00
New Revision: ba177c7286f538a9f4d4962fc8ac6440a2244ed9

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

LOG: [libc] Add a few missing casts (#70850)

Stricter GCC warnings about implicit widening and narrowing cases
necessitate additional explicit casts around some integer operations.

Added: 
    

Modified: 
    libc/src/__support/integer_to_string.h
    libc/src/stdio/printf_core/writer.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/integer_to_string.h b/libc/src/__support/integer_to_string.h
index 29f712461259444..6bbedac68efce2e 100644
--- a/libc/src/__support/integer_to_string.h
+++ b/libc/src/__support/integer_to_string.h
@@ -213,15 +213,15 @@ template <typename T, typename Fmt = radix::Dec> class IntegerToString {
 
     LIBC_INLINE static char digit_char(uint8_t digit) {
       if (digit < 10)
-        return '0' + digit;
-      return (Fmt::IS_UPPERCASE ? 'A' : 'a') + (digit - 10);
+        return '0' + static_cast<char>(digit);
+      return (Fmt::IS_UPPERCASE ? 'A' : 'a') + static_cast<char>(digit - 10);
     }
 
     LIBC_INLINE static void
     write_unsigned_number(UNSIGNED_T value,
                           details::BackwardStringBufferWriter &sink) {
       for (; sink.ok() && value != 0; value /= Fmt::BASE) {
-        const uint8_t digit(value % Fmt::BASE);
+        const uint8_t digit(static_cast<uint8_t>(value % Fmt::BASE));
         sink.push(digit_char(digit));
       }
     }

diff  --git a/libc/src/stdio/printf_core/writer.h b/libc/src/stdio/printf_core/writer.h
index 1d4ff9916ee5f5c..e4f503abc34c52b 100644
--- a/libc/src/stdio/printf_core/writer.h
+++ b/libc/src/stdio/printf_core/writer.h
@@ -93,7 +93,7 @@ class Writer final {
   // 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());
@@ -107,7 +107,7 @@ class Writer final {
   // 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, new_char, length);


        


More information about the libc-commits mailing list