assure.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* Run-time assert-like macros.
  2. Copyright (C) 2014-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser 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