]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/testsuite/gcc.dg/c11-atomic-1.c
replace ISL with isl
[thirdparty/gcc.git] / gcc / testsuite / gcc.dg / c11-atomic-1.c
CommitLineData
267bac10
JM
1/* Test for _Atomic in C11. Test of valid code. See c11-atomic-2.c
2 for more exhaustive tests of assignment cases. */
3/* { dg-do compile } */
4/* { dg-options "-std=c11 -pedantic-errors" } */
5
6/* The use of _Atomic as a qualifier, and of _Atomic (type-name), give
7 the same type. */
8extern _Atomic int a;
9extern _Atomic (int) a;
10extern int *_Atomic b;
11extern _Atomic (int *) b;
12extern void f (int [_Atomic]);
13extern void f (int *_Atomic);
14
15/* _Atomic may be applied to arbitrary types, with or without other
16 qualifiers, and assignments may be made as with non-atomic
17 types. Structure and union elements may be atomic. */
18_Atomic int ai1, ai2;
19int i1;
20volatile _Atomic long double ald1;
21const _Atomic long double ald2;
22long double ld1;
23_Atomic _Complex double acd1, acd2;
24_Complex double d1;
25_Atomic volatile _Bool ab1;
26int *p;
27int *_Atomic restrict ap;
28struct s { char c[1000]; };
29_Atomic struct s as1;
30struct s s1;
31struct t { _Atomic int i; };
32_Atomic struct t at1;
33_Atomic struct t *atp1;
34struct t t1;
35union u { char c[1000]; };
36_Atomic union u au1;
37union u u1;
38union v { _Atomic int i; };
39_Atomic union v av1;
40union v v1;
41
42void
43func (_Atomic volatile long al1)
44{
45 ai1 = ai2;
46 ai1 = i1;
47 i1 = ai2;
48 ai1 = ald2;
49 ald1 = d1;
50 ld1 = acd2;
51 acd1 += ab1;
52 acd2 /= ai1;
53 p = ap;
54 ap = p;
55 ab1 = p;
56 as1 = s1;
57 s1 = as1;
58 at1 = t1;
59 t1 = at1;
60 /* It's unclear whether the undefined behavior (6.5.2.3#5) for
61 accessing elements of atomic structures and unions is at
62 translation or execution time; presume here that it's at
63 execution time. */
64 t1.i = at1.i;
65 at1.i = t1.i;
66 atp1->i = t1.i;
67 au1 = u1;
68 u1 = au1;
69 av1 = v1;
70 v1 = av1;
71 v1.i = av1.i;
72 av1.i = v1.i;
73 /* _Atomic is valid on register variables, even if not particularly
74 useful. */
75 register _Atomic volatile int ra1 = 1, ra2 = 2;
76 ra1 = ra2;
77 ra2 = ra1;
78 /* And on parameters. */
79 al1 = ra1;
80 ra2 = al1;
81}
82
83/* A function may return an atomic type. */
84_Atomic int
85func2 (int i)
86{
87 return i;
88}
89
90/* Casts may specify atomic type. */
91int
92func3 (int i)
93{
94 return func2 ((_Atomic long) i);
95}
96
97/* The _Atomic void type is valid. */
98_Atomic void *avp;
99
100/* An array of atomic elements is valid (the elements being atomic,
101 not the array). */
102_Atomic int aa[10];
103int
104func4 (void)
105{
106 return aa[2];
107}
108
109/* Increment and decrement are valid for atomic types when they are
110 valid for non-atomic types. */
111void
112func5 (void)
113{
114 ald1++;
115 ald1--;
116 ++ald1;
117 --ald1;
118 ai1++;
119 ai1--;
120 ++ai1;
121 --ai1;
122 ab1++;
123 ab1--;
124 ++ab1;
125 --ab1;
126 ap++;
127 ap--;
128 ++ap;
129 --ap;
130}
131
132/* Compound literals may have atomic type. */
133_Atomic int *aiclp = &(_Atomic int) { 1 };
134
135/* Test unary & and *. */
136void
137func6 (void)
138{
139 int i = *aiclp;
140 _Atomic int *p = &ai2;
141}
142
143/* Casts to atomic type are valid (although the _Atomic has little
144 effect because the result is an rvalue). */
145int i2 = (_Atomic int) 1.0;
146
147/* For pointer subtraction and comparisons, _Atomic does not count as
148 a qualifier. Likewise for conditional expressions. */
149_Atomic int *xaip1;
150volatile _Atomic int *xaip2;
151void *xvp1;
152
153void
154func7 (void)
155{
156 int r;
157 r = xaip1 - xaip2;
158 r = xaip1 < xaip2;
159 r = xaip1 > xaip2;
160 r = xaip1 <= xaip2;
161 r = xaip1 >= xaip2;
162 r = xaip1 == xaip2;
163 r = xaip1 != xaip2;
164 r = xaip1 == xvp1;
165 r = xaip1 != xvp1;
166 r = xvp1 == xaip1;
167 r = xvp1 != xaip1;
168 r = xaip1 == 0;
169 r = ((void *) 0) == xaip2;
170 (void) (r ? xaip1 : xaip2);
171 (void) (r ? xvp1 : xaip2);
172 (void) (r ? xaip2 : xvp1);
173 (void) (r ? xaip1 : 0);
174 (void) (r ? 0 : xaip1);
175 /* The result of a conditional expression between a pointer to
176 qualified or unqualified (but not atomic) void, and a pointer to
177 an atomic type, is a pointer to appropriately qualified, not
178 atomic, void. As such, it is valid to use further in conditional
179 expressions with other pointer types. */
180 (void) (r ? xaip1 : (r ? xaip1 : xvp1));
181}
182
183/* Pointer += and -= integer is valid. */
184void
185func8 (void)
186{
187 b += 1;
188 b -= 2ULL;
189 ap += 3;
190}
191
192/* Various other cases of simple assignment are valid (some already
193 tested above). */
194void
195func9 (void)
196{
197 ap = 0;
198 ap = (void *) 0;
199 xvp1 = atp1;
200 atp1 = xvp1;
201}
202
203/* Test compatibility of function types in cases where _Atomic matches
204 (see c11-atomic-3.c for corresponding cases where it doesn't
205 match). */
206void fc0a (int const);
207void fc0a (int);
208void fc0b (int _Atomic);
209void fc0b (int _Atomic);
210void fc1a (int);
211void
212fc1a (x)
213 volatile int x;
214{
215}
216void fc1b (_Atomic int);
217void
218fc1b (x)
219 volatile _Atomic int x;
220{
221}
222void
223fc2a (x)
224 const int x;
225{
226}
227void fc2a (int); /* { dg-warning "follows non-prototype" } */
228void
229fc2b (x)
230 _Atomic int x;
231{
232}
233void fc2b (_Atomic int); /* { dg-warning "follows non-prototype" } */
234void fc3a (int);
235void
236fc3a (x)
237 volatile short x;
238{
239}
240void fc3b (_Atomic int);
241void
242fc3b (x)
243 _Atomic short x;
244{
245}
246void
247fc4a (x)
248 const short x;
249{
250}
251void fc4a (int); /* { dg-warning "follows non-prototype" } */
252void
253fc4b (x)
254 _Atomic short x;
255{
256}
257void fc4b (_Atomic int); /* { dg-warning "follows non-prototype" } */
258
259/* Test cases involving C_MAYBE_CONST_EXPR work. */
260void
261func10 (_Atomic int *p)
262{
263 p[0 / 0] = 1; /* { dg-warning "division by zero" } */
264 p[0 / 0] += 1; /* { dg-warning "division by zero" } */
265 *p = 0 / 0; /* { dg-warning "division by zero" } */
266 *p += 0 / 0; /* { dg-warning "division by zero" } */
267}