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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions libcxx/include/__atomic/check_memory_order.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 9 additions & 6 deletions libcxx/include/__config
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__filesystem/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
12 changes: 8 additions & 4 deletions libcxx/include/__hash_table
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions libcxx/include/__tree
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Loading