build_bug.h 2.9 KB

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