assert.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright (C) 1991-2020 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 defined __cplusplus && __GNUC_PREREQ (2,95)
  28. # define __ASSERT_VOID_CAST static_cast<void>
  29. #else
  30. # define __ASSERT_VOID_CAST (void)
  31. #endif
  32. /* void assert (int expression);
  33. If NDEBUG is defined, do nothing.
  34. If not, and EXPRESSION is zero, print an error message and abort. */
  35. #ifdef NDEBUG
  36. # define assert(expr) (__ASSERT_VOID_CAST (0))
  37. /* void assert_perror (int errnum);
  38. If NDEBUG is defined, do nothing. If not, and ERRNUM is not zero, print an
  39. error message with the error text for ERRNUM and abort.
  40. (This is a GNU extension.) */
  41. # ifdef __USE_GNU
  42. # define assert_perror(errnum) (__ASSERT_VOID_CAST (0))
  43. # endif
  44. #else /* Not NDEBUG. */
  45. #ifndef _ASSERT_H_DECLS
  46. #define _ASSERT_H_DECLS
  47. __BEGIN_DECLS
  48. /* This prints an "Assertion failed" message and aborts. */
  49. extern void __assert_fail (const char *__assertion, const char *__file,
  50. unsigned int __line, const char *__function)
  51. __THROW __attribute__ ((__noreturn__));
  52. /* Likewise, but prints the error text for ERRNUM. */
  53. extern void __assert_perror_fail (int __errnum, const char *__file,
  54. unsigned int __line, const char *__function)
  55. __THROW __attribute__ ((__noreturn__));
  56. /* The following is not at all used here but needed for standard
  57. compliance. */
  58. extern void __assert (const char *__assertion, const char *__file, int __line)
  59. __THROW __attribute__ ((__noreturn__));
  60. __END_DECLS
  61. #endif /* Not _ASSERT_H_DECLS */
  62. /* When possible, define assert so that it does not add extra
  63. parentheses around EXPR. Otherwise, those added parentheses would
  64. suppress warnings we'd expect to be detected by gcc's -Wparentheses. */
  65. # if defined __cplusplus
  66. # define assert(expr) \
  67. (static_cast <bool> (expr) \
  68. ? void (0) \
  69. : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
  70. # elif !defined __GNUC__ || defined __STRICT_ANSI__
  71. # define assert(expr) \
  72. ((expr) \
  73. ? __ASSERT_VOID_CAST (0) \
  74. : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
  75. # else
  76. /* The first occurrence of EXPR is not evaluated due to the sizeof,
  77. but will trigger any pedantic warnings masked by the __extension__
  78. for the second occurrence. The ternary operator is required to
  79. support function pointers and bit fields in this context, and to
  80. suppress the evaluation of variable length arrays. */
  81. # define assert(expr) \
  82. ((void) sizeof ((expr) ? 1 : 0), __extension__ ({ \
  83. if (expr) \
  84. ; /* empty */ \
  85. else \
  86. __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION); \
  87. }))
  88. # endif
  89. # ifdef __USE_GNU
  90. # define assert_perror(errnum) \
  91. (!(errnum) \
  92. ? __ASSERT_VOID_CAST (0) \
  93. : __assert_perror_fail ((errnum), __FILE__, __LINE__, __ASSERT_FUNCTION))
  94. # endif
  95. /* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
  96. which contains the name of the function currently being defined.
  97. This is broken in G++ before version 2.6.
  98. C9x has a similar variable called __func__, but prefer the GCC one since
  99. it demangles C++ function names. */
  100. # if defined __cplusplus ? __GNUC_PREREQ (2, 6) : __GNUC_PREREQ (2, 4)
  101. # define __ASSERT_FUNCTION __extension__ __PRETTY_FUNCTION__
  102. # else
  103. # if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
  104. # define __ASSERT_FUNCTION __func__
  105. # else
  106. # define __ASSERT_FUNCTION ((const char *) 0)
  107. # endif
  108. # endif
  109. #endif /* NDEBUG. */
  110. #if defined __USE_ISOC11 && !defined __cplusplus
  111. # undef static_assert
  112. # define static_assert _Static_assert
  113. #endif