[libc-commits] [libc] 6d1543a - [libc] Add explicit casts for string functions

Alex Brachet via libc-commits libc-commits at lists.llvm.org
Mon Jun 13 14:07:53 PDT 2022


Author: Alex Brachet
Date: 2022-06-13T21:07:45Z
New Revision: 6d1543a16797fa07eecea7e542df5b42422fc721

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

LOG: [libc] Add explicit casts for string functions

This fixes warnings from `-Wimplicit-int-conversion`

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

Added: 
    

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/libc/src/string/memchr.cpp b/libc/src/string/memchr.cpp
index 02b4016398414..a2a537680920d 100644
--- a/libc/src/string/memchr.cpp
+++ b/libc/src/string/memchr.cpp
@@ -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

diff  --git a/libc/src/string/memory_utils/elements_x86.h b/libc/src/string/memory_utils/elements_x86.h
index 89a88c6703d5f..f25c66be74cac 100644
--- a/libc/src/string/memory_utils/elements_x86.h
+++ b/libc/src/string/memory_utils/elements_x86.h
@@ -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) {

diff  --git a/libc/src/string/memrchr.cpp b/libc/src/string/memrchr.cpp
index b010b8ad87c5f..7efac04a63248 100644
--- a/libc/src/string/memrchr.cpp
+++ b/libc/src/string/memrchr.cpp
@@ -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)

diff  --git a/libc/src/string/strchr.cpp b/libc/src/string/strchr.cpp
index ab663eac500e1..724c2a2d16490 100644
--- a/libc/src/string/strchr.cpp
+++ b/libc/src/string/strchr.cpp
@@ -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;

diff  --git a/libc/src/string/strrchr.cpp b/libc/src/string/strrchr.cpp
index b5852add7fe9a..2e987fa2b8604 100644
--- a/libc/src/string/strrchr.cpp
+++ b/libc/src/string/strrchr.cpp
@@ -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)


        


More information about the libc-commits mailing list