| 1 | /* ==================================================================== |
|---|
| 2 | |
|---|
| 3 | File : arb_assert.h |
|---|
| 4 | Purpose : Global assert macro |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | Coded by Ralf Westram (coder@reallysoft.de) in August 2002 |
|---|
| 8 | Copyright Department of Microbiology (Technical University Munich) |
|---|
| 9 | |
|---|
| 10 | Visit our web site at: http://www.arb-home.de/ |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | ==================================================================== */ |
|---|
| 14 | |
|---|
| 15 | #ifndef ARB_ASSERT_H |
|---|
| 16 | #define ARB_ASSERT_H |
|---|
| 17 | |
|---|
| 18 | /* ------------------------------------------------------------ |
|---|
| 19 | * Define SIMPLE_ARB_ASSERT before including this header |
|---|
| 20 | * to avoid ARBDB dependency! |
|---|
| 21 | * |
|---|
| 22 | * ASSERT_CRASH if assert fails debugger stops at assert macro |
|---|
| 23 | * ASSERT_BACKTRACE_AND_CRASH like ASSERT_CRASH - with backtrace |
|---|
| 24 | * ASSERT_ERROR assert prints an error and ARB exits |
|---|
| 25 | * ASSERT_PRINT assert prints a message (anyway) and ARB continues |
|---|
| 26 | * ASSERT_NONE assertions inactive |
|---|
| 27 | * |
|---|
| 28 | * ------------------------------------------------------------ */ |
|---|
| 29 | |
|---|
| 30 | /* check correct definition of DEBUG/NDEBUG */ |
|---|
| 31 | #ifndef NDEBUG |
|---|
| 32 | # ifndef DEBUG |
|---|
| 33 | # error Neither DEBUG nor NDEBUG is defined! |
|---|
| 34 | # endif |
|---|
| 35 | #else |
|---|
| 36 | # ifdef DEBUG |
|---|
| 37 | # error Both DEBUG and NDEBUG are defined - only one should be! |
|---|
| 38 | # endif |
|---|
| 39 | #endif |
|---|
| 40 | |
|---|
| 41 | #ifdef arb_assert |
|---|
| 42 | #error arb_assert already defined |
|---|
| 43 | #endif |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | /* only use ONE of the following ASSERT_xxx defines : */ |
|---|
| 47 | |
|---|
| 48 | #if defined(DEBUG) && !defined(DEVEL_RELEASE) |
|---|
| 49 | |
|---|
| 50 | /* assert that raises SIGSEGV (recommended for DEBUG version!) */ |
|---|
| 51 | // # define ASSERT_CRASH |
|---|
| 52 | # define ASSERT_BACKTRACE_AND_CRASH |
|---|
| 53 | /* test if a bug has to do with assertion code */ |
|---|
| 54 | /* # define ASSERT_NONE */ |
|---|
| 55 | |
|---|
| 56 | #else |
|---|
| 57 | |
|---|
| 58 | /* no assert (recommended for final version!) */ |
|---|
| 59 | # define ASSERT_NONE |
|---|
| 60 | /* assert as error in final version (allows basic debugging of NDEBUG version) */ |
|---|
| 61 | /* # define ASSERT_ERROR */ |
|---|
| 62 | /* assert as print in final version (allows basic debugging of NDEBUG version) */ |
|---|
| 63 | /* # define ASSERT_PRINT */ |
|---|
| 64 | |
|---|
| 65 | #endif |
|---|
| 66 | |
|---|
| 67 | /* ------------------------------------------------------------------------------- |
|---|
| 68 | * Provoke a SIGSEGV (which will stop the debugger or terminated the application) |
|---|
| 69 | * Do backtrace manually here and uninstall SIGSEGV-handler |
|---|
| 70 | * (done because automatic backtrace on SIGSEGV lacks name of current function) |
|---|
| 71 | */ |
|---|
| 72 | |
|---|
| 73 | #define ARB_SIGSEGV(backtrace) do { \ |
|---|
| 74 | if (backtrace) GBK_dump_backtrace(NULL, "ARB_SIGSEGV"); \ |
|---|
| 75 | GBK_install_SIGSEGV_handler(GB_FALSE); \ |
|---|
| 76 | *(int *)0 = 0; \ |
|---|
| 77 | } while(0) |
|---|
| 78 | |
|---|
| 79 | /* ------------------------------------------------------------ */ |
|---|
| 80 | |
|---|
| 81 | /* use ASSERTION_USED for code needed for assertions */ |
|---|
| 82 | #define ASSERTION_USED |
|---|
| 83 | |
|---|
| 84 | /* ------------------------------------------------------------ */ |
|---|
| 85 | |
|---|
| 86 | #if defined(SIMPLE_ARB_ASSERT) |
|---|
| 87 | |
|---|
| 88 | #ifndef ASSERT_NONE |
|---|
| 89 | # define arb_assert(cond) \ |
|---|
| 90 | do { \ |
|---|
| 91 | if (!(cond)) { \ |
|---|
| 92 | fprintf(stderr, "Assertion '%s' failed in '%s' #%i\n", \ |
|---|
| 93 | #cond, __FILE__, __LINE__); \ |
|---|
| 94 | *(int *)0 = 0; \ |
|---|
| 95 | } \ |
|---|
| 96 | } while (0) |
|---|
| 97 | #endif |
|---|
| 98 | |
|---|
| 99 | /* ------------------------------------------------------------ */ |
|---|
| 100 | #else |
|---|
| 101 | |
|---|
| 102 | #ifdef ASSERT_CRASH |
|---|
| 103 | # define arb_assert(cond) \ |
|---|
| 104 | do { \ |
|---|
| 105 | if (!(cond)) ARB_SIGSEGV(0); \ |
|---|
| 106 | } while (0) |
|---|
| 107 | #endif |
|---|
| 108 | |
|---|
| 109 | #ifdef ASSERT_BACKTRACE_AND_CRASH |
|---|
| 110 | # define arb_assert(cond) \ |
|---|
| 111 | do { \ |
|---|
| 112 | if (!(cond)) { \ |
|---|
| 113 | fputs(GBK_assert_msg(#cond, __FILE__, __LINE__), stderr); \ |
|---|
| 114 | fflush(stderr); \ |
|---|
| 115 | ARB_SIGSEGV(1); \ |
|---|
| 116 | } \ |
|---|
| 117 | } while (0) |
|---|
| 118 | #endif |
|---|
| 119 | |
|---|
| 120 | #ifdef ASSERT_ERROR |
|---|
| 121 | # define arb_assert(cond) assert_or_exit(cond) |
|---|
| 122 | #endif |
|---|
| 123 | |
|---|
| 124 | #ifdef ASSERT_PRINT |
|---|
| 125 | # define arb_assert(cond) \ |
|---|
| 126 | do { \ |
|---|
| 127 | fprintf(stderr, "at %s #%i\n", __FILE__, __LINE__); \ |
|---|
| 128 | if (!(cond)) fprintf(stderr, "assertion '%s' failed!\n", #cond); \ |
|---|
| 129 | fflush(stderr); \ |
|---|
| 130 | } while (0) |
|---|
| 131 | #endif |
|---|
| 132 | |
|---|
| 133 | #endif // SIMPLE_ARB_ASSERT |
|---|
| 134 | |
|---|
| 135 | #ifdef ASSERT_NONE |
|---|
| 136 | # undef ASSERTION_USED |
|---|
| 137 | # define arb_assert(cond) |
|---|
| 138 | #endif |
|---|
| 139 | |
|---|
| 140 | #undef ASSERT_CRASH |
|---|
| 141 | #undef ASSERT_BACKTRACE_AND_CRASH |
|---|
| 142 | #undef ASSERT_ERROR |
|---|
| 143 | #undef ASSERT_PRINT |
|---|
| 144 | #undef ASSERT_NONE |
|---|
| 145 | |
|---|
| 146 | #ifndef arb_assert |
|---|
| 147 | # error arb_assert has not been defined -- check ASSERT_xxx definitions |
|---|
| 148 | #endif |
|---|
| 149 | |
|---|
| 150 | #if !defined(SIMPLE_ARB_ASSERT) |
|---|
| 151 | #define assert_or_exit(cond) \ |
|---|
| 152 | do { \ |
|---|
| 153 | if (!(cond)) { \ |
|---|
| 154 | GBK_terminate(GBK_assert_msg(#cond, __FILE__, __LINE__)); \ |
|---|
| 155 | } \ |
|---|
| 156 | } while(0) |
|---|
| 157 | #endif // SIMPLE_ARB_ASSERT |
|---|
| 158 | |
|---|
| 159 | /* ------------------------------------------------------------ */ |
|---|
| 160 | /* use the following macros for parameters etc. only appearing in one version */ |
|---|
| 161 | |
|---|
| 162 | #ifdef DEBUG |
|---|
| 163 | # define IF_DEBUG(x) x |
|---|
| 164 | # define IF_NDEBUG(x) |
|---|
| 165 | #else |
|---|
| 166 | # define IF_DEBUG(x) |
|---|
| 167 | # define IF_NDEBUG(x) x |
|---|
| 168 | #endif |
|---|
| 169 | |
|---|
| 170 | /* ------------------------------------------------------------ */ |
|---|
| 171 | |
|---|
| 172 | #ifdef DEVEL_RELEASE |
|---|
| 173 | #ifdef ASSERTION_USED |
|---|
| 174 | #error Assertions enabled in release |
|---|
| 175 | #endif |
|---|
| 176 | #endif |
|---|
| 177 | |
|---|
| 178 | /* ------------------------------------------------------------ */ |
|---|
| 179 | |
|---|
| 180 | #if !defined(SIMPLE_ARB_ASSERT) |
|---|
| 181 | #ifndef ADLOCAL_H |
|---|
| 182 | #ifndef ARBDB_BASE_H |
|---|
| 183 | #include <arbdb_base.h> |
|---|
| 184 | #endif |
|---|
| 185 | #endif |
|---|
| 186 | #endif // SIMPLE_ARB_ASSERT |
|---|
| 187 | |
|---|
| 188 | #else |
|---|
| 189 | #error arb_assert.h included twice |
|---|
| 190 | #endif /* ARB_ASSERT_H */ |
|---|