[libc-commits] [libc] [libc]: Add `-Wglobal-constructors` for libc tests (PR #131485)

Vinay Deshmukh via libc-commits libc-commits at lists.llvm.org
Sun Mar 23 12:17:00 PDT 2025


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

>From 6dd5a77e6f107b5f83d82405a2f4346498228e73 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 15 Mar 2025 22:01:35 -0400
Subject: [PATCH 1/6] `-Wglobal-constructors`

---
 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 45a36abd8ce1b..fef670885229c 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -57,7 +57,7 @@ function(_get_common_test_compile_options output_var c_test flags)
       list(APPEND compile_options "-Wnewline-eof")
       list(APPEND compile_options "-Wnonportable-system-include-path")
       list(APPEND compile_options "-Wthread-safety")
-      # list(APPEND compile_options "-Wglobal-constructors")
+      list(APPEND compile_options "-Wglobal-constructors")
     endif()
   endif()
   set(${output_var} ${compile_options} PARENT_SCOPE)

>From a142b5be42a18cc1cd06040af671ac6e92a0b2e6 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sat, 15 Mar 2025 22:09:31 -0400
Subject: [PATCH 2/6] Werror to break pipeline

---
 libc/CMakeLists.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt
index ad39ff6fbcb1e..15cbda1df7ded 100644
--- a/libc/CMakeLists.txt
+++ b/libc/CMakeLists.txt
@@ -34,6 +34,7 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug")
   add_definitions("-D_DEBUG")
 endif()
 
+add_compile_options("-Werror")
 
 # Default to C++17
 set(CMAKE_CXX_STANDARD 17)

>From 6b558fb81bfc129455e06d408b6adc03a42aa7be Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Mon, 17 Mar 2025 20:41:38 -0400
Subject: [PATCH 3/6] Use static member instances

---
 libc/test/UnitTest/LibcTest.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libc/test/UnitTest/LibcTest.h b/libc/test/UnitTest/LibcTest.h
index fbeafd0bacb75..2c53a4127becf 100644
--- a/libc/test/UnitTest/LibcTest.h
+++ b/libc/test/UnitTest/LibcTest.h
@@ -350,9 +350,9 @@ CString libc_make_test_file_path_func(const char *file_name);
     }                                                                          \
     void Run() override;                                                       \
     const char *getName() const override { return name; }                      \
-  };                                                                           \
-  TypeList::Tests<SuiteName##_##TestName>::type                                \
+    static TypeList::Tests<SuiteName##_##TestName>::type                       \
       SuiteName##_##TestName##_Instance;                                       \
+  };                                                                           \
   template <typename T> void SuiteName##_##TestName<T>::Run()
 
 #define TYPED_TEST_F(SuiteClass, TestName, TypeList)                           \
@@ -370,9 +370,9 @@ CString libc_make_test_file_path_func(const char *file_name);
     }                                                                          \
     void Run() override;                                                       \
     const char *getName() const override { return name; }                      \
-  };                                                                           \
-  TypeList::Tests<SuiteClass##_##TestName>::type                               \
+    static TypeList::Tests<SuiteClass##_##TestName>::type                      \
       SuiteClass##_##TestName##_Instance;                                      \
+  };                                                                           \
   template <typename T> void SuiteClass##_##TestName<T>::Run()
 
 #define TEST(SuiteName, TestName)                                              \
@@ -383,8 +383,8 @@ CString libc_make_test_file_path_func(const char *file_name);
     SuiteName##_##TestName() { addTest(this); }                                \
     void Run() override;                                                       \
     const char *getName() const override { return #SuiteName "." #TestName; }  \
+    static SuiteName##_##TestName SuiteName##_##TestName##_Instance;           \
   };                                                                           \
-  SuiteName##_##TestName SuiteName##_##TestName##_Instance;                    \
   void SuiteName##_##TestName::Run()
 
 #define TEST_F(SuiteClass, TestName)                                           \
@@ -396,8 +396,8 @@ CString libc_make_test_file_path_func(const char *file_name);
     SuiteClass##_##TestName() { addTest(this); }                               \
     void Run() override;                                                       \
     const char *getName() const override { return #SuiteClass "." #TestName; } \
+    static SuiteClass##_##TestName SuiteClass##_##TestName##_Instance;         \
   };                                                                           \
-  SuiteClass##_##TestName SuiteClass##_##TestName##_Instance;                  \
   void SuiteClass##_##TestName::Run()
 
 // Helper to trick the compiler into ignoring lack of braces on the else

>From 168dc9dfdd19f3d33f16971d38f365f7ac25cca0 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Wed, 19 Mar 2025 20:20:53 -0400
Subject: [PATCH 4/6] cf

---
 libc/test/UnitTest/LibcTest.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/test/UnitTest/LibcTest.h b/libc/test/UnitTest/LibcTest.h
