assure.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Run-time assert-like macros.
  2. Copyright (C) 2014-2020 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert. */
  14. #ifndef _GL_ASSURE_H
  15. #define _GL_ASSURE_H
  16. #include <assert.h>
  17. #include "verify.h"
  18. /* Evaluate an assertion E that is guaranteed to be true.
  19. If NDEBUG is not defined, abort the program if E is false.
  20. If NDEBUG is defined, the compiler can assume E and behavior is
  21. undefined if E is false, fails to evaluate, or has side effects.
  22. Unlike standard 'assert', this macro evaluates E even when NDEBUG
  23. is defined, so as to catch typos, avoid some GCC warnings, and
  24. improve performance when E is simple enough.
  25. Also see the documentation for 'assume' in verify.h. */
  26. #ifdef NDEBUG
  27. # define affirm(E) assume (E)
  28. #else
  29. # define affirm(E) assert (E)
  30. #endif
  31. /* Check E's value at runtime, and report an error and abort if not.
  32. However, do nothing if NDEBUG is defined.
  33. Unlike standard 'assert', this macro compiles E even when NDEBUG
  34. is defined, so as to catch typos and avoid some GCC warnings.
  35. Unlike 'affirm', it is OK for E to use hard-to-optimize features,
  36. since E is not executed if NDEBUG is defined. */
  37. #ifdef NDEBUG
  38. # define assure(E) ((void) (0 && (E)))
  39. #else
  40. # define assure(E) assert (E)
  41. #endif
  42. #endif