[libc-commits] [libc] [libc] Build with -Wshadow (PR #196519)
Nico Weber via libc-commits
libc-commits at lists.llvm.org
Mon May 11 17:59:05 PDT 2026
https://github.com/nico updated https://github.com/llvm/llvm-project/pull/196519
>From 39d85eb46637b50e9c6cef9df08b618cb124ab5f Mon Sep 17 00:00:00 2001
From: Nico Weber <thakis at chromium.org>
Date: Thu, 7 May 2026 11:09:47 -0400
Subject: [PATCH 01/10] [libc] Build with -Wshadow
---
libc/cmake/modules/LLVMLibCCompileOptionRules.cmake | 1 +
1 file changed, 1 insertion(+)
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 2d3703eefa0ac..db541a4dfb985 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -301,6 +301,7 @@ function(_get_common_compile_options output_var flags)
if(NOT CMAKE_COMPILER_IS_GNUCXX)
list(APPEND compile_options "-Wnewline-eof")
list(APPEND compile_options "-Wnonportable-system-include-path")
+ list(APPEND compile_options "-Wshadow")
list(APPEND compile_options "-Wstrict-prototypes")
list(APPEND compile_options "-Wthread-safety")
list(APPEND compile_options "-Wglobal-constructors")
>From 1c7feb984c1345af07e3b7a624b59b5c0860bb4d Mon Sep 17 00:00:00 2001
From: Nico Weber <thakis at chromium.org>
Date: Sun, 10 May 2026 17:48:58 -0400
Subject: [PATCH 02/10] [libc] Fix -Wshadow warnings in inline_strlen.h
---
libc/src/string/memory_utils/generic/inline_strlen.h | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/libc/src/string/memory_utils/generic/inline_strlen.h b/libc/src/string/memory_utils/generic/inline_strlen.h
index e9f1542f41424..833ff9fbcbf80 100644
--- a/libc/src/string/memory_utils/generic/inline_strlen.h
+++ b/libc/src/string/memory_utils/generic/inline_strlen.h
@@ -39,9 +39,8 @@ LIBC_NO_SANITIZE_OOB_ACCESS LIBC_INLINE size_t string_length(const char *src) {
return cpp::find_first_set(shift_mask<char>(mask, offset));
for (;;) {
- cpp::simd<char> chars = cpp::load<cpp::simd<char>>(++aligned,
- /*aligned=*/true);
- cpp::simd_mask<char> mask = chars == null_byte;
+ chars = cpp::load<cpp::simd<char>>(++aligned, /*aligned=*/true);
+ mask = chars == null_byte;
if (cpp::any_of(mask))
return (reinterpret_cast<const char *>(aligned) - src) +
cpp::find_first_set(mask);
@@ -78,8 +77,8 @@ find_first_character(const unsigned char *s, unsigned char c, size_t n) {
for (size_t bytes_checked = sizeof(Vector) - offset; bytes_checked < n;
bytes_checked += sizeof(Vector)) {
aligned++;
- Vector chars = cpp::load<Vector>(aligned, /*aligned=*/true);
- Mask cmp_v = chars == c_byte;
+ chars = cpp::load<Vector>(aligned, /*aligned=*/true);
+ cmp_v = chars == c_byte;
if (cpp::any_of(cmp_v))
return calculate_find_first_character_return(
reinterpret_cast<const char *>(aligned), cmp_v, n - bytes_checked);
>From 321b73008c9b2ca1576f1e71a4961b321a411d5f Mon Sep 17 00:00:00 2001
From: Nico Weber <thakis at chromium.org>
Date: Sun, 10 May 2026 17:49:14 -0400
Subject: [PATCH 03/10] [libc] Fix -Wshadow warning in sqrtf128.h
sqrtf128() contained both `using namespace sqrtf128_internal;`
and `using FPBits = fputil::FPBits<float128>;`, but sqrtf128_internal
also had a `using FPBits = fputil::FPBits<float128>;`. The outer
`using` wasn't actually used, so remove that one.
---
libc/src/__support/math/sqrtf128.h | 2 --
1 file changed, 2 deletions(-)
diff --git a/libc/src/__support/math/sqrtf128.h b/libc/src/__support/math/sqrtf128.h
index d5b54b724c259..14dcbec1acbb6 100644
--- a/libc/src/__support/math/sqrtf128.h
+++ b/libc/src/__support/math/sqrtf128.h
@@ -60,8 +60,6 @@ namespace math {
namespace sqrtf128_internal {
-using FPBits = fputil::FPBits<float128>;
-
template <typename T, typename U = T> LIBC_INLINE constexpr T prod_hi(T, U);
// Get high part of integer multiplications.
>From 72f8d6fa01fb8cb32d4eca09df001320d661c73e Mon Sep 17 00:00:00 2001
From: Nico Weber <thakis at chromium.org>
Date: Sun, 10 May 2026 18:54:49 -0400
Subject: [PATCH 04/10] [libc] Remove some global printf_core declarations in
float_dec_converter.h
fixed_converter.h, float_hex_converter.h have local declarations
with the same name shadowing these, causing -Wshadow warnings.
For now, just don't have global declarations for these.
---
.../stdio/printf_core/float_dec_converter.h | 21 +++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)
diff --git a/libc/src/stdio/printf_core/float_dec_converter.h b/libc/src/stdio/printf_core/float_dec_converter.h
index 509b4e24ada32..8b99688cb993c 100644
--- a/libc/src/stdio/printf_core/float_dec_converter.h
+++ b/libc/src/stdio/printf_core/float_dec_converter.h
@@ -29,11 +29,6 @@
namespace LIBC_NAMESPACE_DECL {
namespace printf_core {
-#ifdef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
-using StorageType = UInt128;
-#else
-using StorageType = fputil::FPBits<long double>::StorageType;
-#endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
using DecimalString = IntegerToString<intmax_t>;
using ExponentString =
IntegerToString<intmax_t, radix::Dec::WithWidth<2>::WithSign>;
@@ -51,7 +46,6 @@ constexpr uint32_t MAX_BLOCK = 999999999;
// constexpr size_t BLOCK_SIZE = 18;
// constexpr uint32_t MAX_BLOCK = 999999999999999999;
-constexpr char DECIMAL_POINT = '.';
LIBC_INLINE RoundDirection get_round_direction(int last_digit, bool truncated,
Sign sign) {
@@ -176,6 +170,7 @@ template <WriteMode write_mode> class FloatWriter {
LIBC_INLINE int flush_buffer(bool round_up_max_blocks = false) {
const char MAX_BLOCK_DIGIT = (round_up_max_blocks ? '0' : '9');
+ constexpr char DECIMAL_POINT = '.';
// Write the most recent buffered block, and mark has_written
if (!has_written) {
@@ -368,6 +363,8 @@ template <WriteMode write_mode> class FloatWriter {
// has_carry should only be true here if every previous digit is 9, which
// implies that the number has never been written.
if (has_carry /* && !has_written */) {
+ constexpr char DECIMAL_POINT = '.';
+
if (has_exp) { // This is in %e style
// Since this is exponential notation, we don't write any more digits
// but we do increment the exponent.
@@ -604,6 +601,12 @@ template <typename T, WriteMode write_mode,
LIBC_INLINE int convert_float_dec_exp_typed(Writer<write_mode> *writer,
const FormatSection &to_conv,
fputil::FPBits<T> float_bits) {
+#ifdef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
+ using StorageType = UInt128;
+#else
+ using StorageType = fputil::FPBits<long double>::StorageType;
+#endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
+
// signed because later we use -FRACTION_LEN
constexpr int32_t FRACTION_LEN = fputil::FPBits<T>::FRACTION_LEN;
int exponent = float_bits.get_explicit_exponent();
@@ -766,6 +769,12 @@ template <typename T, WriteMode write_mode,
LIBC_INLINE int convert_float_dec_auto_typed(Writer<write_mode> *writer,
const FormatSection &to_conv,
fputil::FPBits<T> float_bits) {
+#ifdef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
+ using StorageType = UInt128;
+#else
+ using StorageType = fputil::FPBits<long double>::StorageType;
+#endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
+
// signed because later we use -FRACTION_LEN
constexpr int32_t FRACTION_LEN = fputil::FPBits<T>::FRACTION_LEN;
int exponent = float_bits.get_explicit_exponent();
>From a84ade1f6a230c89e2f969c2abdef2d9cf0ea7d7 Mon Sep 17 00:00:00 2001
From: Nico Weber <thakis at chromium.org>
Date: Sun, 10 May 2026 19:09:49 -0400
Subject: [PATCH 05/10] [libc] Remove global printf_core StorageType
declarations in float_inf_nan_converter.h
fixed_converter.h, float_hex_converter.h have local declarations
with the same name shadowing these, causing -Wshadow warnings.
For now, just don't have global declarations for these.
---
libc/src/stdio/printf_core/float_inf_nan_converter.h | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/libc/src/stdio/printf_core/float_inf_nan_converter.h b/libc/src/stdio/printf_core/float_inf_nan_converter.h
index 379b7c6d3dcd6..973199d55094b 100644
--- a/libc/src/stdio/printf_core/float_inf_nan_converter.h
+++ b/libc/src/stdio/printf_core/float_inf_nan_converter.h
@@ -22,15 +22,15 @@
namespace LIBC_NAMESPACE_DECL {
namespace printf_core {
+template <WriteMode write_mode>
+LIBC_INLINE int convert_inf_nan(Writer<write_mode> *writer,
+ const FormatSection &to_conv) {
#ifdef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
-using StorageType = UInt128;
+ using StorageType = UInt128;
#else
-using StorageType = fputil::FPBits<long double>::StorageType;
+ using StorageType = fputil::FPBits<long double>::StorageType;
#endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
-template <WriteMode write_mode>
-LIBC_INLINE int convert_inf_nan(Writer<write_mode> *writer,
- const FormatSection &to_conv) {
// All of the letters will be defined relative to variable a, which will be
// the appropriate case based on the case of the conversion.
bool is_negative;
>From 03590293c6cbd55322dee927e13b09f3137b5ac1 Mon Sep 17 00:00:00 2001
From: Nico Weber <thakis at chromium.org>
Date: Sun, 10 May 2026 19:12:47 -0400
Subject: [PATCH 06/10] [libc] Fix -Wshadow warning in atanf.h
x_d already exists in an outer scope and isn't modified if
`x_abs < 0x3980'0000`, so no need to have a shadowing variable.
No behavior change.
---
libc/src/__support/math/atanf.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/libc/src/__support/math/atanf.h b/libc/src/__support/math/atanf.h
index 633a95b02d5ff..d583572ea4727 100644
--- a/libc/src/__support/math/atanf.h
+++ b/libc/src/__support/math/atanf.h
@@ -67,7 +67,6 @@ LIBC_INLINE constexpr float atanf(float x) {
#if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT)
return fputil::multiply_add(x, -0x1.0p-25f, x);
#else
- double x_d = static_cast<double>(x);
return static_cast<float>(fputil::multiply_add(x_d, -0x1.0p-25, x_d));
#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
}
>From c1830a86532f6cff42c2f649b34afe3dd937066b Mon Sep 17 00:00:00 2001
From: Nico Weber <thakis at chromium.org>
Date: Sun, 10 May 2026 19:18:33 -0400
Subject: [PATCH 07/10] [libc] Fix a -Wshadow warning in raw_rwlock.h
---
libc/src/__support/threads/raw_rwlock.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/src/__support/threads/raw_rwlock.h b/libc/src/__support/threads/raw_rwlock.h
index 2c14645a2e5ce..9bc1b88cabd69 100644
--- a/libc/src/__support/threads/raw_rwlock.h
+++ b/libc/src/__support/threads/raw_rwlock.h
@@ -399,8 +399,8 @@ class RawRwLock {
// reached.
bool timeout_flag = false;
if (!old.can_acquire<role>(get_preference())) {
- auto result = queue.wait<role>(serial_number, timeout, is_pshared);
- timeout_flag = (!result.has_value() && timeout.has_value());
+ auto wait_result = queue.wait<role>(serial_number, timeout, is_pshared);
+ timeout_flag = (!wait_result.has_value() && timeout.has_value());
}
// Phase 7: unregister ourselves as a pending reader/writer.
>From 7bbf3606298389770d789b99dde23d472d412133 Mon Sep 17 00:00:00 2001
From: Nico Weber <thakis at chromium.org>
Date: Sun, 10 May 2026 19:23:53 -0400
Subject: [PATCH 08/10] [libc] Fix -Wshadow warning in HashTable/table.h
---
libc/src/__support/HashTable/table.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/libc/src/__support/HashTable/table.h b/libc/src/__support/HashTable/table.h
index f42eead2efcf2..578f01e0e2c55 100644
--- a/libc/src/__support/HashTable/table.h
+++ b/libc/src/__support/HashTable/table.h
@@ -208,10 +208,10 @@ struct HashTable {
LIBC_INLINE HashTable *grow() const {
size_t hint = full_capacity() + 1;
- HashState state = this->state;
+ HashState new_state = state;
// migrate to a new random state
- state.update(&hint, sizeof(hint));
- HashTable *new_table = allocate(hint, state.finish());
+ new_state.update(&hint, sizeof(hint));
+ HashTable *new_table = allocate(hint, new_state.finish());
// It is safe to call unsafe_insert() because we know that:
// - the new table has enough capacity to hold all the entries
// - there is no duplicate key in the old table
>From 9a06453bc36ea5d97c8d0a4fcbc7d7f3cb25c4af Mon Sep 17 00:00:00 2001
From: Nico Weber <thakis at chromium.org>
Date: Sun, 10 May 2026 19:33:46 -0400
Subject: [PATCH 09/10] [libc] Fix -Wshadow warnings in getopt.cpp
---
libc/src/unistd/getopt.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/libc/src/unistd/getopt.cpp b/libc/src/unistd/getopt.cpp
index 6da47e73926cc..84b962d0cdda8 100644
--- a/libc/src/unistd/getopt.cpp
+++ b/libc/src/unistd/getopt.cpp
@@ -191,9 +191,9 @@ static GetoptContext ctx{&impl::optarg, &impl::optind, &impl::optopt,
#ifndef LIBC_COPT_PUBLIC_PACKAGING
// This is used exclusively in tests.
-void set_getopt_state(char **optarg, int *optind, int *optopt, unsigned *optpos,
- int *opterr, FILE *errstream) {
- ctx = {optarg, optind, optopt, optpos, opterr, errstream};
+void set_getopt_state(char **optarg_in, int *optind_in, int *optopt_in,
+ unsigned *optpos_in, int *opterr_in, FILE *errstream) {
+ ctx = {optarg_in, optind_in, optopt_in, optpos_in, opterr_in, errstream};
}
#endif
>From 0946de1b5c5292fb3241a2952879f3c9298cb953 Mon Sep 17 00:00:00 2001
From: Nico Weber <thakis at chromium.org>
Date: Sun, 10 May 2026 20:07:34 -0400
Subject: [PATCH 10/10] [libc] Fix -Wshadow warning in IntegrationTest/test.cpp
---
libc/test/IntegrationTest/test.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/test/IntegrationTest/test.cpp b/libc/test/IntegrationTest/test.cpp
index 19eb255e57b04..0a9728f021e90 100644
--- a/libc/test/IntegrationTest/test.cpp
+++ b/libc/test/IntegrationTest/test.cpp
@@ -66,12 +66,12 @@ int atexit(void (*func)(void)) { return LIBC_NAMESPACE::atexit(func); }
static constexpr uint64_t ALIGNMENT = alignof(double);
static constexpr uint64_t MEMORY_SIZE = 256 * 1024 /* 256 KiB */;
alignas(ALIGNMENT) static uint8_t memory[MEMORY_SIZE];
-static size_t ptr = 0;
+static size_t global_ptr = 0;
extern "C" {
void *malloc(size_t size) {
- LIBC_NAMESPACE::cpp::AtomicRef<size_t> ref(ptr);
+ LIBC_NAMESPACE::cpp::AtomicRef<size_t> ref(global_ptr);
size = (size + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
size_t old_ptr =
ref.fetch_add(size, LIBC_NAMESPACE::cpp::MemoryOrder::RELAXED);
More information about the libc-commits
mailing list