build_assert.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #ifndef CCAN_BUILD_ASSERT_H
  2. #define CCAN_BUILD_ASSERT_H
  3. /**
  4. * BUILD_ASSERT - assert a build-time dependency.
  5. * @cond: the compile-time condition which must be true.
  6. *
  7. * Your compile will fail if the condition isn't true, or can't be evaluated
  8. * by the compiler. This can only be used within a function.
  9. *
  10. * Example:
  11. * char *foo_to_char(struct foo *foo)
  12. * {
  13. * // This code needs string to be at start of foo.
  14. * BUILD_ASSERT(offsetof(struct foo, string) == 0);
  15. * return (char *)foo;
  16. * }
  17. */
  18. #define BUILD_ASSERT(cond) \
  19. do { (void) sizeof(char [1 - 2*!(cond)]); } while(0)
  20. /**
  21. * EXPR_BUILD_ASSERT - assert a build-time dependency, as an expression.
  22. * @cond: the compile-time condition which must be true.
  23. *
  24. * Your compile will fail if the condition isn't true, or can't be evaluated
  25. * by the compiler. This can be used in an expression: its value is "0".
  26. *
  27. * Example:
  28. * #define foo_to_char(foo) \
  29. * ((char *)(foo) \
  30. * + EXPR_BUILD_ASSERT(offsetof(struct foo, string) == 0))
  31. */
  32. #define EXPR_BUILD_ASSERT(cond) \
  33. (sizeof(char [1 - 2*!(cond)]) - 1)
  34. #endif /* CCAN_BUILD_ASSERT_H */