[libc-commits] [libc] 4e1a164 - [libc] Fix various -Wconversion warnings in strto*l test code.

Roland McGrath via libc-commits libc-commits at lists.llvm.org
Fri Aug 27 14:04:12 PDT 2021


Author: Roland McGrath
Date: 2021-08-27T14:04:00-07:00
New Revision: 4e1a164d7bd53653f79decc121afe784d2fde0a7

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

LOG: [libc] Fix various -Wconversion warnings in strto*l test code.

The Fuchsia build compiles the libc and test code with lots
of warnings enabled, including all the integer conversion warnings.
There was some sloppy type usage here that triggered some of those.

Reviewed By: michaelrj

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

Added: 
    

Modified: 
    libc/test/src/stdlib/strtol_test.cpp
    libc/test/src/stdlib/strtoll_test.cpp
    libc/test/src/stdlib/strtoul_test.cpp
    libc/test/src/stdlib/strtoull_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/test/src/stdlib/strtol_test.cpp b/libc/test/src/stdlib/strtol_test.cpp
index b611503ed0cf8..29997639a71e4 100644
--- a/libc/test/src/stdlib/strtol_test.cpp
+++ b/libc/test/src/stdlib/strtol_test.cpp
@@ -149,19 +149,19 @@ static char int_to_b36_char(int input) {
   if (input < 0 || input > 36)
     return '0';
   if (input < 10)
-    return '0' + input;
-  return 'A' + input - 10;
+    return static_cast<char>('0' + input);
+  return static_cast<char>('A' + input - 10);
 }
 
 TEST(LlvmLibcStrToLTest, DecodeInOtherBases) {
   char small_string[4] = {'\0', '\0', '\0', '\0'};
-  for (long base = 2; base <= 36; ++base) {
-    for (long first_digit = 0; first_digit <= 36; ++first_digit) {
+  for (int base = 2; base <= 36; ++base) {
+    for (int first_digit = 0; first_digit <= 36; ++first_digit) {
       small_string[0] = int_to_b36_char(first_digit);
       if (first_digit < base) {
         errno = 0;
         ASSERT_EQ(__llvm_libc::strtol(small_string, nullptr, base),
-                  first_digit);
+                  static_cast<long int>(first_digit));
         ASSERT_EQ(errno, 0);
       } else {
         errno = 0;
@@ -171,20 +171,20 @@ TEST(LlvmLibcStrToLTest, DecodeInOtherBases) {
     }
   }
 
-  for (long base = 2; base <= 36; ++base) {
-    for (long first_digit = 0; first_digit <= 36; ++first_digit) {
+  for (int base = 2; base <= 36; ++base) {
+    for (int first_digit = 0; first_digit <= 36; ++first_digit) {
       small_string[0] = int_to_b36_char(first_digit);
-      for (long second_digit = 0; second_digit <= 36; ++second_digit) {
+      for (int second_digit = 0; second_digit <= 36; ++second_digit) {
         small_string[1] = int_to_b36_char(second_digit);
         if (first_digit < base && second_digit < base) {
           errno = 0;
           ASSERT_EQ(__llvm_libc::strtol(small_string, nullptr, base),
-                    second_digit + (first_digit * base));
+                    static_cast<long int>(second_digit + (first_digit * base)));
           ASSERT_EQ(errno, 0);
         } else if (first_digit < base) {
           errno = 0;
           ASSERT_EQ(__llvm_libc::strtol(small_string, nullptr, base),
-                    first_digit);
+                    static_cast<long int>(first_digit));
           ASSERT_EQ(errno, 0);
         } else {
           errno = 0;
@@ -195,24 +195,26 @@ TEST(LlvmLibcStrToLTest, DecodeInOtherBases) {
     }
   }
 
-  for (long base = 2; base <= 36; ++base) {
-    for (long first_digit = 0; first_digit <= 36; ++first_digit) {
+  for (int base = 2; base <= 36; ++base) {
+    for (int first_digit = 0; first_digit <= 36; ++first_digit) {
       small_string[0] = int_to_b36_char(first_digit);
-      for (long second_digit = 0; second_digit <= 36; ++second_digit) {
+      for (int second_digit = 0; second_digit <= 36; ++second_digit) {
         small_string[1] = int_to_b36_char(second_digit);
-        for (long third_digit = 0; third_digit <= 36; ++third_digit) {
+        for (int third_digit = 0; third_digit <= 36; ++third_digit) {
           small_string[2] = int_to_b36_char(third_digit);
 
           if (first_digit < base && second_digit < base && third_digit < base) {
             errno = 0;
             ASSERT_EQ(__llvm_libc::strtol(small_string, nullptr, base),
-                      third_digit + (second_digit * base) +
-                          (first_digit * base * base));
+                      static_cast<long int>(third_digit +
+                                            (second_digit * base) +
+                                            (first_digit * base * base)));
             ASSERT_EQ(errno, 0);
           } else if (first_digit < base && second_digit < base) {
             errno = 0;
-            ASSERT_EQ(__llvm_libc::strtol(small_string, nullptr, base),
-                      second_digit + (first_digit * base));
+            ASSERT_EQ(
+                __llvm_libc::strtol(small_string, nullptr, base),
+                static_cast<long int>(second_digit + (first_digit * base)));
             ASSERT_EQ(errno, 0);
           } else if (first_digit < base) {
             // if the base is 16 there is a special case for the prefix 0X.
@@ -221,7 +223,7 @@ TEST(LlvmLibcStrToLTest, DecodeInOtherBases) {
               if (third_digit < base) {
                 errno = 0;
                 ASSERT_EQ(__llvm_libc::strtol(small_string, nullptr, base),
-                          third_digit);
+                          static_cast<long int>(third_digit));
                 ASSERT_EQ(errno, 0);
               } else {
                 errno = 0;
@@ -231,7 +233,7 @@ TEST(LlvmLibcStrToLTest, DecodeInOtherBases) {
             } else {
               errno = 0;
               ASSERT_EQ(__llvm_libc::strtol(small_string, nullptr, base),
-                        first_digit);
+                        static_cast<long int>(first_digit));
               ASSERT_EQ(errno, 0);
             }
           } else {

diff  --git a/libc/test/src/stdlib/strtoll_test.cpp b/libc/test/src/stdlib/strtoll_test.cpp
index f46524bf72c1a..8e3ce85fa0a46 100644
--- a/libc/test/src/stdlib/strtoll_test.cpp
+++ b/libc/test/src/stdlib/strtoll_test.cpp
@@ -173,19 +173,19 @@ static char int_to_b36_char(int input) {
   if (input < 0 || input > 36)
     return '0';
   if (input < 10)
-    return '0' + input;
-  return 'A' + input - 10;
+    return static_cast<char>('0' + input);
+  return static_cast<char>('A' + input - 10);
 }
 
 TEST(LlvmLibcStrToLLTest, DecodeInOtherBases) {
   char small_string[4] = {'\0', '\0', '\0', '\0'};
-  for (long long base = 2; base <= 36; ++base) {
-    for (long long first_digit = 0; first_digit <= 36; ++first_digit) {
+  for (int base = 2; base <= 36; ++base) {
+    for (int first_digit = 0; first_digit <= 36; ++first_digit) {
       small_string[0] = int_to_b36_char(first_digit);
       if (first_digit < base) {
         errno = 0;
         ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
-                  first_digit);
+                  static_cast<long long int>(first_digit));
         ASSERT_EQ(errno, 0);
       } else {
         errno = 0;
@@ -195,20 +195,21 @@ TEST(LlvmLibcStrToLLTest, DecodeInOtherBases) {
     }
   }
 
-  for (long long base = 2; base <= 36; ++base) {
-    for (long long first_digit = 0; first_digit <= 36; ++first_digit) {
+  for (int base = 2; base <= 36; ++base) {
+    for (int first_digit = 0; first_digit <= 36; ++first_digit) {
       small_string[0] = int_to_b36_char(first_digit);
-      for (long long second_digit = 0; second_digit <= 36; ++second_digit) {
+      for (int second_digit = 0; second_digit <= 36; ++second_digit) {
         small_string[1] = int_to_b36_char(second_digit);
         if (first_digit < base && second_digit < base) {
           errno = 0;
-          ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
-                    second_digit + (first_digit * base));
+          ASSERT_EQ(
+              __llvm_libc::strtoll(small_string, nullptr, base),
+              static_cast<long long int>(second_digit + (first_digit * base)));
           ASSERT_EQ(errno, 0);
         } else if (first_digit < base) {
           errno = 0;
           ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
-                    first_digit);
+                    static_cast<long long int>(first_digit));
           ASSERT_EQ(errno, 0);
         } else {
           errno = 0;
@@ -219,24 +220,26 @@ TEST(LlvmLibcStrToLLTest, DecodeInOtherBases) {
     }
   }
 
-  for (long long base = 2; base <= 36; ++base) {
-    for (long long first_digit = 0; first_digit <= 36; ++first_digit) {
+  for (int base = 2; base <= 36; ++base) {
+    for (int first_digit = 0; first_digit <= 36; ++first_digit) {
       small_string[0] = int_to_b36_char(first_digit);
-      for (long long second_digit = 0; second_digit <= 36; ++second_digit) {
+      for (int second_digit = 0; second_digit <= 36; ++second_digit) {
         small_string[1] = int_to_b36_char(second_digit);
-        for (long long third_digit = 0; third_digit <= 36; ++third_digit) {
+        for (int third_digit = 0; third_digit <= 36; ++third_digit) {
           small_string[2] = int_to_b36_char(third_digit);
 
           if (first_digit < base && second_digit < base && third_digit < base) {
             errno = 0;
             ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
-                      third_digit + (second_digit * base) +
-                          (first_digit * base * base));
+                      static_cast<long long int>(third_digit +
+                                                 (second_digit * base) +
+                                                 (first_digit * base * base)));
             ASSERT_EQ(errno, 0);
           } else if (first_digit < base && second_digit < base) {
             errno = 0;
             ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
-                      second_digit + (first_digit * base));
+                      static_cast<long long int>(second_digit +
+                                                 (first_digit * base)));
             ASSERT_EQ(errno, 0);
           } else if (first_digit < base) {
             // if the base is 16 there is a special case for the prefix 0X.
@@ -245,7 +248,7 @@ TEST(LlvmLibcStrToLLTest, DecodeInOtherBases) {
               if (third_digit < base) {
                 errno = 0;
                 ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
-                          third_digit);
+                          static_cast<long long int>(third_digit));
                 ASSERT_EQ(errno, 0);
               } else {
                 errno = 0;
@@ -256,7 +259,7 @@ TEST(LlvmLibcStrToLLTest, DecodeInOtherBases) {
             } else {
               errno = 0;
               ASSERT_EQ(__llvm_libc::strtoll(small_string, nullptr, base),
-                        first_digit);
+                        static_cast<long long int>(first_digit));
               ASSERT_EQ(errno, 0);
             }
           } else {

diff  --git a/libc/test/src/stdlib/strtoul_test.cpp b/libc/test/src/stdlib/strtoul_test.cpp
index e59ef2ebe8ee2..16ca6b023bd4e 100644
--- a/libc/test/src/stdlib/strtoul_test.cpp
+++ b/libc/test/src/stdlib/strtoul_test.cpp
@@ -8,12 +8,12 @@
 
 #include "src/stdlib/strtoul.h"
 
-#include "utils/UnitTest/Test.h"
-
 #include <errno.h>
 #include <limits.h>
 #include <stddef.h>
 
+#include "utils/UnitTest/Test.h"
+
 TEST(LlvmLibcStrToULTest, InvalidBase) {
   const char *ten = "10";
   errno = 0;
@@ -141,19 +141,19 @@ static char int_to_b36_char(int input) {
   if (input < 0 || input > 36)
     return '0';
   if (input < 10)
-    return '0' + input;
-  return 'A' + input - 10;
+    return static_cast<char>('0' + input);
+  return static_cast<char>('A' + input - 10);
 }
 
 TEST(LlvmLibcStrToULTest, DecodeInOtherBases) {
   char small_string[4] = {'\0', '\0', '\0', '\0'};
-  for (unsigned long base = 2; base <= 36; ++base) {
-    for (unsigned long first_digit = 0; first_digit <= 36; ++first_digit) {
+  for (int base = 2; base <= 36; ++base) {
+    for (int first_digit = 0; first_digit <= 36; ++first_digit) {
       small_string[0] = int_to_b36_char(first_digit);
       if (first_digit < base) {
         errno = 0;
         ASSERT_EQ(__llvm_libc::strtoul(small_string, nullptr, base),
-                  first_digit);
+                  static_cast<unsigned long int>(first_digit));
         ASSERT_EQ(errno, 0);
       } else {
         errno = 0;
@@ -163,20 +163,21 @@ TEST(LlvmLibcStrToULTest, DecodeInOtherBases) {
     }
   }
 
-  for (unsigned long base = 2; base <= 36; ++base) {
-    for (unsigned long first_digit = 0; first_digit <= 36; ++first_digit) {
+  for (int base = 2; base <= 36; ++base) {
+    for (int first_digit = 0; first_digit <= 36; ++first_digit) {
       small_string[0] = int_to_b36_char(first_digit);
-      for (unsigned long second_digit = 0; second_digit <= 36; ++second_digit) {
+      for (int second_digit = 0; second_digit <= 36; ++second_digit) {
         small_string[1] = int_to_b36_char(second_digit);
         if (first_digit < base && second_digit < base) {
           errno = 0;
           ASSERT_EQ(__llvm_libc::strtoul(small_string, nullptr, base),
-                    second_digit + (first_digit * base));
+                    static_cast<unsigned long int>(second_digit +
+                                                   (first_digit * base)));
           ASSERT_EQ(errno, 0);
         } else if (first_digit < base) {
           errno = 0;
           ASSERT_EQ(__llvm_libc::strtoul(small_string, nullptr, base),
-                    first_digit);
+                    static_cast<unsigned long int>(first_digit));
           ASSERT_EQ(errno, 0);
         } else {
           errno = 0;
@@ -187,24 +188,26 @@ TEST(LlvmLibcStrToULTest, DecodeInOtherBases) {
     }
   }
 
-  for (unsigned long base = 2; base <= 36; ++base) {
-    for (unsigned long first_digit = 0; first_digit <= 36; ++first_digit) {
+  for (int base = 2; base <= 36; ++base) {
+    for (int first_digit = 0; first_digit <= 36; ++first_digit) {
       small_string[0] = int_to_b36_char(first_digit);
-      for (unsigned long second_digit = 0; second_digit <= 36; ++second_digit) {
+      for (int second_digit = 0; second_digit <= 36; ++second_digit) {
         small_string[1] = int_to_b36_char(second_digit);
-        for (unsigned long third_digit = 0; third_digit <= 36; ++third_digit) {
+        for (int third_digit = 0; third_digit <= 36; ++third_digit) {
           small_string[2] = int_to_b36_char(third_digit);
 
           if (first_digit < base && second_digit < base && third_digit < base) {
             errno = 0;
             ASSERT_EQ(__llvm_libc::strtoul(small_string, nullptr, base),
-                      third_digit + (second_digit * base) +
-                          (first_digit * base * base));
+                      static_cast<unsigned long int>(
+                          third_digit + (second_digit * base) +
+                          (first_digit * base * base)));
             ASSERT_EQ(errno, 0);
           } else if (first_digit < base && second_digit < base) {
             errno = 0;
             ASSERT_EQ(__llvm_libc::strtoul(small_string, nullptr, base),
-                      second_digit + (first_digit * base));
+                      static_cast<unsigned long int>(second_digit +
+                                                     (first_digit * base)));
             ASSERT_EQ(errno, 0);
           } else if (first_digit < base) {
             // if the base is 16 there is a special case for the prefix 0X.
@@ -213,7 +216,7 @@ TEST(LlvmLibcStrToULTest, DecodeInOtherBases) {
               if (third_digit < base) {
                 errno = 0;
                 ASSERT_EQ(__llvm_libc::strtoul(small_string, nullptr, base),
-                          third_digit);
+                          static_cast<unsigned long int>(third_digit));
                 ASSERT_EQ(errno, 0);
               } else {
                 errno = 0;

diff  --git a/libc/test/src/stdlib/strtoull_test.cpp b/libc/test/src/stdlib/strtoull_test.cpp
index 2dc7872632004..8a2ca11dcd556 100644
--- a/libc/test/src/stdlib/strtoull_test.cpp
+++ b/libc/test/src/stdlib/strtoull_test.cpp
@@ -8,12 +8,12 @@
 
 #include "src/stdlib/strtoull.h"
 
-#include "utils/UnitTest/Test.h"
-
 #include <errno.h>
 #include <limits.h>
 #include <stddef.h>
 
+#include "utils/UnitTest/Test.h"
+
 TEST(LlvmLibcStrToULLTest, InvalidBase) {
   const char *ten = "10";
   errno = 0;
@@ -149,19 +149,19 @@ static char int_to_b36_char(int input) {
   if (input < 0 || input > 36)
     return '0';
   if (input < 10)
-    return '0' + input;
-  return 'A' + input - 10;
+    return static_cast<char>('0' + input);
+  return static_cast<char>('A' + input - 10);
 }
 
 TEST(LlvmLibcStrToULLTest, DecodeInOtherBases) {
   char small_string[4] = {'\0', '\0', '\0', '\0'};
-  for (unsigned long long base = 2; base <= 36; ++base) {
-    for (unsigned long long first_digit = 0; first_digit <= 36; ++first_digit) {
+  for (int base = 2; base <= 36; ++base) {
+    for (int first_digit = 0; first_digit <= 36; ++first_digit) {
       small_string[0] = int_to_b36_char(first_digit);
       if (first_digit < base) {
         errno = 0;
         ASSERT_EQ(__llvm_libc::strtoull(small_string, nullptr, base),
-                  first_digit);
+                  static_cast<unsigned long long int>(first_digit));
         ASSERT_EQ(errno, 0);
       } else {
         errno = 0;
@@ -171,21 +171,21 @@ TEST(LlvmLibcStrToULLTest, DecodeInOtherBases) {
     }
   }
 
-  for (unsigned long long base = 2; base <= 36; ++base) {
-    for (unsigned long long first_digit = 0; first_digit <= 36; ++first_digit) {
+  for (int base = 2; base <= 36; ++base) {
+    for (int first_digit = 0; first_digit <= 36; ++first_digit) {
       small_string[0] = int_to_b36_char(first_digit);
-      for (unsigned long long second_digit = 0; second_digit <= 36;
-           ++second_digit) {
+      for (int second_digit = 0; second_digit <= 36; ++second_digit) {
         small_string[1] = int_to_b36_char(second_digit);
         if (first_digit < base && second_digit < base) {
           errno = 0;
           ASSERT_EQ(__llvm_libc::strtoull(small_string, nullptr, base),
-                    second_digit + (first_digit * base));
+                    static_cast<unsigned long long int>(second_digit +
+                                                        (first_digit * base)));
           ASSERT_EQ(errno, 0);
         } else if (first_digit < base) {
           errno = 0;
           ASSERT_EQ(__llvm_libc::strtoull(small_string, nullptr, base),
-                    first_digit);
+                    static_cast<unsigned long long int>(first_digit));
           ASSERT_EQ(errno, 0);
         } else {
           errno = 0;
@@ -196,26 +196,26 @@ TEST(LlvmLibcStrToULLTest, DecodeInOtherBases) {
     }
   }
 
-  for (unsigned long long base = 2; base <= 36; ++base) {
-    for (unsigned long long first_digit = 0; first_digit <= 36; ++first_digit) {
+  for (int base = 2; base <= 36; ++base) {
+    for (int first_digit = 0; first_digit <= 36; ++first_digit) {
       small_string[0] = int_to_b36_char(first_digit);
-      for (unsigned long long second_digit = 0; second_digit <= 36;
-           ++second_digit) {
+      for (int second_digit = 0; second_digit <= 36; ++second_digit) {
         small_string[1] = int_to_b36_char(second_digit);
-        for (unsigned long long third_digit = 0; third_digit <= 36;
-             ++third_digit) {
+        for (int third_digit = 0; third_digit <= 36; ++third_digit) {
           small_string[2] = int_to_b36_char(third_digit);
 
           if (first_digit < base && second_digit < base && third_digit < base) {
             errno = 0;
             ASSERT_EQ(__llvm_libc::strtoull(small_string, nullptr, base),
-                      third_digit + (second_digit * base) +
-                          (first_digit * base * base));
+                      static_cast<unsigned long long int>(
+                          third_digit + (second_digit * base) +
+                          (first_digit * base * base)));
             ASSERT_EQ(errno, 0);
           } else if (first_digit < base && second_digit < base) {
             errno = 0;
             ASSERT_EQ(__llvm_libc::strtoull(small_string, nullptr, base),
-                      second_digit + (first_digit * base));
+                      static_cast<unsigned long long int>(
+                          second_digit + (first_digit * base)));
             ASSERT_EQ(errno, 0);
           } else if (first_digit < base) {
             // if the base is 16 there is a special case for the prefix 0X.
@@ -224,7 +224,7 @@ TEST(LlvmLibcStrToULLTest, DecodeInOtherBases) {
               if (third_digit < base) {
                 errno = 0;
                 ASSERT_EQ(__llvm_libc::strtoull(small_string, nullptr, base),
-                          third_digit);
+                          static_cast<unsigned long long int>(third_digit));
                 ASSERT_EQ(errno, 0);
               } else {
                 errno = 0;
@@ -235,7 +235,7 @@ TEST(LlvmLibcStrToULLTest, DecodeInOtherBases) {
             } else {
               errno = 0;
               ASSERT_EQ(__llvm_libc::strtoull(small_string, nullptr, base),
-                        first_digit);
+                        static_cast<unsigned long long int>(first_digit));
               ASSERT_EQ(errno, 0);
             }
           } else {


        


More information about the libc-commits mailing list