build_bug.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_NOT_POWER_OF_2(n) (0)
  7. #define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
  8. #define BUILD_BUG_ON_ZERO(e) (0)
  9. #define BUILD_BUG_ON_INVALID(e) (0)
  10. #define BUILD_BUG_ON_MSG(cond, msg) (0)
  11. #define BUILD_BUG_ON(condition) (0)
  12. #define BUILD_BUG() (0)
  13. #else /* __CHECKER__ */
  14. /* Force a compilation error if a constant expression is not a power of 2 */
  15. #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \
  16. BUILD_BUG_ON(((n) & ((n) - 1)) != 0)
  17. #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
  18. BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
  19. /*
  20. * Force a compilation error if condition is true, but also produce a
  21. * result (of value 0 and type size_t), so the expression can be used
  22. * e.g. in a structure initializer (or where-ever else comma expressions
  23. * aren't permitted).
  24. */
  25. #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); }))
  26. /*
  27. * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
  28. * expression but avoids the generation of any code, even if that expression
  29. * has side-effects.
  30. */
  31. #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
  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. * BUILD_BUG_ON - break compile if a condition is true.
  42. * @condition: the condition which the compiler should know is false.
  43. *
  44. * If you have some code which relies on certain constants being equal, or
  45. * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
  46. * detect if someone changes it.
  47. *
  48. * The implementation uses gcc's reluctance to create a negative array, but gcc
  49. * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to
  50. * inline functions). Luckily, in 4.3 they added the "error" function
  51. * attribute just for this type of case. Thus, we use a negative sized array
  52. * (should always create an error on gcc versions older than 4.4) and then call
  53. * an undefined function with the error attribute (should always create an
  54. * error on gcc 4.3 and later). If for some reason, neither creates a
  55. * compile-time error, we'll still have a link-time error, which is harder to
  56. * track down.
  57. */
  58. #ifndef __OPTIMIZE__
  59. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  60. #else
  61. #define BUILD_BUG_ON(condition) \
  62. BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
  63. #endif
  64. /**
  65. * BUILD_BUG - break compile if used.
  66. *
  67. * If you have some code that you expect the compiler to eliminate at
  68. * build time, you should use BUILD_BUG to detect if it is
  69. * unexpectedly used.
  70. */
  71. #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
  72. #endif /* __CHECKER__ */
  73. #endif /* _LINUX_BUILD_BUG_H */