]> git.ipfire.org Git - thirdparty/glibc.git/blame - assert/assert.h
malloc: Don't call __get_thp_mode/__get_thp_size twice
[thirdparty/glibc.git] / assert / assert.h
CommitLineData
66f3e921 1/* Copyright (C) 1991-2026 Free Software Foundation, Inc.
ba1ffaa1 2 This file is part of the GNU C Library.
28f540f4 3
ba1ffaa1 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
28f540f4 8
ba1ffaa1
UD
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
41bdb6e2 12 Lesser General Public License for more details.
28f540f4 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
28f540f4
RM
17
18/*
d1646309 19 * ISO C99 Standard: 7.2 Diagnostics <assert.h>
28f540f4
RM
20 */
21
22#ifdef _ASSERT_H
23
40a55d20
UD
24# undef _ASSERT_H
25# undef assert
fd205052 26# undef __ASSERT_VOID_CAST
40a55d20
UD
27
28# ifdef __USE_GNU
29# undef assert_perror
30# endif
28f540f4
RM
31
32#endif /* assert.h */
33
34#define _ASSERT_H 1
35#include <features.h>
36
e535fb91
JM
37#if __GLIBC_USE (ISOC23)
38# ifndef __STDC_VERSION_ASSERT_H__
39# define __STDC_VERSION_ASSERT_H__ 202311L
40# endif
41#endif
42
fd205052
UD
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
fa7f43a9
JM
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
ea37298b
JW
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))
fa7f43a9
JM
61# define __ASSERT_VARIADIC 1
62#else
63# define __ASSERT_VARIADIC 0
64#endif
65
28f540f4
RM
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
fa7f43a9
JM
73# if __ASSERT_VARIADIC
74# define assert(...) (__ASSERT_VOID_CAST (0))
75# else
76# define assert(expr) (__ASSERT_VOID_CAST (0))
77# endif
28f540f4
RM
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
f21acc89 85# ifdef __USE_GNU
fd205052 86# define assert_perror(errnum) (__ASSERT_VOID_CAST (0))
f21acc89 87# endif
28f540f4
RM
88
89#else /* Not NDEBUG. */
90
28f540f4
RM
91__BEGIN_DECLS
92
93/* This prints an "Assertion failed" message and aborts. */
a784e502
UD
94extern void __assert_fail (const char *__assertion, const char *__file,
95 unsigned int __line, const char *__function)
f6f90474 96 __THROW __attribute__ ((__noreturn__)) __COLD;
28f540f4
RM
97
98/* Likewise, but prints the error text for ERRNUM. */
a784e502
UD
99extern void __assert_perror_fail (int __errnum, const char *__file,
100 unsigned int __line, const char *__function)
f6f90474 101 __THROW __attribute__ ((__noreturn__)) __COLD;
28f540f4 102
8fb81470
UD
103
104/* The following is not at all used here but needed for standard
105 compliance. */
106extern void __assert (const char *__assertion, const char *__file, int __line)
f6f90474 107 __THROW __attribute__ ((__noreturn__)) __COLD;
8fb81470
UD
108
109
ea37298b 110# if __ASSERT_VARIADIC && !defined __cplusplus
fa7f43a9
JM
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. */
114extern _Bool __assert_single_arg (_Bool);
115# endif
116
28f540f4
RM
117__END_DECLS
118
e077349c
JM
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. */
b5889d25 122# if defined __cplusplus
e42ec822
PP
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
ea37298b
JW
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) \
b5889d25
FW
144 (static_cast <bool> (expr) \
145 ? void (0) \
e42ec822
PP
146 : __assert_fail (#expr, __ASSERT_FILE, __ASSERT_LINE, \
147 __ASSERT_FUNCTION))
ea37298b 148# endif
b5889d25 149# elif !defined __GNUC__ || defined __STRICT_ANSI__
fa7f43a9
JM
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) \
e077349c
JM
157 ((expr) \
158 ? __ASSERT_VOID_CAST (0) \
159 : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
fa7f43a9 160# endif
e077349c 161# else
fa7f43a9
JM
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
8b2c63e4
FW
171/* The first occurrence of EXPR is not evaluated due to the sizeof,
172 but will trigger any pedantic warnings masked by the __extension__
b5889d25
FW
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. */
fa7f43a9 176# define assert(expr) \
b5889d25 177 ((void) sizeof ((expr) ? 1 : 0), __extension__ ({ \
e077349c
JM
178 if (expr) \
179 ; /* empty */ \
180 else \
181 __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION); \
8b2c63e4 182 }))
fa7f43a9 183# endif
e077349c 184# endif
28f540f4 185
f21acc89 186# ifdef __USE_GNU
389935d7
UD
187# define assert_perror(errnum) \
188 (!(errnum) \
189 ? __ASSERT_VOID_CAST (0) \
b3715c05 190 : __assert_perror_fail ((errnum), __FILE__, __LINE__, __ASSERT_FUNCTION))
f21acc89 191# endif
28f540f4
RM
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.
05cc5bd9
UD
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. */
d0db5f48 198# if defined __cplusplus ? __GNUC_PREREQ (2, 6) : __GNUC_PREREQ (2, 4)
8b2c63e4 199# define __ASSERT_FUNCTION __extension__ __PRETTY_FUNCTION__
05cc5bd9
UD
200# else
201# if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
202# define __ASSERT_FUNCTION __func__
203# else
a784e502 204# define __ASSERT_FUNCTION ((const char *) 0)
05cc5bd9 205# endif
f21acc89 206# endif
28f540f4 207
28f540f4 208#endif /* NDEBUG. */
839e283e
UD
209
210
b8cc607f
JM
211#if (defined __USE_ISOC11 \
212 && (!defined __STDC_VERSION__ \
213 || __STDC_VERSION__ <= 201710L \
214 || !__GNUC_PREREQ (13, 0)) \
215 && !defined __cplusplus)
839e283e
UD
216# undef static_assert
217# define static_assert _Static_assert
218#endif