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