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 #include "llvm/Support/AlignOf.h"
11 #include "llvm/Support/Compiler.h"
12
13 #include "gtest/gtest.h"
14
15 using namespace llvm;
16
17 namespace {
18
19 // Disable warnings about questionable type definitions.
20 // We're testing that even questionable types work with the alignment utilities.
21 #ifdef _MSC_VER
22 #pragma warning(disable:4584)
23 #endif
24
25 // Define some fixed alignment types to use in these tests.
26 #if __cplusplus == 201103L || __has_feature(cxx_alignas)
27 typedef char alignas(1) A1;
28 typedef char alignas(2) A2;
29 typedef char alignas(4) A4;
30 typedef char alignas(8) A8;
31 #elif defined(__clang__) || defined(__GNUC__)
32 typedef char A1 __attribute__((aligned(1)));
33 typedef char A2 __attribute__((aligned(2)));
34 typedef char A4 __attribute__((aligned(4)));
35 typedef char A8 __attribute__((aligned(8)));
36 #elif defined(_MSC_VER)
37 typedef __declspec(align(1)) char A1;
38 typedef __declspec(align(2)) char A2;
39 typedef __declspec(align(4)) char A4;
40 typedef __declspec(align(8)) char A8;
41 #else
42 # error No supported align as directive.
43 #endif
44
45 // Wrap the forced aligned types in structs to hack around compiler bugs.
46 struct SA1 { A1 a; };
47 struct SA2 { A2 a; };
48 struct SA4 { A4 a; };
49 struct SA8 { A8 a; };
50
51 struct S1 {};
52 struct S2 { char a; };
53 struct S3 { int x; };
54 struct S4 { double y; };
55 struct S5 { A1 a1; A2 a2; A4 a4; A8 a8; };
56 struct S6 { double f(); };
57 struct D1 : S1 {};
58 struct D2 : S6 { float g(); };
59 struct D3 : S2 {};
60 struct D4 : S2 { int x; };
61 struct D5 : S3 { char c; };
62 struct D6 : S2, S3 {};
63 struct D7 : S1, S3 {};
64 struct D8 : S1, D4, D5 { double x[2]; };
65 struct D9 : S1, D1 { S1 s1; };
66 struct V1 { virtual ~V1(); };
67 struct V2 { int x; virtual ~V2(); };
68 struct V3 : V1 { virtual ~V3(); };
69 struct V4 : virtual V2 { int y; virtual ~V4(); };
70 struct V5 : V4, V3 { double z; virtual ~V5(); };
71 struct V6 : S1 { virtual ~V6(); };
72 struct V7 : virtual V2, virtual V6 { virtual ~V7(); };
73 struct V8 : V5, virtual V6, V7 { double zz; virtual ~V8(); };
74
75 // Ensure alignment is a compile-time constant.
76 char LLVM_ATTRIBUTE_UNUSED test_arr1
77 [AlignOf<char>::Alignment > 0]
78 [AlignOf<short>::Alignment > 0]
79 [AlignOf<int>::Alignment > 0]
80 [AlignOf<long>::Alignment > 0]
81 [AlignOf<long long>::Alignment > 0]
82 [AlignOf<float>::Alignment > 0]
83 [AlignOf<double>::Alignment > 0]
84 [AlignOf<long double>::Alignment > 0]
85 [AlignOf<void *>::Alignment > 0]
86 [AlignOf<int *>::Alignment > 0]
87 [AlignOf<double (*)(double)>::Alignment > 0]
88 [AlignOf<double (S6::*)()>::Alignment > 0];
89 char LLVM_ATTRIBUTE_UNUSED test_arr2
90 [AlignOf<A1>::Alignment > 0]
91 [AlignOf<A2>::Alignment > 0]
92 [AlignOf<A4>::Alignment > 0]
93 [AlignOf<A8>::Alignment > 0]
94 [AlignOf<SA1>::Alignment > 0]
95 [AlignOf<SA2>::Alignment > 0]
96 [AlignOf<SA4>::Alignment > 0]
97 [AlignOf<SA8>::Alignment > 0];
98 char LLVM_ATTRIBUTE_UNUSED test_arr3
99 [AlignOf<S1>::Alignment > 0]
100 [AlignOf<S2>::Alignment > 0]
101 [AlignOf<S3>::Alignment > 0]
102 [AlignOf<S4>::Alignment > 0]
103 [AlignOf<S5>::Alignment > 0]
104 [AlignOf<S6>::Alignment > 0];
105 char LLVM_ATTRIBUTE_UNUSED test_arr4
106 [AlignOf<D1>::Alignment > 0]
107 [AlignOf<D2>::Alignment > 0]
108 [AlignOf<D3>::Alignment > 0]
109 [AlignOf<D4>::Alignment > 0]
110 [AlignOf<D5>::Alignment > 0]
111 [AlignOf<D6>::Alignment > 0]
112 [AlignOf<D7>::Alignment > 0]
113 [AlignOf<D8>::Alignment > 0]
114 [AlignOf<D9>::Alignment > 0];
115 char LLVM_ATTRIBUTE_UNUSED test_arr5
116 [AlignOf<V1>::Alignment > 0]
117 [AlignOf<V2>::Alignment > 0]
118 [AlignOf<V3>::Alignment > 0]
119 [AlignOf<V4>::Alignment > 0]
120 [AlignOf<V5>::Alignment > 0]
121 [AlignOf<V6>::Alignment > 0]
122 [AlignOf<V7>::Alignment > 0]
123 [AlignOf<V8>::Alignment > 0];
124
TEST(AlignOfTest,BasicAlignmentInvariants)125 TEST(AlignOfTest, BasicAlignmentInvariants) {
126 // For a very strange reason, many compilers do not support this. Both Clang
127 // and GCC fail to align these properly.
128 EXPECT_EQ(1u, alignOf<A1>());
129 #if 0
130 EXPECT_EQ(2u, alignOf<A2>());
131 EXPECT_EQ(4u, alignOf<A4>());
132 EXPECT_EQ(8u, alignOf<A8>());
133 #endif
134
135 // But once wrapped in structs, the alignment is correctly managed.
136 EXPECT_LE(1u, alignOf<SA1>());
137 EXPECT_LE(2u, alignOf<SA2>());
138 EXPECT_LE(4u, alignOf<SA4>());
139 EXPECT_LE(8u, alignOf<SA8>());
140
141 EXPECT_EQ(1u, alignOf<char>());
142 EXPECT_LE(alignOf<char>(), alignOf<short>());
143 EXPECT_LE(alignOf<short>(), alignOf<int>());
144 EXPECT_LE(alignOf<int>(), alignOf<long>());
145 EXPECT_LE(alignOf<long>(), alignOf<long long>());
146 EXPECT_LE(alignOf<char>(), alignOf<float>());
147 EXPECT_LE(alignOf<float>(), alignOf<double>());
148 EXPECT_LE(alignOf<char>(), alignOf<long double>());
149 EXPECT_LE(alignOf<char>(), alignOf<void *>());
150 EXPECT_EQ(alignOf<void *>(), alignOf<int *>());
151 EXPECT_LE(alignOf<char>(), alignOf<S1>());
152 EXPECT_LE(alignOf<S1>(), alignOf<S2>());
153 EXPECT_LE(alignOf<S1>(), alignOf<S3>());
154 EXPECT_LE(alignOf<S1>(), alignOf<S4>());
155 EXPECT_LE(alignOf<S1>(), alignOf<S5>());
156 EXPECT_LE(alignOf<S1>(), alignOf<S6>());
157 EXPECT_LE(alignOf<S1>(), alignOf<D1>());
158 EXPECT_LE(alignOf<S1>(), alignOf<D2>());
159 EXPECT_LE(alignOf<S1>(), alignOf<D3>());
160 EXPECT_LE(alignOf<S1>(), alignOf<D4>());
161 EXPECT_LE(alignOf<S1>(), alignOf<D5>());
162 EXPECT_LE(alignOf<S1>(), alignOf<D6>());
163 EXPECT_LE(alignOf<S1>(), alignOf<D7>());
164 EXPECT_LE(alignOf<S1>(), alignOf<D8>());
165 EXPECT_LE(alignOf<S1>(), alignOf<D9>());
166 EXPECT_LE(alignOf<S1>(), alignOf<V1>());
167 EXPECT_LE(alignOf<V1>(), alignOf<V2>());
168 EXPECT_LE(alignOf<V1>(), alignOf<V3>());
169 EXPECT_LE(alignOf<V1>(), alignOf<V4>());
170 EXPECT_LE(alignOf<V1>(), alignOf<V5>());
171 EXPECT_LE(alignOf<V1>(), alignOf<V6>());
172 EXPECT_LE(alignOf<V1>(), alignOf<V7>());
173 EXPECT_LE(alignOf<V1>(), alignOf<V8>());
174 }
175
TEST(AlignOfTest,BasicAlignedArray)176 TEST(AlignOfTest, BasicAlignedArray) {
177 // Note: this code exclusively uses the struct-wrapped arbitrarily aligned
178 // types because of the bugs mentioned above where GCC and Clang both
179 // disregard the arbitrary alignment specifier until the type is used to
180 // declare a member of a struct.
181 EXPECT_LE(1u, alignOf<AlignedCharArrayUnion<SA1> >());
182 EXPECT_LE(2u, alignOf<AlignedCharArrayUnion<SA2> >());
183 EXPECT_LE(4u, alignOf<AlignedCharArrayUnion<SA4> >());
184 EXPECT_LE(8u, alignOf<AlignedCharArrayUnion<SA8> >());
185
186 EXPECT_LE(1u, sizeof(AlignedCharArrayUnion<SA1>));
187 EXPECT_LE(2u, sizeof(AlignedCharArrayUnion<SA2>));
188 EXPECT_LE(4u, sizeof(AlignedCharArrayUnion<SA4>));
189 EXPECT_LE(8u, sizeof(AlignedCharArrayUnion<SA8>));
190
191 EXPECT_EQ(1u, (alignOf<AlignedCharArrayUnion<SA1> >()));
192 EXPECT_EQ(2u, (alignOf<AlignedCharArrayUnion<SA1, SA2> >()));
193 EXPECT_EQ(4u, (alignOf<AlignedCharArrayUnion<SA1, SA2, SA4> >()));
194 EXPECT_EQ(8u, (alignOf<AlignedCharArrayUnion<SA1, SA2, SA4, SA8> >()));
195
196 EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<SA1>));
197 EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<SA1, SA2>));
198 EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<SA1, SA2, SA4>));
199 EXPECT_EQ(8u, sizeof(AlignedCharArrayUnion<SA1, SA2, SA4, SA8>));
200
201 EXPECT_EQ(1u, (alignOf<AlignedCharArrayUnion<SA1[1]> >()));
202 EXPECT_EQ(2u, (alignOf<AlignedCharArrayUnion<SA1[2], SA2[1]> >()));
203 EXPECT_EQ(4u, (alignOf<AlignedCharArrayUnion<SA1[42], SA2[55],
204 SA4[13]> >()));
205 EXPECT_EQ(8u, (alignOf<AlignedCharArrayUnion<SA1[2], SA2[1],
206 SA4, SA8> >()));
207
208 EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<SA1[1]>));
209 EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<SA1[2], SA2[1]>));
210 EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<SA1[3], SA2[2], SA4>));
211 EXPECT_EQ(16u, sizeof(AlignedCharArrayUnion<SA1, SA2[3],
212 SA4[3], SA8>));
213
214 // For other tests we simply assert that the alignment of the union mathes
215 // that of the fundamental type and hope that we have any weird type
216 // productions that would trigger bugs.
217 EXPECT_EQ(alignOf<char>(), alignOf<AlignedCharArrayUnion<char> >());
218 EXPECT_EQ(alignOf<short>(), alignOf<AlignedCharArrayUnion<short> >());
219 EXPECT_EQ(alignOf<int>(), alignOf<AlignedCharArrayUnion<int> >());
220 EXPECT_EQ(alignOf<long>(), alignOf<AlignedCharArrayUnion<long> >());
221 EXPECT_EQ(alignOf<long long>(),
222 alignOf<AlignedCharArrayUnion<long long> >());
223 EXPECT_EQ(alignOf<float>(), alignOf<AlignedCharArrayUnion<float> >());
224 EXPECT_EQ(alignOf<double>(), alignOf<AlignedCharArrayUnion<double> >());
225 EXPECT_EQ(alignOf<long double>(),
226 alignOf<AlignedCharArrayUnion<long double> >());
227 EXPECT_EQ(alignOf<void *>(), alignOf<AlignedCharArrayUnion<void *> >());
228 EXPECT_EQ(alignOf<int *>(), alignOf<AlignedCharArrayUnion<int *> >());
229 EXPECT_EQ(alignOf<double (*)(double)>(),
230 alignOf<AlignedCharArrayUnion<double (*)(double)> >());
231 EXPECT_EQ(alignOf<double (S6::*)()>(),
232 alignOf<AlignedCharArrayUnion<double (S6::*)()> >());
233 EXPECT_EQ(alignOf<S1>(), alignOf<AlignedCharArrayUnion<S1> >());
234 EXPECT_EQ(alignOf<S2>(), alignOf<AlignedCharArrayUnion<S2> >());
235 EXPECT_EQ(alignOf<S3>(), alignOf<AlignedCharArrayUnion<S3> >());
236 EXPECT_EQ(alignOf<S4>(), alignOf<AlignedCharArrayUnion<S4> >());
237 EXPECT_EQ(alignOf<S5>(), alignOf<AlignedCharArrayUnion<S5> >());
238 EXPECT_EQ(alignOf<S6>(), alignOf<AlignedCharArrayUnion<S6> >());
239 EXPECT_EQ(alignOf<D1>(), alignOf<AlignedCharArrayUnion<D1> >());
240 EXPECT_EQ(alignOf<D2>(), alignOf<AlignedCharArrayUnion<D2> >());
241 EXPECT_EQ(alignOf<D3>(), alignOf<AlignedCharArrayUnion<D3> >());
242 EXPECT_EQ(alignOf<D4>(), alignOf<AlignedCharArrayUnion<D4> >());
243 EXPECT_EQ(alignOf<D5>(), alignOf<AlignedCharArrayUnion<D5> >());
244 EXPECT_EQ(alignOf<D6>(), alignOf<AlignedCharArrayUnion<D6> >());
245 EXPECT_EQ(alignOf<D7>(), alignOf<AlignedCharArrayUnion<D7> >());
246 EXPECT_EQ(alignOf<D8>(), alignOf<AlignedCharArrayUnion<D8> >());
247 EXPECT_EQ(alignOf<D9>(), alignOf<AlignedCharArrayUnion<D9> >());
248 EXPECT_EQ(alignOf<V1>(), alignOf<AlignedCharArrayUnion<V1> >());
249 EXPECT_EQ(alignOf<V2>(), alignOf<AlignedCharArrayUnion<V2> >());
250 EXPECT_EQ(alignOf<V3>(), alignOf<AlignedCharArrayUnion<V3> >());
251 EXPECT_EQ(alignOf<V4>(), alignOf<AlignedCharArrayUnion<V4> >());
252 EXPECT_EQ(alignOf<V5>(), alignOf<AlignedCharArrayUnion<V5> >());
253 EXPECT_EQ(alignOf<V6>(), alignOf<AlignedCharArrayUnion<V6> >());
254 EXPECT_EQ(alignOf<V7>(), alignOf<AlignedCharArrayUnion<V7> >());
255
256 // Some versions of MSVC get this wrong somewhat disturbingly. The failure
257 // appears to be benign: alignOf<V8>() produces a preposterous value: 12
258 #ifndef _MSC_VER
259 EXPECT_EQ(alignOf<V8>(), alignOf<AlignedCharArrayUnion<V8> >());
260 #endif
261
262 EXPECT_EQ(sizeof(char), sizeof(AlignedCharArrayUnion<char>));
263 EXPECT_EQ(sizeof(char[1]), sizeof(AlignedCharArrayUnion<char[1]>));
264 EXPECT_EQ(sizeof(char[2]), sizeof(AlignedCharArrayUnion<char[2]>));
265 EXPECT_EQ(sizeof(char[3]), sizeof(AlignedCharArrayUnion<char[3]>));
266 EXPECT_EQ(sizeof(char[4]), sizeof(AlignedCharArrayUnion<char[4]>));
267 EXPECT_EQ(sizeof(char[5]), sizeof(AlignedCharArrayUnion<char[5]>));
268 EXPECT_EQ(sizeof(char[8]), sizeof(AlignedCharArrayUnion<char[8]>));
269 EXPECT_EQ(sizeof(char[13]), sizeof(AlignedCharArrayUnion<char[13]>));
270 EXPECT_EQ(sizeof(char[16]), sizeof(AlignedCharArrayUnion<char[16]>));
271 EXPECT_EQ(sizeof(char[21]), sizeof(AlignedCharArrayUnion<char[21]>));
272 EXPECT_EQ(sizeof(char[32]), sizeof(AlignedCharArrayUnion<char[32]>));
273 EXPECT_EQ(sizeof(short), sizeof(AlignedCharArrayUnion<short>));
274 EXPECT_EQ(sizeof(int), sizeof(AlignedCharArrayUnion<int>));
275 EXPECT_EQ(sizeof(long), sizeof(AlignedCharArrayUnion<long>));
276 EXPECT_EQ(sizeof(long long),
277 sizeof(AlignedCharArrayUnion<long long>));
278 EXPECT_EQ(sizeof(float), sizeof(AlignedCharArrayUnion<float>));
279 EXPECT_EQ(sizeof(double), sizeof(AlignedCharArrayUnion<double>));
280 EXPECT_EQ(sizeof(long double),
281 sizeof(AlignedCharArrayUnion<long double>));
282 EXPECT_EQ(sizeof(void *), sizeof(AlignedCharArrayUnion<void *>));
283 EXPECT_EQ(sizeof(int *), sizeof(AlignedCharArrayUnion<int *>));
284 EXPECT_EQ(sizeof(double (*)(double)),
285 sizeof(AlignedCharArrayUnion<double (*)(double)>));
286 EXPECT_EQ(sizeof(double (S6::*)()),
287 sizeof(AlignedCharArrayUnion<double (S6::*)()>));
288 EXPECT_EQ(sizeof(S1), sizeof(AlignedCharArrayUnion<S1>));
289 EXPECT_EQ(sizeof(S2), sizeof(AlignedCharArrayUnion<S2>));
290 EXPECT_EQ(sizeof(S3), sizeof(AlignedCharArrayUnion<S3>));
291 EXPECT_EQ(sizeof(S4), sizeof(AlignedCharArrayUnion<S4>));
292 EXPECT_EQ(sizeof(S5), sizeof(AlignedCharArrayUnion<S5>));
293 EXPECT_EQ(sizeof(S6), sizeof(AlignedCharArrayUnion<S6>));
294 EXPECT_EQ(sizeof(D1), sizeof(AlignedCharArrayUnion<D1>));
295 EXPECT_EQ(sizeof(D2), sizeof(AlignedCharArrayUnion<D2>));
296 EXPECT_EQ(sizeof(D3), sizeof(AlignedCharArrayUnion<D3>));
297 EXPECT_EQ(sizeof(D4), sizeof(AlignedCharArrayUnion<D4>));
298 EXPECT_EQ(sizeof(D5), sizeof(AlignedCharArrayUnion<D5>));
299 EXPECT_EQ(sizeof(D6), sizeof(AlignedCharArrayUnion<D6>));
300 EXPECT_EQ(sizeof(D7), sizeof(AlignedCharArrayUnion<D7>));
301 EXPECT_EQ(sizeof(D8), sizeof(AlignedCharArrayUnion<D8>));
302 EXPECT_EQ(sizeof(D9), sizeof(AlignedCharArrayUnion<D9>));
303 EXPECT_EQ(sizeof(D9[1]), sizeof(AlignedCharArrayUnion<D9[1]>));
304 EXPECT_EQ(sizeof(D9[2]), sizeof(AlignedCharArrayUnion<D9[2]>));
305 EXPECT_EQ(sizeof(D9[3]), sizeof(AlignedCharArrayUnion<D9[3]>));
306 EXPECT_EQ(sizeof(D9[4]), sizeof(AlignedCharArrayUnion<D9[4]>));
307 EXPECT_EQ(sizeof(D9[5]), sizeof(AlignedCharArrayUnion<D9[5]>));
308 EXPECT_EQ(sizeof(D9[8]), sizeof(AlignedCharArrayUnion<D9[8]>));
309 EXPECT_EQ(sizeof(D9[13]), sizeof(AlignedCharArrayUnion<D9[13]>));
310 EXPECT_EQ(sizeof(D9[16]), sizeof(AlignedCharArrayUnion<D9[16]>));
311 EXPECT_EQ(sizeof(D9[21]), sizeof(AlignedCharArrayUnion<D9[21]>));
312 EXPECT_EQ(sizeof(D9[32]), sizeof(AlignedCharArrayUnion<D9[32]>));
313 EXPECT_EQ(sizeof(V1), sizeof(AlignedCharArrayUnion<V1>));
314 EXPECT_EQ(sizeof(V2), sizeof(AlignedCharArrayUnion<V2>));
315 EXPECT_EQ(sizeof(V3), sizeof(AlignedCharArrayUnion<V3>));
316 EXPECT_EQ(sizeof(V4), sizeof(AlignedCharArrayUnion<V4>));
317 EXPECT_EQ(sizeof(V5), sizeof(AlignedCharArrayUnion<V5>));
318 EXPECT_EQ(sizeof(V6), sizeof(AlignedCharArrayUnion<V6>));
319 EXPECT_EQ(sizeof(V7), sizeof(AlignedCharArrayUnion<V7>));
320
321 // Some versions of MSVC also get this wrong. The failure again appears to be
322 // benign: sizeof(V8) is only 52 bytes, but our array reserves 56.
323 #ifndef _MSC_VER
324 EXPECT_EQ(sizeof(V8), sizeof(AlignedCharArrayUnion<V8>));
325 #endif
326 }
327
328 }
329