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 and is currently implemented only for C. */
57#if (__GLIBC_USE (ISOC23) \
58 && (defined __GNUC__ \
59 ? __GNUC_PREREQ (3, 0) \
60 : defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L) \
61 && !defined __cplusplus)
62# define __ASSERT_VARIADIC 1
63#else
64# define __ASSERT_VARIADIC 0
65#endif
66
67/* void assert (int expression);
68
69 If NDEBUG is defined, do nothing.
70 If not, and EXPRESSION is zero, print an error message and abort. */
71
72#ifdef NDEBUG
73
74# if __ASSERT_VARIADIC
75# define assert(...) (__ASSERT_VOID_CAST (0))
76# else
77# define assert(expr) (__ASSERT_VOID_CAST (0))
78# endif
79
80/* void assert_perror (int errnum);
81
82 If NDEBUG is defined, do nothing. If not, and ERRNUM is not zero, print an
83 error message with the error text for ERRNUM and abort.
84 (This is a GNU extension.) */
85
86# ifdef __USE_GNU
87# define assert_perror(errnum) (__ASSERT_VOID_CAST (0))
88# endif
89
90#else /* Not NDEBUG. */
91
92__BEGIN_DECLS
93
94/* This prints an "Assertion failed" message and aborts. */
95extern void __assert_fail (const char *__assertion, const char *__file,
96 unsigned int __line, const char *__function)
97 __THROW __attribute__ ((__noreturn__)) __COLD;
98
99/* Likewise, but prints the error text for ERRNUM. */
100extern void __assert_perror_fail (int __errnum, const char *__file,
101 unsigned int __line, const char *__function)
102 __THROW __attribute__ ((__noreturn__)) __COLD;
103
104
105/* The following is not at all used here but needed for standard
106 compliance. */
107extern void __assert (const char *__assertion, const char *__file, int __line)
108 __THROW __attribute__ ((__noreturn__)) __COLD;
109
110
111# if __ASSERT_VARIADIC
112/* This function is not defined and is not called outside of an
113 unevaluated sizeof, but serves to verify that the argument to
114 assert is a single expression. */
115extern _Bool __assert_single_arg (_Bool);
116# endif
117
118__END_DECLS
119
120/* When possible, define assert so that it does not add extra
121 parentheses around EXPR. Otherwise, those added parentheses would
122 suppress warnings we'd expect to be detected by gcc's -Wparentheses. */
123# if defined __cplusplus
124# if defined __has_builtin
125# if __has_builtin (__builtin_FILE)
126# define __ASSERT_FILE __builtin_FILE ()
127# define __ASSERT_LINE __builtin_LINE ()
128# endif
129# endif
130# if !defined __ASSERT_FILE
131# define __ASSERT_FILE __FILE__
132# define __ASSERT_LINE __LINE__
133# endif
134# define assert(expr) \
135 (static_cast <bool> (expr) \
136 ? void (0) \
137 : __assert_fail (#expr, __ASSERT_FILE, __ASSERT_LINE, \
138 __ASSERT_FUNCTION))
139# elif !defined __GNUC__ || defined __STRICT_ANSI__
140# if __ASSERT_VARIADIC
141# define assert(...) \
142 (((void) sizeof (__assert_single_arg (__VA_ARGS__)), __VA_ARGS__) \
143 ? __ASSERT_VOID_CAST (0) \
144 : __assert_fail (#__VA_ARGS__, __FILE__, __LINE__, __ASSERT_FUNCTION))
145# else
146# define assert(expr) \
147 ((expr) \
148 ? __ASSERT_VOID_CAST (0) \
149 : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
150# endif
151# else
152# if __ASSERT_VARIADIC
153# define assert(...) \
154 ((void) sizeof (__assert_single_arg (__VA_ARGS__)), __extension__ ({ \
155 if (__VA_ARGS__) \
156 ; /* empty */ \
157 else \
158 __assert_fail (#__VA_ARGS__, __FILE__, __LINE__, __ASSERT_FUNCTION); \
159 }))
160# else
161/* The first occurrence of EXPR is not evaluated due to the sizeof,
162 but will trigger any pedantic warnings masked by the __extension__
163 for the second occurrence. The ternary operator is required to
164 support function pointers and bit fields in this context, and to
165 suppress the evaluation of variable length arrays. */
166# define assert(expr) \
167 ((void) sizeof ((expr) ? 1 : 0), __extension__ ({ \
168 if (expr) \
169 ; /* empty */ \
170 else \
171 __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION); \
172 }))
173# endif
174# endif
175
176# ifdef __USE_GNU
177# define assert_perror(errnum) \
178 (!(errnum) \
179 ? __ASSERT_VOID_CAST (0) \
180 : __assert_perror_fail ((errnum), __FILE__, __LINE__, __ASSERT_FUNCTION))
181# endif
182
183/* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
184 which contains the name of the function currently being defined.
185 This is broken in G++ before version 2.6.
186 C9x has a similar variable called __func__, but prefer the GCC one since
187 it demangles C++ function names. */
188# if defined __cplusplus ? __GNUC_PREREQ (2, 6) : __GNUC_PREREQ (2, 4)
189# define __ASSERT_FUNCTION __extension__ __PRETTY_FUNCTION__
190# else
191# if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
192# define __ASSERT_FUNCTION __func__
193# else
194# define __ASSERT_FUNCTION ((const char *) 0)
195# endif
196# endif
197
198#endif /* NDEBUG. */
199
200
201#if (defined __USE_ISOC11 \
202 && (!defined __STDC_VERSION__ \
203 || __STDC_VERSION__ <= 201710L \
204 || !__GNUC_PREREQ (13, 0)) \
205 && !defined __cplusplus)
206# undef static_assert
207# define static_assert _Static_assert
208#endif
209