Assert.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. /**
  22. * Assert_compileTime()
  23. *
  24. * Prevent compilation if assertion is false or not a compile time constant.
  25. * Thanks to http://www.jaggersoft.com/pubs/CVu11_3.html
  26. */
  27. #define Assert_compileTime(isTrue) \
  28. void Assert_compileTime(char x[1 - (!(isTrue))])
  29. Gcc_PRINTF(1, 2)
  30. Gcc_NORETURN
  31. void Assert_failure(const char* format, ...);
  32. #define Assert_fileLine(expr, file, line) do { \
  33. if (!(expr)) { \
  34. Assert_failure("Assertion failure [%s:%d] [%s]\n", (file), (line), \
  35. #expr); \
  36. } \
  37. } while (0)
  38. /* CHECKFILES_IGNORE a ; is expected after the while(0) but it will be supplied by the caller */
  39. /** Runtime assertion which is always applied. */
  40. #define Assert_true(expr) Assert_fileLine((expr), Gcc_SHORT_FILE, Gcc_LINE)
  41. #ifdef PARANOIA
  42. #define Assert_ifParanoid(expr) Assert_true(expr)
  43. #else
  44. #define Assert_ifParanoid(expr) do { } while (0)
  45. /* CHECKFILES_IGNORE a ; is expected after the while(0) but it will be supplied by the caller */
  46. #endif
  47. #ifdef TESTING
  48. #define Assert_ifTesting(expr) Assert_true(expr)
  49. #else
  50. #define Assert_ifTesting(expr) do { } while (0)
  51. /* CHECKFILES_IGNORE a ; is expected after the while(0) but it will be supplied by the caller */
  52. #endif
  53. #endif