VirtualBox

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

最後變更 在這個檔案從97962是 97962,由 vboxsync 提交於 2 年 前

iprt/assertcompile.h: Don't define RTASSERTVAR globally when using GCC as it seems to trigger unused warnings with 3.4.x. We already define both the RTASSERTVAR[1] and RTASSERTVAR[expr ? 1 : 0] for each invocation.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 8.7 KB
 
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.alldomusa.eu.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 *
69 * GCC and IBM VisualAge C/C++ uses this, though GCC doesn't need this global
70 * scope one as it declares it for each use.
71 */
72#ifndef __GNUC__
73extern int RTASSERTVAR[1];
74#endif
75
76/** @def RTASSERT_HAVE_STATIC_ASSERT
77 * Indicates that the compiler implements static_assert(expr, msg).
78 */
79#ifdef _MSC_VER
80# if _MSC_VER >= 1600 && defined(__cplusplus)
81# define RTASSERT_HAVE_STATIC_ASSERT
82# endif
83#endif
84#if defined(__GNUC__) && defined(__GXX_EXPERIMENTAL_CXX0X__)
85# define RTASSERT_HAVE_STATIC_ASSERT
86#endif
87#if RT_CLANG_PREREQ(6, 0)
88# if __has_feature(cxx_static_assert) || __has_feature(c_static_assert)
89# define RTASSERT_HAVE_STATIC_ASSERT
90# endif
91#endif
92#ifdef DOXYGEN_RUNNING
93# define RTASSERT_HAVE_STATIC_ASSERT
94#endif
95
96/** @def AssertCompileNS
97 * Asserts that a compile-time expression is true. If it's not break the build.
98 *
99 * This differs from AssertCompile in that it accepts some more expressions
100 * than what C++0x allows - NS = Non-standard.
101 *
102 * @param expr Expression which should be true.
103 */
104#ifdef __GNUC__
105# define AssertCompileNS(expr) extern int RTASSERTVAR[1] __attribute__((__unused__)), RTASSERTVAR[(expr) ? 1 : 0] __attribute__((__unused__))
106#elif defined(__IBMC__) || defined(__IBMCPP__)
107# define AssertCompileNS(expr) extern int RTASSERTVAR[(expr) ? 1 : 0]
108#else
109# define AssertCompileNS(expr) typedef int RTASSERTTYPE[(expr) ? 1 : 0]
110#endif
111
112/** @def AssertCompile
113 * Asserts that a C++0x compile-time expression is true. If it's not break the
114 * build.
115 * @param expr Expression which should be true.
116 */
117#ifdef RTASSERT_HAVE_STATIC_ASSERT
118# ifdef __cplusplus
119# define AssertCompile(expr) static_assert(!!(expr), #expr)
120# else
121# define AssertCompile(expr) _Static_assert(!!(expr), #expr)
122# endif
123#else
124# define AssertCompile(expr) AssertCompileNS(expr)
125#endif
126
127/** @def RTASSERT_OFFSET_OF()
128 * A offsetof() macro suitable for compile time assertions.
129 * Both GCC v4 and VisualAge for C++ v3.08 has trouble using RT_OFFSETOF.
130 */
131#if defined(__GNUC__)
132# if __GNUC__ >= 4
133# define RTASSERT_OFFSET_OF(a_Type, a_Member) __builtin_offsetof(a_Type, a_Member)
134# else
135# define RTASSERT_OFFSET_OF(a_Type, a_Member) RT_OFFSETOF(a_Type, a_Member)
136# endif
137#elif (defined(__IBMC__) || defined(__IBMCPP__)) && defined(RT_OS_OS2)
138# define RTASSERT_OFFSET_OF(a_Type, a_Member) __offsetof(a_Type, a_Member)
139#elif (defined(__WATCOMC__) && defined(__cplusplus))
140# define RTASSERT_OFFSET_OF(a_Type, a_Member) __offsetof(a_Type, a_Member)
141#else
142# define RTASSERT_OFFSET_OF(a_Type, a_Member) RT_OFFSETOF(a_Type, a_Member)
143#endif
144
145
146/** @def AssertCompileSize
147 * Asserts a size at compile.
148 * @param type The type.
149 * @param size The expected type size.
150 */
151#define AssertCompileSize(type, size) \
152 AssertCompile(sizeof(type) == (size))
153
154/** @def AssertCompileSizeAlignment
155 * Asserts a size alignment at compile.
156 * @param type The type.
157 * @param align The size alignment to assert.
158 */
159#define AssertCompileSizeAlignment(type, align) \
160 AssertCompile(!(sizeof(type) & ((align) - 1)))
161
162/** @def AssertCompileMemberSize
163 * Asserts a member offset alignment at compile.
164 * @param type The type.
165 * @param member The member.
166 * @param size The member size to assert.
167 */
168#define AssertCompileMemberSize(type, member, size) \
169 AssertCompile(RT_SIZEOFMEMB(type, member) == (size))
170
171/** @def AssertCompileMemberSizeAlignment
172 * Asserts a member size alignment at compile.
173 * @param type The type.
174 * @param member The member.
175 * @param align The member size alignment to assert.
176 */
177#define AssertCompileMemberSizeAlignment(type, member, align) \
178 AssertCompile(!(RT_SIZEOFMEMB(type, member) & ((align) - 1)))
179
180/** @def AssertCompileMemberAlignment
181 * Asserts a member offset alignment at compile.
182 * @param type The type.
183 * @param member The member.
184 * @param align The member offset alignment to assert.
185 */
186#define AssertCompileMemberAlignment(type, member, align) \
187 AssertCompile(!(RTASSERT_OFFSET_OF(type, member) & ((align) - 1)))
188
189/** @def AssertCompileMemberOffset
190 * Asserts an offset of a structure member at compile.
191 * @param type The type.
192 * @param member The member.
193 * @param off The expected offset.
194 */
195#define AssertCompileMemberOffset(type, member, off) \
196 AssertCompile(RTASSERT_OFFSET_OF(type, member) == (off))
197
198/** @def AssertCompile2MemberOffsets
199 * Asserts that two (sub-structure) members in union have the same offset.
200 * @param type The type.
201 * @param member1 The first member.
202 * @param member2 The second member.
203 */
204#define AssertCompile2MemberOffsets(type, member1, member2) \
205 AssertCompile(RTASSERT_OFFSET_OF(type, member1) == RTASSERT_OFFSET_OF(type, member2))
206
207/** @def AssertCompileAdjacentMembers
208 * Asserts that two structure members are adjacent.
209 * @param type The type.
210 * @param member1 The first member.
211 * @param member2 The second member.
212 */
213#define AssertCompileAdjacentMembers(type, member1, member2) \
214 AssertCompile(RTASSERT_OFFSET_OF(type, member1) + RT_SIZEOFMEMB(type, member1) == RTASSERT_OFFSET_OF(type, member2))
215
216/** @def AssertCompileMembersAtSameOffset
217 * Asserts that members of two different structures are at the same offset.
218 * @param type1 The first type.
219 * @param member1 The first member.
220 * @param type2 The second type.
221 * @param member2 The second member.
222 */
223#define AssertCompileMembersAtSameOffset(type1, member1, type2, member2) \
224 AssertCompile(RTASSERT_OFFSET_OF(type1, member1) == RTASSERT_OFFSET_OF(type2, member2))
225
226/** @def AssertCompileMembersSameSize
227 * Asserts that members of two different structures have the same size.
228 * @param type1 The first type.
229 * @param member1 The first member.
230 * @param type2 The second type.
231 * @param member2 The second member.
232 */
233#define AssertCompileMembersSameSize(type1, member1, type2, member2) \
234 AssertCompile(RT_SIZEOFMEMB(type1, member1) == RT_SIZEOFMEMB(type2, member2))
235
236/** @def AssertCompileMembersSameSizeAndOffset
237 * Asserts that members of two different structures have the same size and are
238 * at the same offset.
239 * @param type1 The first type.
240 * @param member1 The first member.
241 * @param type2 The second type.
242 * @param member2 The second member.
243 */
244#define AssertCompileMembersSameSizeAndOffset(type1, member1, type2, member2) \
245 AssertCompile( RTASSERT_OFFSET_OF(type1, member1) == RTASSERT_OFFSET_OF(type2, member2) \
246 && RT_SIZEOFMEMB(type1, member1) == RT_SIZEOFMEMB(type2, member2))
247
248/** @} */
249
250#endif /* !IPRT_INCLUDED_assertcompile_h */
251
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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