bug.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef _LINUX_BUG_H
  2. #define _LINUX_BUG_H
  3. #include <asm/bug.h>
  4. enum bug_trap_type {
  5. BUG_TRAP_TYPE_NONE = 0,
  6. BUG_TRAP_TYPE_WARN = 1,
  7. BUG_TRAP_TYPE_BUG = 2,
  8. };
  9. struct pt_regs;
  10. #ifdef __CHECKER__
  11. #define BUILD_BUG_ON_NOT_POWER_OF_2(n)
  12. #define BUILD_BUG_ON_ZERO(e) (0)
  13. #define BUILD_BUG_ON_NULL(e) ((void*)0)
  14. #define BUILD_BUG_ON_INVALID(e) (0)
  15. #define BUILD_BUG_ON(condition)
  16. #define BUILD_BUG() (0)
  17. #else /* __CHECKER__ */
  18. /* Force a compilation error if a constant expression is not a power of 2 */
  19. #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
  20. BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
  21. /* Force a compilation error if condition is true, but also produce a
  22. result (of value 0 and type size_t), so the expression can be used
  23. e.g. in a structure initializer (or where-ever else comma expressions
  24. aren't permitted). */
  25. #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
  26. #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
  27. /*
  28. * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
  29. * expression but avoids the generation of any code, even if that expression
  30. * has side-effects.
  31. */
  32. #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
  33. /**
  34. * BUILD_BUG_ON - break compile if a condition is true.
  35. * @condition: the condition which the compiler should know is false.
  36. *
  37. * If you have some code which relies on certain constants being equal, or
  38. * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
  39. * detect if someone changes it.
  40. *
  41. * The implementation uses gcc's reluctance to create a negative array, but
  42. * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
  43. * to inline functions). So as a fallback we use the optimizer; if it can't
  44. * prove the condition is false, it will cause a link error on the undefined
  45. * "__build_bug_on_failed". This error message can be harder to track down
  46. * though, hence the two different methods.
  47. */
  48. #ifndef __OPTIMIZE__
  49. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  50. #else
  51. extern int __build_bug_on_failed;
  52. #define BUILD_BUG_ON(condition) \
  53. do { \
  54. ((void)sizeof(char[1 - 2*!!(condition)])); \
  55. if (condition) __build_bug_on_failed = 1; \
  56. } while(0)
  57. #endif
  58. /**
  59. * BUILD_BUG - break compile if used.
  60. *
  61. * If you have some code that you expect the compiler to eliminate at
  62. * build time, you should use BUILD_BUG to detect if it is
  63. * unexpectedly used.
  64. */
  65. #define BUILD_BUG() \
  66. do { \
  67. extern void __build_bug_failed(void) \
  68. __linktime_error("BUILD_BUG failed"); \
  69. __build_bug_failed(); \
  70. } while (0)
  71. #endif /* __CHECKER__ */
  72. #ifdef CONFIG_GENERIC_BUG
  73. #include <asm-generic/bug.h>
  74. static inline int is_warning_bug(const struct bug_entry *bug)
  75. {
  76. return bug->flags & BUGFLAG_WARNING;
  77. }
  78. const struct bug_entry *find_bug(unsigned long bugaddr);
  79. enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
  80. /* These are defined by the architecture */
  81. int is_valid_bugaddr(unsigned long addr);
  82. #else /* !CONFIG_GENERIC_BUG */
  83. static inline enum bug_trap_type report_bug(unsigned long bug_addr,
  84. struct pt_regs *regs)
  85. {
  86. return BUG_TRAP_TYPE_BUG;
  87. }
  88. #endif /* CONFIG_GENERIC_BUG */
  89. #endif /* _LINUX_BUG_H */