assert.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. * assert.h
  3. *
  4. * Define a common interface for assertions that builds for both the PAL
  5. * and libOS.
  6. *
  7. */
  8. #ifndef ASSERT_H
  9. #define ASSERT_H
  10. #include <stdnoreturn.h>
  11. #define static_assert _Static_assert
  12. /* All environments should implement warn, which prints a non-optional debug
  13. * message. All environments should also implement __abort, which
  14. * terminates the process.
  15. */
  16. void warn(const char* format, ...) __attribute__((format(printf, 1, 2)));
  17. noreturn void __abort(void);
  18. /* TODO(mkow): We should actually use the standard `NDEBUG`, but that would require changes in the
  19. * build system.
  20. */
  21. #ifdef DEBUG
  22. #define assert(expr) \
  23. ({ \
  24. (!(expr)) ? ({ \
  25. warn("assert failed " __FILE__ ":%d %s\n", __LINE__, #expr); \
  26. __abort(); \
  27. }) \
  28. : (void)0; \
  29. })
  30. #else
  31. #define assert(expr) ((void)0)
  32. #endif
  33. #endif /* ASSERT_H */