[libc-commits] [libc] [libc] Add -Werror for libc tests (PR #160413)
    Vinay Deshmukh via libc-commits 
    libc-commits at lists.llvm.org
       
    Sat Sep 27 19:02:11 PDT 2025
    
    
  
https://github.com/vinay-deshmukh updated https://github.com/llvm/llvm-project/pull/160413
>From 9576513f202177c8733c6cb9fe82c9df5f88ad29 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <vinay_deshmukh at outlook.com>
Date: Tue, 23 Sep 2025 19:48:33 -0400
Subject: [PATCH 1/9] -Werror
---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 19da0ad29cd84..34abc0a58f8cd 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -43,7 +43,7 @@ function(_get_common_test_compile_options output_var c_test flags)
     list(APPEND compile_options "-Wextra")
     # -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror.
     if(NOT LIBC_WNO_ERROR)
-      # list(APPEND compile_options "-Werror")
+      list(APPEND compile_options "-Werror")
     endif()
     list(APPEND compile_options "-Wconversion")
     # FIXME: convert to -Wsign-conversion
>From 26fe540482a2247518492ee1ca3a47721d25b897 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <vinay_deshmukh at outlook.com>
Date: Sat, 27 Sep 2025 20:37:45 -0400
Subject: [PATCH 2/9] error: implicit conversion loses integer precision:
 'unsigned long' to 'int' [-Werror,-Wshorten-64-to-32]
---
 libc/test/src/stdio/fileop_test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/src/stdio/fileop_test.cpp b/libc/test/src/stdio/fileop_test.cpp
index 02328042b92b3..da9a062e83bf2 100644
--- a/libc/test/src/stdio/fileop_test.cpp
+++ b/libc/test/src/stdio/fileop_test.cpp
@@ -100,7 +100,7 @@ TEST_F(LlvmLibcFILETest, SimpleFileOperations) {
   ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
 
   // This is not a readable file.
-  ASSERT_THAT(LIBC_NAMESPACE::fread(data, 1, 1, file),
+  ASSERT_THAT((int)LIBC_NAMESPACE::fread(data, 1, 1, file),
               returns(EQ(0)).with_errno(NE(0)));
 
   ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
>From cd4adf58fbd2313f0627221026c889c7cd7f0093 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <vinay_deshmukh at outlook.com>
Date: Sat, 27 Sep 2025 21:12:04 -0400
Subject: [PATCH 3/9] error: implicit conversion loses integer precision:
 'unsigned long' to 'int' [-Werror,-Wshorten-64-to-32]
---
 libc/test/src/stdio/fileop_test.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/libc/test/src/stdio/fileop_test.cpp b/libc/test/src/stdio/fileop_test.cpp
index da9a062e83bf2..ed4ba26ed8734 100644
--- a/libc/test/src/stdio/fileop_test.cpp
+++ b/libc/test/src/stdio/fileop_test.cpp
@@ -100,8 +100,8 @@ TEST_F(LlvmLibcFILETest, SimpleFileOperations) {
   ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
 
   // This is not a readable file.
-  ASSERT_THAT((int)LIBC_NAMESPACE::fread(data, 1, 1, file),
-              returns(EQ(0)).with_errno(NE(0)));
+  ASSERT_THAT(LIBC_NAMESPACE::fread(data, 1, 1, file),
+              returns(EQ(size_t(0))).with_errno(NE(0)));
 
   ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
 
@@ -175,7 +175,7 @@ TEST_F(LlvmLibcFILETest, FOpenFWriteSizeGreaterThanOne) {
   // Trying to read more should fetch nothing.
   ASSERT_THAT(
       LIBC_NAMESPACE::fread(read_data, sizeof(MyStruct), WRITE_NMEMB, file),
-      returns(EQ(0)).with_errno(EQ(0)));
+      returns(EQ(size_t(0))).with_errno(EQ(0)));
   EXPECT_NE(LIBC_NAMESPACE::feof(file), 0);
   EXPECT_EQ(LIBC_NAMESPACE::ferror(file), 0);
   ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
>From 69d35e29046580dee570cb3e297da7703d029c33 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <vinay_deshmukh at outlook.com>
Date: Sat, 27 Sep 2025 21:14:02 -0400
Subject: [PATCH 4/9] error: unused parameter 'y_start'
 [-Werror,-Wunused-parameter]
---
 libc/test/src/math/exhaustive/bfloat16_sub_test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/src/math/exhaustive/bfloat16_sub_test.cpp b/libc/test/src/math/exhaustive/bfloat16_sub_test.cpp
index 11bc6f59dd294..c3d0d5c3ba4cd 100644
--- a/libc/test/src/math/exhaustive/bfloat16_sub_test.cpp
+++ b/libc/test/src/math/exhaustive/bfloat16_sub_test.cpp
@@ -23,7 +23,7 @@ struct Bfloat16SubChecker : public virtual LIBC_NAMESPACE::testing::Test {
   using FPBits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
   using StorageType = typename FPBits::StorageType;
 
-  uint64_t check(uint16_t x_start, uint16_t x_stop, uint16_t y_start,
+  uint64_t check(uint16_t x_start, uint16_t x_stop, [[maybe_unused]] uint16_t y_start,
                  uint16_t y_stop, mpfr::RoundingMode rounding) {
     mpfr::ForceRoundingMode r(rounding);
     if (!r.success)
>From 96bcb9fcd9bc14e7c18abf236d66a469e422564c Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <vinay_deshmukh at outlook.com>
Date: Sat, 27 Sep 2025 21:20:41 -0400
Subject: [PATCH 5/9] error: unused parameter 'y_start'
 [-Werror,-Wunused-parameter]
---
 libc/test/src/math/exhaustive/bfloat16_add_test.cpp | 2 +-
 libc/test/src/math/exhaustive/bfloat16_div_test.cpp | 2 +-
 libc/test/src/math/exhaustive/bfloat16_mul_test.cpp | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/libc/test/src/math/exhaustive/bfloat16_add_test.cpp b/libc/test/src/math/exhaustive/bfloat16_add_test.cpp
index 3f4c77978a93d..ff942196dccb5 100644
--- a/libc/test/src/math/exhaustive/bfloat16_add_test.cpp
+++ b/libc/test/src/math/exhaustive/bfloat16_add_test.cpp
@@ -23,7 +23,7 @@ struct Bfloat16AddChecker : public virtual LIBC_NAMESPACE::testing::Test {
   using FPBits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
   using StorageType = typename FPBits::StorageType;
 
-  uint64_t check(uint16_t x_start, uint16_t x_stop, uint16_t y_start,
+  uint64_t check(uint16_t x_start, uint16_t x_stop, [[maybe_unused]] uint16_t y_start,
                  uint16_t y_stop, mpfr::RoundingMode rounding) {
     mpfr::ForceRoundingMode r(rounding);
     if (!r.success)
diff --git a/libc/test/src/math/exhaustive/bfloat16_div_test.cpp b/libc/test/src/math/exhaustive/bfloat16_div_test.cpp
index 2648d5f775af5..48750e40d5f61 100644
--- a/libc/test/src/math/exhaustive/bfloat16_div_test.cpp
+++ b/libc/test/src/math/exhaustive/bfloat16_div_test.cpp
@@ -23,7 +23,7 @@ struct Bfloat16DivChecker : public virtual LIBC_NAMESPACE::testing::Test {
   using FPBits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
   using StorageType = typename FPBits::StorageType;
 
-  uint64_t check(uint16_t x_start, uint16_t x_stop, uint16_t y_start,
+  uint64_t check(uint16_t x_start, uint16_t x_stop, [[maybe_unused]] uint16_t y_start,
                  uint16_t y_stop, mpfr::RoundingMode rounding) {
     mpfr::ForceRoundingMode r(rounding);
     if (!r.success)
diff --git a/libc/test/src/math/exhaustive/bfloat16_mul_test.cpp b/libc/test/src/math/exhaustive/bfloat16_mul_test.cpp
index 3cbbcb500a4fb..ab1d46889a4c6 100644
--- a/libc/test/src/math/exhaustive/bfloat16_mul_test.cpp
+++ b/libc/test/src/math/exhaustive/bfloat16_mul_test.cpp
@@ -23,7 +23,7 @@ struct Bfloat16MulChecker : public virtual LIBC_NAMESPACE::testing::Test {
   using FPBits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
   using StorageType = typename FPBits::StorageType;
 
-  uint64_t check(uint16_t x_start, uint16_t x_stop, uint16_t y_start,
+  uint64_t check(uint16_t x_start, uint16_t x_stop, [[maybe_unused]] uint16_t y_start,
                  uint16_t y_stop, mpfr::RoundingMode rounding) {
     mpfr::ForceRoundingMode r(rounding);
     if (!r.success)
>From ccdaa1b097d7bc07b6163d8c198fb5e11ef5eb36 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <vinay_deshmukh at outlook.com>
Date: Sat, 27 Sep 2025 21:28:53 -0400
Subject: [PATCH 6/9] error: implicit conversion loses integer precision:
 'size_t' (aka 'unsigned long') to 'int' [-Werror,-Wshorten-64-to-32]
---
 libc/test/src/stdio/fopen_test.cpp | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/libc/test/src/stdio/fopen_test.cpp b/libc/test/src/stdio/fopen_test.cpp
index 3f651f755e7f3..02612e1889a61 100644
--- a/libc/test/src/stdio/fopen_test.cpp
+++ b/libc/test/src/stdio/fopen_test.cpp
@@ -15,15 +15,14 @@
 #include "test/UnitTest/Test.h"
 
 TEST(LlvmLibcFOpenTest, PrintToFile) {
-  int result;
 
   FILE *file =
       LIBC_NAMESPACE::fopen(APPEND_LIBC_TEST("testdata/test.txt"), "w");
   ASSERT_FALSE(file == nullptr);
 
   static constexpr char STRING[] = "A simple string written to a file\n";
-  result = LIBC_NAMESPACE::fwrite(STRING, 1, sizeof(STRING) - 1, file);
-  EXPECT_GE(result, 0);
+  size_t result = LIBC_NAMESPACE::fwrite(STRING, 1, sizeof(STRING) - 1, file);
+  EXPECT_GE(result, size_t(0));
 
   ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
 
>From 628d6c6cdb81d289bd251d270ef6cdb839a9b496 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <vinay_deshmukh at outlook.com>
Date: Sat, 27 Sep 2025 21:51:20 -0400
Subject: [PATCH 7/9] error: variable 't' is uninitialized when passed as a
 const pointer argument here [-Werror,-Wuninitialized-const-pointer]
---
 libc/test/src/time/ctime_r_test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/src/time/ctime_r_test.cpp b/libc/test/src/time/ctime_r_test.cpp
index ee06c706734fb..084db300bf2d6 100644
--- a/libc/test/src/time/ctime_r_test.cpp
+++ b/libc/test/src/time/ctime_r_test.cpp
@@ -23,7 +23,7 @@ TEST_F(LlvmLibcCtimeR, Nullptr) {
   result = LIBC_NAMESPACE::ctime_r(nullptr, buffer);
   ASSERT_STREQ(nullptr, result);
 
-  time_t t;
+  time_t t = 2147483648; // invalid argument
   result = LIBC_NAMESPACE::ctime_r(&t, nullptr);
   ASSERT_STREQ(nullptr, result);
 }
>From d2921298d3cea4dffde0d399585bb2092eaa98ad Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <vinay_deshmukh at outlook.com>
Date: Sat, 27 Sep 2025 21:52:49 -0400
Subject: [PATCH 8/9] error: implicit conversion loses integer precision:
 'long' to 'int' [-Werror,-Wshorten-64-to-32]
---
 libc/test/src/time/mktime_test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/src/time/mktime_test.cpp b/libc/test/src/time/mktime_test.cpp
index 1dfdd73de5440..0f3f1ecce5bdf 100644
--- a/libc/test/src/time/mktime_test.cpp
+++ b/libc/test/src/time/mktime_test.cpp
@@ -36,7 +36,7 @@ TEST(LlvmLibcMkTime, FailureSetsErrno) {
                     .tm_wday = 0,
                     .tm_yday = 0,
                     .tm_isdst = 0};
-  EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW));
+  EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(time_t(EOVERFLOW)));
 }
 
 TEST(LlvmLibcMkTime, InvalidSeconds) {
>From 9766119d3f89baf7adf0a07318ea875b848c0c76 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <vinay_deshmukh at outlook.com>
Date: Sat, 27 Sep 2025 22:01:49 -0400
Subject: [PATCH 9/9] error: implicit conversion loses integer precision:
 'long' to 'int' [-Werror,-Wshorten-64-to-32]
---
 libc/test/src/time/mktime_test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/src/time/mktime_test.cpp b/libc/test/src/time/mktime_test.cpp
index 0f3f1ecce5bdf..421b3bb8db9eb 100644
--- a/libc/test/src/time/mktime_test.cpp
+++ b/libc/test/src/time/mktime_test.cpp
@@ -36,7 +36,7 @@ TEST(LlvmLibcMkTime, FailureSetsErrno) {
                     .tm_wday = 0,
                     .tm_yday = 0,
                     .tm_isdst = 0};
-  EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(time_t(EOVERFLOW)));
+  EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW));
 }
 
 TEST(LlvmLibcMkTime, InvalidSeconds) {
    
    
More information about the libc-commits
mailing list