1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
24 #include <cppu/macros.hxx>
25 #include <rtl/ustring.hxx>
26 #include <sal/types.h>
42 sal_Int32 n2
CPPU_GCC3_ALIGN( C1
);
51 sal_Int32 n4
CPPU_GCC3_ALIGN( C3
);
61 C5 c6
CPPU_GCC3_ALIGN( C1
);
72 // [-loplugin:fakebool] false positive:
74 // [-loplugin:fakebool] false positive:
76 // [-loplugin:fakebool] false positive:
90 sal_Int16 p
CPPU_GCC3_ALIGN( M
);
105 sal_Int16 p2
CPPU_GCC3_ALIGN( O
);
116 struct second
: public empty
121 struct AlignSize_Impl
131 struct Char2
: public Char1
133 char c2
CPPU_GCC3_ALIGN( Char1
);
135 struct Char3
: public Char2
137 char c3
CPPU_GCC3_ALIGN( Char2
);
148 // [-loplugin:fakebool] false positive:
149 static_assert( static_cast<sal_Bool
>(true) == sal_True
,
150 "must be binary compatible" );
151 // [-loplugin:fakebool] false positive:
152 static_assert( static_cast<sal_Bool
>(false) == sal_False
,
153 "must be binary compatible" );
154 #if SAL_TYPES_ALIGNMENT8 == 2
155 static_assert(offsetof(AlignSize_Impl
, dDouble
) == 2, "offsetof(AlignSize_Impl, dDouble) != 2");
156 static_assert(sizeof(AlignSize_Impl
) == 10, "sizeof(AlignSize_Impl) != 10");
157 #elif SAL_TYPES_ALIGNMENT8 == 4
158 static_assert(offsetof(AlignSize_Impl
, dDouble
) == 4, "offsetof(AlignSize_Impl, dDouble) != 4");
159 static_assert(sizeof(AlignSize_Impl
) == 12, "sizeof(AlignSize_Impl) != 12");
160 #elif SAL_TYPES_ALIGNMENT8 == 8
161 static_assert(offsetof(AlignSize_Impl
, dDouble
) == 8, "offsetof(AlignSize_Impl, dDouble) != 8");
162 static_assert(sizeof(AlignSize_Impl
) == 16, "sizeof(AlignSize_Impl) != 16");
164 # error unexpected alignment of 8 byte types
168 static_assert((SAL_SEQUENCE_HEADER_SIZE
% 8) == 0, "binary compatibility test failed: (SAL_SEQUENCE_HEADER_SIZE % 8) == 0!!!");
170 static_assert(sizeof(Enum
) == sizeof(sal_Int32
), "binary compatibility test failed: (sizeof(Enum) == sizeof(sal_Int32))");
172 static_assert(sizeof(void *) >= sizeof(sal_Int32
), "binary compatibility test failed: (sizeof(void *) >= sizeof(sal_Int32))");
173 static_assert(sizeof(uno_Any
) == sizeof(void *) * 3, "binary compatibility test failed: (sizeof(uno_Any) == sizeof(void *) * 3");
174 static_assert(offsetof(uno_Any
, pType
) == 0, "offsetof(uno_Any, pType) != 0");
175 static_assert(offsetof(uno_Any
, pData
) == 1 * sizeof(void *), "offsetof(uno_Any, pTData) != (1 * sizeof(void *))");
176 static_assert(offsetof(uno_Any
, pReserved
) == 2 * sizeof(void *), "offsetof(uno_Any, pReserved) != (2 * sizeof(void *))");
178 static_assert(sizeof(OUString
) == sizeof(rtl_uString
*), "binary compatibility test failed: sizeof(OUString) != sizeof(rtl_uString *)");
180 #if SAL_TYPES_ALIGNMENT8 == 2
181 static_assert(sizeof(M
) == 6, "sizeof(M) != 6");
182 static_assert(sizeof(N
) == 8, "sizeof(N) != 8");
183 static_assert(sizeof(N2
) == 8, "sizeof(N2) != 8");
184 static_assert(offsetof(N2
, p
) == 6, "offsetof(N2, p) != 6");
186 static_assert(sizeof(M
) == 8, "sizeof(M) != 8");
187 static_assert(sizeof(N
) == 12, "sizeof(N) != 12");
188 static_assert(sizeof(N2
) == 12, "sizeof(N2) != 12");
189 static_assert(offsetof(N2
, p
) == 8, "offsetof(N2, p) != 8");
191 static_assert(offsetof(M
, o
) == 4, "offsetof(M, o) != 4");
193 #if SAL_TYPES_ALIGNMENT8 == 2
194 static_assert(sizeof(O
) == 16, "sizeof(O) != 16");
195 #elif SAL_TYPES_ALIGNMENT8 == 4
196 static_assert(sizeof(O
) == 20, "sizeof(O) != 20");
197 #elif SAL_TYPES_ALIGNMENT8 == 8
198 static_assert(sizeof(O
) == 24, "sizeof(O) != 24");
200 # error unexpected alignment of 8 byte types
203 #if SAL_TYPES_ALIGNMENT8 == 2
204 static_assert(sizeof(C2
) == 6, "sizeof(C2) != 6");
205 static_assert(sizeof(D
) == 6, "sizeof(D) != 6");
206 static_assert(offsetof(D
, e
) == 2, "offsetof(D, e) != 2");
207 static_assert(offsetof(E
, e
) == 6, "offsetof(E, e) != 6");
209 static_assert(sizeof(C2
) == 8, "sizeof(C2) != 8");
210 static_assert(sizeof(D
) == 8, "sizeof(D) != 8");
211 static_assert(offsetof(D
, e
) == 4, "offsetof(D, e) != 4");
212 static_assert(offsetof(E
, e
) == 8, "offsetof(E, e) != 8");
215 static_assert(sizeof(C1
) == 2, "sizeof(C1) != 2");
216 static_assert(offsetof(E
, d
) == 4, "offsetof(E, d) != 4");
218 #if SAL_TYPES_ALIGNMENT8 == 2
219 static_assert(sizeof(C3
) == 18, "sizeof(C3) != 18");
220 static_assert(sizeof(C4
) == 30, "sizeof(C4) != 30");
221 static_assert(sizeof(C5
) == 40, "sizeof(C5) != 40");
222 static_assert(sizeof(C6
) == 44, "sizeof(C6) != 44");
224 static_assert(sizeof(O2
) == 18, "sizeof(O2) != 18");
225 #elif SAL_TYPES_ALIGNMENT8 == 4
226 static_assert(sizeof(C3
) == 20, "sizeof(C3) != 20");
227 static_assert(sizeof(C4
) == 32, "sizeof(C4) != 32");
228 static_assert(sizeof(C5
) == 44, "sizeof(C5) != 44");
229 static_assert(sizeof(C6
) == 52, "sizeof(C6) != 52");
231 static_assert(sizeof(O2
) == 24, "sizeof(O2) != 24");
232 #elif SAL_TYPES_ALIGNMENT8 == 8
233 static_assert(sizeof(C3
) == 24, "sizeof(C3) != 24");
234 static_assert(sizeof(C4
) == 40, "sizeof(C4) != 40");
235 static_assert(sizeof(C5
) == 56, "sizeof(C5) != 56");
236 static_assert(sizeof(C6
) == 72, "sizeof(C6) != 72");
238 static_assert(sizeof(O2
) == 32, "sizeof(O2) != 32");
240 # error unexpected alignment of 8 byte types
243 static_assert(sizeof(Char3
) == 3, "sizeof(Char3) != 3");
245 #if SAL_TYPES_ALIGNMENT8 == 2
246 // max alignment is 2
247 static_assert(sizeof(P
) == 16, "sizeof(P) != 16");
248 #elif SAL_TYPES_ALIGNMENT8 == 4
249 // max alignment is 4
250 static_assert(sizeof(P
) == 20, "sizeof(P) != 20");
251 #elif SAL_TYPES_ALIGNMENT8 == 8
252 // alignment of P is 8, because of P[] ...
253 static_assert(sizeof(P
) == 24, "sizeof(P) != 24");
254 static_assert(sizeof(second
) == sizeof(int), "sizeof(second) != sizeof(int)");
256 # error unexpected alignment of 8 byte types
259 #if OSL_DEBUG_LEVEL > 0 && !defined NDEBUG
263 #if defined __GNUC__ && (__GNUC__ < 12 || (__GNUC__ == 12 && __GNUC_MINOR__ < 1)) && !defined __clang__
264 #pragma GCC diagnostic push
265 #pragma GCC diagnostic ignored "-Wattributes"
267 [[maybe_unused
]] Char3 chars
;
268 #if defined __GNUC__ && (__GNUC__ < 12 || (__GNUC__ == 12 && __GNUC_MINOR__ < 1)) && !defined __clang__
269 #pragma GCC diagnostic pop
274 template<typename T1
, typename T2
> std::size_t OFFSET_OF(T2
T1::* p
) {
275 return reinterpret_cast< size_t >(reinterpret_cast<char *>(&(reinterpret_cast<T1
*>(16)->*p
)) -16);
278 class BinaryCompatible_Impl
281 BinaryCompatible_Impl();
283 BinaryCompatible_Impl::BinaryCompatible_Impl()
285 assert(OFFSET_OF(&N::p
) == 8);
287 assert(OFFSET_OF(&C2::n2
) == 4);
289 #if SAL_TYPES_ALIGNMENT8 == 2
290 assert(OFFSET_OF(&C3::d3
) == 6);
291 assert(OFFSET_OF(&C3::n3
) == 14);
292 assert(OFFSET_OF(&C4::n4
) == 18);
293 assert(OFFSET_OF(&C4::d4
) == 22);
294 assert(OFFSET_OF(&C5::n5
) == 30);
295 assert(OFFSET_OF(&C5::b5
) == 38);
296 assert(OFFSET_OF(&C6::c6
) == 2);
297 assert(OFFSET_OF(&C6::b6
) == 42);
299 assert(OFFSET_OF(&O2::p2
) == 16);
300 #elif SAL_TYPES_ALIGNMENT8 == 4
301 assert(OFFSET_OF(&C3::d3
) == 8);
302 assert(OFFSET_OF(&C3::n3
) == 16);
303 assert(OFFSET_OF(&C4::n4
) == 20);
304 assert(OFFSET_OF(&C4::d4
) == 24);
305 assert(OFFSET_OF(&C5::n5
) == 32);
306 assert(OFFSET_OF(&C5::b5
) == 40);
307 assert(OFFSET_OF(&C6::c6
) == 4);
308 assert(OFFSET_OF(&C6::b6
) == 48);
310 assert(OFFSET_OF(&O2::p2
) == 20);
311 #elif SAL_TYPES_ALIGNMENT8 == 8
312 assert(OFFSET_OF(&C3::d3
) == 8);
313 assert(OFFSET_OF(&C3::n3
) == 16);
314 assert(OFFSET_OF(&C4::n4
) == 24);
315 assert(OFFSET_OF(&C4::d4
) == 32);
316 assert(OFFSET_OF(&C5::n5
) == 40);
317 assert(OFFSET_OF(&C5::b5
) == 48);
318 assert(OFFSET_OF(&C6::c6
) == 8);
319 assert(OFFSET_OF(&C6::b6
) == 64);
321 assert(OFFSET_OF(&O2::p2
) == 24);
323 # error unexpected alignment of 8 byte types
326 assert(OFFSET_OF(&Char4::c
) == 3);
329 BinaryCompatible_Impl aTest
;
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */