build_bug.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /*
  6. * Force a compilation error if condition is true, but also produce a
  7. * result (of value 0 and type int), so the expression can be used
  8. * e.g. in a structure initializer (or where-ever else comma expressions
  9. * aren't permitted).
  10. *
  11. * Take an error message as an optional second argument. If omitted,
  12. * default to the stringification of the tested expression.
  13. */
  14. #define BUILD_BUG_ON_ZERO(e, ...) \
  15. __BUILD_BUG_ON_ZERO_MSG(e, ##__VA_ARGS__, #e " is true")
  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. * @cond: the condition which the compiler should know is false.
  31. * @msg: build-time error message
  32. *
  33. * See BUILD_BUG_ON for description.
  34. */
  35. #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
  36. /**
  37. * BUILD_BUG_ON - break compile if a condition is true.
  38. * @condition: the condition which the compiler should know is false.
  39. *
  40. * If you have some code which relies on certain constants being equal, or
  41. * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
  42. * detect if someone changes it.
  43. */
  44. #define BUILD_BUG_ON(condition) \
  45. BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
  46. /**
  47. * BUILD_BUG - break compile if used.
  48. *
  49. * If you have some code that you expect the compiler to eliminate at
  50. * build time, you should use BUILD_BUG to detect if it is
  51. * unexpectedly used.
  52. */
  53. #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
  54. /**
  55. * static_assert - check integer constant expression at build time
  56. * @expr: expression to be checked
  57. *
  58. * static_assert() is a wrapper for the C11 _Static_assert, with a
  59. * little macro magic to make the message optional (defaulting to the
  60. * stringification of the tested expression).
  61. *
  62. * Contrary to BUILD_BUG_ON(), static_assert() can be used at global
  63. * scope, but requires the expression to be an integer constant
  64. * expression (i.e., it is not enough that __builtin_constant_p() is
  65. * true for expr).
  66. *
  67. * Also note that BUILD_BUG_ON() fails the build if the condition is
  68. * true, while static_assert() fails the build if the expression is
  69. * false.
  70. */
  71. #ifndef static_assert
  72. #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
  73. #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
  74. #endif // static_assert
  75. /*
  76. * Compile time check that field has an expected offset
  77. */
  78. #define ASSERT_STRUCT_OFFSET(type, field, expected_offset) \
  79. BUILD_BUG_ON_MSG(offsetof(type, field) != (expected_offset), \
  80. "Offset of " #field " in " #type " has changed.")
  81. #endif /* _LINUX_BUILD_BUG_H */