[libcxx-commits] [libcxx] 3e957e5 - [libc++] Refactor tests for trivially copyable atomics
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Nov 17 10:32:15 PST 2021
Author: Louis Dionne
Date: 2021-11-17T13:32:12-05:00
New Revision: 3e957e5d66aaab08c33ccea2768f74993f6f554f
URL: https://github.com/llvm/llvm-project/commit/3e957e5d66aaab08c33ccea2768f74993f6f554f
DIFF: https://github.com/llvm/llvm-project/commit/3e957e5d66aaab08c33ccea2768f74993f6f554f.diff
LOG: [libc++] Refactor tests for trivially copyable atomics
- Replace irrelevant synopsis by a comment
- Use a .verify.cpp test instead of .compile.fail.cpp
- Remove unnecessary includes in one of the tests (was a copy-paste error)
Differential Revision: https://reviews.llvm.org/D114094
Added:
libcxx/test/std/atomics/atomics.types.generic/trivially_copyable.verify.cpp
Modified:
libcxx/include/atomic
libcxx/test/std/atomics/atomics.types.generic/trivially_copyable.pass.cpp
Removed:
libcxx/test/std/atomics/atomics.types.generic/trivially_copyable.compile.fail.cpp
################################################################################
diff --git a/libcxx/include/atomic b/libcxx/include/atomic
index 2e2ee63b676d0..5fc53a554974f 100644
--- a/libcxx/include/atomic
+++ b/libcxx/include/atomic
@@ -1440,7 +1440,7 @@ template <typename _Tp,
#endif //_LIBCPP_ATOMIC_ONLY_USE_BUILTINS
struct __cxx_atomic_impl : public _Base {
static_assert(is_trivially_copyable<_Tp>::value,
- "std::atomic<Tp> requires that 'Tp' be a trivially copyable type");
+ "std::atomic<T> requires that 'T' be a trivially copyable type");
_LIBCPP_INLINE_VISIBILITY __cxx_atomic_impl() _NOEXCEPT _LIBCPP_DEFAULT
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR explicit __cxx_atomic_impl(_Tp value) _NOEXCEPT
diff --git a/libcxx/test/std/atomics/atomics.types.generic/trivially_copyable.compile.fail.cpp b/libcxx/test/std/atomics/atomics.types.generic/trivially_copyable.compile.fail.cpp
deleted file mode 100644
index 3ec8ed25c9765..0000000000000
--- a/libcxx/test/std/atomics/atomics.types.generic/trivially_copyable.compile.fail.cpp
+++ /dev/null
@@ -1,71 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <atomic>
-
-// template <class T>
-// struct atomic
-// {
-// bool is_lock_free() const volatile noexcept;
-// bool is_lock_free() const noexcept;
-// void store(T desr, memory_order m = memory_order_seq_cst) volatile noexcept;
-// void store(T desr, memory_order m = memory_order_seq_cst) noexcept;
-// T load(memory_order m = memory_order_seq_cst) const volatile noexcept;
-// T load(memory_order m = memory_order_seq_cst) const noexcept;
-// operator T() const volatile noexcept;
-// operator T() const noexcept;
-// T exchange(T desr, memory_order m = memory_order_seq_cst) volatile noexcept;
-// T exchange(T desr, memory_order m = memory_order_seq_cst) noexcept;
-// bool compare_exchange_weak(T& expc, T desr,
-// memory_order s, memory_order f) volatile noexcept;
-// bool compare_exchange_weak(T& expc, T desr, memory_order s, memory_order f) noexcept;
-// bool compare_exchange_strong(T& expc, T desr,
-// memory_order s, memory_order f) volatile noexcept;
-// bool compare_exchange_strong(T& expc, T desr,
-// memory_order s, memory_order f) noexcept;
-// bool compare_exchange_weak(T& expc, T desr,
-// memory_order m = memory_order_seq_cst) volatile noexcept;
-// bool compare_exchange_weak(T& expc, T desr,
-// memory_order m = memory_order_seq_cst) noexcept;
-// bool compare_exchange_strong(T& expc, T desr,
-// memory_order m = memory_order_seq_cst) volatile noexcept;
-// bool compare_exchange_strong(T& expc, T desr,
-// memory_order m = memory_order_seq_cst) noexcept;
-//
-// atomic() noexcept = default;
-// constexpr atomic(T desr) noexcept;
-// atomic(const atomic&) = delete;
-// atomic& operator=(const atomic&) = delete;
-// atomic& operator=(const atomic&) volatile = delete;
-// T operator=(T) volatile noexcept;
-// T operator=(T) noexcept;
-// };
-
-#include <atomic>
-#include <new>
-#include <cassert>
-#include <thread> // for thread_id
-#include <chrono> // for nanoseconds
-
-struct NotTriviallyCopyable {
- NotTriviallyCopyable ( int i ) : i_(i) {}
- NotTriviallyCopyable ( const NotTriviallyCopyable &rhs) : i_(rhs.i_) {}
- int i_;
-};
-
-template <class T, class >
-void test ( T t ) {
- std::atomic<T> t0(t);
-}
-
-int main(int, char**)
-{
- test(NotTriviallyCopyable(42));
-
- return 0;
-}
diff --git a/libcxx/test/std/atomics/atomics.types.generic/trivially_copyable.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/trivially_copyable.pass.cpp
index 3e394ad7d3afd..747d071490676 100644
--- a/libcxx/test/std/atomics/atomics.types.generic/trivially_copyable.pass.cpp
+++ b/libcxx/test/std/atomics/atomics.types.generic/trivially_copyable.pass.cpp
@@ -11,42 +11,9 @@
// <atomic>
// template <class T>
-// struct atomic
-// {
-// bool is_lock_free() const volatile noexcept;
-// bool is_lock_free() const noexcept;
-// void store(T desr, memory_order m = memory_order_seq_cst) volatile noexcept;
-// void store(T desr, memory_order m = memory_order_seq_cst) noexcept;
-// T load(memory_order m = memory_order_seq_cst) const volatile noexcept;
-// T load(memory_order m = memory_order_seq_cst) const noexcept;
-// operator T() const volatile noexcept;
-// operator T() const noexcept;
-// T exchange(T desr, memory_order m = memory_order_seq_cst) volatile noexcept;
-// T exchange(T desr, memory_order m = memory_order_seq_cst) noexcept;
-// bool compare_exchange_weak(T& expc, T desr,
-// memory_order s, memory_order f) volatile noexcept;
-// bool compare_exchange_weak(T& expc, T desr, memory_order s, memory_order f) noexcept;
-// bool compare_exchange_strong(T& expc, T desr,
-// memory_order s, memory_order f) volatile noexcept;
-// bool compare_exchange_strong(T& expc, T desr,
-// memory_order s, memory_order f) noexcept;
-// bool compare_exchange_weak(T& expc, T desr,
-// memory_order m = memory_order_seq_cst) volatile noexcept;
-// bool compare_exchange_weak(T& expc, T desr,
-// memory_order m = memory_order_seq_cst) noexcept;
-// bool compare_exchange_strong(T& expc, T desr,
-// memory_order m = memory_order_seq_cst) volatile noexcept;
-// bool compare_exchange_strong(T& expc, T desr,
-// memory_order m = memory_order_seq_cst) noexcept;
-//
-// atomic() noexcept = default;
-// constexpr atomic(T desr) noexcept;
-// atomic(const atomic&) = delete;
-// atomic& operator=(const atomic&) = delete;
-// atomic& operator=(const atomic&) volatile = delete;
-// T operator=(T) volatile noexcept;
-// T operator=(T) noexcept;
-// };
+// struct atomic;
+
+// Make sure atomic<TriviallyCopyable> can be instantiated.
#include <atomic>
#include <new>
@@ -54,23 +21,20 @@
#include <thread> // for thread_id
#include <chrono> // for nanoseconds
-#include "test_macros.h"
-
struct TriviallyCopyable {
- TriviallyCopyable ( int i ) : i_(i) {}
- int i_;
- };
+ explicit TriviallyCopyable(int i) : i_(i) { }
+ int i_;
+};
template <class T>
-void test ( T t ) {
- std::atomic<T> t0(t);
- }
+void test(T t) {
+ std::atomic<T> t0(t);
+}
-int main(int, char**)
-{
- test(TriviallyCopyable(42));
- test(std::this_thread::get_id());
- test(std::chrono::nanoseconds(2));
+int main(int, char**) {
+ test(TriviallyCopyable(42));
+ test(std::this_thread::get_id());
+ test(std::chrono::nanoseconds(2));
return 0;
}
diff --git a/libcxx/test/std/atomics/atomics.types.generic/trivially_copyable.verify.cpp b/libcxx/test/std/atomics/atomics.types.generic/trivially_copyable.verify.cpp
new file mode 100644
index 0000000000000..280e6c0f99091
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.generic/trivially_copyable.verify.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: libcpp-has-no-threads
+
+// <atomic>
+
+// template <class T>
+// struct atomic;
+
+// This test checks that we static_assert inside std::atomic<T> when T
+// is not trivially copyable, however Clang will sometimes emit additional
+// errors while trying to instantiate the rest of std::atomic<T>.
+// We silence those to make the test more robust.
+// ADDITIONAL_COMPILE_FLAGS: -Xclang -verify-ignore-unexpected=error
+
+#include <atomic>
+
+struct NotTriviallyCopyable {
+ explicit NotTriviallyCopyable(int i) : i_(i) { }
+ NotTriviallyCopyable(const NotTriviallyCopyable &rhs) : i_(rhs.i_) { }
+ int i_;
+};
+
+void f() {
+ NotTriviallyCopyable x(42);
+ std::atomic<NotTriviallyCopyable> a(x); // expected-error at atomic:* {{std::atomic<T> requires that 'T' be a trivially copyable type}}
+}
More information about the libcxx-commits
mailing list