[libc] Add explicit casts for string functions
authorAlex Brachet <abrachet@google.com>
Mon, 13 Jun 2022 21:06:27 +0000 (21:06 +0000)
committerAlex Brachet <abrachet@google.com>
Mon, 13 Jun 2022 21:07:45 +0000 (21:07 +0000)
This fixes warnings from `-Wimplicit-int-conversion`

Differential revision: https://reviews.llvm.org/D127694

libc/src/string/memchr.cpp
libc/src/string/memory_utils/elements_x86.h
libc/src/string/memrchr.cpp
libc/src/string/strchr.cpp
libc/src/string/strrchr.cpp

index 02b4016398414b315f488fd181a21cbc8f5af212..a2a537680920d8d9de138685e2675c1ec4fb1884 100644 (file)
@@ -17,7 +17,8 @@ namespace __llvm_libc {
 // TODO: Look at performance benefits of comparing words.
 LLVM_LIBC_FUNCTION(void *, memchr, (const void *src, int c, size_t n)) {
   return internal::find_first_character(
-      reinterpret_cast<const unsigned char *>(src), c, n);
+      reinterpret_cast<const unsigned char *>(src),
+      static_cast<unsigned char>(c), n);
 }
 
 } // namespace __llvm_libc
index 89a88c6703d5fa8c5afa3e0f7d1a4582027bc6fa..f25c66be74cac160a5b1fd9b8df92c1a6b8aa2ee 100644 (file)
@@ -67,7 +67,8 @@ struct M128 {
   using T = char __attribute__((__vector_size__(SIZE)));
   static uint16_t mask(T value) {
     // NOLINTNEXTLINE(llvmlibc-callee-namespace)
-    return _mm_movemask_epi8(__llvm_libc::bit_cast<__m128i>(value));
+    return static_cast<uint16_t>(
+        _mm_movemask_epi8(__llvm_libc::bit_cast<__m128i>(value)));
   }
   static uint16_t not_equal_mask(T a, T b) { return mask(a != b); }
   static T load(const char *ptr) {
index b010b8ad87c5fa239b4d92b7e8ca3ce09104bd01..7efac04a63248cd76057ca5d27dcc63a53fa4905 100644 (file)
@@ -14,7 +14,7 @@ namespace __llvm_libc {
 
 LLVM_LIBC_FUNCTION(void *, memrchr, (const void *src, int c, size_t n)) {
   const unsigned char *str = reinterpret_cast<const unsigned char *>(src);
-  const unsigned char ch = c;
+  const unsigned char ch = static_cast<unsigned char>(c);
   for (; n != 0; --n) {
     const unsigned char *s = str + n - 1;
     if (*s == ch)
index ab663eac500e189fb1831e01b95da0f022136e58..724c2a2d164907c9941fca9c2d648bd3b78a3e30 100644 (file)
@@ -14,7 +14,7 @@ namespace __llvm_libc {
 
 // TODO: Look at performance benefits of comparing words.
 LLVM_LIBC_FUNCTION(char *, strchr, (const char *src, int c)) {
-  const char ch = c;
+  const char ch = static_cast<char>(c);
   for (; *src && *src != ch; ++src)
     ;
   return *src == ch ? const_cast<char *>(src) : nullptr;
index b5852add7fe9ad11f63be2693a21ebd15b6aa75d..2e987fa2b86044c052eab4a753db98e7670ec456 100644 (file)
@@ -13,7 +13,7 @@
 namespace __llvm_libc {
 
 LLVM_LIBC_FUNCTION(char *, strrchr, (const char *src, int c)) {
-  const char ch = c;
+  const char ch = static_cast<char>(c);
   char *last_occurrence = nullptr;
   for (; *src; ++src) {
     if (*src == ch)