Files
glibc/assert/assert.h
T

142 lines
4.5 KiB
C
Raw Normal View History

/* Copyright (C) 1991-2021 Free Software Foundation, Inc.
1996-10-31 02:57:12 +00:00
This file is part of the GNU C Library.
1995-02-18 01:27:10 +00:00
1996-10-31 02:57:12 +00:00
The GNU C Library is free software; you can redistribute it and/or
2001-07-06 04:58:11 +00:00
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
1995-02-18 01:27:10 +00:00
1996-10-31 02:57:12 +00:00
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2001-07-06 04:58:11 +00:00
Lesser General Public License for more details.
1995-02-18 01:27:10 +00:00
2001-07-06 04:58:11 +00:00
You should have received a copy of the GNU Lesser General Public
2012-02-09 23:18:22 +00:00
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
1995-02-18 01:27:10 +00:00
/*
2000-09-14 01:43:19 +00:00
* ISO C99 Standard: 7.2 Diagnostics <assert.h>
1995-02-18 01:27:10 +00:00
*/
#ifdef _ASSERT_H
1997-08-20 03:53:21 +00:00
# undef _ASSERT_H
# undef assert
# undef __ASSERT_VOID_CAST
1997-08-20 03:53:21 +00:00
# ifdef __USE_GNU
# undef assert_perror
# endif
1995-02-18 01:27:10 +00:00
#endif /* assert.h */
#define _ASSERT_H 1
#include <features.h>
#if defined __cplusplus && __GNUC_PREREQ (2,95)
# define __ASSERT_VOID_CAST static_cast<void>
#else
# define __ASSERT_VOID_CAST (void)
#endif
1995-02-18 01:27:10 +00:00
/* void assert (int expression);
If NDEBUG is defined, do nothing.
If not, and EXPRESSION is zero, print an error message and abort. */
#ifdef NDEBUG
# define assert(expr) (__ASSERT_VOID_CAST (0))
1995-02-18 01:27:10 +00:00
/* void assert_perror (int errnum);
If NDEBUG is defined, do nothing. If not, and ERRNUM is not zero, print an
error message with the error text for ERRNUM and abort.
(This is a GNU extension.) */
1997-07-12 23:35:19 +00:00
# ifdef __USE_GNU
# define assert_perror(errnum) (__ASSERT_VOID_CAST (0))
1997-07-12 23:35:19 +00:00
# endif
1995-02-18 01:27:10 +00:00
#else /* Not NDEBUG. */
__BEGIN_DECLS
/* This prints an "Assertion failed" message and aborts. */
2012-01-07 23:57:22 -05:00
extern void __assert_fail (const char *__assertion, const char *__file,
unsigned int __line, const char *__function)
1999-10-09 21:56:43 +00:00
__THROW __attribute__ ((__noreturn__));
1995-02-18 01:27:10 +00:00
/* Likewise, but prints the error text for ERRNUM. */
2012-01-07 23:57:22 -05:00
extern void __assert_perror_fail (int __errnum, const char *__file,
unsigned int __line, const char *__function)
1999-10-09 21:56:43 +00:00
__THROW __attribute__ ((__noreturn__));
1995-02-18 01:27:10 +00:00
2000-08-02 00:09:25 +00:00
/* The following is not at all used here but needed for standard
compliance. */
extern void __assert (const char *__assertion, const char *__file, int __line)
__THROW __attribute__ ((__noreturn__));
1995-02-18 01:27:10 +00:00
__END_DECLS
/* When possible, define assert so that it does not add extra
parentheses around EXPR. Otherwise, those added parentheses would
suppress warnings we'd expect to be detected by gcc's -Wparentheses. */
# if defined __cplusplus
# define assert(expr) \
(static_cast <bool> (expr) \
? void (0) \
: __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
# elif !defined __GNUC__ || defined __STRICT_ANSI__
# define assert(expr) \
((expr) \
? __ASSERT_VOID_CAST (0) \
: __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
# else
/* The first occurrence of EXPR is not evaluated due to the sizeof,
but will trigger any pedantic warnings masked by the __extension__
for the second occurrence. The ternary operator is required to
support function pointers and bit fields in this context, and to
suppress the evaluation of variable length arrays. */
# define assert(expr) \
((void) sizeof ((expr) ? 1 : 0), __extension__ ({ \
if (expr) \
; /* empty */ \
else \
__assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION); \
}))
# endif
1995-02-18 01:27:10 +00:00
1997-07-12 23:35:19 +00:00
# ifdef __USE_GNU
2006-08-03 08:30:09 +00:00
# define assert_perror(errnum) \
(!(errnum) \
? __ASSERT_VOID_CAST (0) \
2007-02-16 07:58:29 +00:00
: __assert_perror_fail ((errnum), __FILE__, __LINE__, __ASSERT_FUNCTION))
1997-07-12 23:35:19 +00:00
# endif
1995-02-18 01:27:10 +00:00
/* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
which contains the name of the function currently being defined.
1999-05-18 08:37:39 +00:00
This is broken in G++ before version 2.6.
C9x has a similar variable called __func__, but prefer the GCC one since
it demangles C++ function names. */
1999-06-24 13:08:20 +00:00
# if defined __cplusplus ? __GNUC_PREREQ (2, 6) : __GNUC_PREREQ (2, 4)
# define __ASSERT_FUNCTION __extension__ __PRETTY_FUNCTION__
1999-05-18 08:37:39 +00:00
# else
# if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
# define __ASSERT_FUNCTION __func__
# else
2012-01-07 23:57:22 -05:00
# define __ASSERT_FUNCTION ((const char *) 0)
1999-05-18 08:37:39 +00:00
# endif
1997-07-12 23:35:19 +00:00
# endif
1995-02-18 01:27:10 +00:00
#endif /* NDEBUG. */
2011-12-23 22:55:44 -05:00
2012-01-06 18:11:50 +00:00
#if defined __USE_ISOC11 && !defined __cplusplus
2011-12-23 22:55:44 -05:00
# undef static_assert
# define static_assert _Static_assert
#endif