| 1 |
/* Run-time assert-like macros. |
| 2 |
|
| 3 |
Copyright (C) 2014-2024 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is free software: you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU Lesser General Public License as |
| 7 |
published by the Free Software Foundation; either version 2.1 of the |
| 8 |
License, or (at your option) any later version. |
| 9 |
|
| 10 |
This file is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
/* Written by Paul Eggert. */ |
| 19 |
|
| 20 |
#ifndef _GL_ASSURE_H |
| 21 |
#define _GL_ASSURE_H |
| 22 |
|
| 23 |
#include <assert.h> |
| 24 |
#include "verify.h" |
| 25 |
|
| 26 |
/* Evaluate an assertion E that is guaranteed to be true. |
| 27 |
If NDEBUG is not defined, abort the program if E is false. |
| 28 |
If NDEBUG is defined, the compiler can assume E and behavior is |
| 29 |
undefined if E is false, fails to evaluate, or has side effects. |
| 30 |
|
| 31 |
Unlike standard 'assert', this macro evaluates E even when NDEBUG |
| 32 |
is defined, so as to catch typos, avoid some GCC warnings, and |
| 33 |
improve performance when E is simple enough. |
| 34 |
|
| 35 |
Also see the documentation for 'assume' in verify.h. */ |
| 36 |
|
| 37 |
#ifdef NDEBUG |
| 38 |
# define affirm(E) assume (E) |
| 39 |
#else |
| 40 |
# define affirm(E) assert (E) |
| 41 |
#endif |
| 42 |
|
| 43 |
/* Check E's value at runtime, and report an error and abort if not. |
| 44 |
However, do nothing if NDEBUG is defined. |
| 45 |
|
| 46 |
Unlike standard 'assert', this macro compiles E even when NDEBUG |
| 47 |
is defined, so as to catch typos and avoid some GCC warnings. |
| 48 |
Unlike 'affirm', it is OK for E to use hard-to-optimize features, |
| 49 |
since E is not executed if NDEBUG is defined. */ |
| 50 |
|
| 51 |
#ifdef NDEBUG |
| 52 |
# define assure(E) ((void) (0 && (E))) |
| 53 |
#else |
| 54 |
# define assure(E) assert (E) |
| 55 |
#endif |
| 56 |
|
| 57 |
#endif |