assert.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #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. # define assert(expr) \
  63. ((expr) \
  64. ? __ASSERT_VOID_CAST (0) \
  65. : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
  66. # ifdef __USE_GNU
  67. # define assert_perror(errnum) \
  68. (!(errnum) \
  69. ? __ASSERT_VOID_CAST (0) \
  70. : __assert_perror_fail ((errnum), __FILE__, __LINE__, __ASSERT_FUNCTION))
  71. # endif
  72. /* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
  73. which contains the name of the function currently being defined.
  74. This is broken in G++ before version 2.6.
  75. C9x has a similar variable called __func__, but prefer the GCC one since
  76. it demangles C++ function names. */
  77. # if defined __cplusplus ? __GNUC_PREREQ (2, 6) : __GNUC_PREREQ (2, 4)
  78. # define __ASSERT_FUNCTION __PRETTY_FUNCTION__
  79. # else
  80. # if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
  81. # define __ASSERT_FUNCTION __func__
  82. # else
  83. # define __ASSERT_FUNCTION ((const char *) 0)
  84. # endif
  85. # endif
  86. #endif /* NDEBUG. */
  87. #if defined __USE_ISOC11 && !defined __cplusplus
  88. # undef static_assert
  89. # define static_assert _Static_assert
  90. #endif