[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
Wed Jul 23 05:50:30 PDT 2025


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

>From f7c64c3319aa81ceb6486f8a0bbb54ce72899e9a 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                      | 15 ++++++-----
 libcxx/include/__filesystem/path.h           |  2 +-
 libcxx/include/__hash_table                  | 12 ++++++---
 libcxx/include/__tree                        |  6 +++--
 5 files changed, 42 insertions(+), 21 deletions(-)

diff --git a/libcxx/include/__atomic/check_memory_order.h b/libcxx/include/__atomic/check_memory_order.h
index 536f764a61902..f7b589c1fc24e 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_LEGACY_DIAGNOSE_IF(                                                                                                 \
+      "atomic-memory-ordering",                                                                                        \
+      "warning",                                                                                                       \
+      "memory order argument to atomic operation is invalid",                                                          \
+      __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_LEGACY_DIAGNOSE_IF(                                                                                                 \
+      "atomic-memory-ordering",                                                                                        \
+      "warning",                                                                                                       \
+      "memory order argument to atomic operation is invalid",                                                          \
+      __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_LEGACY_DIAGNOSE_IF(                                                                                                 \
+      "atomic-memory-ordering",                                                                                        \
+      "warning",                                                                                                       \
+      "memory order argument to atomic operation is invalid",                                                          \
+      __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_LEGACY_DIAGNOSE_IF(                                                                                                 \
+      "atomic-memory-ordering",                                                                                        \
+      "warning",                                                                                                       \
+      "memory order argument to atomic operation is invalid",                                                          \
+      __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 19398dd276a17..dba526255503e 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -1075,16 +1075,19 @@ 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(...)
-#  endif
-
+// TODO: Remove _LIBCPP_LEGACY_DIAGNOSE_IF once all compilers are based on at least Clang 20
 #  if __has_attribute(__diagnose_if__) && !defined(_LIBCPP_APPLE_CLANG_VER) &&                                         \
       (!defined(_LIBCPP_CLANG_VER) || _LIBCPP_CLANG_VER >= 2001)
 #    define _LIBCPP_DIAGNOSE_IF(...) __attribute__((__diagnose_if__(__VA_ARGS__)))
+#    define _LIBCPP_LEGACY_DIAGNOSE_IF(warning_group, default_severity, message, ...)                                  \
+      __attribute__((__diagnose_if__(__VA_ARGS__, message, default_severity, warning_group)))
 #  else
+#    if __has_attribute(__diagnose_if__)
+#      define _LIBCPP_LEGACY_DIAGNOSE_IF(warning_group, default_severity, message, ...)                                \
+        __attribute__((__diagnose_if__(__VA_ARGS__, message, "warning")))
+#    else
+#      define _LIBCPP_LEGACY_DIAGNOSE_IF(warning_group, default_severity, message, ...)
+#    endif
 #    define _LIBCPP_DIAGNOSE_IF(...)
 #  endif
 
diff --git a/libcxx/include/__filesystem/path.h b/libcxx/include/__filesystem/path.h
index 381e5678a5855..fd7d58233b2ab 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 03f50d9f3f269..bc333335ea7a7 100644
--- a/libcxx/include/__hash_table
+++ b/libcxx/include/__hash_table
@@ -619,10 +619,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_LEGACY_DIAGNOSE_IF("discard-qual",
+                           "warning",
+                           "the specified comparator type does not provide a viable const call operator",
+                           !__is_invocable_v<_Equal const&, _Key const&, _Key const&>)
+_LIBCPP_LEGACY_DIAGNOSE_IF("discard-qual",
+                           "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 f8bb4f01b1e29..b5062039f49ea 100644
--- a/libcxx/include/__tree
+++ b/libcxx/include/__tree
@@ -776,8 +776,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_LEGACY_DIAGNOSE_IF("discard-qual",
+                           "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