bug.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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(condition)
  15. #define BUILD_BUG() (0)
  16. #else /* __CHECKER__ */
  17. /* Force a compilation error if a constant expression is not a power of 2 */
  18. #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
  19. BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
  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. #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
  25. #define BUILD_BUG_ON_NULL(e) ((void *)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 - break compile if a condition is true.
  34. * @condition: the condition which the compiler should know is false.
  35. *
  36. * If you have some code which relies on certain constants being equal, or
  37. * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
  38. * detect if someone changes it.
  39. *
  40. * The implementation uses gcc's reluctance to create a negative array, but
  41. * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
  42. * to inline functions). So as a fallback we use the optimizer; if it can't
  43. * prove the condition is false, it will cause a link error on the undefined
  44. * "__build_bug_on_failed". This error message can be harder to track down
  45. * though, hence the two different methods.
  46. */
  47. #ifndef __OPTIMIZE__
  48. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  49. #else
  50. extern int __build_bug_on_failed;
  51. #define BUILD_BUG_ON(condition) \
  52. do { \
  53. ((void)sizeof(char[1 - 2*!!(condition)])); \
  54. if (condition) __build_bug_on_failed = 1; \
  55. } while(0)
  56. #endif
  57. /**
  58. * BUILD_BUG - break compile if used.
  59. *
  60. * If you have some code that you expect the compiler to eliminate at
  61. * build time, you should use BUILD_BUG to detect if it is
  62. * unexpectedly used.
  63. */
  64. #define BUILD_BUG() \
  65. do { \
  66. extern void __build_bug_failed(void) \
  67. __linktime_error("BUILD_BUG failed"); \
  68. __build_bug_failed(); \
  69. } while (0)
  70. #endif /* __CHECKER__ */
  71. #ifdef CONFIG_GENERIC_BUG
  72. #include <asm-generic/bug.h>
  73. static inline int is_warning_bug(const struct bug_entry *bug)
  74. {
  75. return bug->flags & BUGFLAG_WARNING;
  76. }
  77. const struct bug_entry *find_bug(unsigned long bugaddr);
  78. enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
  79. /* These are defined by the architecture */
  80. int is_valid_bugaddr(unsigned long addr);
  81. #else /* !CONFIG_GENERIC_BUG */
  82. static inline enum bug_trap_type report_bug(unsigned long bug_addr,
  83. struct pt_regs *regs)
  84. {
  85. return BUG_TRAP_TYPE_BUG;
  86. }
  87. #endif /* CONFIG_GENERIC_BUG */
  88. #endif /* _LINUX_BUG_H */