assert.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright (C) 1991,1992,1994-2001,2003,2004,2007
  2. Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  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.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. /*
  17. * ISO C99 Standard: 7.2 Diagnostics <assert.h>
  18. */
  19. #ifdef _ASSERT_H
  20. # undef _ASSERT_H
  21. # undef assert
  22. # undef __ASSERT_VOID_CAST
  23. # ifdef __USE_GNU
  24. # undef assert_perror
  25. # endif
  26. #endif /* assert.h */
  27. #define _ASSERT_H 1
  28. #include <features.h>
  29. #if defined __cplusplus && __GNUC_PREREQ (2,95)
  30. # define __ASSERT_VOID_CAST static_cast<void>
  31. #else
  32. # define __ASSERT_VOID_CAST (void)
  33. #endif
  34. /* void assert (int expression);
  35. If NDEBUG is defined, do nothing.
  36. If not, and EXPRESSION is zero, print an error message and abort. */
  37. #ifdef NDEBUG
  38. # define assert(expr) (__ASSERT_VOID_CAST (0))
  39. /* void assert_perror (int errnum);
  40. If NDEBUG is defined, do nothing. If not, and ERRNUM is not zero, print an
  41. error message with the error text for ERRNUM and abort.
  42. (This is a GNU extension.) */
  43. # ifdef __USE_GNU
  44. # define assert_perror(errnum) (__ASSERT_VOID_CAST (0))
  45. # endif
  46. #else /* Not NDEBUG. */
  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,
  55. __const char *__function)
  56. __THROW __attribute__ ((__noreturn__));
  57. /* The following is not at all used here but needed for standard
  58. compliance. */
  59. extern void __assert (const char *__assertion, const char *__file, int __line)
  60. __THROW __attribute__ ((__noreturn__));
  61. __END_DECLS
  62. # define assert(expr) \
  63. ((expr) \
  64. ? __ASSERT_VOID_CAST (0) \
  65. : __assert_fail (__STRING(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. */