Skip to content

Commit 8ef8460

Browse files
committed
Add -Watomic-memory-ordering diagnostic tests for atomic_ref
1 parent d14e446 commit 8ef8460

File tree

5 files changed

+179
-0
lines changed

5 files changed

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

0 commit comments

Comments
 (0)