| 1 |
#ifndef __CRITCL_CUTIL_ASSERT_H |
| 2 |
#define __CRITCL_CUTIL_ASSERT_H 1 |
| 3 |
|
| 4 |
/* |
| 5 |
* Copyright (c) 2017-2020 Andreas Kupries <[email protected]> |
| 6 |
* = = == === ===== ======== ============= ===================== |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <tcl.h> |
| 10 |
|
| 11 |
/* |
| 12 |
* Macros for assertions, controlled via CRITCL_ASSERT. |
| 13 |
* Especially a helper to check array bounds, and counted |
| 14 |
* abort. |
| 15 |
*/ |
| 16 |
|
| 17 |
#ifdef CRITCL_ASSERT |
| 18 |
|
| 19 |
#define CRITCL_CUTIL_XSTR(x) #x |
| 20 |
#define CRITCL_CUTIL_STR(x) CRITCL_CUTIL_XSTR(x) |
| 21 |
#define CRITCL_CUTIL_RANGEOK(i,n) ((0 <= (i)) && ((i) < (n))) |
| 22 |
|
| 23 |
#define ASSERT(x,msg) if (!(x)) { \ |
| 24 |
Tcl_Panic (msg " (" #x "), in file %s @line %d", \ |
| 25 |
__FILE__, __LINE__); \ |
| 26 |
} |
| 27 |
|
| 28 |
#define ASSERT_VA(x,msg,format,...) if (!(x)) { \ |
| 29 |
Tcl_Panic (msg " (" #x "), in file %s @line %d, " format, \ |
| 30 |
__FILE__, __LINE__, __VA_ARGS__); \ |
| 31 |
} |
| 32 |
|
| 33 |
#define ASSERT_BOUNDS(i,n) \ |
| 34 |
ASSERT_VA (CRITCL_CUTIL_RANGEOK(i,n), \ |
| 35 |
"array index out of bounds", \ |
| 36 |
CRITCL_CUTIL_STR(i) \ |
| 37 |
" = (%d) >= (%d) = " \ |
| 38 |
CRITCL_CUTIL_STR(n), \ |
| 39 |
i, n) |
| 40 |
|
| 41 |
#define STOPAFTER(x) { \ |
| 42 |
static int count = (x); \ |
| 43 |
count --; \ |
| 44 |
if (!count) { Tcl_Panic ("stop"); } \ |
| 45 |
} |
| 46 |
|
| 47 |
#else /* ! CRITCL_ASSERT */ |
| 48 |
|
| 49 |
#define ASSERT(x,msg) |
| 50 |
#define ASSERT_VA(x,msg,format,...) |
| 51 |
#define ASSERT_BOUNDS(i,n) |
| 52 |
#define STOPAFTER(x) |
| 53 |
|
| 54 |
#endif |
| 55 |
|
| 56 |
/* |
| 57 |
* = = == === ===== ======== ============= ===================== |
| 58 |
*/ |
| 59 |
|
| 60 |
#endif /* __CRITCL_CUTIL_H */ |
| 61 |
|
| 62 |
/* |
| 63 |
* Local Variables: |
| 64 |
* mode: c |
| 65 |
* c-basic-offset: 4 |
| 66 |
* fill-column: 78 |
| 67 |
* End: |
| 68 |
*/ |