xref: /freebsd/include/assert.h (revision 712f81feea416e9f8aaf040173883876a50a7d34)
159deaec5SRodney W. Grimes /*-
22321c474SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
32321c474SPedro F. Giffuni  *
459deaec5SRodney W. Grimes  * Copyright (c) 1992, 1993
559deaec5SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
659deaec5SRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
759deaec5SRodney W. Grimes  * All or some portions of this file are derived from material licensed
859deaec5SRodney W. Grimes  * to the University of California by American Telephone and Telegraph
959deaec5SRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
1059deaec5SRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
1159deaec5SRodney W. Grimes  *
1259deaec5SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1359deaec5SRodney W. Grimes  * modification, are permitted provided that the following conditions
1459deaec5SRodney W. Grimes  * are met:
1559deaec5SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1659deaec5SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1759deaec5SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1859deaec5SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1959deaec5SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20f2556687SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
2159deaec5SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2259deaec5SRodney W. Grimes  *    without specific prior written permission.
2359deaec5SRodney W. Grimes  *
2459deaec5SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2559deaec5SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2659deaec5SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2759deaec5SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2859deaec5SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2959deaec5SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3059deaec5SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3159deaec5SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3259deaec5SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3359deaec5SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3459deaec5SRodney W. Grimes  * SUCH DAMAGE.
3559deaec5SRodney W. Grimes  */
3659deaec5SRodney W. Grimes 
37c44a2767SMike Barcroft #include <sys/cdefs.h>
38c44a2767SMike Barcroft 
3959deaec5SRodney W. Grimes /*
4059deaec5SRodney W. Grimes  * Unlike other ANSI header files, <assert.h> may usefully be included
4159deaec5SRodney W. Grimes  * multiple times, with and without NDEBUG defined.
4259deaec5SRodney W. Grimes  */
4359deaec5SRodney W. Grimes 
4459deaec5SRodney W. Grimes #undef assert
4559deaec5SRodney W. Grimes #undef _assert
46*712f81feSKyle Evans #undef __assert_unreachable
4759deaec5SRodney W. Grimes 
4859deaec5SRodney W. Grimes #ifdef NDEBUG
4959deaec5SRodney W. Grimes #define	assert(e)	((void)0)
5059deaec5SRodney W. Grimes #define	_assert(e)	((void)0)
51*712f81feSKyle Evans #if __BSD_VISIBLE
52*712f81feSKyle Evans #define	__assert_unreachable()	__unreachable()
53*712f81feSKyle Evans #endif	/* __BSD_VISIBLE */
5459deaec5SRodney W. Grimes #else
5559deaec5SRodney W. Grimes #define	_assert(e)	assert(e)
56c44a2767SMike Barcroft 
578c825c1aSJeroen Ruigrok van der Werven #define	assert(e)	((e) ? (void)0 : __assert(__func__, __FILE__, \
588c825c1aSJeroen Ruigrok van der Werven 			    __LINE__, #e))
59*712f81feSKyle Evans #if __BSD_VISIBLE
60*712f81feSKyle Evans #define	__assert_unreachable()	assert(0 && "unreachable segment reached")
61*712f81feSKyle Evans #endif	/* __BSD_VISIBLE */
62c44a2767SMike Barcroft #endif /* NDEBUG */
6359deaec5SRodney W. Grimes 
6419d6d0ebSPoul-Henning Kamp #ifndef _ASSERT_H_
6519d6d0ebSPoul-Henning Kamp #define _ASSERT_H_
66c6d3530dSEd Schouten 
67ffa01562SEd Schouten /*
68ffa01562SEd Schouten  * Static assertions.  In principle we could define static_assert for
69ffa01562SEd Schouten  * C++ older than C++11, but this breaks if _Static_assert is
70ffa01562SEd Schouten  * implemented as a macro.
71ffa01562SEd Schouten  *
72ffa01562SEd Schouten  * C++ template parameters may contain commas, even if not enclosed in
73ffa01562SEd Schouten  * parentheses, causing the _Static_assert macro to be invoked with more
74ffa01562SEd Schouten  * than two parameters.
75ffa01562SEd Schouten  */
76ffa01562SEd Schouten #if __ISO_C_VISIBLE >= 2011 && !defined(__cplusplus)
77c6d3530dSEd Schouten #define	static_assert	_Static_assert
78c6d3530dSEd Schouten #endif
79c6d3530dSEd Schouten 
8059deaec5SRodney W. Grimes __BEGIN_DECLS
8162779d80SEd Schouten void __assert(const char *, const char *, int, const char *) __dead2;
8259deaec5SRodney W. Grimes __END_DECLS
83c6d3530dSEd Schouten 
8422ac70fcSPoul-Henning Kamp #endif /* !_ASSERT_H_ */
85