aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/c-c++-common/builtin-has-attribute-7.c
blob: 1326aed32a79ec2fd9ef62cffaf04cc2f4202f57 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* Verify that __builtin_has_attribute detects attributes aligned
   and packed in various forms of array dereferencing and indirection
   expressions correspondingly to __alignof__.
   { dg-do compile }
   { dg-options "-Wall -Wno-unused -ftrack-macro-expansion=0" }
   { dg-require-effective-target size24plus } */

#define ATTR(...) __attribute__ ((__VA_ARGS__))
#define ALIGN(N)  ATTR (aligned (N))

#define Assert(expr) typedef int _Assert [1 - 2 * !(expr)]

/* Verify that  __builtin_has_attribute (EXPR, align (ALIGN)) returns
   the EXPECTed result.  When EXPECT is true, verify that the EXPression
   has the expected ALIGNment.    */
#define A3(expect, expr, align) do {					\
    Assert (!expect || __alignof__ (expr) == align);			\
    Assert (expect == __builtin_has_attribute (expr, aligned (align))); \
  } while (0)

#define A(expect, expr)							\
  Assert (expect == __builtin_has_attribute (expr, aligned))		\

enum { PA = __alignof__ (void*) };

/* Define pointer to pointer types, with different alignments
   at each level of indirection.  */
typedef struct S8 { char a[8]; }   S8;
typedef ALIGN (8)  S8              I8;
typedef ALIGN (16) I8             *P16_I8;
typedef            P16_I8         *P_P16_I8;
typedef ALIGN (32) P_P16_I8       *P32_P_P16_I8;
typedef            P32_P_P16_I8   *P_P32_P_P16_I8;
typedef ALIGN (64) P_P32_P_P16_I8 *P64_P_P32_P_P16_I8;

Assert ( 8 == __alignof__ (I8));
Assert (16 == __alignof__ (P16_I8));
Assert (PA == __alignof__ (P_P16_I8));
Assert (32 == __alignof__ (P32_P_P16_I8));
Assert (PA == __alignof__ (P_P32_P_P16_I8));
Assert (64 == __alignof__ (P64_P_P32_P_P16_I8));


/* Similar to the pointer of pointers above, define array of array
   types, with different alignments at each level of indirection.  */
typedef struct S64 { char a[64]; } S64;
typedef ALIGN (64) S64             I64;
typedef ALIGN (32) I64             A32_I64[3];
typedef            A32_I64         A_A32_I64[5];
typedef ALIGN (16) A_A32_I64       A16_A_A32_I64[7];
typedef            A16_A_A32_I64   A_A16_A_A32_I64[11];
typedef ALIGN (8)  A_A16_A_A32_I64 A8_A_A16_A_A32_I64[13];

Assert (64 == __alignof__ (I64));
Assert (32 == __alignof__ (A32_I64));
/* With no explicit alignment, an array of overaligned elements
   is considered to have the alignment of its elements.  */
Assert (32 == __alignof__ (A_A32_I64));
Assert (16 == __alignof__ (A16_A_A32_I64));
Assert (16 == __alignof__ (A_A16_A_A32_I64));
Assert ( 8 == __alignof__ (A8_A_A16_A_A32_I64));


