Skip to content

Commit e235432

Browse files
authored
[libc++] Add -Watomic-memory-ordering diagnostic tests for atomic_ref (#130206)
Fix #130053
1 parent f84f4e1 commit e235432

11 files changed

+190
-0
lines changed

libcxx/test/libcxx/atomics/atomics.ref/assert.compare_exchange_strong.pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//===----------------------------------------------------------------------===//
12
//
23
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
34
// See https://llvm.org/LICENSE.txt for license information.

libcxx/test/libcxx/atomics/atomics.ref/assert.compare_exchange_weak.pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//===----------------------------------------------------------------------===//
12
//
23
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
34
// See https://llvm.org/LICENSE.txt for license information.

libcxx/test/libcxx/atomics/atomics.ref/assert.ctor.pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//===----------------------------------------------------------------------===//
12
//
23
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
34
// See https://llvm.org/LICENSE.txt for license information.

libcxx/test/libcxx/atomics/atomics.ref/assert.load.pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//===----------------------------------------------------------------------===//
12
//
23
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
34
// See https://llvm.org/LICENSE.txt for license information.

libcxx/test/libcxx/atomics/atomics.ref/assert.store.pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//===----------------------------------------------------------------------===//
12
//
23
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
34
// See https://llvm.org/LICENSE.txt for license information.

libcxx/test/libcxx/atomics/atomics.ref/assert.wait.pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//===----------------------------------------------------------------------===//
12
//
23
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
34
// See https://llvm.org/LICENSE.txt for license information.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: diagnose-if-support
10+
// UNSUPPORTED: c++03, c++11, c++14, c++17
11+
12+
// <atomic>
13+
14+
// bool compare_exchange_strong(T& expected, T desired, memory_order success,
15+
// memory_order failure) const noexcept;
16+
//
17+
// Preconditions: failure is memory_order::relaxed, memory_order::consume,
18+
// memory_order::acquire, or memory_order::seq_cst.
19+
20+
#include <atomic>
21+
22+
void test() {
23+
using T = int;
24+
25+
T x(T(1));
26+
std::atomic_ref const a(x);
27+
28+
T expected(T(2));
29+
T const desired(T(3));
30+
std::memory_order const success = std::memory_order_relaxed;
31+
// clang-format off
32+
a.compare_exchange_strong(expected, desired, success, std::memory_order_relaxed);
33+
a.compare_exchange_strong(expected, desired, success, std::memory_order_consume);
34+
a.compare_exchange_strong(expected, desired, success, std::memory_order_acquire);
35+
a.compare_exchange_strong(expected, desired, success, std::memory_order_seq_cst);
36+
a.compare_exchange_strong(expected, desired, success, std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
37+
a.compare_exchange_strong(expected, desired, success, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
38+
// clang-format on
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: diagnose-if-support
10+
// UNSUPPORTED: c++03, c++11, c++14, c++17
11+
12+
// <atomic>
13+
14+
// bool compare_exchange_weak(T& expected, T desired, memory_order success,
15+
// memory_order failure) const noexcept;
16+
//
17+
// Preconditions: failure is memory_order::relaxed, memory_order::consume,
18+
// memory_order::acquire, or memory_order::seq_cst.
19+
20+
#include <atomic>
21+
22+
void test() {
23+
using T = int;
24+
25+
T x(T(42));
26+
std::atomic_ref const a(x);
27+
28+
T expected(T(2));
29+
T const desired(T(3));
30+
std::memory_order const success = std::memory_order_relaxed;
31+
// clang-format off
32+
a.compare_exchange_weak(expected, desired, success, std::memory_order_relaxed);
33+
a.compare_exchange_weak(expected, desired, success, std::memory_order_consume);
34+
a.compare_exchange_weak(expected, desired, success, std::memory_order_acquire);
35+
a.compare_exchange_weak(expected, desired, success, std::memory_order_seq_cst);
36+
a.compare_exchange_weak(expected, desired, success, std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
37+
a.compare_exchange_weak(expected, desired, success, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
38+
// clang-format on
39+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: diagnose-if-support
10+
// UNSUPPORTED: c++03, c++11, c++14, c++17
11+
12+
// <atomic>
13+
14+
// T load(memory_order order = memory_order::seq_cst) const noexcept;
15+
//
16+
// Preconditions: order is memory_order::relaxed, memory_order::consume, memory_order::acquire, or memory_order::seq_cst.
17+
18+
#include <atomic>
19+
20+
void test() {
21+
using T = int;
22+
23+
T x(T(1));
24+
std::atomic_ref const a(x);
25+
26+
// clang-format off
27+
(void)a.load(std::memory_order_relaxed);
28+
(void)a.load(std::memory_order_consume);
29+
(void)a.load(std::memory_order_acquire);
30+
(void)a.load(std::memory_order_seq_cst);
31+
(void)a.load(std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
32+
(void)a.load(std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
33+
// clang-format on
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: diagnose-if-support
10+
// UNSUPPORTED: c++03, c++11, c++14, c++17
11+
12+
// <atomic>
13+
14+
// void store(T desired, memory_order order = memory_order::seq_cst) const noexcept;
15+
//
16+
// Preconditions: order is memory_order::relaxed, memory_order::release, or memory_order::seq_cst.
17+
18+
#include <atomic>
19+
20+
void test() {
21+
using T = int;
22+
23+
T x(T(1));
24+
std::atomic_ref const a(x);
25+
26+
T const desired(T(2));
27+
28+
// clang-format off
29+
a.store(desired, std::memory_order_relaxed);
30+
a.store(desired, std::memory_order_release);
31+
a.store(desired, std::memory_order_seq_cst);
32+
a.store(desired, std::memory_order_consume); // expected-warning {{memory order argument to atomic operation is invalid}}
33+
a.store(desired, std::memory_order_acquire); // expected-warning {{memory order argument to atomic operation is invalid}}
34+
a.store(desired, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
35+
// clang-format on
36+
}

0 commit comments

Comments
 (0)