Skip to content

[libc++] Add warning groups to diagnose_if when available #128759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

philnik777
Copy link
Contributor

No description provided.

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions ,h -- libcxx/include/__atomic/check_memory_order.h libcxx/include/__config libcxx/include/__filesystem/path.h libcxx/include/__hash_table libcxx/include/__tree
View the diff from clang-format here.
diff --git a/libcxx/include/__atomic/check_memory_order.h b/libcxx/include/__atomic/check_memory_order.h
index f7b589c1f..0263424f7 100644
--- a/libcxx/include/__atomic/check_memory_order.h
+++ b/libcxx/include/__atomic/check_memory_order.h
@@ -16,28 +16,28 @@
 #endif
 
 #define _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m)                                                                          \
-  _LIBCPP_LEGACY_DIAGNOSE_IF(                                                                                                 \
+  _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_LEGACY_DIAGNOSE_IF(                                                                                                 \
+  _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_LEGACY_DIAGNOSE_IF(                                                                                                 \
+  _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_LEGACY_DIAGNOSE_IF(                                                                                                 \
+  _LIBCPP_LEGACY_DIAGNOSE_IF(                                                                                          \
       "atomic-memory-ordering",                                                                                        \
       "warning",                                                                                                       \
       "memory order argument to atomic operation is invalid",                                                          \

@philnik777 philnik777 marked this pull request as ready for review July 23, 2025 12:55
@philnik777 philnik777 requested a review from a team as a code owner July 23, 2025 12:55
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jul 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 23, 2025

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/128759.diff

5 Files Affected:

  • (modified) libcxx/include/__atomic/check_memory_order.h (+20-8)
  • (modified) libcxx/include/__config (+9-6)
  • (modified) libcxx/include/__filesystem/path.h (+1-1)
  • (modified) libcxx/include/__hash_table (+8-4)
  • (modified) libcxx/include/__tree (+4-2)
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();
 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants