Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Sema / test-wunaligned-access.cpp
blob33f518310b0b1fecc709e6f6c4b2ec08573951c6
1 // RUN: %clang_cc1 %s -triple=armv7-none-none-eabi -verify -Wunaligned-access -S -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.
15 // Packed-Unpacked Tests (No Pragma)
17 struct T1 {
18 char a;
19 int b;
22 struct __attribute__((packed)) U1 {
23 char a;
24 T1 b; // expected-warning {{field b within 'U1' is less aligned than 'T1' and is usually due to 'U1' being packed, which can lead to unaligned accesses}}
25 int c;
28 struct __attribute__((packed)) U2 {
29 char a;
30 T1 b __attribute__((aligned(4)));
31 int c;
34 struct __attribute__((packed)) U3 {
35 char a;
36 char b;
37 short c;
38 T1 d;
41 struct __attribute__((packed)) U4 {
42 T1 a;
43 int b;
46 struct __attribute__((aligned(4), packed)) U5 {
47 char a;
48 T1 b; // expected-warning {{field b within 'U5' is less aligned than 'T1' and is usually due to 'U5' being packed, which can lead to unaligned accesses}}
49 int c;
52 struct __attribute__((aligned(4), packed)) U6 {
53 char a;
54 char b;
55 short c;
56 T1 d;
59 // Packed-Unpacked Tests with Pragma
61 #pragma pack(push, 1)
63 struct __attribute__((packed)) U7 {
64 char a;
65 T1 b; // expected-warning {{field b within 'U7' is less aligned than 'T1' and is usually due to 'U7' being packed, which can lead to unaligned accesses}}
66 int c;
69 struct __attribute__((packed)) U8 {
70 char a;
71 T1 b __attribute__((aligned(4))); // expected-warning {{field b within 'U8' is less aligned than 'T1' and is usually due to 'U8' being packed, which can lead to unaligned accesses}}
72 int c;
75 struct __attribute__((aligned(4))) U9 {
76 char a;
77 T1 b; // expected-warning {{field b within 'U9' is less aligned than 'T1' and is usually due to 'U9' being packed, which can lead to unaligned accesses}}
78 int c;
81 struct U10 {
82 char a;
83 T1 b; // expected-warning {{field b within 'U10' is less aligned than 'T1' and is usually due to 'U10' being packed, which can lead to unaligned accesses}}
84 int c;
87 #pragma pack(pop)
89 // Packed-Packed Tests
91 struct __attribute__((packed)) T2 {
92 char a;
93 int b;
96 struct __attribute__((packed)) U11 {
97 char a;
98 T2 b;
99 int c;
102 #pragma pack(push, 1)
103 struct U12 {
104 char a;
105 T2 b;
106 int c;
108 #pragma pack(pop)
110 // Unpacked-Packed Tests
112 struct U13 {
113 char a;
114 T2 b;
115 int c;
118 struct U14 {
119 char a;
120 T2 b __attribute__((aligned(4)));
121 int c;
124 // Unpacked-Unpacked Test
126 struct T3 {
127 char a;
128 int b;
131 struct U15 {
132 char a;
133 T3 b;
134 int c;
137 // Packed-Packed-Unpacked Test (No pragma)
139 struct __attribute__((packed)) A1 {
140 char a;
141 T1 b; // expected-warning {{field b within 'A1' is less aligned than 'T1' and is usually due to 'A1' being packed, which can lead to unaligned accesses}}
144 struct __attribute__((packed)) U16 {
145 char a;
146 A1 b;
147 int c;
150 struct __attribute__((packed)) A2 {
151 char a;
152 T1 b __attribute__((aligned(4)));
155 struct __attribute__((packed)) U17 {
156 char a;
157 A2 b; // expected-warning {{field b within 'U17' is less aligned than 'A2' and is usually due to 'U17' being packed, which can lead to unaligned accesses}}
158 int c;
161 // Packed-Unpacked-Packed tests
163 struct A3 {
164 char a;
165 T2 b;
168 struct __attribute__((packed)) U18 {
169 char a;
170 A3 b;
171 int c;
174 struct A4 {
175 char a;
176 T2 b;
177 int c;
180 #pragma pack(push, 1)
181 struct U19 {
182 char a;
183 A4 b; // expected-warning {{field b within 'U19' is less aligned than 'A4' and is usually due to 'U19' being packed, which can lead to unaligned accesses}}
184 int c;
186 #pragma pack(pop)
188 // Packed-Unpacked-Unpacked tests
190 struct A5 {
191 char a;
192 T1 b;
195 struct __attribute__((packed)) U20 {
196 char a;
197 A5 b; // expected-warning {{field b within 'U20' is less aligned than 'A5' and is usually due to 'U20' being packed, which can lead to unaligned accesses}}
198 int c;
201 struct A6 {
202 char a;
203 T1 b;
206 #pragma pack(push, 1)
207 struct U21 {
208 char a;
209 A6 b; // expected-warning {{field b within 'U21' is less aligned than 'A6' and is usually due to 'U21' being packed, which can lead to unaligned accesses}}
210 int c;
212 #pragma pack(pop)
214 // Unpacked-Packed-Packed test
216 struct __attribute__((packed)) A7 {
217 char a;
218 T2 b;
221 struct U22 {
222 char a;
223 A7 b;
224 int c;
227 // Unpacked-Packed-Unpacked tests
229 struct __attribute__((packed)) A8 {
230 char a;
231 T1 b; // expected-warning {{field b within 'A8' is less aligned than 'T1' and is usually due to 'A8' being packed, which can lead to unaligned accesses}}
234 struct U23 {
235 char a;
236 A8 b;
237 int c;
240 struct __attribute__((packed)) A9 {
241 char a;
242 T1 b __attribute__((aligned(4)));
245 struct U24 {
246 char a;
247 A9 b;
248 int c;
251 struct U1 s1;
252 struct U2 s2;
253 struct U3 s3;
254 struct U4 s4;
255 struct U5 s5;
256 struct U6 s6;
257 struct U7 s7;
258 struct U8 s8;
259 struct U9 s9;
260 struct U10 s10;
261 struct U11 s11;
262 struct U12 s12;
263 struct U13 s13;
264 struct U14 s14;
265 struct U15 s15;
266 struct U16 s16;
267 struct U17 s17;
268 struct U18 s18;
269 struct U19 s19;
270 struct U20 s20;
271 struct U21 s21;
272 struct U22 s22;
273 struct U23 s23;
274 struct U24 s24;