xref: /llvm-project/clang/test/Sema/test-wunaligned-access.c (revision c5de4dd1eab00df76c1a68c5f397304ceacb71f2)
1 // RUN: %clang_cc1 %s -triple=armv7-none-none-eabi -verify -Wunaligned-access -emit-llvm -o %t
2 // REQUIRES: arm-registered-target
3 //
4 // This test suite tests the warning triggered by the -Wunaligned-access option.
5 // The warning occurs when a struct or other type of record contains a field
6 // that is itself a record. The outer record must be a packed structure, while
7 // while the inner record must be unpacked. This is the fundamental condition
8 // for the warning to be triggered. Some of these tests may have three layers.
9 //
10 // The command line option -fsyntax-only is not used as Clang needs to be
11 // forced to layout the structs used in this test.
12 // The triple in the command line above is used for the assumptions about
13 // size and alignment of types.
14 
15 // Set 1
16 struct T1 {
17   char a;
18   int b;
19 };
20 
21 struct __attribute__((packed)) U1 {
22   char a;
23   struct T1 b; // expected-warning {{field b within 'struct U1' is less aligned than 'struct T1' and is usually due to 'struct U1' being packed, which can lead to unaligned accesses}}
24   int c;
25 };
26 
27 struct __attribute__((packed)) U2 {
28   char a;
29   struct T1 b __attribute__((aligned(2))); // expected-warning {{field b within 'struct U2' is less aligned than 'struct T1' and is usually due to 'struct U2' being packed, which can lead to unaligned accesses}}
30   int c;
31 };
32 
33 struct __attribute__((packed)) U3 {
34   char a;
35   struct T1 b __attribute__((aligned(4)));
36   int c;
37 };
38 
39 struct __attribute__((aligned(2))) U4 {
40   char a;
41   struct T1 b;
42   int c;
43 };
44 
45 struct U5 {
46   char a;
47   struct T1 b;
48   int c;
49 };
50 
51 struct U6 {
52   char a;
53   int b;
54   struct T1 c __attribute__((aligned(2)));
55 };
56 
57 struct __attribute__((packed)) U7 {
58   short a;
59   short b;
60   char c;
61   struct T1 d; // expected-warning {{field d within 'struct U7' is less aligned than 'struct T1' and is usually due to 'struct U7' being packed, which can lead to unaligned accesses}}
62 };
63 
64 struct U8 {
65   short a;
66   short b;
67   char c;
68   struct T1 d;
69 };
70 
71 struct __attribute__((packed)) U9 {
72   short a;
73   short b;
74   char c;
75   struct T1 d __attribute__((aligned(4)));
76 };
77 
78 struct __attribute__((packed)) U10 {
79   short a;
80   short b;
81   char c;
82   struct T1 d __attribute__((aligned(2))); // expected-warning {{field d within 'struct U10' is less aligned than 'struct T1' and is usually due to 'struct U10' being packed, which can lead to unaligned accesses}}
83 };
84 
85 struct __attribute__((aligned(2))) U11 {
86   short a;
87   short b;
88   char c;
89   struct T1 d;
90 };
91 
92 // Set 2
93 #pragma pack(push, 1)
94 
95 struct U12 {
96   char a;
97   struct T1 b; // expected-warning {{field b within 'struct U12' is less aligned than 'struct T1' and is usually due to 'struct U12' being packed, which can lead to unaligned accesses}}
98   int c;
99 };
100 
101 struct __attribute__((packed)) U13 {
102   char a;
103   struct T1 b; // expected-warning {{field b within 'struct U13' is less aligned than 'struct T1' and is usually due to 'struct U13' being packed, which can lead to unaligned accesses}}
104   int c;
105 };
106 
107 struct __attribute__((packed)) U14 {
108   char a;
109   struct T1 b __attribute__((aligned(4))); // expected-warning {{field b within 'struct U14' is less aligned than 'struct T1' and is usually due to 'struct U14' being packed, which can lead to unaligned accesses}}
110   int c;
111 };
112 
113 struct __attribute__((aligned(2))) U15 {
114   char a;
115   struct T1 b; // expected-warning {{field b within 'struct U15' is less aligned than 'struct T1' and is usually due to 'struct U15' being packed, which can lead to unaligned accesses}}
116   int c;
117 };
118 
119 struct U16 {
120   char a;
121   char b;
122   short c;
123   struct T1 d;
124 };
125 
126 struct U17 {
127   char a;
128   char b;
129   short c;
130   struct T1 d __attribute__((aligned(4)));
131 };
132 
133 struct __attribute__((packed)) U18 {
134   char a;
135   short b;
136   struct T1 c __attribute__((aligned(4))); // expected-warning {{field c within 'struct U18' is less aligned than 'struct T1' and is usually due to 'struct U18' being packed, which can lead to unaligned accesses}}
137 };
138 
139 struct __attribute__((aligned(4))) U19 {
140   char a;
141   struct T1 b; // expected-warning {{field b within 'struct U19' is less aligned than 'struct T1' and is usually due to 'struct U19' being packed, which can lead to unaligned accesses}}
142   int c;
143 };
144 
145 struct __attribute__((aligned(4))) U20 {
146   char a[4];
147   struct T1 b;
148   int c;
149 };
150 
151 struct U21 {
152   char a;
153   short c;
154   struct T1 d; // expected-warning {{field d within 'struct U21' is less aligned than 'struct T1' and is usually due to 'struct U21' being packed, which can lead to unaligned accesses}}
155 };
156 
157 struct U22 {
158   char a;
159   short c;
160   struct T1 d __attribute__((aligned(4))); // expected-warning {{field d within 'struct U22' is less aligned than 'struct T1' and is usually due to 'struct U22' being packed, which can lead to unaligned accesses}}
161 };
162 
163 #pragma pack(pop)
164 
165 // Set 3
166 #pragma pack(push, 2)
167 
168 struct __attribute__((packed)) U23 {
169   char a;
170   struct T1 b; // expected-warning {{field b within 'struct U23' is less aligned than 'struct T1' and is usually due to 'struct U23' being packed, which can lead to unaligned accesses}}
171   int c;
172 };
173 
174 struct U24 {
175   char a;
176   struct T1 b; // expected-warning {{field b within 'struct U24' is less aligned than 'struct T1' and is usually due to 'struct U24' being packed, which can lead to unaligned accesses}}
177   int c;
178 };
179 
180 struct U25 {
181   char a;
182   char b;
183   short c;
184   struct T1 d;
185 };
186 
187 struct U26 {
188   char a;
189   char b;
190   short c;
191   struct T1 d;
192 };
193 
194 #pragma pack(pop)
195 
196 // Set 4
197 
198 struct __attribute__((packed)) T2 {
199   char a;
200   struct T1 b; // expected-warning {{field b within 'struct T2' is less aligned than 'struct T1' and is usually due to 'struct T2' being packed, which can lead to unaligned accesses}}
201 };
202 
203 struct T3 {
204   char a;
205   struct T1 b;
206 };
207 
208 struct __attribute__((packed)) U27 {
209   char a;
210   struct T2 b;
211   int c;
212 };
213 
214 struct U28 {
215   char a;
216   char _p[2];
217   struct T2 b;
218   int c;
219 };
220 
221 struct U29 {
222   char a;
223   struct T3 b;
224   int c;
225 };
226 
227 struct __attribute__((packed)) U30 {
228   char a;
229   struct T3 b; // expected-warning {{field b within 'struct U30' is less aligned than 'struct T3' and is usually due to 'struct U30' being packed, which can lead to unaligned accesses}}
230   int c;
231 };
232 
233 struct __attribute__((packed)) U31 {
234   char a;
235   struct T2 b __attribute__((aligned(4)));
236 };
237 
238 struct __attribute__((packed)) U32 {
239   char a;
240   char b;
241   char c;
242   char d;
243   struct T3 e;
244 };
245 
246 struct __attribute__((packed)) U33 {
247   char a;
248   char b;
249   char c;
250   char d;
251   struct T2 e __attribute__((aligned(4)));
252 };
253 
254 struct __attribute__((packed)) U34 {
255   char a;
256   struct T1 b __attribute__((packed)); // expected-warning {{field b within 'struct U34' is less aligned than 'struct T1' and is usually due to 'struct U34' being packed, which can lead to unaligned accesses}}
257   struct T2 c;
258 };
259 
260 struct __attribute__((packed)) U35 {
261   char a;
262   struct T4 {
263     char b;
264     struct T1 c;
265   } d; // expected-warning {{field d within 'struct U35' is less aligned than 'struct T4' and is usually due to 'struct U35' being packed, which can lead to unaligned accesses}}
266 };
267 
268 // Set 5
269 
270 #pragma pack(push, 1)
271 struct T5 {
272   char a;
273   struct T1 b; // expected-warning {{field b within 'struct T5' is less aligned than 'struct T1' and is usually due to 'struct T5' being packed, which can lead to unaligned accesses}}
274 };
275 #pragma pack(pop)
276 
277 #pragma pack(push, 1)
278 struct U36 {
279   char a;
280   struct T5 b;
281   int c;
282 };
283 
284 struct U37 {
285   char a;
286   struct T3 b; // expected-warning {{field b within 'struct U37' is less aligned than 'struct T3' and is usually due to 'struct U37' being packed, which can lead to unaligned accesses}}
287   int c;
288 };
289 #pragma pack(pop)
290 struct U38 {
291   char a;
292   struct T5 b __attribute__((aligned(4)));
293   int c;
294 };
295 
296 #pragma pack(push, 1)
297 
298 #pragma pack(push, 4)
299 struct U39 {
300   char a;
301   struct T5 b;
302   int c;
303 };
304 #pragma pack(pop)
305 
306 #pragma pack(pop)
307 
308 // Set 6
309 
310 struct __attribute__((packed)) A1 {
311   char a;
312   struct T1 b; // expected-warning {{field b within 'struct A1' is less aligned than 'struct T1' and is usually due to 'struct A1' being packed, which can lead to unaligned accesses}}
313 };
314 
315 struct A2 {
316   char a;
317   struct T1 b;
318 };
319 
320 struct __attribute__((packed)) A3 {
321   char a;
322   struct T1 b __attribute__((aligned(4)));
323 };
324 
325 #pragma pack(push, 1)
326 struct A4 {
327   char a;
328   struct T1 b; // expected-warning {{field b within 'struct A4' is less aligned than 'struct T1' and is usually due to 'struct A4' being packed, which can lead to unaligned accesses}}
329 };
330 
331 struct A5 {
332   char a;
333   struct T1 b __attribute__((aligned(4))); // expected-warning {{field b within 'struct A5' is less aligned than 'struct T1' and is usually due to 'struct A5' being packed, which can lead to unaligned accesses}}
334 };
335 #pragma pack(pop)
336 
337 struct __attribute__((packed)) A6 {
338   struct T1 a;
339 };
340 
341 struct A7 {
342   char a;
343   struct T1 b __attribute__((packed));
344 };
345 
346 struct A8 {
347   char a;
348   char b;
349   short c;
350   struct T1 d;
351 };
352 
353 struct A9 {
354   char a;
355   struct T2 b;
356 };
357 
358 struct A10 {
359   char a;
360   struct T2 b __attribute__((aligned(4)));
361 };
362 
363 struct __attribute__((packed)) A11 {
364   char a;
365   struct T2 b;
366 };
367 
368 struct __attribute__((packed)) U40 {
369   char a;
370   struct A1 b;
371   int c;
372 };
373 
374 struct __attribute__((packed)) U41 {
375   char a;
376   struct A3 b; // expected-warning {{field b within 'struct U41' is less aligned than 'struct A3' and is usually due to 'struct U41' being packed, which can lead to unaligned accesses}}
377   int c;
378 };
379 
380 #pragma pack(push, 1)
381 struct U42 {
382   char a;
383   struct A1 b;
384   int c;
385 };
386 #pragma pack(pop)
387 
388 struct __attribute__((packed)) U43 {
389   char a;
390   struct A9 b;
391   int c;
392 };
393 
394 struct __attribute__((packed)) U44 {
395   char a;
396   struct A10 b; // expected-warning {{field b within 'struct U44' is less aligned than 'struct A10' and is usually due to 'struct U44' being packed, which can lead to unaligned accesses}}
397   int c;
398 };
399 
400 #pragma pack(push, 1)
401 
402 struct U45 {
403   char a;
404   struct A10 b; // expected-warning {{field b within 'struct U45' is less aligned than 'struct A10' and is usually due to 'struct U45' being packed, which can lead to unaligned accesses}}
405   int c;
406 };
407 
408 #pragma pack(pop)
409 
410 struct __attribute__((packed)) U46 {
411   char a;
412   struct A2 b; // expected-warning {{field b within 'struct U46' is less aligned than 'struct A2' and is usually due to 'struct U46' being packed, which can lead to unaligned accesses}}
413   int c;
414 };
415 
416 struct __attribute__((packed)) U47 {
417   char a;
418   struct A8 b; // expected-warning {{field b within 'struct U47' is less aligned than 'struct A8' and is usually due to 'struct U47' being packed, which can lead to unaligned accesses}}
419   int c;
420 };
421 
422 #pragma pack(push, 1)
423 struct U48 {
424   char a;
425   struct A8 b; // expected-warning {{field b within 'struct U48' is less aligned than 'struct A8' and is usually due to 'struct U48' being packed, which can lead to unaligned accesses}}
426   int c;
427 };
428 #pragma pack(pop)
429 
430 struct U49 {
431   char a;
432   struct A11 b;
433   int c;
434 };
435 
436 struct U50 {
437   char a;
438   struct A1 b;
439   int c;
440 };
441 
442 struct U51 {
443   char a;
444   struct A5 b;
445   int c;
446 };
447 
448 struct __attribute__((packed)) U52 {
449   char a;
450   struct A6 b;
451 };
452 
453 struct U53 {
454   char a;
455   struct A4 b;
456 };
457 
458 struct U54 {
459   char b;
460   struct A7 c;
461 };
462 
463 struct U1 s1;
464 struct U2 s2;
465 struct U3 s3;
466 struct U4 s4;
467 struct U5 s5;
468 struct U6 s6;
469 struct U7 s7;
470 struct U8 s8;
471 struct U9 s9;
472 struct U10 s10;
473 struct U11 s11;
474 struct U12 s12;
475 struct U13 s13;
476 struct U14 s14;
477 struct U15 s15;
478 struct U16 s16;
479 struct U17 s17;
480 struct U18 s18;
481 struct U19 s19;
482 struct U20 s20;
483 struct U21 s21;
484 struct U22 s22;
485 struct U23 s23;
486 struct U24 s24;
487 struct U25 s25;
488 struct U26 s26;
489 struct U27 s27;
490 struct U28 s28;
491 struct U29 s29;
492 struct U30 s30;
493 struct U31 s31;
494 struct U32 s32;
495 struct U33 s33;
496 struct U34 s34;
497 struct U35 s35;
498 struct U36 s36;
499 struct U37 s37;
500 struct U38 s38;
501 struct U39 s39;
502 struct U40 s40;
503 struct U41 s41;
504 struct U42 s42;
505 struct U43 s43;
506 struct U44 s44;
507 struct U45 s45;
508 struct U46 s46;
509 struct U47 s47;
510 struct U48 s48;
511 struct U49 s49;
512 struct U50 s50;
513 struct U51 s51;
514 struct U52 s52;
515 struct U53 s53;
516 struct U54 s54;
517