]>
git.ipfire.org Git - thirdparty/glibc.git/blob - assert/assert.h
471e225ea60a5c944797f9df181669d20d7b505e
1 /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
19 * ISO C99 Standard: 7.2 Diagnostics <assert.h>
26 # undef __ASSERT_VOID_CAST
37 #if __GLIBC_USE (ISOC23)
38 # ifndef __STDC_VERSION_ASSERT_H__
39 # define __STDC_VERSION_ASSERT_H__ 202311L
43 #if defined __cplusplus && __GNUC_PREREQ (2,95)
44 # define __ASSERT_VOID_CAST static_cast<void>
46 # define __ASSERT_VOID_CAST (void)
49 /* C23 makes assert a variadic macro so that expressions with a comma
50 not between parentheses, but that would still be valid as a single
51 function argument, such as those involving compound literals with a
52 comma in the initializer list, can be passed to assert. This
53 depends on support for variadic macros (added in C99 and GCC 2.95),
54 and on support for _Bool (added in C99 and GCC 3.0) in order to
55 validate that only a single expression is passed as an argument. */
56 #if ((__GLIBC_USE (ISOC23) \
57 && (defined __GNUC__ \
58 ? __GNUC_PREREQ (3, 0) \
59 : defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)) \
60 || (defined __cplusplus && __cplusplus > 202302L))
61 # define __ASSERT_VARIADIC 1
63 # define __ASSERT_VARIADIC 0
66 /* void assert (int expression);
68 If NDEBUG is defined, do nothing.
69 If not, and EXPRESSION is zero, print an error message and abort. */
73 # if __ASSERT_VARIADIC
74 # define assert(...) (__ASSERT_VOID_CAST (0))
76 # define assert(expr) (__ASSERT_VOID_CAST (0))
79 /* void assert_perror (int errnum);
81 If NDEBUG is defined, do nothing. If not, and ERRNUM is not zero, print an
82 error message with the error text for ERRNUM and abort.
83 (This is a GNU extension.) */
86 # define assert_perror(errnum) (__ASSERT_VOID_CAST (0))
89 #else /* Not NDEBUG. */
93 /* This prints an "Assertion failed" message and aborts. */
94 extern void __assert_fail (const char *__assertion
, const char *__file
,
95 unsigned int __line
, const char *__function
)
96 __THROW
__attribute__ ((__noreturn__
)) __COLD
;
98 /* Likewise, but prints the error text for ERRNUM. */
99 extern void __assert_perror_fail (int __errnum
, const char *__file
,
100 unsigned int __line
, const char *__function
)
101 __THROW
__attribute__ ((__noreturn__
)) __COLD
;
104 /* The following is not at all used here but needed for standard
106 extern void __assert (const char *__assertion
, const char *__file
, int __line
)
107 __THROW
__attribute__ ((__noreturn__
)) __COLD
;
110 # if __ASSERT_VARIADIC && !defined __cplusplus
111 /* This function is not defined and is not called outside of an
112 unevaluated sizeof, but serves to verify that the argument to
113 assert is a single expression. */
114 extern _Bool
__assert_single_arg (_Bool
);
119 /* When possible, define assert so that it does not add extra
120 parentheses around EXPR. Otherwise, those added parentheses would
121 suppress warnings we'd expect to be detected by gcc's -Wparentheses. */
122 # if defined __cplusplus
123 # if defined __has_builtin
124 # if __has_builtin (__builtin_FILE)
125 # define __ASSERT_FILE __builtin_FILE ()
126 # define __ASSERT_LINE __builtin_LINE ()
129 # if !defined __ASSERT_FILE
130 # define __ASSERT_FILE __FILE__
131 # define __ASSERT_LINE __LINE__
133 # if __ASSERT_VARIADIC
134 /* The first test of __VA_ARGS__ evaluates it without converting scoped
135 enumeration values to bool, and the second test checks that it is a
136 single expression without evaluating it. */
137 # define assert(...) \
139 ? void (1 ? 1 : bool (__VA_ARGS__)) \
140 : __assert_fail (#__VA_ARGS__, __ASSERT_FILE, __ASSERT_LINE, \
143 # define assert(expr) \
144 (static_cast <bool> (expr) \
146 : __assert_fail (#expr, __ASSERT_FILE, __ASSERT_LINE, \
149 # elif !defined __GNUC__ || defined __STRICT_ANSI__
150 # if __ASSERT_VARIADIC
151 # define assert(...) \
152 (((void) sizeof (__assert_single_arg (__VA_ARGS__)), __VA_ARGS__) \
153 ? __ASSERT_VOID_CAST (0) \
154 : __assert_fail (#__VA_ARGS__, __FILE__, __LINE__, __ASSERT_FUNCTION))
156 # define assert(expr) \
158 ? __ASSERT_VOID_CAST (0) \
159 : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
162 # if __ASSERT_VARIADIC
163 # define assert(...) \
164 ((void) sizeof (__assert_single_arg (__VA_ARGS__)), __extension__ ({ \
168 __assert_fail (#__VA_ARGS__, __FILE__, __LINE__, __ASSERT_FUNCTION); \
171 /* The first occurrence of EXPR is not evaluated due to the sizeof,
172 but will trigger any pedantic warnings masked by the __extension__
173 for the second occurrence. The ternary operator is required to
174 support function pointers and bit fields in this context, and to
175 suppress the evaluation of variable length arrays. */
176 # define assert(expr) \
177 ((void) sizeof ((expr) ? 1 : 0), __extension__ ({ \
181 __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION); \
187 # define assert_perror(errnum) \
189 ? __ASSERT_VOID_CAST (0) \
190 : __assert_perror_fail ((errnum), __FILE__, __LINE__, __ASSERT_FUNCTION))
193 /* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
194 which contains the name of the function currently being defined.
195 This is broken in G++ before version 2.6.
196 C9x has a similar variable called __func__, but prefer the GCC one since
197 it demangles C++ function names. */
198 # if defined __cplusplus ? __GNUC_PREREQ (2, 6) : __GNUC_PREREQ (2, 4)
199 # define __ASSERT_FUNCTION __extension__ __PRETTY_FUNCTION__
201 # if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
202 # define __ASSERT_FUNCTION __func__
204 # define __ASSERT_FUNCTION ((const char *) 0)
211 #if (defined __USE_ISOC11 \
212 && (!defined __STDC_VERSION__ \
213 || __STDC_VERSION__ <= 201710L \
214 || !__GNUC_PREREQ (13, 0)) \
215 && !defined __cplusplus)
216 # undef static_assert
217 # define static_assert _Static_assert