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