blob: 4c7e061081542a418c345594cbe47d5769a167d3 [file] [log] [blame]
Dominic Chen184c6242017-03-03 18:02:02 +00001// RUN: %clang_analyze_cc1 -triple x86_64-unknown-linux -std=c++14 -analyzer-checker=optin.performance -analyzer-config optin.performance.Padding:AllowedPad=2 -verify %s
Ben Craig97c191c2015-12-14 21:38:59 +00002
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +00003// expected-warning@+7{{\
4Excessive padding in 'struct IntSandwich' (6 padding bytes, where 2 is optimal). \
5Optimal fields order: \
6i, \
7c1, \
8c2, \
9}}
Ben Craig97c191c2015-12-14 21:38:59 +000010struct IntSandwich {
11 char c1;
12 int i;
13 char c2;
14};
15
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +000016// expected-warning@+7{{\
17Excessive padding in 'struct TurDuckHen' (6 padding bytes, where 2 is optimal). \
18Optimal fields order: \
19i, \
20c1, \
21c2, \
22}}
Ben Craig97c191c2015-12-14 21:38:59 +000023struct TurDuckHen {
24 char c1;
25 struct IntSandwich i;
26 char c2;
27};
28
29#pragma pack(push)
30#pragma pack(2)
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +000031// expected-warning@+11{{\
32Excessive padding in 'struct SmallIntSandwich' (4 padding bytes, where 0 is optimal). \
33Optimal fields order: \
34i1, \
35i2, \
36i3, \
37c1, \
38c2, \
39c3, \
40c4, \
41}}
Ben Craig97c191c2015-12-14 21:38:59 +000042struct SmallIntSandwich {
43 char c1;
44 int i1;
45 char c2;
46 int i2;
47 char c3;
48 int i3;
49 char c4;
50};
51#pragma pack(pop)
52
53union SomeUnion { // no-warning
54 char c;
55 short s;
56 int i;
57};
58
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +000059// expected-warning@+7{{\
60Excessive padding in 'struct HoldsAUnion' (6 padding bytes, where 2 is optimal). \
61Optimal fields order: \
62u, \
63c1, \
64c2, \
65}}
Ben Craig97c191c2015-12-14 21:38:59 +000066struct HoldsAUnion {
67 char c1;
68 union SomeUnion u;
69 char c2;
70};
71
72struct SmallCharArray { // no-warning
73 char c[5];
74};
75
76struct MediumIntArray { // no-warning
77 int i[5];
78};
79
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +000080// expected-warning@+7{{\
81Excessive padding in 'struct StructSandwich' (6 padding bytes, where 2 is optimal). \
82Optimal fields order: \
83m, \
84s, \
85s2, \
86}}
Ben Craig97c191c2015-12-14 21:38:59 +000087struct StructSandwich {
88 struct SmallCharArray s;
89 struct MediumIntArray m;
90 struct SmallCharArray s2;
91};
92
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +000093// expected-warning@+7{{\
94Excessive padding in 'TypedefSandwich' (6 padding bytes, where 2 is optimal). \
95Optimal fields order: \
96i, \
97c1, \
98c2, \
99}}
Ben Craig97c191c2015-12-14 21:38:59 +0000100typedef struct {
101 char c1;
102 int i;
103 char c2;
104} TypedefSandwich;
105
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +0000106// expected-warning@+7{{\
107Excessive padding in 'struct StructAttrAlign' (10 padding bytes, where 2 is optimal). \
108Optimal fields order: \
109i, \
110c1, \
111c2, \
112}}
Ben Craig97c191c2015-12-14 21:38:59 +0000113struct StructAttrAlign {
114 char c1;
115 int i;
116 char c2;
117} __attribute__((aligned(8)));
118
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +0000119// expected-warning@+8{{\
120Excessive padding in 'struct OverlyAlignedChar' (8185 padding bytes, where 4089 is optimal). \
121Optimal fields order: \
122c, \
123c1, \
124c2, \
125x, \
126}}
Ben Craig97c191c2015-12-14 21:38:59 +0000127struct OverlyAlignedChar {
128 char c1;
129 int x;
130 char c2;
131 char c __attribute__((aligned(4096)));
132};
133
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +0000134// expected-warning@+7{{\
135Excessive padding in 'struct HoldsOverlyAlignedChar' (8190 padding bytes, where 4094 is optimal). \
136Optimal fields order: \
137o, \
138c1, \
139c2, \
140}}
Ben Craig97c191c2015-12-14 21:38:59 +0000141struct HoldsOverlyAlignedChar {
142 char c1;
143 struct OverlyAlignedChar o;
144 char c2;
145};
146
147void internalStructFunc() {
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +0000148 // expected-warning@+7{{\
149Excessive padding in 'struct X' (6 padding bytes, where 2 is optimal). \
150Optimal fields order: \
151t, \
152c1, \
153c2, \
154}}
Ben Craig97c191c2015-12-14 21:38:59 +0000155 struct X {
156 char c1;
157 int t;
158 char c2;
159 };
160 struct X obj;
161}
162
163void typedefStructFunc() {
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +0000164 // expected-warning@+7{{\
165Excessive padding in 'S' (6 padding bytes, where 2 is optimal). \
166Optimal fields order: \
167t, \
168c1, \
169c2, \
170}}
Ben Craig97c191c2015-12-14 21:38:59 +0000171 typedef struct {
172 char c1;
173 int t;
174 char c2;
175 } S;
176 S obj;
177}
178
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +0000179// expected-warning@+7{{\
180Excessive padding in 'struct DefaultAttrAlign' (22 padding bytes, where 6 is optimal). \
181Optimal fields order: \
182i, \
183c1, \
184c2, \
185}}
Ben Craig97c191c2015-12-14 21:38:59 +0000186struct DefaultAttrAlign {
187 char c1;
188 long long i;
189 char c2;
190} __attribute__((aligned));
191
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +0000192// expected-warning@+7{{\
193Excessive padding in 'struct SmallArrayShortSandwich' (2 padding bytes, where 0 is optimal). \
194Optimal fields order: \
195s, \
196c1, \
197c2, \
198}}
Ben Craig97c191c2015-12-14 21:38:59 +0000199struct SmallArrayShortSandwich {
200 char c1;
201 short s;
202 char c2;
203} ShortArray[20];
204
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +0000205// expected-warning@+7{{\
206Excessive padding in 'struct SmallArrayInFunc' (2 padding bytes, where 0 is optimal). \
207Optimal fields order: \
208s, \
209c1, \
210c2, \
211}}
Ben Craig97c191c2015-12-14 21:38:59 +0000212struct SmallArrayInFunc {
213 char c1;
214 short s;
215 char c2;
216};
217
218void arrayHolder() {
219 struct SmallArrayInFunc Arr[15];
220}
221
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +0000222// expected-warning@+7{{\
223Excessive padding in 'class VirtualIntSandwich' (10 padding bytes, where 2 is optimal). \
224Optimal fields order: \
225i, \
226c1, \
227c2, \
228}}
Ben Craig97c191c2015-12-14 21:38:59 +0000229class VirtualIntSandwich {
230 virtual void foo() {}
231 char c1;
232 int i;
233 char c2;
234};
235
236// constructed so as not to have tail padding
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +0000237// expected-warning@+8{{\
238Excessive padding in 'class InnerPaddedB' (6 padding bytes, where 2 is optimal). \
239Optimal fields order: \
240i1, \
241i2, \
242c1, \
243c2, \
244}}
Ben Craig97c191c2015-12-14 21:38:59 +0000245class InnerPaddedB {
246 char c1;
247 int i1;
248 char c2;
249 int i2;
250};
251
252class Empty {}; // no-warning
253
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +0000254// expected-warning@+7{{\
255Excessive padding in 'class LotsOfSpace' (6 padding bytes, where 2 is optimal). \
256Optimal fields order: \
257i, \
258e1, \
259e2, \
260}}
Ben Craig97c191c2015-12-14 21:38:59 +0000261class LotsOfSpace {
262 Empty e1;
263 int i;
264 Empty e2;
265};
266
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +0000267// expected-warning@+7{{\
268Excessive padding in 'TypedefSandwich2' (6 padding bytes, where 2 is optimal). \
269Optimal fields order: \
270t, \
271c1, \
272c2, \
273}}
Ben Craig97c191c2015-12-14 21:38:59 +0000274typedef struct {
275 char c1;
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +0000276 // expected-warning@+7{{\
277Excessive padding in 'TypedefSandwich2::NestedTypedef' (6 padding bytes, where 2 is optimal). \
278Optimal fields order: \
279i, \
280c1, \
281c2, \
282}}
Ben Craig97c191c2015-12-14 21:38:59 +0000283 typedef struct {
284 char c1;
285 int i;
286 char c2;
287 } NestedTypedef;
288 NestedTypedef t;
289 char c2;
290} TypedefSandwich2;
291
292template <typename T>
293struct Foo {
Saleem Abdulrasoolb9250472016-08-15 20:16:13 +0000294 // expected-warning@+7{{\
295Excessive padding in 'struct Foo<int>::Nested' (6 padding bytes, where 2 is optimal). \
296Optimal fields order: \
297t, \
298c1, \
299c2, \
300}}
Ben Craig97c191c2015-12-14 21:38:59 +0000301 struct Nested {
302 char c1;
303 T t;
304 char c2;
305 };
306};
307
308struct Holder { // no-warning
309 Foo<int>::Nested t1;
310 Foo<char>::Nested t2;
311};