Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_BUILD_BUG_H
0003 #define _LINUX_BUILD_BUG_H
0004 
0005 #include <linux/compiler.h>
0006 
0007 #ifdef __CHECKER__
0008 #define BUILD_BUG_ON_ZERO(e) (0)
0009 #else /* __CHECKER__ */
0010 /*
0011  * Force a compilation error if condition is true, but also produce a
0012  * result (of value 0 and type int), so the expression can be used
0013  * e.g. in a structure initializer (or where-ever else comma expressions
0014  * aren't permitted).
0015  */
0016 #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
0017 #endif /* __CHECKER__ */
0018 
0019 /* Force a compilation error if a constant expression is not a power of 2 */
0020 #define __BUILD_BUG_ON_NOT_POWER_OF_2(n)    \
0021     BUILD_BUG_ON(((n) & ((n) - 1)) != 0)
0022 #define BUILD_BUG_ON_NOT_POWER_OF_2(n)          \
0023     BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
0024 
0025 /*
0026  * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
0027  * expression but avoids the generation of any code, even if that expression
0028  * has side-effects.
0029  */
0030 #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
0031 
0032 /**
0033  * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied
0034  *            error message.
0035  * @condition: the condition which the compiler should know is false.
0036  *
0037  * See BUILD_BUG_ON for description.
0038  */
0039 #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
0040 
0041 /**
0042  * BUILD_BUG_ON - break compile if a condition is true.
0043  * @condition: the condition which the compiler should know is false.
0044  *
0045  * If you have some code which relies on certain constants being equal, or
0046  * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
0047  * detect if someone changes it.
0048  */
0049 #define BUILD_BUG_ON(condition) \
0050     BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
0051 
0052 /**
0053  * BUILD_BUG - break compile if used.
0054  *
0055  * If you have some code that you expect the compiler to eliminate at
0056  * build time, you should use BUILD_BUG to detect if it is
0057  * unexpectedly used.
0058  */
0059 #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
0060 
0061 /**
0062  * static_assert - check integer constant expression at build time
0063  *
0064  * static_assert() is a wrapper for the C11 _Static_assert, with a
0065  * little macro magic to make the message optional (defaulting to the
0066  * stringification of the tested expression).
0067  *
0068  * Contrary to BUILD_BUG_ON(), static_assert() can be used at global
0069  * scope, but requires the expression to be an integer constant
0070  * expression (i.e., it is not enough that __builtin_constant_p() is
0071  * true for expr).
0072  *
0073  * Also note that BUILD_BUG_ON() fails the build if the condition is
0074  * true, while static_assert() fails the build if the expression is
0075  * false.
0076  */
0077 #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
0078 #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
0079 
0080 #endif  /* _LINUX_BUILD_BUG_H */