1 //=== - llvm/unittest/Support/AlignOfTest.cpp - Alignment utility tests ---===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifdef _MSC_VER
11 // Disable warnings about alignment-based structure padding.
12 // This must be above the includes to suppress warnings in included templates.
13 #pragma warning(disable:4324)
14 #endif
15
16 #include "llvm/Support/AlignOf.h"
17 #include "llvm/Support/Compiler.h"
18 #include "gtest/gtest.h"
19
20 using namespace llvm;
21
22 namespace {
23 // Disable warnings about questionable type definitions.
24 // We're testing that even questionable types work with the alignment utilities.
25 #ifdef _MSC_VER
26 #pragma warning(disable:4584)
27 #endif
28
29 // Suppress direct base '{anonymous}::S1' inaccessible in '{anonymous}::D9'
30 // due to ambiguity warning.
31 #ifdef __clang__
32 #pragma clang diagnostic ignored "-Wunknown-pragmas"
33 #pragma clang diagnostic ignored "-Winaccessible-base"
34 #elif ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
35 // Pragma based warning suppression was introduced in GGC 4.2. Additionally
36 // this warning is "enabled by default". The warning still appears if -Wall is
37 // suppressed. Apparently GCC suppresses it when -w is specifed, which is odd.
38 #pragma GCC diagnostic warning "-w"
39 #endif
40
41 // Define some fixed alignment types to use in these tests.
42 struct LLVM_ALIGNAS(1) A1 {};
43 struct LLVM_ALIGNAS(2) A2 {};
44 struct LLVM_ALIGNAS(4) A4 {};
45 struct LLVM_ALIGNAS(8) A8 {};
46
47 struct S1 {};
48 struct S2 { char a; };
49 struct S3 { int x; };
50 struct S4 { double y; };
51 struct S5 { A1 a1; A2 a2; A4 a4; A8 a8; };
52 struct S6 { double f(); };
53 struct D1 : S1 {};
54 struct D2 : S6 { float g(); };
55 struct D3 : S2 {};
56 struct D4 : S2 { int x; };
57 struct D5 : S3 { char c; };
58 struct D6 : S2, S3 {};
59 struct D7 : S1, S3 {};
60 struct D8 : S1, D4, D5 { double x[2]; };
61 struct D9 : S1, D1 { S1 s1; };
62 struct V1 { virtual ~V1(); };
63 struct V2 { int x; virtual ~V2(); };
64 struct V3 : V1 {
65 ~V3() override;
66 };
67 struct V4 : virtual V2 { int y;
68 ~V4() override;
69 };
70 struct V5 : V4, V3 { double z;
71 ~V5() override;
72 };
73 struct V6 : S1 { virtual ~V6(); };
74 struct V7 : virtual V2, virtual V6 {
75 ~V7() override;
76 };
77 struct V8 : V5, virtual V6, V7 { double zz;
78 ~V8() override;
79 };
80
f()81 double S6::f() { return 0.0; }
g()82 float D2::g() { return 0.0f; }
~V1()83 V1::~V1() {}
~V2()84 V2::~V2() {}
~V3()85 V3::~V3() {}
~V4()86 V4::~V4() {}
~V5()87 V5::~V5() {}
~V6()88 V6::~V6() {}
~V7()89 V7::~V7() {}
~V8()90 V8::~V8() {}
91
92 struct Abstract1 {
~Abstract1__anon091a585f0111::Abstract193 virtual ~Abstract1() {}
94 virtual void method() = 0;
95
96 char c;
97 };
98
99 struct Abstract2 : Abstract1 {
100 ~Abstract2() override = default;
101 double d;
102 };
103
104 struct Final final : Abstract2 {
method__anon091a585f0111::Final105 void method() override {}
106 };
107
108 // Ensure alignment is a compile-time constant.
109 char LLVM_ATTRIBUTE_UNUSED test_arr1
110 [AlignOf<char>::Alignment > 0]
111 [AlignOf<short>::Alignment > 0]
112 [AlignOf<int>::Alignment > 0]
113 [AlignOf<long>::Alignment > 0]
114 [AlignOf<long long>::Alignment > 0]
115 [AlignOf<float>::Alignment > 0]
116 [AlignOf<double>::Alignment > 0]
117 [AlignOf<long double>::Alignment > 0]
118 [AlignOf<void *>::Alignment > 0]
119 [AlignOf<int *>::Alignment > 0]
120 [AlignOf<double (*)(double)>::Alignment > 0]
121 [AlignOf<double (S6::*)()>::Alignment > 0];
122 char LLVM_ATTRIBUTE_UNUSED test_arr2
123 [AlignOf<A1>::Alignment > 0]
124 [AlignOf<A2>::Alignment > 0]
125 [AlignOf<A4>::Alignment > 0]
126 [AlignOf<A8>::Alignment > 0];
127 char LLVM_ATTRIBUTE_UNUSED test_arr3
128 [AlignOf<S1>::Alignment > 0]
129 [AlignOf<S2>::Alignment > 0]
130 [AlignOf<S3>::Alignment > 0]
131 [AlignOf<S4>::Alignment > 0]
132 [AlignOf<S5>::Alignment > 0]
133 [AlignOf<S6>::Alignment > 0];
134 char LLVM_ATTRIBUTE_UNUSED test_arr4
135 [AlignOf<D1>::Alignment > 0]
136 [AlignOf<D2>::Alignment > 0]
137 [AlignOf<D3>::Alignment > 0]
138 [AlignOf<D4>::Alignment > 0]
139 [AlignOf<D5>::Alignment > 0]
140 [AlignOf<D6>::Alignment > 0]
141 [AlignOf<D7>::Alignment > 0]
142 [AlignOf<D8>::Alignment > 0]
143 [AlignOf<D9>::Alignment > 0];
144 char LLVM_ATTRIBUTE_UNUSED test_arr5
145 [AlignOf<V1>::Alignment > 0]
146 [AlignOf<V2>::Alignment > 0]
147 [AlignOf<V3>::Alignment > 0]
148 [AlignOf<V4>::Alignment > 0]
149 [AlignOf<V5>::Alignment > 0]
150 [AlignOf<V6>::Alignment > 0]
151 [AlignOf<V7>::Alignment > 0]
152 [AlignOf<V8>::Alignment > 0];
153
TEST(AlignOfTest,BasicAlignmentInvariants)154 TEST(AlignOfTest, BasicAlignmentInvariants) {
155 EXPECT_LE(1u, alignOf<A1>());
156 EXPECT_LE(2u, alignOf<A2>());
157 EXPECT_LE(4u, alignOf<A4>());
158 EXPECT_LE(8u, alignOf<A8>());
159
160 EXPECT_EQ(1u, alignOf<char>());
161 EXPECT_LE(alignOf<char>(), alignOf<short>());
162 EXPECT_LE(alignOf<short>(), alignOf<int>());
163 EXPECT_LE(alignOf<int>(), alignOf<long>());
164 EXPECT_LE(alignOf<long>(), alignOf<long long>());
165 EXPECT_LE(alignOf<char>(), alignOf<float>());
166 EXPECT_LE(alignOf<float>(), alignOf<double>());
167 EXPECT_LE(alignOf<char>(), alignOf<long double>());
168 EXPECT_LE(alignOf<char>(), alignOf<void *>());
169 EXPECT_EQ(alignOf<void *>(), alignOf<int *>());
170 EXPECT_LE(alignOf<char>(), alignOf<S1>());
171 EXPECT_LE(alignOf<S1>(), alignOf<S2>());
172 EXPECT_LE(alignOf<S1>(), alignOf<S3>());
173 EXPECT_LE(alignOf<S1>(), alignOf<S4>());
174 EXPECT_LE(alignOf<S1>(), alignOf<S5>());
175 EXPECT_LE(alignOf<S1>(), alignOf<S6>());
176 EXPECT_LE(alignOf<S1>(), alignOf<D1>());
177 EXPECT_LE(alignOf<S1>(), alignOf<D2>());
178 EXPECT_LE(alignOf<S1>(), alignOf<D3>());
179 EXPECT_LE(alignOf<S1>(), alignOf<D4>());
180 EXPECT_LE(alignOf<S1>(), alignOf<D5>());
181 EXPECT_LE(alignOf<S1>(), alignOf<D6>());
182 EXPECT_LE(alignOf<S1>(), alignOf<D7>());
183 EXPECT_LE(alignOf<S1>(), alignOf<D8>());
184 EXPECT_LE(alignOf<S1>(), alignOf<D9>());
185 EXPECT_LE(alignOf<S1>(), alignOf<V1>());
186 EXPECT_LE(alignOf<V1>(), alignOf<V2>());
187 EXPECT_LE(alignOf<V1>(), alignOf<V3>());
188 EXPECT_LE(alignOf<V1>(), alignOf<V4>());
189 EXPECT_LE(alignOf<V1>(), alignOf<V5>());
190 EXPECT_LE(alignOf<V1>(), alignOf<V6>());
191 EXPECT_LE(alignOf<V1>(), alignOf<V7>());
192 EXPECT_LE(alignOf<V1>(), alignOf<V8>());
193
194 EXPECT_LE(alignOf<char>(), alignOf<Abstract1>());
195 EXPECT_LE(alignOf<double>(), alignOf<Abstract2>());
196 EXPECT_LE(alignOf<Abstract2>(), alignOf<Final>());
197 }
198
TEST(AlignOfTest,BasicAlignedArray)199 TEST(AlignOfTest, BasicAlignedArray) {
200 EXPECT_LE(1u, alignOf<AlignedCharArrayUnion<A1> >());
201 EXPECT_LE(2u, alignOf<AlignedCharArrayUnion<A2> >());
202 EXPECT_LE(4u, alignOf<AlignedCharArrayUnion<A4> >());
203 EXPECT_LE(8u, alignOf<AlignedCharArrayUnion<A8> >());
204
205 EXPECT_LE(1u, sizeof(AlignedCharArrayUnion<A1>));
206 EXPECT_LE(2u, sizeof(AlignedCharArrayUnion<A2>));
207 EXPECT_LE(4u, sizeof(AlignedCharArrayUnion<A4>));
208 EXPECT_LE(8u, sizeof(AlignedCharArrayUnion<A8>));
209
210 EXPECT_EQ(1u, (alignOf<AlignedCharArrayUnion<A1> >()));
211 EXPECT_EQ(2u, (alignOf<AlignedCharArrayUnion<A1, A2> >()));
212 EXPECT_EQ(4u, (alignOf<AlignedCharArrayUnion<A1, A2, A4> >()));
213 EXPECT_EQ(8u, (alignOf<AlignedCharArrayUnion<A1, A2, A4, A8> >()));
214
215 EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<A1>));
216 EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<A1, A2>));
217 EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<A1, A2, A4>));
218 EXPECT_EQ(8u, sizeof(AlignedCharArrayUnion<A1, A2, A4, A8>));
219
220 EXPECT_EQ(1u, (alignOf<AlignedCharArrayUnion<A1[1]> >()));
221 EXPECT_EQ(2u, (alignOf<AlignedCharArrayUnion<A1[2], A2[1]> >()));
222 EXPECT_EQ(4u, (alignOf<AlignedCharArrayUnion<A1[42], A2[55],
223 A4[13]> >()));
224 EXPECT_EQ(8u, (alignOf<AlignedCharArrayUnion<A1[2], A2[1],
225 A4, A8> >()));
226
227 EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<A1[1]>));
228 EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<A1[2], A2[1]>));
229 EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<A1[3], A2[2], A4>));
230 EXPECT_EQ(16u, sizeof(AlignedCharArrayUnion<A1, A2[3],
231 A4[3], A8>));
232
233 // For other tests we simply assert that the alignment of the union mathes
234 // that of the fundamental type and hope that we have any weird type
235 // productions that would trigger bugs.
236 EXPECT_EQ(alignOf<char>(), alignOf<AlignedCharArrayUnion<char> >());
237 EXPECT_EQ(alignOf<short>(), alignOf<AlignedCharArrayUnion<short> >());
238 EXPECT_EQ(alignOf<int>(), alignOf<AlignedCharArrayUnion<int> >());
239 EXPECT_EQ(alignOf<long>(), alignOf<AlignedCharArrayUnion<long> >());
240 EXPECT_EQ(alignOf<long long>(),
241 alignOf<AlignedCharArrayUnion<long long> >());
242 EXPECT_EQ(alignOf<float>(), alignOf<AlignedCharArrayUnion<float> >());
243 EXPECT_EQ(alignOf<double>(), alignOf<AlignedCharArrayUnion<double> >());
244 EXPECT_EQ(alignOf<long double>(),
245 alignOf<AlignedCharArrayUnion<long double> >());
246 EXPECT_EQ(alignOf<void *>(), alignOf<AlignedCharArrayUnion<void *> >());
247 EXPECT_EQ(alignOf<int *>(), alignOf<AlignedCharArrayUnion<int *> >());
248 EXPECT_EQ(alignOf<double (*)(double)>(),
249 alignOf<AlignedCharArrayUnion<double (*)(double)> >());
250 EXPECT_EQ(alignOf<double (S6::*)()>(),
251 alignOf<AlignedCharArrayUnion<double (S6::*)()> >());
252 EXPECT_EQ(alignOf<S1>(), alignOf<AlignedCharArrayUnion<S1> >());
253 EXPECT_EQ(alignOf<S2>(), alignOf<AlignedCharArrayUnion<S2> >());
254 EXPECT_EQ(alignOf<S3>(), alignOf<AlignedCharArrayUnion<S3> >());
255 EXPECT_EQ(alignOf<S4>(), alignOf<AlignedCharArrayUnion<S4> >());
256 EXPECT_EQ(alignOf<S5>(), alignOf<AlignedCharArrayUnion<S5> >());
257 EXPECT_EQ(alignOf<S6>(), alignOf<AlignedCharArrayUnion<S6> >());
258 EXPECT_EQ(alignOf<D1>(), alignOf<AlignedCharArrayUnion<D1> >());
259 EXPECT_EQ(alignOf<D2>(), alignOf<AlignedCharArrayUnion<D2> >());
260 EXPECT_EQ(alignOf<D3>(), alignOf<AlignedCharArrayUnion<D3> >());
261 EXPECT_EQ(alignOf<D4>(), alignOf<AlignedCharArrayUnion<D4> >());
262 EXPECT_EQ(alignOf<D5>(), alignOf<AlignedCharArrayUnion<D5> >());
263 EXPECT_EQ(alignOf<D6>(), alignOf<AlignedCharArrayUnion<D6> >());
264 EXPECT_EQ(alignOf<D7>(), alignOf<AlignedCharArrayUnion<D7> >());
265 EXPECT_EQ(alignOf<D8>(), alignOf<AlignedCharArrayUnion<D8> >());
266 EXPECT_EQ(alignOf<D9>(), alignOf<AlignedCharArrayUnion<D9> >());
267 EXPECT_EQ(alignOf<V1>(), alignOf<AlignedCharArrayUnion<V1> >());
268 EXPECT_EQ(alignOf<V2>(), alignOf<AlignedCharArrayUnion<V2> >());
269 EXPECT_EQ(alignOf<V3>(), alignOf<AlignedCharArrayUnion<V3> >());
270 EXPECT_EQ(alignOf<V4>(), alignOf<AlignedCharArrayUnion<V4> >());
271 EXPECT_EQ(alignOf<V5>(), alignOf<AlignedCharArrayUnion<V5> >());
272 EXPECT_EQ(alignOf<V6>(), alignOf<AlignedCharArrayUnion<V6> >());
273 EXPECT_EQ(alignOf<V7>(), alignOf<AlignedCharArrayUnion<V7> >());
274
275 // Some versions of MSVC get this wrong somewhat disturbingly. The failure
276 // appears to be benign: alignOf<V8>() produces a preposterous value: 12
277 #ifndef _MSC_VER
278 EXPECT_EQ(alignOf<V8>(), alignOf<AlignedCharArrayUnion<V8> >());
279 #endif
280
281 EXPECT_EQ(sizeof(char), sizeof(AlignedCharArrayUnion<char>));
282 EXPECT_EQ(sizeof(char[1]), sizeof(AlignedCharArrayUnion<char[1]>));
283 EXPECT_EQ(sizeof(char[2]), sizeof(AlignedCharArrayUnion<char[2]>));
284 EXPECT_EQ(sizeof(char[3]), sizeof(AlignedCharArrayUnion<char[3]>));
285 EXPECT_EQ(sizeof(char[4]), sizeof(AlignedCharArrayUnion<char[4]>));
286 EXPECT_EQ(sizeof(char[5]), sizeof(AlignedCharArrayUnion<char[5]>));
287 EXPECT_EQ(sizeof(char[8]), sizeof(AlignedCharArrayUnion<char[8]>));
288 EXPECT_EQ(sizeof(char[13]), sizeof(AlignedCharArrayUnion<char[13]>));
289 EXPECT_EQ(sizeof(char[16]), sizeof(AlignedCharArrayUnion<char[16]>));
290 EXPECT_EQ(sizeof(char[21]), sizeof(AlignedCharArrayUnion<char[21]>));
291 EXPECT_EQ(sizeof(char[32]), sizeof(AlignedCharArrayUnion<char[32]>));
292 EXPECT_EQ(sizeof(short), sizeof(AlignedCharArrayUnion<short>));
293 EXPECT_EQ(sizeof(int), sizeof(AlignedCharArrayUnion<int>));
294 EXPECT_EQ(sizeof(long), sizeof(AlignedCharArrayUnion<long>));
295 EXPECT_EQ(sizeof(long long),
296 sizeof(AlignedCharArrayUnion<long long>));
297 EXPECT_EQ(sizeof(float), sizeof(AlignedCharArrayUnion<float>));
298 EXPECT_EQ(sizeof(double), sizeof(AlignedCharArrayUnion<double>));
299 EXPECT_EQ(sizeof(long double),
300 sizeof(AlignedCharArrayUnion<long double>));
301 EXPECT_EQ(sizeof(void *), sizeof(AlignedCharArrayUnion<void *>));
302 EXPECT_EQ(sizeof(int *), sizeof(AlignedCharArrayUnion<int *>));
303 EXPECT_EQ(sizeof(double (*)(double)),
304 sizeof(AlignedCharArrayUnion<double (*)(double)>));
305 EXPECT_EQ(sizeof(double (S6::*)()),
306 sizeof(AlignedCharArrayUnion<double (S6::*)()>));
307 EXPECT_EQ(sizeof(S1), sizeof(AlignedCharArrayUnion<S1>));
308 EXPECT_EQ(sizeof(S2), sizeof(AlignedCharArrayUnion<S2>));
309 EXPECT_EQ(sizeof(S3), sizeof(AlignedCharArrayUnion<S3>));
310 EXPECT_EQ(sizeof(S4), sizeof(AlignedCharArrayUnion<S4>));
311 EXPECT_EQ(sizeof(S5), sizeof(AlignedCharArrayUnion<S5>));
312 EXPECT_EQ(sizeof(S6), sizeof(AlignedCharArrayUnion<S6>));
313 EXPECT_EQ(sizeof(D1), sizeof(AlignedCharArrayUnion<D1>));
314 EXPECT_EQ(sizeof(D2), sizeof(AlignedCharArrayUnion<D2>));
315 EXPECT_EQ(sizeof(D3), sizeof(AlignedCharArrayUnion<D3>));
316 EXPECT_EQ(sizeof(D4), sizeof(AlignedCharArrayUnion<D4>));
317 EXPECT_EQ(sizeof(D5), sizeof(AlignedCharArrayUnion<D5>));
318 EXPECT_EQ(sizeof(D6), sizeof(AlignedCharArrayUnion<D6>));
319 EXPECT_EQ(sizeof(D7), sizeof(AlignedCharArrayUnion<D7>));
320 EXPECT_EQ(sizeof(D8), sizeof(AlignedCharArrayUnion<D8>));
321 EXPECT_EQ(sizeof(D9), sizeof(AlignedCharArrayUnion<D9>));
322 EXPECT_EQ(sizeof(D9[1]), sizeof(AlignedCharArrayUnion<D9[1]>));
323 EXPECT_EQ(sizeof(D9[2]), sizeof(AlignedCharArrayUnion<D9[2]>));
324 EXPECT_EQ(sizeof(D9[3]), sizeof(AlignedCharArrayUnion<D9[3]>));
325 EXPECT_EQ(sizeof(D9[4]), sizeof(AlignedCharArrayUnion<D9[4]>));
326 EXPECT_EQ(sizeof(D9[5]), sizeof(AlignedCharArrayUnion<D9[5]>));
327 EXPECT_EQ(sizeof(D9[8]), sizeof(AlignedCharArrayUnion<D9[8]>));
328 EXPECT_EQ(sizeof(D9[13]), sizeof(AlignedCharArrayUnion<D9[13]>));
329 EXPECT_EQ(sizeof(D9[16]), sizeof(AlignedCharArrayUnion<D9[16]>));
330 EXPECT_EQ(sizeof(D9[21]), sizeof(AlignedCharArrayUnion<D9[21]>));
331 EXPECT_EQ(sizeof(D9[32]), sizeof(AlignedCharArrayUnion<D9[32]>));
332 EXPECT_EQ(sizeof(V1), sizeof(AlignedCharArrayUnion<V1>));
333 EXPECT_EQ(sizeof(V2), sizeof(AlignedCharArrayUnion<V2>));
334 EXPECT_EQ(sizeof(V3), sizeof(AlignedCharArrayUnion<V3>));
335 EXPECT_EQ(sizeof(V4), sizeof(AlignedCharArrayUnion<V4>));
336 EXPECT_EQ(sizeof(V5), sizeof(AlignedCharArrayUnion<V5>));
337 EXPECT_EQ(sizeof(V6), sizeof(AlignedCharArrayUnion<V6>));
338 EXPECT_EQ(sizeof(V7), sizeof(AlignedCharArrayUnion<V7>));
339
340 // Some versions of MSVC also get this wrong. The failure again appears to be
341 // benign: sizeof(V8) is only 52 bytes, but our array reserves 56.
342 #ifndef _MSC_VER
343 EXPECT_EQ(sizeof(V8), sizeof(AlignedCharArrayUnion<V8>));
344 #endif
345
346 EXPECT_EQ(1u, (alignOf<AlignedCharArray<1, 1> >()));
347 EXPECT_EQ(2u, (alignOf<AlignedCharArray<2, 1> >()));
348 EXPECT_EQ(4u, (alignOf<AlignedCharArray<4, 1> >()));
349 EXPECT_EQ(8u, (alignOf<AlignedCharArray<8, 1> >()));
350 EXPECT_EQ(16u, (alignOf<AlignedCharArray<16, 1> >()));
351
352 EXPECT_EQ(1u, sizeof(AlignedCharArray<1, 1>));
353 EXPECT_EQ(7u, sizeof(AlignedCharArray<1, 7>));
354 EXPECT_EQ(2u, sizeof(AlignedCharArray<2, 2>));
355 EXPECT_EQ(16u, sizeof(AlignedCharArray<2, 16>));
356 }
357 } // end anonymous namespace
358