void test_arrays (void)
{
  /* Verify that the aligned attribute on each of the composite types
     is detected corresponding to the result of __alignof__.  */
  A (1, (*(A8_A_A16_A_A32_I64*)0));
  A3 (1, (*(A8_A_A16_A_A32_I64*)0), 8);
  A3 (0, (*(A8_A_A16_A_A32_I64*)0)[0], 8);
  /* GCC propagates the user-align bit from element types to their
     arrays but it doesn't propagate the attribute itself.  The built-in
     considers both the  bit and the attribute so it succeeds below even
     though the referenced type isn't declared with the attribute.  */
  A3 (0, (*(A8_A_A16_A_A32_I64*)0)[0], 8);
  A3 (1, (*(A8_A_A16_A_A32_I64*)0)[0], 16);
  A3 (0, (*(A8_A_A16_A_A32_I64*)0)[0], 32);
  A3 (0, (*(A8_A_A16_A_A32_I64*)0)[0][1], 8);
  A3 (1, (*(A8_A_A16_A_A32_I64*)0)[0][1], 16);
  A3 (0, (*(A8_A_A16_A_A32_I64*)0)[0][1], 32);
  A3 (0, (*(A8_A_A16_A_A32_I64*)0)[0][1][2], 16);
  A3 (1, (*(A8_A_A16_A_A32_I64*)0)[0][1][2], 32);
  A3 (0, (*(A8_A_A16_A_A32_I64*)0)[0][1][2], 64);
  A3 (0, (*(A8_A_A16_A_A32_I64*)0)[0][1][2][3], 16);
  A3 (1, (*(A8_A_A16_A_A32_I64*)0)[0][1][2][3], 32);
  A3 (0, (*(A8_A_A16_A_A32_I64*)0)[0][1][2][3], 64);
  A3 (0, (*(A8_A_A16_A_A32_I64*)0)[0][1][2][3][4], 32);
  A3 (1, (*(A8_A_A16_A_A32_I64*)0)[0][1][2][3][4], 64);
  A3 (0, (*(A8_A_A16_A_A32_I64*)0)[0][1][2][3][4], 128);

  A8_A_A16_A_A32_I64 a;
  A3 (0, a[0], 8);
  A3 (1, a[0], 16);
  A3 (0, a[0], 32);
  A3 (0, a[0][1], 8);
  A3 (1, a[0][1], 16);
  A3 (0, a[0][1], 32);
  A3 (0, a[0][1][2], 16);
  A3 (1, a[0][1][2], 32);
  A3 (0, a[0][1][2], 64);
  A3 (0, a[0][1][2][3], 16);
  A3 (1, a[0][1][2][3], 32);
  A3 (0, a[0][1][2][3], 64);
  A3 (0, a[0][1][2][3][4], 32);
  A3 (1, a[0][1][2][3][4], 64);
  A3 (0, a[0][1][2][3][4], 128);
}

void test_pointers (void)
{
  /* Verify that the aligned attribute on each of the composite pointer
     types is detected corresponding to the result of __alignof__.  */
  A (1, I8);
  A3 (0, I8, 4);
  A3 (1, I8, 8);

  A (1, P16_I8);
  A3 (0, P16_I8, 8);
  A3 (1, P16_I8, 16);

  A (0, P_P16_I8);
  A3 (0, P_P16_I8, 8);
  A3 (0, P_P16_I8, 16);

  A (1, P32_P_P16_I8);
  A3 (0, P32_P_P16_I8, 8);
  A3 (0, P32_P_P16_I8, 16);
  A3 (1, P32_P_P16_I8, 32);

  A (0, P_P32_P_P16_I8);

  A (1, P64_P_P32_P_P16_I8);
  A3 (0, P64_P_P32_P_P16_I8, 8);
  A3 (0, P64_P_P32_P_P16_I8, 16);
  A3 (0, P64_P_P32_P_P16_I8, 32);
  A3 (1, P64_P_P32_P_P16_I8, 64);


  /* Verify that the attribute on each of the composite types is detected
     in the type of each of the indirection expressions.  */
  A (1, *(P16_I8)0);
  A3 (1, *(P16_I8)0, 8);
  A3 (0, *(P16_I8)0, 16);

  A (1, *(P_P16_I8)0);
  A3 (0, *(P_P16_I8)0, 8);
  A3 (1, *(P_P16_I8)0, 16);

  A (0, *(P32_P_P16_I8)0);
  A3 (0, *(P32_P_P16_I8)0, 8);
  A3 (0, *(P32_P_P16_I8)0, 16);
  A3 (0, *(P32_P_P16_I8)0, 32);

  A (1, *(P_P32_P_P16_I8)0);
  A3 (1, *(P_P32_P_P16_I8)0, 32);

  A (0, *(P64_P_P32_P_P16_I8)0);

  /* Verify that the attribute on each of the composite types is detected
     in the type of each of the subscipting expressions.  */
  A (1, ((P16_I8)0)[0]);
  A3 (1, ((P16_I8)0)[1], 8);
  A3 (0, ((P16_I8)0)[2], 16);

  A (1, ((P_P16_I8)0)[3]);
  A3 (0, ((P_P16_I8)0)[4], 8);
  A3 (1, ((P_P16_I8)0)[5], 16);

  A (0, ((P32_P_P16_I8)0)[6]);
  A3 (0, ((P32_P_P16_I8)0)[7], 8);
  A3 (0, ((P32_P_P16_I8)0)[8], 16);
  A3 (0, ((P32_P_P16_I8)0)[9], 32);

  A (1, ((P_P32_P_P16_I8)0)[10]);
  A3 (1, ((P_P32_P_P16_I8)0)[11], 32);

  A (0, ((P64_P_P32_P_P16_I8)0)[12]);


  /* Verify that the attribute on each of the composite types is detected
     in the type of each of the subscipting expression involving variables.  */

  I8                   i8;
  P16_I8               p16_i8 = &i8;
  P_P16_I8             p_p16_i8 = &p16_i8;
  P32_P_P16_I8         p32_p_p16_i8 = &p_p16_i8;
  P_P32_P_P16_I8       p_p32_p_p16_i8 = &p32_p_p16_i8;
  P64_P_P32_P_P16_I8   p64_p_p32_p_p16_i8 = &p_p32_p_p16_i8;

  A (1, p16_i8[0]);
  A3 (1, p16_i8[1], 8);
  A3 (0, p16_i8[2], 16);

  A (1, p_p16_i8[3]);
  A3 (0, p_p16_i8[4], 8);
  A3 (1, p_p16_i8[5], 16);

  A (0, p32_p_p16_i8[6]);
  A3 (0, p32_p_p16_i8[7], 8);
  A3 (0, p32_p_p16_i8[8], 16);
  A3 (0, p32_p_p16_i8[9], 32);

  A (1, p_p32_p_p16_i8[10]);
  A3 (1, p_p32_p_p16_i8[11], 32);


  A (1, p_p16_i8[0][1]);
  A3 (1, p_p16_i8[1][2], 8);
  A3 (0, p_p16_i8[2][3], 16);


  A (0, p64_p_p32_p_p16_i8[0]);

  A (1, p64_p_p32_p_p16_i8[0][1]);
  A3 (0, p64_p_p32_p_p16_i8[0][2], 16);
  A3 (1, p64_p_p32_p_p16_i8[0][3], 32);
  A3 (0, p64_p_p32_p_p16_i8[0][4], 64);

  A (0, p64_p_p32_p_p16_i8[0][1][2]);

  A (1, p64_p_p32_p_p16_i8[0][1][2][3]);
  A3 (0, p64_p_p32_p_p16_i8[0][1][2][4], 8);
  A3 (1, p64_p_p32_p_p16_i8[0][1][2][4], 16);
  A3 (0, p64_p_p32_p_p16_i8[0][1][2][4], 32);

  A (1, p64_p_p32_p_p16_i8[0][1][2][3][4]);
  A3 (1, p64_p_p32_p_p16_i8[0][1][2][3][5], 8);
  A3 (0, p64_p_p32_p_p16_i8[0][1][2][4][6], 16);


  /* Same as above but using the indirection expression.  */
  A (0, *p64_p_p32_p_p16_i8);

  A (1, **p64_p_p32_p_p16_i8);
  A3 (0, **p64_p_p32_p_p16_i8, 16);
  A3 (1, **p64_p_p32_p_p16_i8, 32);
  A3 (0, **p64_p_p32_p_p16_i8, 64);

  A (0, ***p64_p_p32_p_p16_i8);

  A (1, ****p64_p_p32_p_p16_i8);
  A3 (0, ****p64_p_p32_p_p16_i8, 8);
  A3 (1, ****p64_p_p32_p_p16_i8, 16);
  A3 (0, ****p64_p_p32_p_p16_i8, 32);

  A (1, *****p64_p_p32_p_p16_i8);
  A3 (1, *****p64_p_p32_p_p16_i8, 8);
  A3 (0, *****p64_p_p32_p_p16_i8, 16);
}


