Assert.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Assert_H
  16. #define Assert_H
  17. #include "util/UniqueName.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  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. struct UniqueName_get() { unsigned int assertFailed : (isTrue); }
  29. /** Runtime assertion which is always applied. */
  30. #define Assert_always(expr) do { \
  31. if (!(expr)) { \
  32. fprintf(stderr, "%s:%d Assertion failed: %s\n", \
  33. __FILE__, __LINE__, Assert_STRING(expr)); \
  34. abort(); \
  35. } \
  36. } while (0)
  37. /* CHECKFILES_IGNORE a ; is expected after the while(0) but it will be supplied by the caller */
  38. // Turn off assertions when the code is more stable.
  39. #define Assert_true(expr) Assert_always(expr)
  40. #else
  41. #include "util/UniqueName.h"
  42. #endif