assert.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. /*
  15. * ISO C99 Standard: 7.2 Diagnostics <assert.h>
  16. */
  17. #ifdef _ASSERT_H
  18. # undef _ASSERT_H
  19. # undef assert
  20. # undef __ASSERT_VOID_CAST
  21. # ifdef __USE_GNU
  22. # undef assert_perror
  23. # endif
  24. #endif /* assert.h */
  25. #define _ASSERT_H 1
  26. #include <features.h>
  27. #if __GLIBC_USE (ISOC23)
  28. # ifndef __STDC_VERSION_ASSERT_H__
  29. # define __STDC_VERSION_ASSERT_H__ 202311L
  30. # endif
  31. #endif
  32. #if defined __cplusplus && __GNUC_PREREQ (2,95)
  33. # define __ASSERT_VOID_CAST static_cast<void>
  34. #else
  35. # define __ASSERT_VOID_CAST (void)
  36. #endif
  37. /* C23 makes assert a variadic macro so that expressions with a comma
  38. not between parentheses, but that would still be valid as a single
  39. function argument, such as those involving compound literals with a
  40. comma in the initializer list, can be passed to assert. This
  41. depends on support for variadic macros (added in C99 and GCC 2.95),
  42. and on support for _Bool (added in C99 and GCC 3.0) in order to
  43. validate that only a single expression is passed as an argument,
  44. and is currently implemented only for C. */
  45. #if (__GLIBC_USE (ISOC23) \
  46. && (defined __GNUC__ \
  47. ? __GNUC_PREREQ (3, 0) \
  48. : defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L) \
  49. && !defined __cplusplus)
  50. # define __ASSERT_VARIADIC 1
  51. #else
  52. # define __ASSERT_VARIADIC 0
  53. #endif
  54. /* void assert (int expression);
  55. If NDEBUG is defined, do nothing.
  56. If not, and EXPRESSION is zero, print an error message and abort. */
  57. #ifdef NDEBUG
  58. # if __ASSERT_VARIADIC
  59. # define assert(...) (__ASSERT_VOID_CAST (0))
  60. # else
  61. # define assert(expr) (__ASSERT_VOID_CAST (0))
  62. # endif
  63. /* void assert_perror (int errnum);
  64. If NDEBUG is defined, do nothing. If not, and ERRNUM is not zero, print an
  65. error message with the error text for ERRNUM and abort.
  66. (This is a GNU extension.) */
  67. # ifdef __USE_GNU
  68. # define assert_perror(errnum) (__ASSERT_VOID_CAST (0))
  69. # endif
  70. #else /* Not NDEBUG. */
  71. __BEGIN_DECLS
  72. /* This prints an "Assertion failed" message and aborts. */
  73. extern void __assert_fail (const char *__assertion, const char *__file,
  74. unsigned int __line, const char *__function)
  75. __THROW __attribute__ ((__noreturn__)) __COLD;
  76. /* Likewise, but prints the error text for ERRNUM. */
  77. extern void __assert_perror_fail (int __errnum, const char *__file,
  78. unsigned int __line, const char *__function)
  79. __THROW __attribute__ ((__noreturn__)) __COLD;
  80. /* The following is not at all used here but needed for standard
  81. compliance. */
  82. extern void __assert (const char *__assertion, const char *__file, int __line)
  83. __THROW __attribute__ ((__noreturn__)) __COLD;
  84. # if __ASSERT_VARIADIC
  85. /* This function is not defined and is not called outside of an
  86. unevaluated sizeof, but serves to verify that the argument to
  87. assert is a single expression. */
  88. extern _Bool __assert_single_arg (_Bool);
  89. # endif
  90. __END_DECLS
  91. /* When possible, define assert so that it does not add extra
  92. parentheses around EXPR. Otherwise, those added parentheses would
  93. suppress warnings we'd expect to be detected by gcc's -Wparentheses. */
  94. # if defined __cplusplus
  95. # if defined __has_builtin
  96. # if __has_builtin (__builtin_FILE)
  97. # define __ASSERT_FILE __builtin_FILE ()
  98. # define __ASSERT_LINE __builtin_LINE ()
  99. # endif
  100. # endif
  101. # if !defined __ASSERT_FILE
  102. # define __ASSERT_FILE __FILE__
  103. # define __ASSERT_LINE __LINE__
  104. # endif
  105. # define assert(expr) \
  106. (static_cast <bool> (expr) \
  107. ? void (0) \
  108. : __assert_fail (#expr, __ASSERT_FILE, __ASSERT_LINE, \
  109. __ASSERT_FUNCTION))
  110. # elif !defined __GNUC__ || defined __STRICT_ANSI__
  111. # if __ASSERT_VARIADIC
  112. # define assert(...) \
  113. (((void) sizeof (__assert_single_arg (__VA_ARGS__)), __VA_ARGS__) \
  114. ? __ASSERT_VOID_CAST (0) \
  115. : __assert_fail (#__VA_ARGS__, __FILE__, __LINE__, __ASSERT_FUNCTION))
  116. # else
  117. # define assert(expr) \
  118. ((expr) \
  119. ? __ASSERT_VOID_CAST (0) \
  120. : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
  121. # endif
  122. # else
  123. # if __ASSERT_VARIADIC
  124. # define assert(...) \
  125. ((void) sizeof (__assert_single_arg (__VA_ARGS__)), __extension__ ({ \
  126. if (__VA_ARGS__) \
  127. ; /* empty */ \
  128. else \
  129. __assert_fail (#__VA_ARGS__, __FILE__, __LINE__, __ASSERT_FUNCTION); \
  130. }))
  131. # else
  132. /* The first occurrence of EXPR is not evaluated due to the sizeof,
  133. but will trigger any pedantic warnings masked by the __extension__
  134. for the second occurrence. The ternary operator is required to
  135. support function pointers and bit fields in this context, and to
  136. suppress the evaluation of variable length arrays. */
  137. # define assert(expr) \
  138. ((void) sizeof ((expr) ? 1 : 0), __extension__ ({ \
  139. if (expr) \
  140. ; /* empty */ \
  141. else \
  142. __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION); \
  143. }))
  144. # endif
  145. # endif
  146. # ifdef __USE_GNU
  147. # define assert_perror(errnum) \
  148. (!(errnum) \
  149. ? __ASSERT_VOID_CAST (0) \
  150. : __assert_perror_fail ((errnum), __FILE__, __LINE__, __ASSERT_FUNCTION))
  151. # endif
  152. /* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
  153. which contains the name of the function currently being defined.
  154. This is broken in G++ before version 2.6.
  155. C9x has a similar variable called __func__, but prefer the GCC one since
  156. it demangles C++ function names. */
  157. # if defined __cplusplus ? __GNUC_PREREQ (2, 6) : __GNUC_PREREQ (2, 4)
  158. # define __ASSERT_FUNCTION __extension__ __PRETTY_FUNCTION__
  159. # else
  160. # if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
  161. # define __ASSERT_FUNCTION __func__
  162. # else
  163. # define __ASSERT_FUNCTION ((const char *) 0)
  164. # endif
  165. # endif
  166. #endif /* NDEBUG. */
  167. #if (defined __USE_ISOC11 \
  168. && (!defined __STDC_VERSION__ \
  169. || __STDC_VERSION__ <= 201710L \
  170. || !__GNUC_PREREQ (13, 0)) \
  171. && !defined __cplusplus)
  172. # undef static_assert
  173. # define static_assert _Static_assert
  174. #endif