~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/include/linux/build_bug.h

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php