1 //=== - llvm/unittest/Support/AlignOfTest.cpp - Alignment utility tests ---===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 // Disable warnings about alignment-based structure padding.
11 // This must be above the includes to suppress warnings in included templates.
12 #pragma warning(disable:4324)
15 #include "llvm/Support/AlignOf.h"
16 #include "llvm/Support/Compiler.h"
17 #include "gtest/gtest.h"
22 // Disable warnings about questionable type definitions.
23 // We're testing that even questionable types work with the alignment utilities.
25 #pragma warning(disable:4584)
28 // Suppress direct base '{anonymous}::S1' inaccessible in '{anonymous}::D9'
29 // due to ambiguity warning.
31 #pragma clang diagnostic ignored "-Wunknown-pragmas"
32 #pragma clang diagnostic ignored "-Winaccessible-base"
33 #elif ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
34 // Pragma based warning suppression was introduced in GGC 4.2. Additionally
35 // this warning is "enabled by default". The warning still appears if -Wall is
36 // suppressed. Apparently GCC suppresses it when -w is specifed, which is odd.
37 #pragma GCC diagnostic warning "-w"
40 // Define some fixed alignment types to use in these tests.
41 struct alignas(1) A1
{};
42 struct alignas(2) A2
{};
43 struct alignas(4) A4
{};
44 struct alignas(8) A8
{};
47 struct S2
{ char a
; };
49 struct S4
{ double y
; };
50 struct S5
{ A1 a1
; A2 a2
; A4 a4
; A8 a8
; };
51 struct S6
{ double f(); };
53 struct D2
: S6
{ float g(); };
55 struct D4
: S2
{ int x
; };
56 struct D5
: S3
{ char c
; };
57 struct D6
: S2
, S3
{};
58 struct D7
: S1
, S3
{};
59 struct D8
: S1
, D4
, D5
{ double x
[2]; };
60 struct D9
: S1
, D1
{ S1 s1
; };
61 struct V1
{ virtual ~V1(); };
62 struct V2
{ int x
; virtual ~V2(); };
66 struct V4
: virtual V2
{ int y
;
69 struct V5
: V4
, V3
{ double z
;
72 struct V6
: S1
{ virtual ~V6(); };
73 struct V7
: virtual V2
, virtual V6
{
76 struct V8
: V5
, virtual V6
, V7
{ double zz
;
80 double S6::f() { return 0.0; }
81 float D2::g() { return 0.0f
; }
91 template <typename M
> struct T
{ M m
; };
93 TEST(AlignOfTest
, BasicAlignedArray
) {
94 EXPECT_LE(1u, alignof(AlignedCharArrayUnion
<A1
>));
95 EXPECT_LE(2u, alignof(AlignedCharArrayUnion
<A2
>));
96 EXPECT_LE(4u, alignof(AlignedCharArrayUnion
<A4
>));
97 EXPECT_LE(8u, alignof(AlignedCharArrayUnion
<A8
>));
99 EXPECT_LE(1u, sizeof(AlignedCharArrayUnion
<A1
>));
100 EXPECT_LE(2u, sizeof(AlignedCharArrayUnion
<A2
>));
101 EXPECT_LE(4u, sizeof(AlignedCharArrayUnion
<A4
>));
102 EXPECT_LE(8u, sizeof(AlignedCharArrayUnion
<A8
>));
104 EXPECT_EQ(1u, (alignof(AlignedCharArrayUnion
<A1
>)));
105 EXPECT_EQ(2u, (alignof(AlignedCharArrayUnion
<A1
, A2
>)));
106 EXPECT_EQ(4u, (alignof(AlignedCharArrayUnion
<A1
, A2
, A4
>)));
107 EXPECT_EQ(8u, (alignof(AlignedCharArrayUnion
<A1
, A2
, A4
, A8
>)));
109 EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion
<A1
>));
110 EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion
<A1
, A2
>));
111 EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion
<A1
, A2
, A4
>));
112 EXPECT_EQ(8u, sizeof(AlignedCharArrayUnion
<A1
, A2
, A4
, A8
>));
114 EXPECT_EQ(1u, (alignof(AlignedCharArrayUnion
<A1
[1]>)));
115 EXPECT_EQ(2u, (alignof(AlignedCharArrayUnion
<A1
[2], A2
[1]>)));
116 EXPECT_EQ(4u, (alignof(AlignedCharArrayUnion
<A1
[42], A2
[55], A4
[13]>)));
117 EXPECT_EQ(8u, (alignof(AlignedCharArrayUnion
<A1
[2], A2
[1], A4
, A8
>)));
119 EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion
<A1
[1]>));
120 EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion
<A1
[2], A2
[1]>));
121 EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion
<A1
[3], A2
[2], A4
>));
122 EXPECT_EQ(16u, sizeof(AlignedCharArrayUnion
<A1
, A2
[3],
125 // For other tests we simply assert that the alignment of the union mathes
126 // that of the fundamental type and hope that we have any weird type
127 // productions that would trigger bugs.
128 EXPECT_EQ(alignof(T
<char>), alignof(AlignedCharArrayUnion
<char>));
129 EXPECT_EQ(alignof(T
<short>), alignof(AlignedCharArrayUnion
<short>));
130 EXPECT_EQ(alignof(T
<int>), alignof(AlignedCharArrayUnion
<int>));
131 EXPECT_EQ(alignof(T
<long>), alignof(AlignedCharArrayUnion
<long>));
132 EXPECT_EQ(alignof(T
<long long>), alignof(AlignedCharArrayUnion
<long long>));
133 EXPECT_EQ(alignof(T
<float>), alignof(AlignedCharArrayUnion
<float>));
135 EXPECT_LE(alignof(T
<double>), alignof(AlignedCharArrayUnion
<double>));
136 EXPECT_LE(alignof(T
<long double>),
137 alignof(AlignedCharArrayUnion
<long double>));
138 EXPECT_LE(alignof(S4
), alignof(AlignedCharArrayUnion
<S4
>));
140 EXPECT_EQ(alignof(T
<double>), alignof(AlignedCharArrayUnion
<double>));
141 EXPECT_EQ(alignof(T
<long double>),
142 alignof(AlignedCharArrayUnion
<long double>));
143 EXPECT_EQ(alignof(S4
), alignof(AlignedCharArrayUnion
<S4
>));
145 EXPECT_EQ(alignof(T
<void *>), alignof(AlignedCharArrayUnion
<void *>));
146 EXPECT_EQ(alignof(T
<int *>), alignof(AlignedCharArrayUnion
<int *>));
147 EXPECT_EQ(alignof(T
<double (*)(double)>),
148 alignof(AlignedCharArrayUnion
<double (*)(double)>));
149 EXPECT_EQ(alignof(T
<double (S6::*)()>),
150 alignof(AlignedCharArrayUnion
<double (S6::*)()>));
151 EXPECT_EQ(alignof(S1
), alignof(AlignedCharArrayUnion
<S1
>));
152 EXPECT_EQ(alignof(S2
), alignof(AlignedCharArrayUnion
<S2
>));
153 EXPECT_EQ(alignof(S3
), alignof(AlignedCharArrayUnion
<S3
>));
154 EXPECT_EQ(alignof(S5
), alignof(AlignedCharArrayUnion
<S5
>));
155 EXPECT_EQ(alignof(S6
), alignof(AlignedCharArrayUnion
<S6
>));
156 EXPECT_EQ(alignof(D1
), alignof(AlignedCharArrayUnion
<D1
>));
157 EXPECT_EQ(alignof(D2
), alignof(AlignedCharArrayUnion
<D2
>));
158 EXPECT_EQ(alignof(D3
), alignof(AlignedCharArrayUnion
<D3
>));
159 EXPECT_EQ(alignof(D4
), alignof(AlignedCharArrayUnion
<D4
>));
160 EXPECT_EQ(alignof(D5
), alignof(AlignedCharArrayUnion
<D5
>));
161 EXPECT_EQ(alignof(D6
), alignof(AlignedCharArrayUnion
<D6
>));
162 EXPECT_EQ(alignof(D7
), alignof(AlignedCharArrayUnion
<D7
>));
163 EXPECT_EQ(alignof(D8
), alignof(AlignedCharArrayUnion
<D8
>));
164 EXPECT_EQ(alignof(D9
), alignof(AlignedCharArrayUnion
<D9
>));
165 EXPECT_EQ(alignof(V1
), alignof(AlignedCharArrayUnion
<V1
>));
166 EXPECT_EQ(alignof(V2
), alignof(AlignedCharArrayUnion
<V2
>));
167 EXPECT_EQ(alignof(V3
), alignof(AlignedCharArrayUnion
<V3
>));
168 EXPECT_EQ(alignof(V4
), alignof(AlignedCharArrayUnion
<V4
>));
169 EXPECT_EQ(alignof(V5
), alignof(AlignedCharArrayUnion
<V5
>));
170 EXPECT_EQ(alignof(V6
), alignof(AlignedCharArrayUnion
<V6
>));
171 EXPECT_EQ(alignof(V7
), alignof(AlignedCharArrayUnion
<V7
>));
173 // Some versions of MSVC get this wrong somewhat disturbingly. The failure
174 // appears to be benign: alignof(V8) produces a preposterous value: 12
176 EXPECT_EQ(alignof(V8
), alignof(AlignedCharArrayUnion
<V8
>));
179 EXPECT_EQ(sizeof(char), sizeof(AlignedCharArrayUnion
<char>));
180 EXPECT_EQ(sizeof(char[1]), sizeof(AlignedCharArrayUnion
<char[1]>));
181 EXPECT_EQ(sizeof(char[2]), sizeof(AlignedCharArrayUnion
<char[2]>));
182 EXPECT_EQ(sizeof(char[3]), sizeof(AlignedCharArrayUnion
<char[3]>));
183 EXPECT_EQ(sizeof(char[4]), sizeof(AlignedCharArrayUnion
<char[4]>));
184 EXPECT_EQ(sizeof(char[5]), sizeof(AlignedCharArrayUnion
<char[5]>));
185 EXPECT_EQ(sizeof(char[8]), sizeof(AlignedCharArrayUnion
<char[8]>));
186 EXPECT_EQ(sizeof(char[13]), sizeof(AlignedCharArrayUnion
<char[13]>));
187 EXPECT_EQ(sizeof(char[16]), sizeof(AlignedCharArrayUnion
<char[16]>));
188 EXPECT_EQ(sizeof(char[21]), sizeof(AlignedCharArrayUnion
<char[21]>));
189 EXPECT_EQ(sizeof(char[32]), sizeof(AlignedCharArrayUnion
<char[32]>));
190 EXPECT_EQ(sizeof(short), sizeof(AlignedCharArrayUnion
<short>));
191 EXPECT_EQ(sizeof(int), sizeof(AlignedCharArrayUnion
<int>));
192 EXPECT_EQ(sizeof(long), sizeof(AlignedCharArrayUnion
<long>));
193 EXPECT_EQ(sizeof(long long),
194 sizeof(AlignedCharArrayUnion
<long long>));
195 EXPECT_EQ(sizeof(float), sizeof(AlignedCharArrayUnion
<float>));
196 EXPECT_EQ(sizeof(double), sizeof(AlignedCharArrayUnion
<double>));
197 EXPECT_EQ(sizeof(long double),
198 sizeof(AlignedCharArrayUnion
<long double>));
199 EXPECT_EQ(sizeof(void *), sizeof(AlignedCharArrayUnion
<void *>));
200 EXPECT_EQ(sizeof(int *), sizeof(AlignedCharArrayUnion
<int *>));
201 EXPECT_EQ(sizeof(double (*)(double)),
202 sizeof(AlignedCharArrayUnion
<double (*)(double)>));
203 EXPECT_EQ(sizeof(double (S6::*)()),
204 sizeof(AlignedCharArrayUnion
<double (S6::*)()>));
205 EXPECT_EQ(sizeof(S1
), sizeof(AlignedCharArrayUnion
<S1
>));
206 EXPECT_EQ(sizeof(S2
), sizeof(AlignedCharArrayUnion
<S2
>));
207 EXPECT_EQ(sizeof(S3
), sizeof(AlignedCharArrayUnion
<S3
>));
208 EXPECT_EQ(sizeof(S4
), sizeof(AlignedCharArrayUnion
<S4
>));
209 EXPECT_EQ(sizeof(S5
), sizeof(AlignedCharArrayUnion
<S5
>));
210 EXPECT_EQ(sizeof(S6
), sizeof(AlignedCharArrayUnion
<S6
>));
211 EXPECT_EQ(sizeof(D1
), sizeof(AlignedCharArrayUnion
<D1
>));
212 EXPECT_EQ(sizeof(D2
), sizeof(AlignedCharArrayUnion
<D2
>));
213 EXPECT_EQ(sizeof(D3
), sizeof(AlignedCharArrayUnion
<D3
>));
214 EXPECT_EQ(sizeof(D4
), sizeof(AlignedCharArrayUnion
<D4
>));
215 EXPECT_EQ(sizeof(D5
), sizeof(AlignedCharArrayUnion
<D5
>));
216 EXPECT_EQ(sizeof(D6
), sizeof(AlignedCharArrayUnion
<D6
>));
217 EXPECT_EQ(sizeof(D7
), sizeof(AlignedCharArrayUnion
<D7
>));
218 EXPECT_EQ(sizeof(D8
), sizeof(AlignedCharArrayUnion
<D8
>));
219 EXPECT_EQ(sizeof(D9
), sizeof(AlignedCharArrayUnion
<D9
>));
220 EXPECT_EQ(sizeof(D9
[1]), sizeof(AlignedCharArrayUnion
<D9
[1]>));
221 EXPECT_EQ(sizeof(D9
[2]), sizeof(AlignedCharArrayUnion
<D9
[2]>));
222 EXPECT_EQ(sizeof(D9
[3]), sizeof(AlignedCharArrayUnion
<D9
[3]>));
223 EXPECT_EQ(sizeof(D9
[4]), sizeof(AlignedCharArrayUnion
<D9
[4]>));
224 EXPECT_EQ(sizeof(D9
[5]), sizeof(AlignedCharArrayUnion
<D9
[5]>));
225 EXPECT_EQ(sizeof(D9
[8]), sizeof(AlignedCharArrayUnion
<D9
[8]>));
226 EXPECT_EQ(sizeof(D9
[13]), sizeof(AlignedCharArrayUnion
<D9
[13]>));
227 EXPECT_EQ(sizeof(D9
[16]), sizeof(AlignedCharArrayUnion
<D9
[16]>));
228 EXPECT_EQ(sizeof(D9
[21]), sizeof(AlignedCharArrayUnion
<D9
[21]>));
229 EXPECT_EQ(sizeof(D9
[32]), sizeof(AlignedCharArrayUnion
<D9
[32]>));
230 EXPECT_EQ(sizeof(V1
), sizeof(AlignedCharArrayUnion
<V1
>));
231 EXPECT_EQ(sizeof(V2
), sizeof(AlignedCharArrayUnion
<V2
>));
232 EXPECT_EQ(sizeof(V3
), sizeof(AlignedCharArrayUnion
<V3
>));
233 EXPECT_EQ(sizeof(V4
), sizeof(AlignedCharArrayUnion
<V4
>));
234 EXPECT_EQ(sizeof(V5
), sizeof(AlignedCharArrayUnion
<V5
>));
235 EXPECT_EQ(sizeof(V6
), sizeof(AlignedCharArrayUnion
<V6
>));
236 EXPECT_EQ(sizeof(V7
), sizeof(AlignedCharArrayUnion
<V7
>));
238 // Some versions of MSVC also get this wrong. The failure again appears to be
239 // benign: sizeof(V8) is only 52 bytes, but our array reserves 56.
241 EXPECT_EQ(sizeof(V8
), sizeof(AlignedCharArrayUnion
<V8
>));
244 } // end anonymous namespace