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