xref: /freebsd/sys/compat/linuxkpi/common/include/linux/build_bug.h (revision 344a04ad2c5e62b2905bc2f8b6f992ae590403fd)
131ef2909SVladimir Kondratyev /*-
231ef2909SVladimir Kondratyev  * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org>
331ef2909SVladimir Kondratyev  * Copyright (c) 2018 Johannes Lundberg <johalun0@gmail.com>
431ef2909SVladimir Kondratyev  * Copyright (c) 2021 The FreeBSD Foundation
531ef2909SVladimir Kondratyev  * Copyright (c) 2021 Vladimir Kondratyev <wulf@FreeBSD.org>
631ef2909SVladimir Kondratyev  * Copyright (c) 2023 Serenity Cyber Security, LLC
731ef2909SVladimir Kondratyev  *
831ef2909SVladimir Kondratyev  * Portions of this software were developed by Bjoern A. Zeeb
931ef2909SVladimir Kondratyev  * under sponsorship from the FreeBSD Foundation.
1031ef2909SVladimir Kondratyev  *
1131ef2909SVladimir Kondratyev  * Redistribution and use in source and binary forms, with or without
1231ef2909SVladimir Kondratyev  * modification, are permitted provided that the following conditions
1331ef2909SVladimir Kondratyev  * are met:
1431ef2909SVladimir Kondratyev  * 1. Redistributions of source code must retain the above copyright
1531ef2909SVladimir Kondratyev  *    notice unmodified, this list of conditions, and the following
1631ef2909SVladimir Kondratyev  *    disclaimer.
1731ef2909SVladimir Kondratyev  * 2. Redistributions in binary form must reproduce the above copyright
1831ef2909SVladimir Kondratyev  *    notice, this list of conditions and the following disclaimer in the
1931ef2909SVladimir Kondratyev  *    documentation and/or other materials provided with the distribution.
2031ef2909SVladimir Kondratyev  *
2131ef2909SVladimir Kondratyev  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2231ef2909SVladimir Kondratyev  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2331ef2909SVladimir Kondratyev  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2431ef2909SVladimir Kondratyev  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2531ef2909SVladimir Kondratyev  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2631ef2909SVladimir Kondratyev  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2731ef2909SVladimir Kondratyev  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2831ef2909SVladimir Kondratyev  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2931ef2909SVladimir Kondratyev  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3031ef2909SVladimir Kondratyev  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131ef2909SVladimir Kondratyev  */
3231ef2909SVladimir Kondratyev 
3331ef2909SVladimir Kondratyev #ifndef	_LINUXKPI_LINUX_BUILD_BUG_H_
3431ef2909SVladimir Kondratyev #define	_LINUXKPI_LINUX_BUILD_BUG_H_
3531ef2909SVladimir Kondratyev 
3631ef2909SVladimir Kondratyev #include <sys/param.h>
3731ef2909SVladimir Kondratyev 
3831ef2909SVladimir Kondratyev #include <linux/compiler.h>
3931ef2909SVladimir Kondratyev 
4031ef2909SVladimir Kondratyev /*
4131ef2909SVladimir Kondratyev  * BUILD_BUG_ON() can happen inside functions where _Static_assert() does not
4231ef2909SVladimir Kondratyev  * seem to work.  Use old-schoold-ish CTASSERT from before commit
4331ef2909SVladimir Kondratyev  * a3085588a88fa58eb5b1eaae471999e1995a29cf but also make sure we do not
4431ef2909SVladimir Kondratyev  * end up with an unused typedef or variable. The compiler should optimise
4531ef2909SVladimir Kondratyev  * it away entirely.
4631ef2909SVladimir Kondratyev  */
4731ef2909SVladimir Kondratyev #define	_O_CTASSERT(x)		_O__CTASSERT(x, __LINE__)
4831ef2909SVladimir Kondratyev #define	_O__CTASSERT(x, y)	_O___CTASSERT(x, y)
4931ef2909SVladimir Kondratyev #define	_O___CTASSERT(x, y)	while (0) { \
5031ef2909SVladimir Kondratyev     typedef char __assert_line_ ## y[(x) ? 1 : -1]; \
51*344a04adSMark Johnston     __assert_line_ ## y _x __unused; \
5231ef2909SVladimir Kondratyev     _x[0] = '\0'; \
5331ef2909SVladimir Kondratyev }
5431ef2909SVladimir Kondratyev 
5531ef2909SVladimir Kondratyev #define	BUILD_BUG()			do { CTASSERT(0); } while (0)
5631ef2909SVladimir Kondratyev #define	BUILD_BUG_ON(x)			do { _O_CTASSERT(!(x)) } while (0)
5731ef2909SVladimir Kondratyev #define	BUILD_BUG_ON_MSG(x, msg)	BUILD_BUG_ON(x)
5831ef2909SVladimir Kondratyev #define	BUILD_BUG_ON_NOT_POWER_OF_2(x)	BUILD_BUG_ON(!powerof2(x))
5931ef2909SVladimir Kondratyev #define	BUILD_BUG_ON_INVALID(expr)	while (0) { (void)(expr); }
6031ef2909SVladimir Kondratyev #define	BUILD_BUG_ON_ZERO(x)	((int)sizeof(struct { int:-((x) != 0); }))
6131ef2909SVladimir Kondratyev 
6231ef2909SVladimir Kondratyev #define static_assert(x, ...)		__static_assert(x, ##__VA_ARGS__, #x)
6331ef2909SVladimir Kondratyev #define __static_assert(x, msg, ...)	_Static_assert(x, msg)
6431ef2909SVladimir Kondratyev 
6531ef2909SVladimir Kondratyev #endif
66