• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Ensure alignment is a compile-time constant.
93 char LLVM_ATTRIBUTE_UNUSED test_arr1
94   [AlignOf<char>::Alignment > 0]
95   [AlignOf<short>::Alignment > 0]
96   [AlignOf<int>::Alignment > 0]
97   [AlignOf<long>::Alignment > 0]
98   [AlignOf<long long>::Alignment > 0]
99   [AlignOf<float>::Alignment > 0]
100   [AlignOf<double>::Alignment > 0]
101   [AlignOf<long double>::Alignment > 0]
102   [AlignOf<void *>::Alignment > 0]
103   [AlignOf<int *>::Alignment > 0]
104   [AlignOf<double (*)(double)>::Alignment > 0]
105   [AlignOf<double (S6::*)()>::Alignment > 0];
106 char LLVM_ATTRIBUTE_UNUSED test_arr2
107   [AlignOf<A1>::Alignment > 0]
108   [AlignOf<A2>::Alignment > 0]
109   [AlignOf<A4>::Alignment > 0]
110   [AlignOf<A8>::Alignment > 0];
111 char LLVM_ATTRIBUTE_UNUSED test_arr3
112   [AlignOf<S1>::Alignment > 0]
113   [AlignOf<S2>::Alignment > 0]
114   [AlignOf<S3>::Alignment > 0]
115   [AlignOf<S4>::Alignment > 0]
116   [AlignOf<S5>::Alignment > 0]
117   [AlignOf<S6>::Alignment > 0];
118 char LLVM_ATTRIBUTE_UNUSED test_arr4
119   [AlignOf<D1>::Alignment > 0]
120   [AlignOf<D2>::Alignment > 0]
121   [AlignOf<D3>::Alignment > 0]
122   [AlignOf<D4>::Alignment > 0]
123   [AlignOf<D5>::Alignment > 0]
124   [AlignOf<D6>::Alignment > 0]
125   [AlignOf<D7>::Alignment > 0]
126   [AlignOf<D8>::Alignment > 0]
127   [AlignOf<D9>::Alignment > 0];
128 char LLVM_ATTRIBUTE_UNUSED test_arr5
129   [AlignOf<V1>::Alignment > 0]
130   [AlignOf<V2>::Alignment > 0]
131   [AlignOf<V3>::Alignment > 0]
132   [AlignOf<V4>::Alignment > 0]
133   [AlignOf<V5>::Alignment > 0]
134   [AlignOf<V6>::Alignment > 0]
135   [AlignOf<V7>::Alignment > 0]
136   [AlignOf<V8>::Alignment > 0];
137 
TEST(AlignOfTest,BasicAlignmentInvariants)138 TEST(AlignOfTest, BasicAlignmentInvariants) {
139   EXPECT_LE(1u, alignOf<A1>());
140   EXPECT_LE(2u, alignOf<A2>());
141   EXPECT_LE(4u, alignOf<A4>());
142   EXPECT_LE(8u, alignOf<A8>());
143 
144   EXPECT_EQ(1u, alignOf<char>());
145   EXPECT_LE(alignOf<char>(),   alignOf<short>());
146   EXPECT_LE(alignOf<short>(),  alignOf<int>());
147   EXPECT_LE(alignOf<int>(),    alignOf<long>());
148   EXPECT_LE(alignOf<long>(),   alignOf<long long>());
149   EXPECT_LE(alignOf<char>(),   alignOf<float>());
150   EXPECT_LE(alignOf<float>(),  alignOf<double>());
151   EXPECT_LE(alignOf<char>(),   alignOf<long double>());
152   EXPECT_LE(alignOf<char>(),   alignOf<void *>());
153   EXPECT_EQ(alignOf<void *>(), alignOf<int *>());
154   EXPECT_LE(alignOf<char>(),   alignOf<S1>());
155   EXPECT_LE(alignOf<S1>(),     alignOf<S2>());
156   EXPECT_LE(alignOf<S1>(),     alignOf<S3>());
157   EXPECT_LE(alignOf<S1>(),     alignOf<S4>());
158   EXPECT_LE(alignOf<S1>(),     alignOf<S5>());
159   EXPECT_LE(alignOf<S1>(),     alignOf<S6>());
160   EXPECT_LE(alignOf<S1>(),     alignOf<D1>());
161   EXPECT_LE(alignOf<S1>(),     alignOf<D2>());
162   EXPECT_LE(alignOf<S1>(),     alignOf<D3>());
163   EXPECT_LE(alignOf<S1>(),     alignOf<D4>());
164   EXPECT_LE(alignOf<S1>(),     alignOf<D5>());
165   EXPECT_LE(alignOf<S1>(),     alignOf<D6>());
166   EXPECT_LE(alignOf<S1>(),     alignOf<D7>());
167   EXPECT_LE(alignOf<S1>(),     alignOf<D8>());
168   EXPECT_LE(alignOf<S1>(),     alignOf<D9>());
169   EXPECT_LE(alignOf<S1>(),     alignOf<V1>());
170   EXPECT_LE(alignOf<V1>(),     alignOf<V2>());
171   EXPECT_LE(alignOf<V1>(),     alignOf<V3>());
172   EXPECT_LE(alignOf<V1>(),     alignOf<V4>());
173   EXPECT_LE(alignOf<V1>(),     alignOf<V5>());
174   EXPECT_LE(alignOf<V1>(),     alignOf<V6>());
175   EXPECT_LE(alignOf<V1>(),     alignOf<V7>());
176   EXPECT_LE(alignOf<V1>(),     alignOf<V8>());
177 }
178 
TEST(AlignOfTest,BasicAlignedArray)179 TEST(AlignOfTest, BasicAlignedArray) {
180   EXPECT_LE(1u, alignOf<AlignedCharArrayUnion<A1> >());
181   EXPECT_LE(2u, alignOf<AlignedCharArrayUnion<A2> >());
182   EXPECT_LE(4u, alignOf<AlignedCharArrayUnion<A4> >());
183   EXPECT_LE(8u, alignOf<AlignedCharArrayUnion<A8> >());
184 
185   EXPECT_LE(1u, sizeof(AlignedCharArrayUnion<A1>));
186   EXPECT_LE(2u, sizeof(AlignedCharArrayUnion<A2>));
187   EXPECT_LE(4u, sizeof(AlignedCharArrayUnion<A4>));
188   EXPECT_LE(8u, sizeof(AlignedCharArrayUnion<A8>));
189 
190   EXPECT_EQ(1u, (alignOf<AlignedCharArrayUnion<A1> >()));
191   EXPECT_EQ(2u, (alignOf<AlignedCharArrayUnion<A1, A2> >()));
192   EXPECT_EQ(4u, (alignOf<AlignedCharArrayUnion<A1, A2, A4> >()));
193   EXPECT_EQ(8u, (alignOf<AlignedCharArrayUnion<A1, A2, A4, A8> >()));
194 
195   EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<A1>));
196   EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<A1, A2>));
197   EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<A1, A2, A4>));
198   EXPECT_EQ(8u, sizeof(AlignedCharArrayUnion<A1, A2, A4, A8>));
199 
200   EXPECT_EQ(1u, (alignOf<AlignedCharArrayUnion<A1[1]> >()));
201   EXPECT_EQ(2u, (alignOf<AlignedCharArrayUnion<A1[2], A2[1]> >()));
202   EXPECT_EQ(4u, (alignOf<AlignedCharArrayUnion<A1[42], A2[55],
203                                                A4[13]> >()));
204   EXPECT_EQ(8u, (alignOf<AlignedCharArrayUnion<A1[2], A2[1],
205                                                A4, A8> >()));
206 
207   EXPECT_EQ(1u,  sizeof(AlignedCharArrayUnion<A1[1]>));
208   EXPECT_EQ(2u,  sizeof(AlignedCharArrayUnion<A1[2], A2[1]>));
209   EXPECT_EQ(4u,  sizeof(AlignedCharArrayUnion<A1[3], A2[2], A4>));
210   EXPECT_EQ(16u, sizeof(AlignedCharArrayUnion<A1, A2[3],
211                                               A4[3], A8>));
212 
213   // For other tests we simply assert that the alignment of the union mathes
214   // that of the fundamental type and hope that we have any weird type
215   // productions that would trigger bugs.
216   EXPECT_EQ(alignOf<char>(), alignOf<AlignedCharArrayUnion<char> >());
217   EXPECT_EQ(alignOf<short>(), alignOf<AlignedCharArrayUnion<short> >());
218   EXPECT_EQ(alignOf<int>(), alignOf<AlignedCharArrayUnion<int> >());
219   EXPECT_EQ(alignOf<long>(), alignOf<AlignedCharArrayUnion<long> >());
220   EXPECT_EQ(alignOf<long long>(),
221             alignOf<AlignedCharArrayUnion<long long> >());
222   EXPECT_EQ(alignOf<float>(), alignOf<AlignedCharArrayUnion<float> >());
223   EXPECT_EQ(alignOf<double>(), alignOf<AlignedCharArrayUnion<double> >());
224   EXPECT_EQ(alignOf<long double>(),
225             alignOf<AlignedCharArrayUnion<long double> >());
226   EXPECT_EQ(alignOf<void *>(), alignOf<AlignedCharArrayUnion<void *> >());
227   EXPECT_EQ(alignOf<int *>(), alignOf<AlignedCharArrayUnion<int *> >());
228   EXPECT_EQ(alignOf<double (*)(double)>(),
229             alignOf<AlignedCharArrayUnion<double (*)(double)> >());
230   EXPECT_EQ(alignOf<double (S6::*)()>(),
231             alignOf<AlignedCharArrayUnion<double (S6::*)()> >());
232   EXPECT_EQ(alignOf<S1>(), alignOf<AlignedCharArrayUnion<S1> >());
233   EXPECT_EQ(alignOf<S2>(), alignOf<AlignedCharArrayUnion<S2> >());
234   EXPECT_EQ(alignOf<S3>(), alignOf<AlignedCharArrayUnion<S3> >());
235   EXPECT_EQ(alignOf<S4>(), alignOf<AlignedCharArrayUnion<S4> >());
236   EXPECT_EQ(alignOf<S5>(), alignOf<AlignedCharArrayUnion<S5> >());
237   EXPECT_EQ(alignOf<S6>(), alignOf<AlignedCharArrayUnion<S6> >());
238   EXPECT_EQ(alignOf<D1>(), alignOf<AlignedCharArrayUnion<D1> >());
239   EXPECT_EQ(alignOf<D2>(), alignOf<AlignedCharArrayUnion<D2> >());
240   EXPECT_EQ(alignOf<D3>(), alignOf<AlignedCharArrayUnion<D3> >());
241   EXPECT_EQ(alignOf<D4>(), alignOf<AlignedCharArrayUnion<D4> >());
242   EXPECT_EQ(alignOf<D5>(), alignOf<AlignedCharArrayUnion<D5> >());
243   EXPECT_EQ(alignOf<D6>(), alignOf<AlignedCharArrayUnion<D6> >());
244   EXPECT_EQ(alignOf<D7>(), alignOf<AlignedCharArrayUnion<D7> >());
245   EXPECT_EQ(alignOf<D8>(), alignOf<AlignedCharArrayUnion<D8> >());
246   EXPECT_EQ(alignOf<D9>(), alignOf<AlignedCharArrayUnion<D9> >());
247   EXPECT_EQ(alignOf<V1>(), alignOf<AlignedCharArrayUnion<V1> >());
248   EXPECT_EQ(alignOf<V2>(), alignOf<AlignedCharArrayUnion<V2> >());
249   EXPECT_EQ(alignOf<V3>(), alignOf<AlignedCharArrayUnion<V3> >());
250   EXPECT_EQ(alignOf<V4>(), alignOf<AlignedCharArrayUnion<V4> >());
251   EXPECT_EQ(alignOf<V5>(), alignOf<AlignedCharArrayUnion<V5> >());
252   EXPECT_EQ(alignOf<V6>(), alignOf<AlignedCharArrayUnion<V6> >());
253   EXPECT_EQ(alignOf<V7>(), alignOf<AlignedCharArrayUnion<V7> >());
254 
255   // Some versions of MSVC get this wrong somewhat disturbingly. The failure
256   // appears to be benign: alignOf<V8>() produces a preposterous value: 12
257 #ifndef _MSC_VER
258   EXPECT_EQ(alignOf<V8>(), alignOf<AlignedCharArrayUnion<V8> >());
259 #endif
260 
261   EXPECT_EQ(sizeof(char), sizeof(AlignedCharArrayUnion<char>));
262   EXPECT_EQ(sizeof(char[1]), sizeof(AlignedCharArrayUnion<char[1]>));
263   EXPECT_EQ(sizeof(char[2]), sizeof(AlignedCharArrayUnion<char[2]>));
264   EXPECT_EQ(sizeof(char[3]), sizeof(AlignedCharArrayUnion<char[3]>));
265   EXPECT_EQ(sizeof(char[4]), sizeof(AlignedCharArrayUnion<char[4]>));
266   EXPECT_EQ(sizeof(char[5]), sizeof(AlignedCharArrayUnion<char[5]>));
267   EXPECT_EQ(sizeof(char[8]), sizeof(AlignedCharArrayUnion<char[8]>));
268   EXPECT_EQ(sizeof(char[13]), sizeof(AlignedCharArrayUnion<char[13]>));
269   EXPECT_EQ(sizeof(char[16]), sizeof(AlignedCharArrayUnion<char[16]>));
270   EXPECT_EQ(sizeof(char[21]), sizeof(AlignedCharArrayUnion<char[21]>));
271   EXPECT_EQ(sizeof(char[32]), sizeof(AlignedCharArrayUnion<char[32]>));
272   EXPECT_EQ(sizeof(short), sizeof(AlignedCharArrayUnion<short>));
273   EXPECT_EQ(sizeof(int), sizeof(AlignedCharArrayUnion<int>));
274   EXPECT_EQ(sizeof(long), sizeof(AlignedCharArrayUnion<long>));
275   EXPECT_EQ(sizeof(long long),
276             sizeof(AlignedCharArrayUnion<long long>));
277   EXPECT_EQ(sizeof(float), sizeof(AlignedCharArrayUnion<float>));
278   EXPECT_EQ(sizeof(double), sizeof(AlignedCharArrayUnion<double>));
279   EXPECT_EQ(sizeof(long double),
280             sizeof(AlignedCharArrayUnion<long double>));
281   EXPECT_EQ(sizeof(void *), sizeof(AlignedCharArrayUnion<void *>));
282   EXPECT_EQ(sizeof(int *), sizeof(AlignedCharArrayUnion<int *>));
283   EXPECT_EQ(sizeof(double (*)(double)),
284             sizeof(AlignedCharArrayUnion<double (*)(double)>));
285   EXPECT_EQ(sizeof(double (S6::*)()),
286             sizeof(AlignedCharArrayUnion<double (S6::*)()>));
287   EXPECT_EQ(sizeof(S1), sizeof(AlignedCharArrayUnion<S1>));
288   EXPECT_EQ(sizeof(S2), sizeof(AlignedCharArrayUnion<S2>));
289   EXPECT_EQ(sizeof(S3), sizeof(AlignedCharArrayUnion<S3>));
290   EXPECT_EQ(sizeof(S4), sizeof(AlignedCharArrayUnion<S4>));
291   EXPECT_EQ(sizeof(S5), sizeof(AlignedCharArrayUnion<S5>));
292   EXPECT_EQ(sizeof(S6), sizeof(AlignedCharArrayUnion<S6>));
293   EXPECT_EQ(sizeof(D1), sizeof(AlignedCharArrayUnion<D1>));
294   EXPECT_EQ(sizeof(D2), sizeof(AlignedCharArrayUnion<D2>));
295   EXPECT_EQ(sizeof(D3), sizeof(AlignedCharArrayUnion<D3>));
296   EXPECT_EQ(sizeof(D4), sizeof(AlignedCharArrayUnion<D4>));
297   EXPECT_EQ(sizeof(D5), sizeof(AlignedCharArrayUnion<D5>));
298   EXPECT_EQ(sizeof(D6), sizeof(AlignedCharArrayUnion<D6>));
299   EXPECT_EQ(sizeof(D7), sizeof(AlignedCharArrayUnion<D7>));
300   EXPECT_EQ(sizeof(D8), sizeof(AlignedCharArrayUnion<D8>));
301   EXPECT_EQ(sizeof(D9), sizeof(AlignedCharArrayUnion<D9>));
302   EXPECT_EQ(sizeof(D9[1]), sizeof(AlignedCharArrayUnion<D9[1]>));
303   EXPECT_EQ(sizeof(D9[2]), sizeof(AlignedCharArrayUnion<D9[2]>));
304   EXPECT_EQ(sizeof(D9[3]), sizeof(AlignedCharArrayUnion<D9[3]>));
305   EXPECT_EQ(sizeof(D9[4]), sizeof(AlignedCharArrayUnion<D9[4]>));
306   EXPECT_EQ(sizeof(D9[5]), sizeof(AlignedCharArrayUnion<D9[5]>));
307   EXPECT_EQ(sizeof(D9[8]), sizeof(AlignedCharArrayUnion<D9[8]>));
308   EXPECT_EQ(sizeof(D9[13]), sizeof(AlignedCharArrayUnion<D9[13]>));
309   EXPECT_EQ(sizeof(D9[16]), sizeof(AlignedCharArrayUnion<D9[16]>));
310   EXPECT_EQ(sizeof(D9[21]), sizeof(AlignedCharArrayUnion<D9[21]>));
311   EXPECT_EQ(sizeof(D9[32]), sizeof(AlignedCharArrayUnion<D9[32]>));
312   EXPECT_EQ(sizeof(V1), sizeof(AlignedCharArrayUnion<V1>));
313   EXPECT_EQ(sizeof(V2), sizeof(AlignedCharArrayUnion<V2>));
314   EXPECT_EQ(sizeof(V3), sizeof(AlignedCharArrayUnion<V3>));
315   EXPECT_EQ(sizeof(V4), sizeof(AlignedCharArrayUnion<V4>));
316   EXPECT_EQ(sizeof(V5), sizeof(AlignedCharArrayUnion<V5>));
317   EXPECT_EQ(sizeof(V6), sizeof(AlignedCharArrayUnion<V6>));
318   EXPECT_EQ(sizeof(V7), sizeof(AlignedCharArrayUnion<V7>));
319 
320   // Some versions of MSVC also get this wrong. The failure again appears to be
321   // benign: sizeof(V8) is only 52 bytes, but our array reserves 56.
322 #ifndef _MSC_VER
323   EXPECT_EQ(sizeof(V8), sizeof(AlignedCharArrayUnion<V8>));
324 #endif
325 
326   EXPECT_EQ(1u, (alignOf<AlignedCharArray<1, 1> >()));
327   EXPECT_EQ(2u, (alignOf<AlignedCharArray<2, 1> >()));
328   EXPECT_EQ(4u, (alignOf<AlignedCharArray<4, 1> >()));
329   EXPECT_EQ(8u, (alignOf<AlignedCharArray<8, 1> >()));
330   EXPECT_EQ(16u, (alignOf<AlignedCharArray<16, 1> >()));
331 
332   EXPECT_EQ(1u, sizeof(AlignedCharArray<1, 1>));
333   EXPECT_EQ(7u, sizeof(AlignedCharArray<1, 7>));
334   EXPECT_EQ(2u, sizeof(AlignedCharArray<2, 2>));
335   EXPECT_EQ(16u, sizeof(AlignedCharArray<2, 16>));
336 }
337 }
338