Back to index

glibc  2.9
assert.h
Go to the documentation of this file.
00001 /* Copyright (C) 1991,1992,1994-2001,2003,2004,2007
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 /*
00021  *     ISO C99 Standard: 7.2 Diagnostics  <assert.h>
00022  */
00023 
00024 #ifdef _ASSERT_H
00025 
00026 # undef       _ASSERT_H
00027 # undef       assert
00028 # undef __ASSERT_VOID_CAST
00029 
00030 # ifdef       __USE_GNU
00031 #  undef assert_perror
00032 # endif
00033 
00034 #endif /* assert.h   */
00035 
00036 #define       _ASSERT_H     1
00037 #include <features.h>
00038 
00039 #if defined __cplusplus && __GNUC_PREREQ (2,95)
00040 # define __ASSERT_VOID_CAST static_cast<void>
00041 #else
00042 # define __ASSERT_VOID_CAST (void)
00043 #endif
00044 
00045 /* void assert (int expression);
00046 
00047    If NDEBUG is defined, do nothing.
00048    If not, and EXPRESSION is zero, print an error message and abort.  */
00049 
00050 #ifdef NDEBUG
00051 
00052 # define assert(expr)              (__ASSERT_VOID_CAST (0))
00053 
00054 /* void assert_perror (int errnum);
00055 
00056    If NDEBUG is defined, do nothing.  If not, and ERRNUM is not zero, print an
00057    error message with the error text for ERRNUM and abort.
00058    (This is a GNU extension.) */
00059 
00060 # ifdef       __USE_GNU
00061 #  define assert_perror(errnum)    (__ASSERT_VOID_CAST (0))
00062 # endif
00063 
00064 #else /* Not NDEBUG.  */
00065 
00066 __BEGIN_DECLS
00067 
00068 /* This prints an "Assertion failed" message and aborts.  */
00069 extern void __assert_fail (__const char *__assertion, __const char *__file,
00070                         unsigned int __line, __const char *__function)
00071      __THROW __attribute__ ((__noreturn__));
00072 
00073 /* Likewise, but prints the error text for ERRNUM.  */
00074 extern void __assert_perror_fail (int __errnum, __const char *__file,
00075                               unsigned int __line,
00076                               __const char *__function)
00077      __THROW __attribute__ ((__noreturn__));
00078 
00079 
00080 /* The following is not at all used here but needed for standard
00081    compliance.  */
00082 extern void __assert (const char *__assertion, const char *__file, int __line)
00083      __THROW __attribute__ ((__noreturn__));
00084 
00085 
00086 __END_DECLS
00087 
00088 # define assert(expr)                                                 \
00089   ((expr)                                                      \
00090    ? __ASSERT_VOID_CAST (0)                                    \
00091    : __assert_fail (__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION))
00092 
00093 # ifdef       __USE_GNU
00094 #  define assert_perror(errnum)                                       \
00095   (!(errnum)                                                   \
00096    ? __ASSERT_VOID_CAST (0)                                    \
00097    : __assert_perror_fail ((errnum), __FILE__, __LINE__, __ASSERT_FUNCTION))
00098 # endif
00099 
00100 /* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
00101    which contains the name of the function currently being defined.
00102    This is broken in G++ before version 2.6.
00103    C9x has a similar variable called __func__, but prefer the GCC one since
00104    it demangles C++ function names.  */
00105 # if defined __cplusplus ? __GNUC_PREREQ (2, 6) : __GNUC_PREREQ (2, 4)
00106 #   define __ASSERT_FUNCTION       __PRETTY_FUNCTION__
00107 # else
00108 #  if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
00109 #   define __ASSERT_FUNCTION       __func__
00110 #  else
00111 #   define __ASSERT_FUNCTION       ((__const char *) 0)
00112 #  endif
00113 # endif
00114 
00115 #endif /* NDEBUG.  */