bug.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifndef _LINUX_BUG_H
  2. #define _LINUX_BUG_H
  3. #include <linux/compiler.h>
  4. #ifdef __CHECKER__
  5. #define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
  6. #define BUILD_BUG_ON_ZERO(e) (0)
  7. #define BUILD_BUG_ON_NULL(e) ((void*)0)
  8. #define BUILD_BUG_ON_INVALID(e) (0)
  9. #define BUILD_BUG_ON(condition) (0)
  10. #define BUILD_BUG() (0)
  11. #else /* __CHECKER__ */
  12. /* Force a compilation error if a constant expression is not a power of 2 */
  13. #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
  14. BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
  15. /* Force a compilation error if condition is true, but also produce a
  16. result (of value 0 and type size_t), so the expression can be used
  17. e.g. in a structure initializer (or where-ever else comma expressions
  18. aren't permitted). */
  19. #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
  20. #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
  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 - break compile if a condition is true.
  29. * @condition: the condition which the compiler should know is false.
  30. *
  31. * If you have some code which relies on certain constants being equal, or
  32. * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
  33. * detect if someone changes it.
  34. *
  35. * The implementation uses gcc's reluctance to create a negative array, but gcc
  36. * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to
  37. * inline functions). Luckily, in 4.3 they added the "error" function
  38. * attribute just for this type of case. Thus, we use a negative sized array
  39. * (should always create an error on gcc versions older than 4.4) and then call
  40. * an undefined function with the error attribute (should always create an
  41. * error on gcc 4.3 and later). If for some reason, neither creates a
  42. * compile-time error, we'll still have a link-time error, which is harder to
  43. * track down.
  44. */
  45. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  46. #endif /* __CHECKER__ */
  47. #endif /* _LINUX_BUG_H */