1 //=== - llvm/unittest/Support/AlignOfTest.cpp - Alignment utility tests ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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)
16 #include "llvm/Support/AlignOf.h"
17 #include "llvm/Support/Compiler.h"
18 #include "gtest/gtest.h"
23 // Disable warnings about questionable type definitions.
24 // We're testing that even questionable types work with the alignment utilities.
26 #pragma warning(disable:4584)
29 // Suppress direct base '{anonymous}::S1' inaccessible in '{anonymous}::D9'
30 // due to ambiguity warning.
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"
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 {};
48 struct S2 { char a; };
50 struct S4 { double y; };
51 struct S5 { A1 a1; A2 a2; A4 a4; A8 a8; };
52 struct S6 { double f(); };
54 struct D2 : S6 { float g(); };
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(); };
67 struct V4 : virtual V2 { int y;
70 struct V5 : V4, V3 { double z;
73 struct V6 : S1 { virtual ~V6(); };
74 struct V7 : virtual V2, virtual V6 {
77 struct V8 : V5, virtual V6, V7 { double zz;
81 double S6::f() { return 0.0; }
82 float D2::g() { return 0.0f; }
92 template <typename M> struct T { M m; };
94 TEST(AlignOfTest, BasicAlignedArray) {
95 EXPECT_LE(1u, alignof(AlignedCharArrayUnion<A1>));
96 EXPECT_LE(2u, alignof(AlignedCharArrayUnion<A2>));
97 EXPECT_LE(4u, alignof(AlignedCharArrayUnion<A4>));
98 EXPECT_LE(8u, alignof(AlignedCharArrayUnion<A8>));
100 EXPECT_LE(1u, sizeof(AlignedCharArrayUnion<A1>));
101 EXPECT_LE(2u, sizeof(AlignedCharArrayUnion<A2>));
102 EXPECT_LE(4u, sizeof(AlignedCharArrayUnion<A4>));
103 EXPECT_LE(8u, sizeof(AlignedCharArrayUnion<A8>));
105 EXPECT_EQ(1u, (alignof(AlignedCharArrayUnion<A1>)));
106 EXPECT_EQ(2u, (alignof(AlignedCharArrayUnion<A1, A2>)));
107 EXPECT_EQ(4u, (alignof(AlignedCharArrayUnion<A1, A2, A4>)));
108 EXPECT_EQ(8u, (alignof(AlignedCharArrayUnion<A1, A2, A4, A8>)));
110 EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<A1>));
111 EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<A1, A2>));
112 EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<A1, A2, A4>));
113 EXPECT_EQ(8u, sizeof(AlignedCharArrayUnion<A1, A2, A4, A8>));
115 EXPECT_EQ(1u, (alignof(AlignedCharArrayUnion<A1[1]>)));
116 EXPECT_EQ(2u, (alignof(AlignedCharArrayUnion<A1[2], A2[1]>)));
117 EXPECT_EQ(4u, (alignof(AlignedCharArrayUnion<A1[42], A2[55], A4[13]>)));
118 EXPECT_EQ(8u, (alignof(AlignedCharArrayUnion<A1[2], A2[1], A4, A8>)));
120 EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<A1[1]>));
121 EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<A1[2], A2[1]>));
122 EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<A1[3], A2[2], A4>));
123 EXPECT_EQ(16u, sizeof(AlignedCharArrayUnion<A1, A2[3],
126 // For other tests we simply assert that the alignment of the union mathes
127 // that of the fundamental type and hope that we have any weird type
128 // productions that would trigger bugs.
129 EXPECT_EQ(alignof(T<char>), alignof(AlignedCharArrayUnion<char>));
130 EXPECT_EQ(alignof(T<short>), alignof(AlignedCharArrayUnion<short>));
131 EXPECT_EQ(alignof(T<int>), alignof(AlignedCharArrayUnion<int>));
132 EXPECT_EQ(alignof(T<long>), alignof(AlignedCharArrayUnion<long>));
133 EXPECT_EQ(alignof(T<long long>), alignof(AlignedCharArrayUnion<long long>));
134 EXPECT_EQ(alignof(T<float>), alignof(AlignedCharArrayUnion<float>));
135 EXPECT_EQ(alignof(T<double>), alignof(AlignedCharArrayUnion<double>));
136 EXPECT_EQ(alignof(T<long double>),
137 alignof(AlignedCharArrayUnion<long double>));
138 EXPECT_EQ(alignof(T<void *>), alignof(AlignedCharArrayUnion<void *>));
139 EXPECT_EQ(alignof(T<int *>), alignof(AlignedCharArrayUnion<int *>));
140 EXPECT_EQ(alignof(T<double (*)(double)>),
141 alignof(AlignedCharArrayUnion<double (*)(double)>));
142 EXPECT_EQ(alignof(T<double (S6::*)()>),
143 alignof(AlignedCharArrayUnion<double (S6::*)()>));
144 EXPECT_EQ(alignof(S1), alignof(AlignedCharArrayUnion<S1>));
145 EXPECT_EQ(alignof(S2), alignof(AlignedCharArrayUnion<S2>));
146 EXPECT_EQ(alignof(S3), alignof(AlignedCharArrayUnion<S3>));
147 EXPECT_EQ(alignof(S4), alignof(AlignedCharArrayUnion<S4>));
148 EXPECT_EQ(alignof(S5), alignof(AlignedCharArrayUnion<S5>));
149 EXPECT_EQ(alignof(S6), alignof(AlignedCharArrayUnion<S6>));
150 EXPECT_EQ(alignof(D1), alignof(AlignedCharArrayUnion<D1>));
151 EXPECT_EQ(alignof(D2), alignof(AlignedCharArrayUnion<D2>));
152 EXPECT_EQ(alignof(D3), alignof(AlignedCharArrayUnion<D3>));
153 EXPECT_EQ(alignof(D4), alignof(AlignedCharArrayUnion<D4>));
154 EXPECT_EQ(alignof(D5), alignof(AlignedCharArrayUnion<D5>));
155 EXPECT_EQ(alignof(D6), alignof(AlignedCharArrayUnion<D6>));
156 EXPECT_EQ(alignof(D7), alignof(AlignedCharArrayUnion<D7>));
157 EXPECT_EQ(alignof(D8), alignof(AlignedCharArrayUnion<D8>));
158 EXPECT_EQ(alignof(D9), alignof(AlignedCharArrayUnion<D9>));
159 EXPECT_EQ(alignof(V1), alignof(AlignedCharArrayUnion<V1>));
160 EXPECT_EQ(alignof(V2), alignof(AlignedCharArrayUnion<V2>));
161 EXPECT_EQ(alignof(V3), alignof(AlignedCharArrayUnion<V3>));
162 EXPECT_EQ(alignof(V4), alignof(AlignedCharArrayUnion<V4>));
163 EXPECT_EQ(alignof(V5), alignof(AlignedCharArrayUnion<V5>));
164 EXPECT_EQ(alignof(V6), alignof(AlignedCharArrayUnion<V6>));
165 EXPECT_EQ(alignof(V7), alignof(AlignedCharArrayUnion<V7>));
167 // Some versions of MSVC get this wrong somewhat disturbingly. The failure
168 // appears to be benign: alignof(V8) produces a preposterous value: 12
170 EXPECT_EQ(alignof(V8), alignof(AlignedCharArrayUnion<V8>));
173 EXPECT_EQ(sizeof(char), sizeof(AlignedCharArrayUnion<char>));
174 EXPECT_EQ(sizeof(char[1]), sizeof(AlignedCharArrayUnion<char[1]>));
175 EXPECT_EQ(sizeof(char[2]), sizeof(AlignedCharArrayUnion<char[2]>));
176 EXPECT_EQ(sizeof(char[3]), sizeof(AlignedCharArrayUnion<char[3]>));
177 EXPECT_EQ(sizeof(char[4]), sizeof(AlignedCharArrayUnion<char[4]>));
178 EXPECT_EQ(sizeof(char[5]), sizeof(AlignedCharArrayUnion<char[5]>));
179 EXPECT_EQ(sizeof(char[8]), sizeof(AlignedCharArrayUnion<char[8]>));
180 EXPECT_EQ(sizeof(char[13]), sizeof(AlignedCharArrayUnion<char[13]>));
181 EXPECT_EQ(sizeof(char[16]), sizeof(AlignedCharArrayUnion<char[16]>));
182 EXPECT_EQ(sizeof(char[21]), sizeof(AlignedCharArrayUnion<char[21]>));
183 EXPECT_EQ(sizeof(char[32]), sizeof(AlignedCharArrayUnion<char[32]>));
184 EXPECT_EQ(sizeof(short), sizeof(AlignedCharArrayUnion<short>));
185 EXPECT_EQ(sizeof(int), sizeof(AlignedCharArrayUnion<int>));
186 EXPECT_EQ(sizeof(long), sizeof(AlignedCharArrayUnion<long>));
187 EXPECT_EQ(sizeof(long long),
188 sizeof(AlignedCharArrayUnion<long long>));
189 EXPECT_EQ(sizeof(float), sizeof(AlignedCharArrayUnion<float>));
190 EXPECT_EQ(sizeof(double), sizeof(AlignedCharArrayUnion<double>));
191 EXPECT_EQ(sizeof(long double),
192 sizeof(AlignedCharArrayUnion<long double>));
193 EXPECT_EQ(sizeof(void *), sizeof(AlignedCharArrayUnion<void *>));
194 EXPECT_EQ(sizeof(int *), sizeof(AlignedCharArrayUnion<int *>));
195 EXPECT_EQ(sizeof(double (*)(double)),
196 sizeof(AlignedCharArrayUnion<double (*)(double)>));
197 EXPECT_EQ(sizeof(double (S6::*)()),
198 sizeof(AlignedCharArrayUnion<double (S6::*)()>));
199 EXPECT_EQ(sizeof(S1), sizeof(AlignedCharArrayUnion<S1>));
200 EXPECT_EQ(sizeof(S2), sizeof(AlignedCharArrayUnion<S2>));
201 EXPECT_EQ(sizeof(S3), sizeof(AlignedCharArrayUnion<S3>));
202 EXPECT_EQ(sizeof(S4), sizeof(AlignedCharArrayUnion<S4>));
203 EXPECT_EQ(sizeof(S5), sizeof(AlignedCharArrayUnion<S5>));
204 EXPECT_EQ(sizeof(S6), sizeof(AlignedCharArrayUnion<S6>));
205 EXPECT_EQ(sizeof(D1), sizeof(AlignedCharArrayUnion<D1>));
206 EXPECT_EQ(sizeof(D2), sizeof(AlignedCharArrayUnion<D2>));
207 EXPECT_EQ(sizeof(D3), sizeof(AlignedCharArrayUnion<D3>));
208 EXPECT_EQ(sizeof(D4), sizeof(AlignedCharArrayUnion<D4>));
209 EXPECT_EQ(sizeof(D5), sizeof(AlignedCharArrayUnion<D5>));
210 EXPECT_EQ(sizeof(D6), sizeof(AlignedCharArrayUnion<D6>));
211 EXPECT_EQ(sizeof(D7), sizeof(AlignedCharArrayUnion<D7>));
212 EXPECT_EQ(sizeof(D8), sizeof(AlignedCharArrayUnion<D8>));
213 EXPECT_EQ(sizeof(D9), sizeof(AlignedCharArrayUnion<D9>));
214 EXPECT_EQ(sizeof(D9[1]), sizeof(AlignedCharArrayUnion<D9[1]>));
215 EXPECT_EQ(sizeof(D9[2]), sizeof(AlignedCharArrayUnion<D9[2]>));
216 EXPECT_EQ(sizeof(D9[3]), sizeof(AlignedCharArrayUnion<D9[3]>));
217 EXPECT_EQ(sizeof(D9[4]), sizeof(AlignedCharArrayUnion<D9[4]>));
218 EXPECT_EQ(sizeof(D9[5]), sizeof(AlignedCharArrayUnion<D9[5]>));
219 EXPECT_EQ(sizeof(D9[8]), sizeof(AlignedCharArrayUnion<D9[8]>));
220 EXPECT_EQ(sizeof(D9[13]), sizeof(AlignedCharArrayUnion<D9[13]>));
221 EXPECT_EQ(sizeof(D9[16]), sizeof(AlignedCharArrayUnion<D9[16]>));
222 EXPECT_EQ(sizeof(D9[21]), sizeof(AlignedCharArrayUnion<D9[21]>));
223 EXPECT_EQ(sizeof(D9[32]), sizeof(AlignedCharArrayUnion<D9[32]>));
224 EXPECT_EQ(sizeof(V1), sizeof(AlignedCharArrayUnion<V1>));
225 EXPECT_EQ(sizeof(V2), sizeof(AlignedCharArrayUnion<V2>));
226 EXPECT_EQ(sizeof(V3), sizeof(AlignedCharArrayUnion<V3>));
227 EXPECT_EQ(sizeof(V4), sizeof(AlignedCharArrayUnion<V4>));
228 EXPECT_EQ(sizeof(V5), sizeof(AlignedCharArrayUnion<V5>));
229 EXPECT_EQ(sizeof(V6), sizeof(AlignedCharArrayUnion<V6>));
230 EXPECT_EQ(sizeof(V7), sizeof(AlignedCharArrayUnion<V7>));
232 // Some versions of MSVC also get this wrong. The failure again appears to be
233 // benign: sizeof(V8) is only 52 bytes, but our array reserves 56.
235 EXPECT_EQ(sizeof(V8), sizeof(AlignedCharArrayUnion<V8>));
238 EXPECT_EQ(1u, (alignof(AlignedCharArray<1, 1>)));
239 EXPECT_EQ(2u, (alignof(AlignedCharArray<2, 1>)));
240 EXPECT_EQ(4u, (alignof(AlignedCharArray<4, 1>)));
241 EXPECT_EQ(8u, (alignof(AlignedCharArray<8, 1>)));
242 EXPECT_EQ(16u, (alignof(AlignedCharArray<16, 1>)));
244 EXPECT_EQ(1u, sizeof(AlignedCharArray<1, 1>));
245 EXPECT_EQ(7u, sizeof(AlignedCharArray<1, 7>));
246 EXPECT_EQ(2u, sizeof(AlignedCharArray<2, 2>));
247 EXPECT_EQ(16u, sizeof(AlignedCharArray<2, 16>));
249 } // end anonymous namespace