index 2c53a4127becf..ed75e230218ca 100644
--- a/libc/test/UnitTest/LibcTest.h
+++ b/libc/test/UnitTest/LibcTest.h
@@ -351,7 +351,7 @@ CString libc_make_test_file_path_func(const char *file_name);
     void Run() override;                                                       \
     const char *getName() const override { return name; }                      \
     static TypeList::Tests<SuiteName##_##TestName>::type                       \
-      SuiteName##_##TestName##_Instance;                                       \
+        SuiteName##_##TestName##_Instance;                                     \
   };                                                                           \
   template <typename T> void SuiteName##_##TestName<T>::Run()
 
@@ -371,7 +371,7 @@ CString libc_make_test_file_path_func(const char *file_name);
     void Run() override;                                                       \
     const char *getName() const override { return name; }                      \
     static TypeList::Tests<SuiteClass##_##TestName>::type                      \
-      SuiteClass##_##TestName##_Instance;                                      \
+        SuiteClass##_##TestName##_Instance;                                    \
   };                                                                           \
   template <typename T> void SuiteClass##_##TestName<T>::Run()
 

>From 70ba2f96202ea8cf43fb6914afcadd4d5f765a19 Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Thu, 20 Mar 2025 18:31:53 -0400
Subject: [PATCH 5/6] try to isolate warning

---
 libc/CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt
index 15cbda1df7ded..b86eefc05e782 100644
--- a/libc/CMakeLists.txt
+++ b/libc/CMakeLists.txt
@@ -34,7 +34,7 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug")
   add_definitions("-D_DEBUG")
 endif()
 
-add_compile_options("-Werror")
+add_compile_options("-Werror=global-constructors")
 
 # Default to C++17
 set(CMAKE_CXX_STANDARD 17)

>From 9bb0c5b4e417700ad5f87cf0173bc1203f054d7a Mon Sep 17 00:00:00 2001
From: Vinay Deshmukh <32487576+vinay-deshmukh at users.noreply.github.com>
Date: Sun, 23 Mar 2025 15:16:20 -0400
Subject: [PATCH 6/6] constexpr ctor

---
 libc/test/UnitTest/LibcTest.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/libc/test/UnitTest/LibcTest.h b/libc/test/UnitTest/LibcTest.h
index ed75e230218ca..3da119cc070b4 100644
--- a/libc/test/UnitTest/LibcTest.h
+++ b/libc/test/UnitTest/LibcTest.h
@@ -343,7 +343,7 @@ CString libc_make_test_file_path_func(const char *file_name);
   public:                                                                      \
     using ParamType = T;                                                       \
     char name[256];                                                            \
-    SuiteName##_##TestName() {                                                 \
+    constexpr SuiteName##_##TestName() {                                       \
       addTest(this);                                                           \
       LIBC_NAMESPACE::testing::internal::GenerateName<T>(                      \
           name, sizeof(name), #SuiteName "." #TestName);                       \
@@ -363,7 +363,7 @@ CString libc_make_test_file_path_func(const char *file_name);
   public:                                                                      \
     using ParamType = T;                                                       \
     char name[256];                                                            \
-    SuiteClass##_##TestName() {                                                \
+    constexpr SuiteClass##_##TestName() {                                      \
       SuiteClass<T>::addTest(this);                                            \
       LIBC_NAMESPACE::testing::internal::GenerateName<T>(                      \
           name, sizeof(name), #SuiteClass "." #TestName);                      \
@@ -380,7 +380,7 @@ CString libc_make_test_file_path_func(const char *file_name);
                 "All LLVM-libc TEST suite names must start with 'LlvmLibc'."); \
   class SuiteName##_##TestName : public LIBC_NAMESPACE::testing::Test {        \
   public:                                                                      \
-    SuiteName##_##TestName() { addTest(this); }                                \
+    constexpr SuiteName##_##TestName() { addTest(this); }                      \
     void Run() override;                                                       \
     const char *getName() const override { return #SuiteName "." #TestName; }  \
     static SuiteName##_##TestName SuiteName##_##TestName##_Instance;           \
@@ -393,7 +393,7 @@ CString libc_make_test_file_path_func(const char *file_name);
       "All LLVM-libc TEST_F suite class names must start with 'LlvmLibc'.");   \
   class SuiteClass##_##TestName : public SuiteClass {                          \
   public:                                                                      \
-    SuiteClass##_##TestName() { addTest(this); }                               \
+    constexpr SuiteClass##_##TestName() { addTest(this); }                     \
     void Run() override;                                                       \
     const char *getName() const override { return #SuiteClass "." #TestName; } \
     static SuiteClass##_##TestName SuiteClass##_##TestName##_Instance;         \



More information about the libc-commits mailing list