assert.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright (C) 1991-2016 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. <http://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. __BEGIN_DECLS
  46. /* This prints an "Assertion failed" message and aborts. */
  47. extern void __assert_fail (const char *__assertion, const char *__file,
  48. unsigned int __line, const char *__function)
  49. __THROW __attribute__ ((__noreturn__));
  50. /* Likewise, but prints the error text for ERRNUM. */
  51. extern void __assert_perror_fail (int __errnum, const char *__file,
  52. unsigned int __line, const char *__function)
  53. __THROW __attribute__ ((__noreturn__));
  54. /* The following is not at all used here but needed for standard
  55. compliance. */
  56. extern void __assert (const char *__assertion, const char *__file, int __line)
  57. __THROW __attribute__ ((__noreturn__));
  58. __END_DECLS
  59. # define assert(expr) \
  60. ((expr) \
  61. ? __ASSERT_VOID_CAST (0) \
  62. : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
  63. # ifdef __USE_GNU
  64. # define assert_perror(errnum) \
  65. (!(errnum) \
  66. ? __ASSERT_VOID_CAST (0) \
  67. : __assert_perror_fail ((errnum), __FILE__, __LINE__, __ASSERT_FUNCTION))
  68. # endif
  69. /* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
  70. which contains the name of the function currently being defined.
  71. This is broken in G++ before version 2.6.
  72. C9x has a similar variable called __func__, but prefer the GCC one since
  73. it demangles C++ function names. */
  74. # if defined __cplusplus ? __GNUC_PREREQ (2, 6) : __GNUC_PREREQ (2, 4)
  75. # define __ASSERT_FUNCTION __PRETTY_FUNCTION__
  76. # else
  77. # if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
  78. # define __ASSERT_FUNCTION __func__
  79. # else
  80. # define __ASSERT_FUNCTION ((const char *) 0)
  81. # endif
  82. # endif
  83. #endif /* NDEBUG. */
  84. #if defined __USE_ISOC11 && !defined __cplusplus
  85. # undef static_assert
  86. # define static_assert _Static_assert
  87. #endif