Assert.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Assert_H
  16. #define Assert_H
  17. #include "util/Gcc.h"
  18. #include "util/Linker.h"
  19. Linker_require("util/Assert.c")
  20. #define Assert_STRING(x) #x
  21. #define Assert_compileTime(isTrue) _Static_assert(isTrue, #isTrue)
  22. Gcc_PRINTF(1, 2)
  23. Gcc_NORETURN
  24. void Assert_failure(const char* format, ...);
  25. #define Assert_fileLine(expr, file, line) do { \
  26. if (!(expr)) { \
  27. Assert_failure("Assertion failure [%s:%d] [%s]\n", (file), (line), \
  28. #expr); \
  29. } \
  30. } while (0)
  31. /* CHECKFILES_IGNORE a ; is expected after the while(0) but it will be supplied by the caller */
  32. /** Runtime assertion which is always applied. */
  33. #define Assert_true(expr) Assert_fileLine((expr), Gcc_SHORT_FILE, Gcc_LINE)
  34. #ifdef PARANOIA
  35. #define Assert_ifParanoid(expr) Assert_true(expr)
  36. #else
  37. #define Assert_ifParanoid(expr) do { } while (0)
  38. /* CHECKFILES_IGNORE a ; is expected after the while(0) but it will be supplied by the caller */
  39. #endif
  40. #ifdef TESTING
  41. #define Assert_ifTesting(expr) Assert_true(expr)
  42. #else
  43. #define Assert_ifTesting(expr) do { } while (0)
  44. /* CHECKFILES_IGNORE a ; is expected after the while(0) but it will be supplied by the caller */
  45. #endif
  46. #endif