[libcxx-commits] [libcxx] Fix bug in atomic_ref's calculation of lock_free-ness. (PR #93427)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun May 26 14:28:18 PDT 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 972f297f712d822208ceae7546c516cd3696e4b1 5394683f072a20c679cee83413d15fd1e94e0608 -- libcxx/include/__atomic/atomic_ref.h libcxx/test/std/atomics/atomics.ref/is_always_lock_free.pass.cpp libcxx/test/support/atomic_helpers.h
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/libcxx/test/std/atomics/atomics.ref/is_always_lock_free.pass.cpp b/libcxx/test/std/atomics/atomics.ref/is_always_lock_free.pass.cpp
index a56455e59a..0773f9eeac 100644
--- a/libcxx/test/std/atomics/atomics.ref/is_always_lock_free.pass.cpp
+++ b/libcxx/test/std/atomics/atomics.ref/is_always_lock_free.pass.cpp
@@ -20,22 +20,20 @@
 #include "test_macros.h"
 #include "atomic_helpers.h"
 
-
 template <typename T>
 void check_always_lock_free_subsumes_is_lock_free(std::atomic_ref<T> const a) {
   if (is_lock_free_status_known<T>()) {
-      constexpr LockFreeStatus known_status = get_known_atomic_lock_free_status<T>();
-
-      static_assert(std::atomic_ref<T>::is_always_lock_free == (known_status == LockFreeStatus::always),
-              "is_always_lock_free is inconsistent with known lock-free status");
-      if (known_status == LockFreeStatus::always) {
-        assert(a.is_lock_free() && "is_lock_free() is inconsistent with known lock-free status");
-      } else if (known_status == LockFreeStatus::never) {
-        assert(!a.is_lock_free() && "is_lock_free() is inconsistent with known lock-free status");
-      } else {
-        assert(a.is_lock_free() || !a.is_lock_free()); // This is kinda dumb, but we might as well call the function once.
-      }
-
+    constexpr LockFreeStatus known_status = get_known_atomic_lock_free_status<T>();
+
+    static_assert(std::atomic_ref<T>::is_always_lock_free == (known_status == LockFreeStatus::always),
+                  "is_always_lock_free is inconsistent with known lock-free status");
+    if (known_status == LockFreeStatus::always) {
+      assert(a.is_lock_free() && "is_lock_free() is inconsistent with known lock-free status");
+    } else if (known_status == LockFreeStatus::never) {
+      assert(!a.is_lock_free() && "is_lock_free() is inconsistent with known lock-free status");
+    } else {
+      assert(a.is_lock_free() || !a.is_lock_free()); // This is kinda dumb, but we might as well call the function once.
+    }
   }
   std::same_as<const bool> decltype(auto) is_always_lock_free = std::atomic_ref<T>::is_always_lock_free;
   if (is_always_lock_free) {
@@ -49,11 +47,10 @@ void check_always_lock_free_subsumes_is_lock_free(std::atomic_ref<T> const a) {
   do {                                                                                                                 \
     typedef T type;                                                                                                    \
     type obj{};                                                                                                        \
-    check_always_lock_free_subsumes_is_lock_free(std::atomic_ref<type>(obj));                                                                \
+    check_always_lock_free_subsumes_is_lock_free(std::atomic_ref<type>(obj));                                          \
   } while (0)
 
 void check_always_lock_free_types() {
-
   static_assert(std::atomic_ref<int>::is_always_lock_free);
   static_assert(std::atomic_ref<char>::is_always_lock_free);
 }
diff --git a/libcxx/test/support/atomic_helpers.h b/libcxx/test/support/atomic_helpers.h
index db1aa4d353..36ccccfe2c 100644
--- a/libcxx/test/support/atomic_helpers.h
+++ b/libcxx/test/support/atomic_helpers.h
@@ -50,23 +50,22 @@ constexpr bool msvc_is_lock_free_macro_value() {
 #  error "Unknown compiler"
 #endif
 enum class LockFreeStatus { unknown = -1, never = 0, sometimes = 1, always = 2 };
-#define COMPARE_TYPES(T1, T2) \
-  (sizeof(T1) == sizeof(T2) && alignof(T1) >= alignof(T2))
+#define COMPARE_TYPES(T1, T2) (sizeof(T1) == sizeof(T2) && alignof(T1) >= alignof(T2))
 
 template <class T>
 constexpr inline LockFreeStatus get_known_atomic_lock_free_status() {
-  return LockFreeStatus{COMPARE_TYPES(T, char)
-           ? TEST_ATOMIC_CHAR_LOCK_FREE
-           : (COMPARE_TYPES(T, short)
-                  ? TEST_ATOMIC_SHORT_LOCK_FREE
-                  : (COMPARE_TYPES(T, int)
-                         ? TEST_ATOMIC_INT_LOCK_FREE
-                         : (COMPARE_TYPES(T, long)
-                                ? TEST_ATOMIC_LONG_LOCK_FREE
-                                : (COMPARE_TYPES(T, long long)
-                                       ? TEST_ATOMIC_LLONG_LOCK_FREE
-                                       : (COMPARE_TYPES(T, void*) ? TEST_ATOMIC_POINTER_LOCK_FREE
-                                                                                   : -1)))))};
+  return LockFreeStatus{
+      COMPARE_TYPES(T, char)
+          ? TEST_ATOMIC_CHAR_LOCK_FREE
+          : (COMPARE_TYPES(T, short)
+                 ? TEST_ATOMIC_SHORT_LOCK_FREE
+                 : (COMPARE_TYPES(T, int)
+                        ? TEST_ATOMIC_INT_LOCK_FREE
+                        : (COMPARE_TYPES(T, long)
+                               ? TEST_ATOMIC_LONG_LOCK_FREE
+                               : (COMPARE_TYPES(T, long long)
+                                      ? TEST_ATOMIC_LLONG_LOCK_FREE
+                                      : (COMPARE_TYPES(T, void*) ? TEST_ATOMIC_POINTER_LOCK_FREE : -1)))))};
 }
 
 template <class T>
@@ -81,7 +80,6 @@ static_assert(is_lock_free_status_known<long>(), "");
 static_assert(is_lock_free_status_known<long long>(), "");
 static_assert(is_lock_free_status_known<void*>(), "");
 
-
 // These macros are somewhat suprising to use, since they take the values 0, 1, or 2.
 // To make the tests clearer, get rid of them in preference of AtomicInfo.
 #undef TEST_ATOMIC_CHAR_LOCK_FREE

``````````

</details>


https://github.com/llvm/llvm-project/pull/93427


More information about the libcxx-commits mailing list