blob: e22ee6c95689c38744431fed387e246439fb10d6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
// PR c++/91361 - P1152R4: Deprecating some uses of volatile.
// { dg-do compile { target c++20 } }
// { dg-options "-Wno-deprecated" }
#define ACCESS_ONCE(x) (*(volatile __typeof(x) *)&(x))
struct S {
volatile int a : 4;
int b : 2;
};
struct T {
int a : 4;
int b : 2;
};
union U {
char c;
int i;
};
struct W {
W();
W(volatile W&);
W& operator=(volatile W&) volatile;
};
volatile int
fn (volatile int i)
{
volatile int v = 10;
int *volatile p = nullptr;
// Pre/post ++/--.
v++;
++v;
v--;
--v;
p++;
++p;
p--;
--p;
return v + i + *p;
}
void
fn2 ()
{
volatile int vi = 42;
int i = 24;
// Discarded-value expression ([expr.context]).
// The lvalue-to-rvalue conversion is applied here:
vi;
// ...but not here. Otherwise we'd write to VI and then immediately read it.
vi = 42;
vi = i;
vi = i = 42;
i = vi = 42;
&(vi = i);
(vi = 42, 45);
(i = vi = 42, 10);
i = vi; // LHS not volatile.
i = (vi = i, 42);
static_cast<void>(vi = i);
static_cast<void>(i = vi = 42);
(void)(vi = i);
(void)(i = vi = 42);
// Unevaluated operand.
decltype(vi = 42) x = vi;
decltype(i = vi = 42) x3 = i;
// Compound assignments.
vi += i;
vi -= i;
vi %= i;
vi ^= i;
vi |= i;
vi /= i;
vi = vi += 42;
vi += vi = 42;
i *= vi;
decltype(vi -= 42) x2 = vi;
// Structured bindings.
int a[] = { 10, 5 };
const auto & [cxr, cyr] = a;
const volatile auto & [cvxr, cvyr] = a;
volatile auto & [vxr, vyr] = a;
}
void
fn3 ()
{
volatile int i, j, k = 0;
i = j = k;
ACCESS_ONCE(j);
S s;
s.b = 1;
volatile U u;
u.c = 42;
i = u.c = 42;
u.c += 42;
volatile T t;
t.a = 3;
j = t.a = 3;
t.a += 3;
volatile int *src = &i;
*src; // No assignment, don't warn.
}
void
fn4 ()
{
volatile W vw;
W w;
// Assignment to objects of a class is defined by the copy/move assignment
// operator.
vw = w;
w = vw;
}
template<typename T>
void raccoon ()
{
volatile T t, u;
t = 42;
u = t = 42;
t &= 42;
}
void
fn5 ()
{
raccoon<int>();
}
|