]> git.ipfire.org Git - thirdparty/glibc.git/blob - assert/assert.h
471e225ea60a5c944797f9df181669d20d7b505e
[thirdparty/glibc.git] / assert / assert.h
1 /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
17
18 /*
19 * ISO C99 Standard: 7.2 Diagnostics <assert.h>
20 */
21
22 #ifdef _ASSERT_H
23
24 # undef _ASSERT_H
25 # undef assert
26 # undef __ASSERT_VOID_CAST
27
28 # ifdef __USE_GNU
29 # undef assert_perror
30 # endif
31
32 #endif /* assert.h */
33
34 #define _ASSERT_H 1
35 #include <features.h>
36
37 #if __GLIBC_USE (ISOC23)
38 # ifndef __STDC_VERSION_ASSERT_H__
39 # define __STDC_VERSION_ASSERT_H__ 202311L
40 # endif
41 #endif
42
43 #if defined __cplusplus && __GNUC_PREREQ (2,95)
44 # define __ASSERT_VOID_CAST static_cast<void>
45 #else
46 # define __ASSERT_VOID_CAST (void)
47 #endif
48
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
62 #else
63 # define __ASSERT_VARIADIC 0
64 #endif
65
66 /* void assert (int expression);
67
68 If NDEBUG is defined, do nothing.
69 If not, and EXPRESSION is zero, print an error message and abort. */
70
71 #ifdef NDEBUG
72
73 # if __ASSERT_VARIADIC
74 # define assert(...) (__ASSERT_VOID_CAST (0))
75 # else
76 # define assert(expr) (__ASSERT_VOID_CAST (0))
77 # endif
78
79 /* void assert_perror (int errnum);
80
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.) */
84
85 # ifdef __USE_GNU
86 # define assert_perror(errnum) (__ASSERT_VOID_CAST (0))
87 # endif
88
89 #else /* Not NDEBUG. */
90
91 __BEGIN_DECLS
92
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;
97
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;
102
103
104 /* The following is not at all used here but needed for standard
105 compliance. */
106 extern void __assert (const char *__assertion, const char *__file, int __line)
107 __THROW __attribute__ ((__noreturn__)) __COLD;
108
109
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);
115 # endif
116
117 __END_DECLS
118
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 ()
127 # endif
128 # endif
129 # if !defined __ASSERT_FILE
130 # define __ASSERT_FILE __FILE__
131 # define __ASSERT_LINE __LINE__
132 # endif
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(...) \
138 ((__VA_ARGS__) \
139 ? void (1 ? 1 : bool (__VA_ARGS__)) \
140 : __assert_fail (#__VA_ARGS__, __ASSERT_FILE, __ASSERT_LINE, \
141 __ASSERT_FUNCTION))
142 # else
143 # define assert(expr) \
144 (static_cast <bool> (expr) \
145 ? void (0) \
146 : __assert_fail (#expr, __ASSERT_FILE, __ASSERT_LINE, \
147 __ASSERT_FUNCTION))
148 # endif
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))
155 # else
156 # define assert(expr) \
157 ((expr) \
158 ? __ASSERT_VOID_CAST (0) \
159 : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
160 # endif
161 # else
162 # if __ASSERT_VARIADIC
163 # define assert(...) \
164 ((void) sizeof (__assert_single_arg (__VA_ARGS__)), __extension__ ({ \
165 if (__VA_ARGS__) \
166 ; /* empty */ \
167 else \
168 __assert_fail (#__VA_ARGS__, __FILE__, __LINE__, __ASSERT_FUNCTION); \
169 }))
170 # else
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__ ({ \
178 if (expr) \
179 ; /* empty */ \
180 else \
181 __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION); \
182 }))
183 # endif
184 # endif
185
186 # ifdef __USE_GNU
187 # define assert_perror(errnum) \
188 (!(errnum) \
189 ? __ASSERT_VOID_CAST (0) \
190 : __assert_perror_fail ((errnum), __FILE__, __LINE__, __ASSERT_FUNCTION))
191 # endif
192
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__
200 # else
201 # if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
202 # define __ASSERT_FUNCTION __func__
203 # else
204 # define __ASSERT_FUNCTION ((const char *) 0)
205 # endif
206 # endif
207
208 #endif /* NDEBUG. */
209
210
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
218 #endif