^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef _LINUX_BUILD_BUG_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define _LINUX_BUILD_BUG_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #ifdef __CHECKER__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define BUILD_BUG_ON_ZERO(e) (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #else /* __CHECKER__ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Force a compilation error if condition is true, but also produce a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * result (of value 0 and type int), so the expression can be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * e.g. in a structure initializer (or where-ever else comma expressions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * aren't permitted).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #endif /* __CHECKER__ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /* Force a compilation error if a constant expression is not a power of 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) BUILD_BUG_ON(((n) & ((n) - 1)) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * expression but avoids the generation of any code, even if that expression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * has side-effects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * error message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * @condition: the condition which the compiler should know is false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * See BUILD_BUG_ON for description.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * BUILD_BUG_ON - break compile if a condition is true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * @condition: the condition which the compiler should know is false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * If you have some code which relies on certain constants being equal, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * detect if someone changes it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define BUILD_BUG_ON(condition) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * BUILD_BUG - break compile if used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * If you have some code that you expect the compiler to eliminate at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * build time, you should use BUILD_BUG to detect if it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * unexpectedly used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * static_assert - check integer constant expression at build time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * static_assert() is a wrapper for the C11 _Static_assert, with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * little macro magic to make the message optional (defaulting to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * stringification of the tested expression).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * Contrary to BUILD_BUG_ON(), static_assert() can be used at global
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * scope, but requires the expression to be an integer constant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * expression (i.e., it is not enough that __builtin_constant_p() is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * true for expr).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * Also note that BUILD_BUG_ON() fails the build if the condition is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * true, while static_assert() fails the build if the expression is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #ifndef static_assert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #endif // static_assert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #endif /* _LINUX_BUILD_BUG_H */