1 // FIXME -Wno-aix-compat added temporarily while the diagnostic is being
2 // refined.
3 // RUN: %clang_analyze_cc1 -verify -Wno-aix-compat %s \
4 // RUN:   -analyzer-checker=optin.performance \
5 // RUN:   -analyzer-config optin.performance.Padding:AllowedPad=2
6 
7 // RUN: not %clang_analyze_cc1 -verify -Wno-aix-compat %s \
8 // RUN:   -analyzer-checker=core \
9 // RUN:   -analyzer-checker=optin.performance.Padding \
10 // RUN:   -analyzer-config optin.performance.Padding:AllowedPad=-10 \
11 // RUN:   2>&1 | FileCheck %s -check-prefix=CHECK-PAD-NEGATIVE-VALUE
12 
13 // CHECK-PAD-NEGATIVE-VALUE: (frontend): invalid input for checker option
14 // CHECK-PAD-NEGATIVE-VALUE-SAME: 'optin.performance.Padding:AllowedPad', that
15 // CHECK-PAD-NEGATIVE-VALUE-SAME: expects a non-negative value
16 
17 #if __has_include(<stdalign.h>)
18 #include <stdalign.h>
19 #endif
20 
21 #if __has_include(<stdalign.h>) || defined(__cplusplus)
22 // expected-warning@+1{{Excessive padding in 'struct FieldAttrAlign' (6 padding}}
23 struct FieldAttrAlign {
24   char c1;
25   alignas(4) int i;
26   char c2;
27 };
28 
29 // expected-warning@+1{{Excessive padding in 'struct FieldAttrOverAlign' (10 padding}}
30 struct FieldAttrOverAlign {
31   char c1;
32   alignas(8) int i;
33   char c2;
34 };
35 
36 #endif // __has_include(<stdalign.h>) || defined(__cplusplus)
37 
38 // Re-ordering members of these structs won't reduce padding, so don't warn
39 struct LeadingChar { // no-warning
40   char c;
41   int i;
42 };
43 
44 struct TrailingChar { // no-warning
45   int i;
46   char c;
47 };
48 
49 struct Helpless { // no-warning
50   struct TrailingChar i1;
51   struct LeadingChar i2;
52   char c;
53 };
54 
55 #pragma pack(push)
56 #pragma pack(1)
57 struct SquishedIntSandwich { // no-warning
58   char c1;
59   int i;
60   char c2;
61 };
62 #pragma pack(pop)
63 
64 // Re-ordering members of these structs will reduce padding, so warn
65 struct IntSandwich { // expected-warning{{Excessive padding in 'struct IntSandwich'}}
66   char c1;
67   int i;
68   char c2;
69 };
70 
71 struct TurDuckHen { // expected-warning{{Excessive padding in 'struct TurDuckHen'}}
72   char c1;
73   struct IntSandwich i;
74   char c2;
75 };
76 
77 #pragma pack(push)
78 #pragma pack(2)
79 struct SmallIntSandwich { // expected-warning{{Excessive padding in 'struct SmallIntSandwich'}}
80   char c1;
81   int i1;
82   char c2;
83   int i2;
84   char c3;
85   int i3;
86   char c4;
87 };
88 #pragma pack(pop)
89 
90 union SomeUnion { // no-warning
91   char c;
92   short s;
93   int i;
94 };
95 
96 struct HoldsAUnion { // expected-warning{{Excessive padding in 'struct HoldsAUnion'}}
97   char c1;
98   union SomeUnion u;
99   char c2;
100 };
101 
102 struct BigCharArray { // no-warning
103   char c[129];
104 };
105 
106 struct SmallCharArray { // no-warning
107   char c[5];
108 };
109 
110 struct MediumIntArray { // no-warning
111   int i[5];
112 };
113 
114 struct LargeSizeToSmallSize { // expected-warning{{Excessive padding in 'struct LargeSizeToSmallSize'}}
115   struct BigCharArray b;
116   struct MediumIntArray m;
117   struct SmallCharArray s;
118 };
119 
120 struct LargeAlignToSmallAlign { // no-warning
121   struct MediumIntArray m;
122   struct BigCharArray b;
123   struct SmallCharArray s;
124 };
125 
126 // Currently ignoring VLA padding problems.  Still need to make sure we don't
127 // choke on VLAs though
128 struct HoldsVLA { // no-warning
129   char c1;
130   int x;
131   char c2;
132   int vla[];
133 };
134 
135 // Currently ignoring bitfield padding problems.  Still need to make sure we
136 // don't choke on bitfields though
137 struct HoldsBitfield { // no-warning
138   char c1;
139   int x;
140   char c2;
141   unsigned char b1 : 3;
142   unsigned char b2 : 3;
143   unsigned char b3 : 2;
144 };
145 
146 typedef struct { // expected-warning{{Excessive padding in 'TypedefSandwich'}}
147   char c1;
148   int i;
149   char c2;
150 } TypedefSandwich;
151 
152 // expected-warning@+1{{Excessive padding in 'struct StructAttrAlign' (10 padding}}
153 struct StructAttrAlign {
154   char c1;
155   int i;
156   char c2;
157 } __attribute__((aligned(8)));
158 
159 struct CorrectOverlyAlignedChar { // no-warning
160   char c __attribute__((aligned(4096)));
161   char c1;
162   int x1;
163   char c2;
164   int x2;
165   char c3;
166 };
167 
168 struct OverlyAlignedChar { // expected-warning{{Excessive padding in 'struct OverlyAlignedChar'}}
169   char c1;
170   int x;
171   char c2;
172   char c __attribute__((aligned(4096)));
173 };
174 
175 struct HoldsOverlyAlignedChar { // expected-warning{{Excessive padding in 'struct HoldsOverlyAlignedChar'}}
176   char c1;
177   struct OverlyAlignedChar o;
178   char c2;
179 };
180 
181 void internalStructFunc() {
182   struct X { // expected-warning{{Excessive padding in 'struct X'}}
183     char c1;
184     int t;
185     char c2;
186   };
187   struct X obj;
188 }
189 
190 void typedefStructFunc() {
191   typedef struct { // expected-warning{{Excessive padding in 'S'}}
192     char c1;
193     int t;
194     char c2;
195   } S;
196   S obj;
197 }
198 
199 void anonStructFunc() {
200   struct { // expected-warning{{Excessive padding in 'struct (unnamed}}
201     char c1;
202     int t;
203     char c2;
204   } obj;
205 }
206 
207 struct CorrectDefaultAttrAlign { // no-warning
208   long long i;
209   char c1;
210   char c2;
211 } __attribute__((aligned));
212 
213 struct TooSmallShortSandwich { // no-warning
214   char c1;
215   short s;
216   char c2;
217 };
218 
219 // expected-warning@+1{{Excessive padding in 'struct SmallArrayShortSandwich'}}
220 struct SmallArrayShortSandwich {
221   char c1;
222   short s;
223   char c2;
224 } ShortArray[20];
225 
226 // expected-warning@+1{{Excessive padding in 'struct SmallArrayInFunc'}}
227 struct SmallArrayInFunc {
228   char c1;
229   short s;
230   char c2;
231 };
232 
233 void arrayHolder() {
234   struct SmallArrayInFunc Arr[15];
235 }
236 
237 // xxxexpected-warning@+1{{Excessive padding in 'struct SmallArrayInStruct'}}
238 struct SmallArrayInStruct {
239   char c1;
240   short s;
241   char c2;
242 };
243 
244 struct HoldsSmallArray {
245   struct SmallArrayInStruct Field[20];
246 } HoldsSmallArrayElt;
247 
248 void nestedPadding() {
249   struct HoldsSmallArray Arr[15];
250 }
251