S8 f_S8 (void);
I8 f_I8 (void);
P16_I8 f_P16_I8 (void);
P_P16_I8 f_P_P16_I8 (void);
P32_P_P16_I8 f_P32_P_P16_I8 (void);
P_P32_P_P16_I8 f_P_P32_P_P16_I8 (void);
P64_P_P32_P_P16_I8 f_P64_P_P32_P_P16_I8 (void);

void test_function_call (void)
{
  /* Verify that the aligned attribute on each of the composite pointer
     types returned by the functions is detected corresponding to
     the result of __alignof__.  */

  A (0, f_S8 ());

  A (1, f_I8 ());
  A3 (1, f_I8 (), 8);
  A3 (0, f_I8 (), 16);

  A (1, f_P16_I8 ());
  A3 (0, f_P16_I8 (), 8);
  A3 (1, f_P16_I8 (), 16);
  A3 (0, f_P16_I8 (), 32);

  A (1, *f_P16_I8 ());
  A3 (1, *f_P16_I8 (), 8);
  A3 (0, *f_P16_I8 (), 16);

  A (0, f_P_P16_I8 ());

  A (1, *f_P_P16_I8 ());
  A3 (0, *f_P_P16_I8 (), 8);
  A3 (1, *f_P_P16_I8 (), 16);
  A3 (0, *f_P_P16_I8 (), 32);

  A (1, **f_P_P16_I8 ());
  A3 (1, **f_P_P16_I8 (), 8);
  A3 (0, **f_P_P16_I8 (), 16);
  A3 (0, **f_P_P16_I8 (), 32);
}


