[libc-commits] [libc] [libc][pthread] fix -Wmissing-field-initializers (PR #126314)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Tue Feb 11 14:17:58 PST 2025


https://github.com/nickdesaulniers updated https://github.com/llvm/llvm-project/pull/126314

>From 6ef84f257c414f7f564add2579c8f98fb342bf82 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Fri, 7 Feb 2025 13:38:52 -0800
Subject: [PATCH 1/6] [libc][pthread] fix -Wmissing-field-initializers

Fixes:

    llvm-project/libc/test/integration/src/pthread/pthread_rwlock_test.cpp:59:29:
    warning: missing field '__preference' initializer
    [-Wmissing-field-initializers]
       59 |   pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
          |                             ^

Also, add a test that demonstrates the same issue for
PTHREAD_MUTEX_INITIALIZER, and fix that, too.

PTHREAD_ONCE_INIT does not have this issue and does have test coverage.
---
 libc/include/llvm-libc-macros/pthread-macros.h           | 4 ++--
 libc/test/integration/src/pthread/pthread_mutex_test.cpp | 4 ++++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/libc/include/llvm-libc-macros/pthread-macros.h b/libc/include/llvm-libc-macros/pthread-macros.h
index 8a144dbd2e611..2b1440f9308ca 100644
--- a/libc/include/llvm-libc-macros/pthread-macros.h
+++ b/libc/include/llvm-libc-macros/pthread-macros.h
@@ -25,8 +25,8 @@
 #define PTHREAD_PROCESS_PRIVATE 0
 #define PTHREAD_PROCESS_SHARED 1
 
-#define PTHREAD_MUTEX_INITIALIZER {0}
-#define PTHREAD_RWLOCK_INITIALIZER {0}
+#define PTHREAD_MUTEX_INITIALIZER {}
+#define PTHREAD_RWLOCK_INITIALIZER {}
 
 // glibc extensions
 #define PTHREAD_STACK_MIN (1 << 14) // 16KB
diff --git a/libc/test/integration/src/pthread/pthread_mutex_test.cpp b/libc/test/integration/src/pthread/pthread_mutex_test.cpp
index ce2a3538924da..ec8488da4ead9 100644
--- a/libc/test/integration/src/pthread/pthread_mutex_test.cpp
+++ b/libc/test/integration/src/pthread/pthread_mutex_test.cpp
@@ -186,6 +186,10 @@ void multiple_waiters() {
   LIBC_NAMESPACE::pthread_mutex_destroy(&counter_lock);
 }
 
+// Test the initializer
+[[gnu::unused]]
+static pthread_mutex_t test_initializer = PTHREAD_MUTEX_INITIALIZER;
+
 TEST_MAIN() {
   relay_counter();
   wait_and_step();

>From c380f97ad3c866e4dda5bfd22b5f7d0fc4324c3f Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 10 Feb 2025 09:52:03 -0800
Subject: [PATCH 2/6] switch to aggregate initialization

---
 .../include/llvm-libc-macros/pthread-macros.h | 53 ++++++++++++++++++-
 1 file changed, 51 insertions(+), 2 deletions(-)

diff --git a/libc/include/llvm-libc-macros/pthread-macros.h b/libc/include/llvm-libc-macros/pthread-macros.h
index 2b1440f9308ca..46e0025894288 100644
--- a/libc/include/llvm-libc-macros/pthread-macros.h
+++ b/libc/include/llvm-libc-macros/pthread-macros.h
@@ -9,6 +9,8 @@
 #ifndef LLVM_LIBC_MACROS_PTHREAD_MACRO_H
 #define LLVM_LIBC_MACROS_PTHREAD_MACRO_H
 
+#include "include/llvm-libc-macros/null-macro.h"
+
 #define PTHREAD_CREATE_JOINABLE 0
 #define PTHREAD_CREATE_DETACHED 1
 
@@ -25,8 +27,55 @@
 #define PTHREAD_PROCESS_PRIVATE 0
 #define PTHREAD_PROCESS_SHARED 1
 
-#define PTHREAD_MUTEX_INITIALIZER {}
-#define PTHREAD_RWLOCK_INITIALIZER {}
+#ifdef __linux__
+#define PTHREAD_MUTEX_INITIALIZER                                              \
+  {                                                                            \
+      /* .__timed = */ 0,                                                      \
+      /* .__recursive = */ 0,                                                  \
+      /* .__robust = */ 0,                                                     \
+      /* .__owner = */ NULL,                                                   \
+      /* .__lock_count = */ 0,                                                 \
+      /* .__futex_word = */                                                    \
+      {                                                                        \
+          /* .__word = */ 0,                                                   \
+      },                                                                       \
+  }
+#else
+#define PTHREAD_MUTEX_INITIALIZER                                              \
+  {                                                                            \
+      /* .__timed = */ 0,      /* .__recursive = */ 0,                         \
+      /* .__robust = */ 0,     /* .__owner = */ NULL,                          \
+      /* .__lock_count = */ 0,                                                 \
+  }
+#endif
+
+#define PTHREAD_RWLOCK_INITIALIZER                                             \
+  {                                                                            \
+      /* .__is_pshared = */ 0,                                                 \
+      /* .__preference = */ 0,                                                 \
+      /* .__state = */ 0,                                                      \
+      /* .__write_tid = */ 0,                                                  \
+      /* .__wait_queue_mutex = */                                              \
+      {                                                                        \
+          /* .__word = */ 0,                                                   \
+      },                                                                       \
+      /* .__pending_readers = */                                               \
+      {                                                                        \
+          /* .__word = */ 0,                                                   \
+      },                                                                       \
+      /* .__pending_writers = */                                               \
+      {                                                                        \
+          /* .__word = */ 0,                                                   \
+      },                                                                       \
+      /* .__reader_serialization = */                                          \
+      {                                                                        \
+          /* .__word = */ 0,                                                   \
+      },                                                                       \
+      /* .__writer_serialization = */                                          \
+      {                                                                        \
+          /* .__word = */ 0,                                                   \
+      },                                                                       \
+  }
 
 // glibc extensions
 #define PTHREAD_STACK_MIN (1 << 14) // 16KB

>From bfc84ca4ff1e58948ed985bb1792f0e416e73171 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 10 Feb 2025 09:53:01 -0800
Subject: [PATCH 3/6] s/gnu::unused/maybe_unused/

---
 libc/test/integration/src/pthread/pthread_mutex_test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/integration/src/pthread/pthread_mutex_test.cpp b/libc/test/integration/src/pthread/pthread_mutex_test.cpp
index ec8488da4ead9..137daed6bd283 100644
--- a/libc/test/integration/src/pthread/pthread_mutex_test.cpp
+++ b/libc/test/integration/src/pthread/pthread_mutex_test.cpp
@@ -187,7 +187,7 @@ void multiple_waiters() {
 }
 
 // Test the initializer
-[[gnu::unused]]
+[[maybe_unused]]
 static pthread_mutex_t test_initializer = PTHREAD_MUTEX_INITIALIZER;
 
 TEST_MAIN() {

>From 868122b70a1a93b8cbac7390181043ba3bd924cc Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 10 Feb 2025 09:58:12 -0800
Subject: [PATCH 4/6] reformat

---
 libc/include/llvm-libc-macros/pthread-macros.h | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/libc/include/llvm-libc-macros/pthread-macros.h b/libc/include/llvm-libc-macros/pthread-macros.h
index 46e0025894288..16204a1e708e0 100644
--- a/libc/include/llvm-libc-macros/pthread-macros.h
+++ b/libc/include/llvm-libc-macros/pthread-macros.h
@@ -34,8 +34,7 @@
       /* .__recursive = */ 0,                                                  \
       /* .__robust = */ 0,                                                     \
       /* .__owner = */ NULL,                                                   \
-      /* .__lock_count = */ 0,                                                 \
-      /* .__futex_word = */                                                    \
+      /* .__lock_count = */ 0, /* .__futex_word = */                           \
       {                                                                        \
           /* .__word = */ 0,                                                   \
       },                                                                       \
@@ -54,24 +53,19 @@
       /* .__is_pshared = */ 0,                                                 \
       /* .__preference = */ 0,                                                 \
       /* .__state = */ 0,                                                      \
-      /* .__write_tid = */ 0,                                                  \
-      /* .__wait_queue_mutex = */                                              \
+      /* .__write_tid = */ 0, /* .__wait_queue_mutex = */                      \
       {                                                                        \
           /* .__word = */ 0,                                                   \
-      },                                                                       \
-      /* .__pending_readers = */                                               \
+      }, /* .__pending_readers = */                                            \
       {                                                                        \
           /* .__word = */ 0,                                                   \
-      },                                                                       \
-      /* .__pending_writers = */                                               \
+      }, /* .__pending_writers = */                                            \
       {                                                                        \
           /* .__word = */ 0,                                                   \
-      },                                                                       \
-      /* .__reader_serialization = */                                          \
+      }, /* .__reader_serialization = */                                       \
       {                                                                        \
           /* .__word = */ 0,                                                   \
-      },                                                                       \
-      /* .__writer_serialization = */                                          \
+      }, /* .__writer_serialization = */                                       \
       {                                                                        \
           /* .__word = */ 0,                                                   \
       },                                                                       \

