[libcxx-commits] [libcxx] [libc++] Add warning groups to diagnose_if when available (PR #128759)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Feb 25 10:53:21 PST 2025


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/128759

None

>From 1c5e21b7ea16932ab36dfe2a2c97640cc0e95a0a Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Tue, 25 Feb 2025 19:52:57 +0100
Subject: [PATCH] [libc++] Add warning groups to diagnose_if when available

---
 libcxx/include/__atomic/check_memory_order.h | 28 ++++++++++++++------
 libcxx/include/__config                      | 12 ++++++---
 libcxx/include/__filesystem/path.h           |  2 +-
 libcxx/include/__hash_table                  | 12 ++++++---
 libcxx/include/__tree                        |  6 +++--
 5 files changed, 41 insertions(+), 19 deletions(-)

diff --git a/libcxx/include/__atomic/check_memory_order.h b/libcxx/include/__atomic/check_memory_order.h
index 536f764a61902..ed836a2606cbc 100644
--- a/libcxx/include/__atomic/check_memory_order.h
+++ b/libcxx/include/__atomic/check_memory_order.h
@@ -16,19 +16,31 @@
 #endif
 
 #define _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m)                                                                          \
-  _LIBCPP_DIAGNOSE_WARNING(__m == memory_order_consume || __m == memory_order_acquire || __m == memory_order_acq_rel,  \
-                           "memory order argument to atomic operation is invalid")
+  _LIBCPP_DIAGNOSE_IF(                                                                                                 \
+      "atomic-memory-ordering",                                                                                        \
+      "memory order argument to atomic operation is invalid",                                                          \
+      "warning",                                                                                                       \
+      __m == memory_order_consume || __m == memory_order_acquire || __m == memory_order_acq_rel)
 
 #define _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m)                                                                           \
-  _LIBCPP_DIAGNOSE_WARNING(__m == memory_order_release || __m == memory_order_acq_rel,                                 \
-                           "memory order argument to atomic operation is invalid")
+  _LIBCPP_DIAGNOSE_IF(                                                                                                 \
+      "atomic-memory-ordering",                                                                                        \
+      "memory order argument to atomic operation is invalid",                                                          \
+      "warning",                                                                                                       \
+      __m == memory_order_release || __m == memory_order_acq_rel)
 
 #define _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__m, __f)                                                                  \
-  _LIBCPP_DIAGNOSE_WARNING(__f == memory_order_release || __f == memory_order_acq_rel,                                 \
-                           "memory order argument to atomic operation is invalid")
+  _LIBCPP_DIAGNOSE_IF(                                                                                                 \
+      "atomic-memory-ordering",                                                                                        \
+      "memory order argument to atomic operation is invalid",                                                          \
+      "warning",                                                                                                       \
+      __f == memory_order_release || __f == memory_order_acq_rel)
 
 #define _LIBCPP_CHECK_WAIT_MEMORY_ORDER(__m)                                                                           \
-  _LIBCPP_DIAGNOSE_WARNING(__m == memory_order_release || __m == memory_order_acq_rel,                                 \
-                           "memory order argument to atomic operation is invalid")
+  _LIBCPP_DIAGNOSE_IF(                                                                                                 \
+      "atomic-memory-ordering",                                                                                        \
+      "memory order argument to atomic operation is invalid",                                                          \
+      "warning",                                                                                                       \
+      __m == memory_order_release || __m == memory_order_acq_rel)
 
 #endif // _LIBCPP___ATOMIC_CHECK_MEMORY_ORDER_H
diff --git a/libcxx/include/__config b/libcxx/include/__config
index 53900e40655ef..4025c6500f8e4 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -1151,10 +1151,14 @@ typedef __char32_t char32_t;
 #    define _LIBCPP_NO_DESTROY
 #  endif
 
-#  if __has_attribute(__diagnose_if__)
-#    define _LIBCPP_DIAGNOSE_WARNING(...) __attribute__((__diagnose_if__(__VA_ARGS__, "warning")))
-#  else
-#    define _LIBCPP_DIAGNOSE_WARNING(...)
+#  if defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 2010
+#    define _LIBCPP_DIAGNOSE_IF(warning_group, default_severity, message, ...)                                         \
+      __attribute__((__diagnose_if__(__VA_ARGS__, message, default_severity, warning_group)))
+#  elif __has_attribute(__diagnose_if__)
+#    define _LIBCPP_DIAGNOSE_IF(warning_group, default_severity, message, ...)                                         \
+      __attribute__((__diagnose_if__(__VA_ARGS__, message, "warning")))
+#  else
+#    define _LIBCPP_DIAGNOSE_IF(warning_group, default_severity, message, ...)
 #  endif
 
 #  if __has_cpp_attribute(_Clang::__lifetimebound__)
diff --git a/libcxx/include/__filesystem/path.h b/libcxx/include/__filesystem/path.h
index 698ae209ae1f8..433686d235994 100644
--- a/libcxx/include/__filesystem/path.h
+++ b/libcxx/include/__filesystem/path.h
@@ -521,7 +521,7 @@ class _LIBCPP_EXPORTED_FROM_ABI path {
     return *this;
   }
 
-  // FIXME: Use _LIBCPP_DIAGNOSE_WARNING to produce a diagnostic when __src
+  // FIXME: Use _LIBCPP_DIAGNOSE_IF to produce a diagnostic when __src
   // is known at compile time to be "/' since the user almost certainly intended
   // to append a separator instead of overwriting the path with "/"
   template <class _Source>
diff --git a/libcxx/include/__hash_table b/libcxx/include/__hash_table
index d7b312f8774fc..488b2ace18420 100644
--- a/libcxx/include/__hash_table
+++ b/libcxx/include/__hash_table
@@ -650,10 +650,14 @@ struct __enforce_unordered_container_requirements {
 
 template <class _Key, class _Hash, class _Equal>
 #ifndef _LIBCPP_CXX03_LANG
-_LIBCPP_DIAGNOSE_WARNING(!__is_invocable_v<_Equal const&, _Key const&, _Key const&>,
-                         "the specified comparator type does not provide a viable const call operator")
-_LIBCPP_DIAGNOSE_WARNING(!__is_invocable_v<_Hash const&, _Key const&>,
-                         "the specified hash functor does not provide a viable const call operator")
+_LIBCPP_DIAGNOSE_IF("pedantic",
+                    "warning",
+                    "the specified comparator type does not provide a viable const call operator",
+                    !__is_invocable_v<_Equal const&, _Key const&, _Key const&>)
+_LIBCPP_DIAGNOSE_IF("pedantic",
+                    "warning",
+                    "the specified hash functor does not provide a viable const call operator",
+                    !__is_invocable_v<_Hash const&, _Key const&>)
 #endif
     typename __enforce_unordered_container_requirements<_Key, _Hash, _Equal>::type
     __diagnose_unordered_container_requirements(int);
diff --git a/libcxx/include/__tree b/libcxx/include/__tree
index c627641d5d86f..ef766655ded61 100644
--- a/libcxx/include/__tree
+++ b/libcxx/include/__tree
@@ -876,8 +876,10 @@ private:
 
 template <class _Tp, class _Compare>
 #ifndef _LIBCPP_CXX03_LANG
-_LIBCPP_DIAGNOSE_WARNING(!__is_invocable_v<_Compare const&, _Tp const&, _Tp const&>,
-                         "the specified comparator type does not provide a viable const call operator")
+_LIBCPP_DIAGNOSE_IF("pedantic",
+                    "warning",
+                    "the specified comparator type does not provide a viable const call operator",
+                    !__is_invocable_v<_Compare const&, _Tp const&, _Tp const&>)
 #endif
 int __diagnose_non_const_comparator();
 



More information about the libcxx-commits mailing list