AlignOfTest.cpp revision 585de7a1b1a51fa29b53bbc8faf3cc07e43bd993
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
15using namespace llvm;
16
17namespace {
18
19// Define some fixed alignment types to use in these tests.
20#if __cplusplus == 201103L || __has_feature(cxx_alignas)
21typedef char alignas(1) A1;
22typedef char alignas(2) A2;
23typedef char alignas(4) A4;
24typedef char alignas(8) A8;
25#elif defined(__clang__) || defined(__GNUC__)
26typedef char A1 __attribute__((aligned(1)));
27typedef char A2 __attribute__((aligned(2)));
28typedef char A4 __attribute__((aligned(4)));
29typedef char A8 __attribute__((aligned(8)));
30#elif defined(_MSC_VER)
31typedef __declspec(align(1)) char A1;
32typedef __declspec(align(2)) char A2;
33typedef __declspec(align(4)) char A4;
34typedef __declspec(align(8)) char A8;
35#else
36# error No supported align as directive.
37#endif
38
39// Wrap the forced aligned types in structs to hack around compiler bugs.
40struct SA1 { A1 a; };
41struct SA2 { A2 a; };
42struct SA4 { A4 a; };
43struct SA8 { A8 a; };
44
45struct S1 {};
46struct S2 { char a; };
47struct S3 { int x; };
48struct S4 { double y; };
49struct S5 { A1 a1; A2 a2; A4 a4; A8 a8; };
50struct S6 { double f(); };
51struct D1 : S1 {};
52struct D2 : S6 { float g(); };
53struct D3 : S2 {};
54struct D4 : S2 { int x; };
55struct D5 : S3 { char c; };
56struct D6 : S2, S3 {};
57struct D7 : S1, S3 {};
58struct D8 : S1, D4, D5 { double x[2]; };
59struct D9 : S1, D1 { S1 s1; };
60struct V1 { virtual ~V1(); };
61struct V2 { int x; virtual ~V2(); };
62struct V3 : V1 { virtual ~V3(); };
63struct V4 : virtual V2 { int y; virtual ~V4(); };
64struct V5 : V4, V3 { double z; virtual ~V5(); };
65struct V6 : S1 { virtual ~V6(); };
66struct V7 : virtual V2, virtual V6 { virtual ~V7(); };
67struct V8 : V5, virtual V6, V7 { double zz; virtual ~V8(); };
68
69// Ensure alignment is a compile-time constant.
70char LLVM_ATTRIBUTE_UNUSED test_arr
71  [AlignOf<char>::Alignment]
72  [AlignOf<short>::Alignment]
73  [AlignOf<int>::Alignment]
74  [AlignOf<long>::Alignment]
75  [AlignOf<long long>::Alignment]
76  [AlignOf<float>::Alignment]
77  [AlignOf<double>::Alignment]
78  [AlignOf<long double>::Alignment]
79  [AlignOf<void *>::Alignment]
80  [AlignOf<int *>::Alignment]
81  [AlignOf<double (*)(double)>::Alignment]
82  [AlignOf<double (S6::*)()>::Alignment]
83  [AlignOf<A1>::Alignment]
84  [AlignOf<A2>::Alignment]
85  [AlignOf<A4>::Alignment]
86  [AlignOf<A8>::Alignment]
87  [AlignOf<SA1>::Alignment]
88  [AlignOf<SA2>::Alignment]
89  [AlignOf<SA4>::Alignment]
90  [AlignOf<SA8>::Alignment]
91  [AlignOf<S1>::Alignment]
92  [AlignOf<S2>::Alignment]
93  [AlignOf<S3>::Alignment]
94  [AlignOf<S4>::Alignment]
95  [AlignOf<S5>::Alignment]
96  [AlignOf<S6>::Alignment]
97  [AlignOf<D1>::Alignment]
98  [AlignOf<D2>::Alignment]
99  [AlignOf<D3>::Alignment]
100  [AlignOf<D4>::Alignment]
101  [AlignOf<D5>::Alignment]
102  [AlignOf<D6>::Alignment]
103  [AlignOf<D7>::Alignment]
104  [AlignOf<D8>::Alignment]
105  [AlignOf<D9>::Alignment]
106  [AlignOf<V1>::Alignment]
107  [AlignOf<V2>::Alignment]
108  [AlignOf<V3>::Alignment]
109  [AlignOf<V4>::Alignment]
110  [AlignOf<V5>::Alignment]
111  [AlignOf<V6>::Alignment]
112  [AlignOf<V7>::Alignment]
113  [AlignOf<V8>::Alignment];
114
115TEST(AlignOfTest, BasicAlignmentInvariants) {
116  // For a very strange reason, many compilers do not support this. Both Clang
117  // and GCC fail to align these properly.
118  EXPECT_EQ(1u, alignOf<A1>());
119#if 0
120  EXPECT_EQ(2u, alignOf<A2>());
121  EXPECT_EQ(4u, alignOf<A4>());
122  EXPECT_EQ(8u, alignOf<A8>());
123#endif
124
125  // But once wrapped in structs, the alignment is correctly managed.
126  EXPECT_LE(1u, alignOf<SA1>());
127  EXPECT_LE(2u, alignOf<SA2>());
128  EXPECT_LE(4u, alignOf<SA4>());
129  EXPECT_LE(8u, alignOf<SA8>());
130
131  EXPECT_EQ(1u, alignOf<char>());
132  EXPECT_LE(alignOf<char>(),   alignOf<short>());
133  EXPECT_LE(alignOf<short>(),  alignOf<int>());
134  EXPECT_LE(alignOf<int>(),    alignOf<long>());
135  EXPECT_LE(alignOf<long>(),   alignOf<long long>());
136  EXPECT_LE(alignOf<char>(),   alignOf<float>());
137  EXPECT_LE(alignOf<float>(),  alignOf<double>());
138  EXPECT_LE(alignOf<double>(), alignOf<long double>());
139  EXPECT_LE(alignOf<char>(),   alignOf<void *>());
140  EXPECT_EQ(alignOf<void *>(), alignOf<int *>());
141  EXPECT_LE(alignOf<char>(),   alignOf<S1>());
142  EXPECT_LE(alignOf<S1>(),     alignOf<S2>());
143  EXPECT_LE(alignOf<S1>(),     alignOf<S3>());
144  EXPECT_LE(alignOf<S1>(),     alignOf<S4>());
145  EXPECT_LE(alignOf<S1>(),     alignOf<S5>());
146  EXPECT_LE(alignOf<S1>(),     alignOf<S6>());
147  EXPECT_LE(alignOf<S1>(),     alignOf<D1>());
148  EXPECT_LE(alignOf<S1>(),     alignOf<D2>());
149  EXPECT_LE(alignOf<S1>(),     alignOf<D3>());
150  EXPECT_LE(alignOf<S1>(),     alignOf<D4>());
151  EXPECT_LE(alignOf<S1>(),     alignOf<D5>());
152  EXPECT_LE(alignOf<S1>(),     alignOf<D6>());
153  EXPECT_LE(alignOf<S1>(),     alignOf<D7>());
154  EXPECT_LE(alignOf<S1>(),     alignOf<D8>());
155  EXPECT_LE(alignOf<S1>(),     alignOf<D9>());
156  EXPECT_LE(alignOf<S1>(),     alignOf<V1>());
157  EXPECT_LE(alignOf<V1>(),     alignOf<V2>());
158  EXPECT_LE(alignOf<V1>(),     alignOf<V3>());
159  EXPECT_LE(alignOf<V1>(),     alignOf<V4>());
160  EXPECT_LE(alignOf<V1>(),     alignOf<V5>());
161  EXPECT_LE(alignOf<V1>(),     alignOf<V6>());
162  EXPECT_LE(alignOf<V1>(),     alignOf<V7>());
163  EXPECT_LE(alignOf<V1>(),     alignOf<V8>());
164}
165
166TEST(AlignOfTest, BasicAlignedArray) {
167  // Note: this code exclusively uses the struct-wrapped arbitrarily aligned
168  // types because of the bugs mentioned above where GCC and Clang both
169  // disregard the arbitrary alignment specifier until the type is used to
170  // declare a member of a struct.
171  EXPECT_LE(1u, alignOf<AlignedCharArray<SA1>::union_type>());
172  EXPECT_LE(2u, alignOf<AlignedCharArray<SA2>::union_type>());
173  EXPECT_LE(4u, alignOf<AlignedCharArray<SA4>::union_type>());
174  EXPECT_LE(8u, alignOf<AlignedCharArray<SA8>::union_type>());
175
176  EXPECT_LE(1u, sizeof(AlignedCharArray<SA1>::union_type));
177  EXPECT_LE(2u, sizeof(AlignedCharArray<SA2>::union_type));
178  EXPECT_LE(4u, sizeof(AlignedCharArray<SA4>::union_type));
179  EXPECT_LE(8u, sizeof(AlignedCharArray<SA8>::union_type));
180
181  EXPECT_EQ(1u, (alignOf<AlignedCharArray<SA1>::union_type>()));
182  EXPECT_EQ(2u, (alignOf<AlignedCharArray<SA1, SA2>::union_type>()));
183  EXPECT_EQ(4u, (alignOf<AlignedCharArray<SA1, SA2, SA4>::union_type>()));
184  EXPECT_EQ(8u, (alignOf<AlignedCharArray<SA1, SA2, SA4, SA8>::union_type>()));
185
186  EXPECT_EQ(1u, sizeof(AlignedCharArray<SA1>::union_type));
187  EXPECT_EQ(2u, sizeof(AlignedCharArray<SA1, SA2>::union_type));
188  EXPECT_EQ(4u, sizeof(AlignedCharArray<SA1, SA2, SA4>::union_type));
189  EXPECT_EQ(8u, sizeof(AlignedCharArray<SA1, SA2, SA4, SA8>::union_type));
190
191  EXPECT_EQ(1u, (alignOf<AlignedCharArray<SA1[1]>::union_type>()));
192  EXPECT_EQ(2u, (alignOf<AlignedCharArray<SA1[2], SA2[1]>::union_type>()));
193  EXPECT_EQ(4u, (alignOf<AlignedCharArray<SA1[42], SA2[55],
194                                          SA4[13]>::union_type>()));
195  EXPECT_EQ(8u, (alignOf<AlignedCharArray<SA1[2], SA2[1],
196                                          SA4, SA8>::union_type>()));
197
198  EXPECT_EQ(1u,  sizeof(AlignedCharArray<SA1[1]>::union_type));
199  EXPECT_EQ(2u,  sizeof(AlignedCharArray<SA1[2], SA2[1]>::union_type));
200  EXPECT_EQ(4u,  sizeof(AlignedCharArray<SA1[3], SA2[2], SA4>::union_type));
201  EXPECT_EQ(16u, sizeof(AlignedCharArray<SA1, SA2[3],
202                                         SA4[3], SA8>::union_type));
203
204  // For other tests we simply assert that the alignment of the union mathes
205  // that of the fundamental type and hope that we have any weird type
206  // productions that would trigger bugs.
207  EXPECT_EQ(alignOf<char>(), alignOf<AlignedCharArray<char>::union_type>());
208  EXPECT_EQ(alignOf<short>(), alignOf<AlignedCharArray<short>::union_type>());
209  EXPECT_EQ(alignOf<int>(), alignOf<AlignedCharArray<int>::union_type>());
210  EXPECT_EQ(alignOf<long>(), alignOf<AlignedCharArray<long>::union_type>());
211  EXPECT_EQ(alignOf<long long>(),
212            alignOf<AlignedCharArray<long long>::union_type>());
213  EXPECT_EQ(alignOf<float>(), alignOf<AlignedCharArray<float>::union_type>());
214  EXPECT_EQ(alignOf<double>(), alignOf<AlignedCharArray<double>::union_type>());
215  EXPECT_EQ(alignOf<long double>(),
216            alignOf<AlignedCharArray<long double>::union_type>());
217  EXPECT_EQ(alignOf<void *>(), alignOf<AlignedCharArray<void *>::union_type>());
218  EXPECT_EQ(alignOf<int *>(), alignOf<AlignedCharArray<int *>::union_type>());
219  EXPECT_EQ(alignOf<double (*)(double)>(),
220            alignOf<AlignedCharArray<double (*)(double)>::union_type>());
221  EXPECT_EQ(alignOf<double (S6::*)()>(),
222            alignOf<AlignedCharArray<double (S6::*)()>::union_type>());
223  EXPECT_EQ(alignOf<S1>(), alignOf<AlignedCharArray<S1>::union_type>());
224  EXPECT_EQ(alignOf<S2>(), alignOf<AlignedCharArray<S2>::union_type>());
225  EXPECT_EQ(alignOf<S3>(), alignOf<AlignedCharArray<S3>::union_type>());
226  EXPECT_EQ(alignOf<S4>(), alignOf<AlignedCharArray<S4>::union_type>());
227  EXPECT_EQ(alignOf<S5>(), alignOf<AlignedCharArray<S5>::union_type>());
228  EXPECT_EQ(alignOf<S6>(), alignOf<AlignedCharArray<S6>::union_type>());
229  EXPECT_EQ(alignOf<D1>(), alignOf<AlignedCharArray<D1>::union_type>());
230  EXPECT_EQ(alignOf<D2>(), alignOf<AlignedCharArray<D2>::union_type>());
231  EXPECT_EQ(alignOf<D3>(), alignOf<AlignedCharArray<D3>::union_type>());
232  EXPECT_EQ(alignOf<D4>(), alignOf<AlignedCharArray<D4>::union_type>());
233  EXPECT_EQ(alignOf<D5>(), alignOf<AlignedCharArray<D5>::union_type>());
234  EXPECT_EQ(alignOf<D6>(), alignOf<AlignedCharArray<D6>::union_type>());
235  EXPECT_EQ(alignOf<D7>(), alignOf<AlignedCharArray<D7>::union_type>());
236  EXPECT_EQ(alignOf<D8>(), alignOf<AlignedCharArray<D8>::union_type>());
237  EXPECT_EQ(alignOf<D9>(), alignOf<AlignedCharArray<D9>::union_type>());
238  EXPECT_EQ(alignOf<V1>(), alignOf<AlignedCharArray<V1>::union_type>());
239  EXPECT_EQ(alignOf<V2>(), alignOf<AlignedCharArray<V2>::union_type>());
240  EXPECT_EQ(alignOf<V3>(), alignOf<AlignedCharArray<V3>::union_type>());
241  EXPECT_EQ(alignOf<V4>(), alignOf<AlignedCharArray<V4>::union_type>());
242  EXPECT_EQ(alignOf<V5>(), alignOf<AlignedCharArray<V5>::union_type>());
243  EXPECT_EQ(alignOf<V6>(), alignOf<AlignedCharArray<V6>::union_type>());
244  EXPECT_EQ(alignOf<V7>(), alignOf<AlignedCharArray<V7>::union_type>());
245  EXPECT_EQ(alignOf<V8>(), alignOf<AlignedCharArray<V8>::union_type>());
246}
247
248}
249