void test_compound_literal (void)
{
  A (0, (S8){ });

  A (1, (I8){ });
  A3 (1, (I8){ }, 8);
  A3 (0, (I8){ }, 16);

  A (1, (I64){ });
  A3 (0, (I64){ }, 8);
  A3 (0, (I64){ }, 16);
  A3 (0, (I64){ }, 32);
  A3 (1, (I64){ }, 64);

  A (1, (A32_I64){ 0 });
  A3 (0, (A32_I64){ 0 }, 8);
  A3 (0, (A32_I64){ 0 }, 16);
  A3 (1, (A32_I64){ 0 }, 32);
  A3 (0, (A32_I64){ 0 }, 64);

  A (1, ((A32_I64){ 0 })[0]);
  A3 (0, ((A32_I64){ 0 })[0], 8);
  A3 (0, ((A32_I64){ 0 })[0], 16);
  A3 (0, ((A32_I64){ 0 })[0], 32);
  A3 (1, ((A32_I64){ 0 })[0], 64);
}


void test_ternary_expression (int i)
{
  A (0, (0 ? (S8){ } : (S8){ }));

  A (1, (1 ? (I8){ } : (I8){ }));
  A3 (1, (2 ? (I8){ } : (I8){ }), 8);
  A3 (0, (3 ? (I8){ } : (I8){ }), 16);

  A (1, (4 ? (I64){ } : (I64){ }));
  A3 (0, (5 ? (I64){ } : (I64){ }), 8);
  A3 (0, (6 ? (I64){ } : (I64){ }), 16);
  A3 (0, (7 ? (I64){ } : (I64){ }), 32);
  A3 (1, (8 ? (I64){ } : (I64){ }), 64);

#if !__cplusplus
  /* Suppress -Wc++-compat warning: converting an array compound literal
     to a pointer is ill-formed in C++  */
# pragma GCC diagnostic ignored "-Wc++-compat"

  A (0, (9 ? (A32_I64){ } : (A32_I64){ })); 
  A3 (0, (i ? (A32_I64){ } : (A32_I64){ }), 8);
  A3 (0, (i++ ? (A32_I64){ } : (A32_I64){ }), 16);
  A3 (0, (++i ? (A32_I64){ } : (A32_I64){ }), 32);
  A3 (0, (!i ? (A32_I64){ } : (A32_I64){ }), 64);

  A (1, (0 ? (A32_I64){ } : (A32_I64){ })[0]);
  A3 (0, (1 ? (A32_I64){ } : (A32_I64){ })[1], 8);
  A3 (0, (2 ? (A32_I64){ } : (A32_I64){ })[2], 16);
  A3 (0, (3 ? (A32_I64){ } : (A32_I64){ })[3], 32);
  A3 (1, (3 ? (A32_I64){ } : (A32_I64){ })[i], 64);
#endif
}


void test_comma_expression (int i)
{
#if __cplusplus
  /* In C++, the type of the comma expressions whose operand is an array
     is the array itself with any attributes it was defined with.  */
# define R 1
#else
  /* In C, the type of the comma expressions whose operand is an array
     is a pointer type that does not include any attributes the array
     was defined with.  */
# define R 0
/* Suppress -Wc++-compat warning: converting an array compound literal
   to a pointer is ill-formed in C++
   G++ accepts the conversion in unevaluated contexts without a warning.  */
# pragma GCC diagnostic ignored "-Wc++-compat"
#endif

  A (0, (0, (S8){ }));

  A (1, (0, (I8){ }));
  A3 (1, (1, (I8){ }), 8);
  A3 (0, (2, (I8){ }), 16);

  A (1, (3, (I64){ }));
  A3 (0, (4, (I64){ }), 8);
  A3 (0, (5, (I64){ }), 16);
  A3 (0, (6, (I64){ }), 32);
  A3 (1, (7, (I64){ }), 64);

  A (R, (8, (A32_I64){ }));
  A3 (0, (9, (A32_I64){ }), 8);
  A3 (0, ((void)0, (A32_I64){ }), 16);
  A3 (R, ((I64){ },(A32_I64){ }), 32);
  A3 (0, (0, (A32_I64){ }), 64);

  A (1, (1, ((A32_I64){ })[0]));
  A3 (0, (2, ((A32_I64){ })[0]), 8);
  A3 (0, (i++, ((A32_I64){ })[0]), 16);
  A3 (0, (++i, ((A32_I64){ })[0]), 32);
  A3 (1, (i = 0, ((A32_I64){ })[0]), 64);
}