1 | /** @file
|
---|
2 | * IPRT - Compile Time Assertions.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2023 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 | */
|
---|
63 | typedef 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 | *
|
---|
69 | * GCC and IBM VisualAge C/C++ uses this. GCC doesn't technicaly need this
|
---|
70 | * global scope one as it declares it for each use, however things get
|
---|
71 | * complicated in C++ code where most GCC and clang versions gets upset by mixed
|
---|
72 | * "C" and "C++" versions of the symbol when using inside and outside
|
---|
73 | * RT_C_DECLS_BEGIN/END. The GCC 3.3.x and 3.4.x versions we use, OTOH will
|
---|
74 | * always complain about unused RTASSERTVAR for each AssertCompileNS use in a
|
---|
75 | * function if we declare it globally, so we don't do it for those, but we do
|
---|
76 | * for 4.x+ to prevent linkage confusion.
|
---|
77 | */
|
---|
78 | #if !defined(__cplusplus) || !defined(__GNUC__)
|
---|
79 | extern int RTASSERTVAR[1];
|
---|
80 | #elif RT_GNUC_PREREQ(4, 0) || defined(__clang_major__) /* Not sure when they fixed the global scoping __unused__/whatever problem. */
|
---|
81 | RT_C_DECLS_BEGIN
|
---|
82 | extern int RTASSERTVAR[1];
|
---|
83 | RT_C_DECLS_END
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | /** @def RTASSERT_HAVE_STATIC_ASSERT
|
---|
87 | * Indicates that the compiler implements static_assert(expr, msg).
|
---|
88 | */
|
---|
89 | #ifdef _MSC_VER
|
---|
90 | # if _MSC_VER >= 1600 && defined(__cplusplus)
|
---|
91 | # define RTASSERT_HAVE_STATIC_ASSERT
|
---|
92 | # endif
|
---|
93 | #endif
|
---|
94 | #if defined(__GNUC__) && defined(__GXX_EXPERIMENTAL_CXX0X__)
|
---|
95 | # define RTASSERT_HAVE_STATIC_ASSERT
|
---|
96 | #endif
|
---|
97 | #if RT_CLANG_PREREQ(6, 0)
|
---|
98 | # if __has_feature(cxx_static_assert) || __has_feature(c_static_assert)
|
---|
99 | # define RTASSERT_HAVE_STATIC_ASSERT
|
---|
100 | # endif
|
---|
101 | #endif
|
---|
102 | #ifdef DOXYGEN_RUNNING
|
---|
103 | # define RTASSERT_HAVE_STATIC_ASSERT
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | /** @def AssertCompileNS
|
---|
107 | * Asserts that a compile-time expression is true. If it's not break the build.
|
---|
108 | *
|
---|
109 | * This differs from AssertCompile in that it accepts some more expressions
|
---|
110 | * than what C++0x allows - NS = Non-standard.
|
---|
111 | *
|
---|
112 | * @param expr Expression which should be true.
|
---|
113 | */
|
---|
114 | #ifdef __GNUC__
|
---|
115 | # define AssertCompileNS(expr) AssertCompileNS2(expr,RTASSERTVAR)
|
---|
116 | # define AssertCompileNS2(expr,a_VarName) extern int a_VarName[ 1 ] __attribute__((__unused__)), \
|
---|
117 | a_VarName[(expr) ? 1 : 0] __attribute__((__unused__))
|
---|
118 | #elif defined(__IBMC__) || defined(__IBMCPP__)
|
---|
119 | # define AssertCompileNS(expr) extern int RTASSERTVAR[(expr) ? 1 : 0]
|
---|
120 | #else
|
---|
121 | # define AssertCompileNS(expr) typedef int RTASSERTTYPE[(expr) ? 1 : 0]
|
---|
122 | #endif
|
---|
123 |
|
---|
124 | /** @def AssertCompile
|
---|
125 | * Asserts that a C++0x compile-time expression is true. If it's not break the
|
---|
126 | * build.
|
---|
127 | * @param expr Expression which should be true.
|
---|
128 | */
|
---|
129 | #ifdef RTASSERT_HAVE_STATIC_ASSERT
|
---|
130 | # ifdef __cplusplus
|
---|
131 | # define AssertCompile(expr) static_assert(!!(expr), #expr)
|
---|
132 | # else
|
---|
133 | # define AssertCompile(expr) _Static_assert(!!(expr), #expr)
|
---|
134 | # endif
|
---|
135 | #else
|
---|
136 | # define AssertCompile(expr) AssertCompileNS(expr)
|
---|
137 | #endif
|
---|
138 |
|
---|
139 | /** @def RTASSERT_OFFSET_OF()
|
---|
140 | * A offsetof() macro suitable for compile time assertions.
|
---|
141 | * Both GCC v4 and VisualAge for C++ v3.08 has trouble using RT_OFFSETOF.
|
---|
142 | */
|
---|
143 | #if defined(__GNUC__)
|
---|
144 | # if __GNUC__ >= 4
|
---|
145 | # define RTASSERT_OFFSET_OF(a_Type, a_Member) __builtin_offsetof(a_Type, a_Member)
|
---|
146 | # else
|
---|
147 | # define RTASSERT_OFFSET_OF(a_Type, a_Member) RT_OFFSETOF(a_Type, a_Member)
|
---|
148 | # endif
|
---|
149 | #elif (defined(__IBMC__) || defined(__IBMCPP__)) && defined(RT_OS_OS2)
|
---|
150 | # define RTASSERT_OFFSET_OF(a_Type, a_Member) __offsetof(a_Type, a_Member)
|
---|
151 | #elif (defined(__WATCOMC__) && defined(__cplusplus))
|
---|
152 | # define RTASSERT_OFFSET_OF(a_Type, a_Member) __offsetof(a_Type, a_Member)
|
---|
153 | #else
|
---|
154 | # define RTASSERT_OFFSET_OF(a_Type, a_Member) RT_OFFSETOF(a_Type, a_Member)
|
---|
155 | #endif
|
---|
156 |
|
---|
157 |
|
---|
158 | /** @def AssertCompileSize
|
---|
159 | * Asserts a size at compile.
|
---|
160 | * @param type The type.
|
---|
161 | * @param size The expected type size.
|
---|
162 | */
|
---|
163 | #define AssertCompileSize(type, size) \
|
---|
164 | AssertCompile(sizeof(type) == (size))
|
---|
165 |
|
---|
166 | /** @def AssertCompileSizeAlignment
|
---|
167 | * Asserts a size alignment at compile.
|
---|
168 | * @param type The type.
|
---|
169 | * @param align The size alignment to assert.
|
---|
170 | */
|
---|
171 | #define AssertCompileSizeAlignment(type, align) \
|
---|
172 | AssertCompile(!(sizeof(type) & ((align) - 1)))
|
---|
173 |
|
---|
174 | /** @def AssertCompileMemberSize
|
---|
175 | * Asserts a member offset alignment at compile.
|
---|
176 | * @param type The type.
|
---|
177 | * @param member The member.
|
---|
178 | * @param size The member size to assert.
|
---|
179 | */
|
---|
180 | #define AssertCompileMemberSize(type, member, size) \
|
---|
181 | AssertCompile(RT_SIZEOFMEMB(type, member) == (size))
|
---|
182 |
|
---|
183 | /** @def AssertCompileMemberSizeAlignment
|
---|
184 | * Asserts a member size alignment at compile.
|
---|
185 | * @param type The type.
|
---|
186 | * @param member The member.
|
---|
187 | * @param align The member size alignment to assert.
|
---|
188 | */
|
---|
189 | #define AssertCompileMemberSizeAlignment(type, member, align) \
|
---|
190 | AssertCompile(!(RT_SIZEOFMEMB(type, member) & ((align) - 1)))
|
---|
191 |
|
---|
192 | /** @def AssertCompileMemberAlignment
|
---|
193 | * Asserts a member offset alignment at compile.
|
---|
194 | * @param type The type.
|
---|
195 | * @param member The member.
|
---|
196 | * @param align The member offset alignment to assert.
|
---|
197 | */
|
---|
198 | #define AssertCompileMemberAlignment(type, member, align) \
|
---|
199 | AssertCompile(!(RTASSERT_OFFSET_OF(type, member) & ((align) - 1)))
|
---|
200 |
|
---|
201 | /** @def AssertCompileMemberOffset
|
---|
202 | * Asserts an offset of a structure member at compile.
|
---|
203 | * @param type The type.
|
---|
204 | * @param member The member.
|
---|
205 | * @param off The expected offset.
|
---|
206 | */
|
---|
207 | #define AssertCompileMemberOffset(type, member, off) \
|
---|
208 | AssertCompile(RTASSERT_OFFSET_OF(type, member) == (off))
|
---|
209 |
|
---|
210 | /** @def AssertCompile2MemberOffsets
|
---|
211 | * Asserts that two (sub-structure) members in union have the same offset.
|
---|
212 | * @param type The type.
|
---|
213 | * @param member1 The first member.
|
---|
214 | * @param member2 The second member.
|
---|
215 | */
|
---|
216 | #define AssertCompile2MemberOffsets(type, member1, member2) \
|
---|
217 | AssertCompile(RTASSERT_OFFSET_OF(type, member1) == RTASSERT_OFFSET_OF(type, member2))
|
---|
218 |
|
---|
219 | /** @def AssertCompileAdjacentMembers
|
---|
220 | * Asserts that two structure members are adjacent.
|
---|
221 | * @param type The type.
|
---|
222 | * @param member1 The first member.
|
---|
223 | * @param member2 The second member.
|
---|
224 | */
|
---|
225 | #define AssertCompileAdjacentMembers(type, member1, member2) \
|
---|
226 | AssertCompile(RTASSERT_OFFSET_OF(type, member1) + RT_SIZEOFMEMB(type, member1) == RTASSERT_OFFSET_OF(type, member2))
|
---|
227 |
|
---|
228 | /** @def AssertCompileMembersAtSameOffset
|
---|
229 | * Asserts that members of two different structures are at the same offset.
|
---|
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 AssertCompileMembersAtSameOffset(type1, member1, type2, member2) \
|
---|
236 | AssertCompile(RTASSERT_OFFSET_OF(type1, member1) == RTASSERT_OFFSET_OF(type2, member2))
|
---|
237 |
|
---|
238 | /** @def AssertCompileMembersSameSize
|
---|
239 | * Asserts that members of two different structures have the same size.
|
---|
240 | * @param type1 The first type.
|
---|
241 | * @param member1 The first member.
|
---|
242 | * @param type2 The second type.
|
---|
243 | * @param member2 The second member.
|
---|
244 | */
|
---|
245 | #define AssertCompileMembersSameSize(type1, member1, type2, member2) \
|
---|
246 | AssertCompile(RT_SIZEOFMEMB(type1, member1) == RT_SIZEOFMEMB(type2, member2))
|
---|
247 |
|
---|
248 | /** @def AssertCompileMembersSameSizeAndOffset
|
---|
249 | * Asserts that members of two different structures have the same size and are
|
---|
250 | * at the same offset.
|
---|
251 | * @param type1 The first type.
|
---|
252 | * @param member1 The first member.
|
---|
253 | * @param type2 The second type.
|
---|
254 | * @param member2 The second member.
|
---|
255 | */
|
---|
256 | #define AssertCompileMembersSameSizeAndOffset(type1, member1, type2, member2) \
|
---|
257 | AssertCompile( RTASSERT_OFFSET_OF(type1, member1) == RTASSERT_OFFSET_OF(type2, member2) \
|
---|
258 | && RT_SIZEOFMEMB(type1, member1) == RT_SIZEOFMEMB(type2, member2))
|
---|
259 |
|
---|
260 | /** @} */
|
---|
261 |
|
---|
262 | #endif /* !IPRT_INCLUDED_assertcompile_h */
|
---|
263 |
|
---|