VirtualBox

source: vbox/trunk/include/iprt/assertcompile.h@ 96758

Last change on this file since 96758 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.6 KB
Line 
1/** @file
2 * IPRT - Compile Time Assertions.
3 */
4
5/*
6 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
14 * License.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
23 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
29 *
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef IPRT_INCLUDED_assertcompile_h
37#define IPRT_INCLUDED_assertcompile_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/cdefs.h>
43
44/** @defgroup grp_rt_assert_compile Compile time assertions
45 * @ingroup grp_rt
46 *
47 * These assertions are used to check structure sizes, member/size alignments
48 * and similar compile time expressions.
49 *
50 * @remarks As you might have noticed, the AssertCompile macros don't follow the
51 * coding guidelines wrt to macros supposedly being all uppercase and
52 * underscored. For various reasons they don't, and nobody has
53 * complained yet.
54 *
55 * @{
56 */
57
58/**
59 * RTASSERTTYPE is the type the AssertCompile() macro redefines.
60 * It has no other function and shouldn't be used.
61 * Visual C++ uses this.
62 */
63typedef int RTASSERTTYPE[1];
64
65/**
66 * RTASSERTVAR is the type the AssertCompile() macro redefines.
67 * It has no other function and shouldn't be used.
68 * GCC uses this.
69 */
70#ifdef __GNUC__
71RT_C_DECLS_BEGIN
72#endif
73extern int RTASSERTVAR[1];
74#ifdef __GNUC__
75RT_C_DECLS_END
76#endif
77
78/** @def RTASSERT_HAVE_STATIC_ASSERT
79 * Indicates that the compiler implements static_assert(expr, msg).
80 */
81#ifdef _MSC_VER
82# if _MSC_VER >= 1600 && defined(__cplusplus)
83# define RTASSERT_HAVE_STATIC_ASSERT
84# endif
85#endif
86#if defined(__GNUC__) && defined(__GXX_EXPERIMENTAL_CXX0X__)
87# define RTASSERT_HAVE_STATIC_ASSERT
88#endif
89#if RT_CLANG_PREREQ(6, 0)
90# if __has_feature(cxx_static_assert) || __has_feature(c_static_assert)
91# define RTASSERT_HAVE_STATIC_ASSERT
92# endif
93#endif
94#ifdef DOXYGEN_RUNNING
95# define RTASSERT_HAVE_STATIC_ASSERT
96#endif
97
98/** @def AssertCompileNS
99 * Asserts that a compile-time expression is true. If it's not break the build.
100 *
101 * This differs from AssertCompile in that it accepts some more expressions
102 * than what C++0x allows - NS = Non-standard.
103 *
104 * @param expr Expression which should be true.
105 */
106#ifdef __GNUC__
107# define AssertCompileNS(expr) extern int RTASSERTVAR[1] __attribute__((__unused__)), RTASSERTVAR[(expr) ? 1 : 0] __attribute__((__unused__))
108#elif defined(__IBMC__) || defined(__IBMCPP__)
109# define AssertCompileNS(expr) extern int RTASSERTVAR[(expr) ? 1 : 0]
110#else
111# define AssertCompileNS(expr) typedef int RTASSERTTYPE[(expr) ? 1 : 0]
112#endif
113
114/** @def AssertCompile
115 * Asserts that a C++0x compile-time expression is true. If it's not break the
116 * build.
117 * @param expr Expression which should be true.
118 */
119#ifdef RTASSERT_HAVE_STATIC_ASSERT
120# ifdef __cplusplus
121# define AssertCompile(expr) static_assert(!!(expr), #expr)
122# else
123# define AssertCompile(expr) _Static_assert(!!(expr), #expr)
124# endif
125#else
126# define AssertCompile(expr) AssertCompileNS(expr)
127#endif
128
129/** @def RTASSERT_OFFSET_OF()
130 * A offsetof() macro suitable for compile time assertions.
131 * Both GCC v4 and VisualAge for C++ v3.08 has trouble using RT_OFFSETOF.
132 */
133#if defined(__GNUC__)
134# if __GNUC__ >= 4
135# define RTASSERT_OFFSET_OF(a_Type, a_Member) __builtin_offsetof(a_Type, a_Member)
136# else
137# define RTASSERT_OFFSET_OF(a_Type, a_Member) RT_OFFSETOF(a_Type, a_Member)
138# endif
139#elif (defined(__IBMC__) || defined(__IBMCPP__)) && defined(RT_OS_OS2)
140# define RTASSERT_OFFSET_OF(a_Type, a_Member) __offsetof(a_Type, a_Member)
141#elif (defined(__WATCOMC__) && defined(__cplusplus))
142# define RTASSERT_OFFSET_OF(a_Type, a_Member) __offsetof(a_Type, a_Member)
143#else
144# define RTASSERT_OFFSET_OF(a_Type, a_Member) RT_OFFSETOF(a_Type, a_Member)
145#endif
146
147
148/** @def AssertCompileSize
149 * Asserts a size at compile.
150 * @param type The type.
151 * @param size The expected type size.
152 */
153#define AssertCompileSize(type, size) \
154 AssertCompile(sizeof(type) == (size))
155
156/** @def AssertCompileSizeAlignment
157 * Asserts a size alignment at compile.
158 * @param type The type.
159 * @param align The size alignment to assert.
160 */
161#define AssertCompileSizeAlignment(type, align) \
162 AssertCompile(!(sizeof(type) & ((align) - 1)))
163
164/** @def AssertCompileMemberSize
165 * Asserts a member offset alignment at compile.
166 * @param type The type.
167 * @param member The member.
168 * @param size The member size to assert.
169 */
170#define AssertCompileMemberSize(type, member, size) \
171 AssertCompile(RT_SIZEOFMEMB(type, member) == (size))
172
173/** @def AssertCompileMemberSizeAlignment
174 * Asserts a member size alignment at compile.
175 * @param type The type.
176 * @param member The member.
177 * @param align The member size alignment to assert.
178 */
179#define AssertCompileMemberSizeAlignment(type, member, align) \
180 AssertCompile(!(RT_SIZEOFMEMB(type, member) & ((align) - 1)))
181
182/** @def AssertCompileMemberAlignment
183 * Asserts a member offset alignment at compile.
184 * @param type The type.
185 * @param member The member.
186 * @param align The member offset alignment to assert.
187 */
188#define AssertCompileMemberAlignment(type, member, align) \
189 AssertCompile(!(RTASSERT_OFFSET_OF(type, member) & ((align) - 1)))
190
191/** @def AssertCompileMemberOffset
192 * Asserts an offset of a structure member at compile.
193 * @param type The type.
194 * @param member The member.
195 * @param off The expected offset.
196 */
197#define AssertCompileMemberOffset(type, member, off) \
198 AssertCompile(RTASSERT_OFFSET_OF(type, member) == (off))
199
200/** @def AssertCompile2MemberOffsets
201 * Asserts that two (sub-structure) members in union have the same offset.
202 * @param type The type.
203 * @param member1 The first member.
204 * @param member2 The second member.
205 */
206#define AssertCompile2MemberOffsets(type, member1, member2) \
207 AssertCompile(RTASSERT_OFFSET_OF(type, member1) == RTASSERT_OFFSET_OF(type, member2))
208
209/** @def AssertCompileAdjacentMembers
210 * Asserts that two structure members are adjacent.
211 * @param type The type.
212 * @param member1 The first member.
213 * @param member2 The second member.
214 */
215#define AssertCompileAdjacentMembers(type, member1, member2) \
216 AssertCompile(RTASSERT_OFFSET_OF(type, member1) + RT_SIZEOFMEMB(type, member1) == RTASSERT_OFFSET_OF(type, member2))
217
218/** @def AssertCompileMembersAtSameOffset
219 * Asserts that members of two different structures are at the same offset.
220 * @param type1 The first type.
221 * @param member1 The first member.
222 * @param type2 The second type.
223 * @param member2 The second member.
224 */
225#define AssertCompileMembersAtSameOffset(type1, member1, type2, member2) \
226 AssertCompile(RTASSERT_OFFSET_OF(type1, member1) == RTASSERT_OFFSET_OF(type2, member2))
227
228/** @def AssertCompileMembersSameSize
229 * Asserts that members of two different structures have the same size.
230 * @param type1 The first type.
231 * @param member1 The first member.
232 * @param type2 The second type.
233 * @param member2 The second member.
234 */
235#define AssertCompileMembersSameSize(type1, member1, type2, member2) \
236 AssertCompile(RT_SIZEOFMEMB(type1, member1) == RT_SIZEOFMEMB(type2, member2))
237
238/** @def AssertCompileMembersSameSizeAndOffset
239 * Asserts that members of two different structures have the same size and are
240 * at the same offset.
241 * @param type1 The first type.
242 * @param member1 The first member.
243 * @param type2 The second type.
244 * @param member2 The second member.
245 */
246#define AssertCompileMembersSameSizeAndOffset(type1, member1, type2, member2) \
247 AssertCompile( RTASSERT_OFFSET_OF(type1, member1) == RTASSERT_OFFSET_OF(type2, member2) \
248 && RT_SIZEOFMEMB(type1, member1) == RT_SIZEOFMEMB(type2, member2))
249
250/** @} */
251
252#endif /* !IPRT_INCLUDED_assertcompile_h */
253
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette