[libc-commits] [libc] [libc] Add `-Wsign-conversion` (PR #129811)

Vinay Deshmukh via libc-commits libc-commits at lists.llvm.org
Wed Mar 5 17:23:20 PST 2025


https://github.com/vinay-deshmukh updated https://github.com/llvm/llvm-project/pull/129811

>From b8a722781bc74c50d7e19dd658bfb82682de048f Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Tue, 4 Mar 2025 20:20:19 -0500
Subject: [PATCH 1/4] Add `-Wsign-conversion`

---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 03b76508fdf6c..d332d5d044045 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -37,6 +37,7 @@ function(_get_common_test_compile_options output_var c_test flags)
       # list(APPEND compile_options "-Werror")
     endif()
     list(APPEND compile_options "-Wconversion")
+    list(APPEND compile_options "-Wsign-conversion")
     list(APPEND compile_options "-Wimplicit-fallthrough")
     list(APPEND compile_options "-Wwrite-strings")
     # Silence this warning because _Complex is a part of C99.

>From ff8e3d5094b3313df8aef28bd7b30796d2a81064 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Tue, 4 Mar 2025 20:33:19 -0500
Subject: [PATCH 2/4] WIP: sign-conversion

---
 libc/src/__support/big_int.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index f44624a7eafce..17714fbad6293 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -285,7 +285,10 @@ LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array,
       return 0;
     if (i >= int(N))
       return is_neg ? cpp::numeric_limits<word>::max() : 0;
+    #pragma GCC diagnostic push
+    #pragma GCC diagnostic ignored "-Wsign-conversion"
     return array[i];
+    #pragma GCC diagnostic pop
   };
   const size_t index_offset = offset / WORD_BITS;
   const size_t bit_offset = offset % WORD_BITS;
@@ -296,7 +299,10 @@ LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array,
   for (size_t index = 0; index < N; ++index) {
     const word part1 = safe_get_at(index + index_offset);
     const word part2 = safe_get_at(index + index_offset + 1);
+    #pragma GCC diagnostic push
+    #pragma GCC diagnostic ignored "-Wsign-conversion"
     word &dst = out[at(index)];
+    #pragma GCC diagnostic pop
     if (bit_offset == 0)
       dst = part1; // no crosstalk between parts.
     else if constexpr (direction == LEFT)

>From 83f4ec0a4efd345afd12cdd6adb997d37febb029 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Tue, 4 Mar 2025 20:53:06 -0500
Subject: [PATCH 3/4] clang-format

---
 libc/src/__support/big_int.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index 17714fbad6293..26b783dc0f357 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -285,10 +285,10 @@ LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array,
       return 0;
     if (i >= int(N))
       return is_neg ? cpp::numeric_limits<word>::max() : 0;
-    #pragma GCC diagnostic push
-    #pragma GCC diagnostic ignored "-Wsign-conversion"
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wsign-conversion"
     return array[i];
-    #pragma GCC diagnostic pop
+#pragma GCC diagnostic pop
   };
   const size_t index_offset = offset / WORD_BITS;
   const size_t bit_offset = offset % WORD_BITS;
@@ -299,10 +299,10 @@ LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array,
   for (size_t index = 0; index < N; ++index) {
     const word part1 = safe_get_at(index + index_offset);
     const word part2 = safe_get_at(index + index_offset + 1);
-    #pragma GCC diagnostic push
-    #pragma GCC diagnostic ignored "-Wsign-conversion"
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wsign-conversion"
     word &dst = out[at(index)];
-    #pragma GCC diagnostic pop
+#pragma GCC diagnostic pop
     if (bit_offset == 0)
       dst = part1; // no crosstalk between parts.
     else if constexpr (direction == LEFT)

>From 0d17ad6f3de660bcf5efcbb42359c6e70a1cf948 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Wed, 5 Mar 2025 20:22:56 -0500
Subject: [PATCH 4/4] better cast

---
 libc/src/__support/big_int.h | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index 26b783dc0f357..04e9f3cf656a1 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -285,10 +285,7 @@ LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array,
       return 0;
     if (i >= int(N))
       return is_neg ? cpp::numeric_limits<word>::max() : 0;
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wsign-conversion"
-    return array[i];
-#pragma GCC diagnostic pop
+    return array[static_cast<size_t>(i)];
   };
   const size_t index_offset = offset / WORD_BITS;
   const size_t bit_offset = offset % WORD_BITS;
@@ -299,10 +296,7 @@ LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array,
   for (size_t index = 0; index < N; ++index) {
     const word part1 = safe_get_at(index + index_offset);
     const word part2 = safe_get_at(index + index_offset + 1);
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wsign-conversion"
-    word &dst = out[at(index)];
-#pragma GCC diagnostic pop
+    word &dst = out[static_cast<size_t>(at(index))];
     if (bit_offset == 0)
       dst = part1; // no crosstalk between parts.
     else if constexpr (direction == LEFT)



More information about the libc-commits mailing list