>From 0f16df9d00816fc384d7bc22e1feefddac151048 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 10 Feb 2025 11:01:06 -0800
Subject: [PATCH 5/6] fix include path

---
 libc/include/llvm-libc-macros/pthread-macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/include/llvm-libc-macros/pthread-macros.h b/libc/include/llvm-libc-macros/pthread-macros.h
index 16204a1e708e0..68c0a5e53fbe2 100644
--- a/libc/include/llvm-libc-macros/pthread-macros.h
+++ b/libc/include/llvm-libc-macros/pthread-macros.h
@@ -9,7 +9,7 @@
 #ifndef LLVM_LIBC_MACROS_PTHREAD_MACRO_H
 #define LLVM_LIBC_MACROS_PTHREAD_MACRO_H
 
-#include "include/llvm-libc-macros/null-macro.h"
+#include "null-macro.h"
 
 #define PTHREAD_CREATE_JOINABLE 0
 #define PTHREAD_CREATE_DETACHED 1

>From ff3e6d90744439e205e18def8fbdef20679c3391 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 11 Feb 2025 14:17:44 -0800
Subject: [PATCH 6/6] cmake

---
 libc/include/llvm-libc-macros/CMakeLists.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 441b550543f67..7a92132123c0e 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -320,6 +320,8 @@ add_macro_header(
   pthread_macros
   HDR
     pthread-macros.h
+  DEPENDS
+    .null_macro
 )
 
 add_macro_header(



More information about the libc-commits mailing list