SDL_assert.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. SDL - Simple DirectMedia Layer
  3. Copyright (C) 1997-2011 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Sam Lantinga
  16. [email protected]
  17. */
  18. #ifndef _SDL_assert_h
  19. #define _SDL_assert_h
  20. #include "SDL_config.h"
  21. #include "begin_code.h"
  22. /* Set up for C function definitions, even when using C++ */
  23. #ifdef __cplusplus
  24. /* *INDENT-OFF* */
  25. extern "C" {
  26. /* *INDENT-ON* */
  27. #endif
  28. #ifndef SDL_ASSERT_LEVEL
  29. #ifdef SDL_DEFAULT_ASSERT_LEVEL
  30. #define SDL_ASSERT_LEVEL SDL_DEFAULT_ASSERT_LEVEL
  31. #elif defined(_DEBUG) || defined(DEBUG) || \
  32. (defined(__GNUC__) && !defined(__OPTIMIZE__))
  33. #define SDL_ASSERT_LEVEL 2
  34. #else
  35. #define SDL_ASSERT_LEVEL 1
  36. #endif
  37. #endif /* SDL_ASSERT_LEVEL */
  38. /*
  39. These are macros and not first class functions so that the debugger breaks
  40. on the assertion line and not in some random guts of SDL, and so each
  41. assert can have unique static variables associated with it.
  42. */
  43. #if defined(_MSC_VER) && !defined(_WIN32_WCE)
  44. #include <intrin.h>
  45. #define SDL_TriggerBreakpoint() __debugbreak()
  46. #elif (defined(__GNUC__) && ((__i386__) || (__x86_64__)))
  47. #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "int $3\n\t" )
  48. #elif defined(HAVE_SIGNAL_H)
  49. #include <signal.h>
  50. #define SDL_TriggerBreakpoint() raise(SIGTRAP)
  51. #else
  52. /* How do we trigger breakpoints on this platform? */
  53. #define SDL_TriggerBreakpoint()
  54. #endif
  55. #if (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */
  56. # define SDL_FUNCTION __func__
  57. #elif ((__GNUC__ >= 2) || defined(_MSC_VER))
  58. # define SDL_FUNCTION __FUNCTION__
  59. #else
  60. # define SDL_FUNCTION "???"
  61. #endif
  62. #define SDL_FILE __FILE__
  63. #define SDL_LINE __LINE__
  64. /*
  65. sizeof (x) makes the compiler still parse the expression even without
  66. assertions enabled, so the code is always checked at compile time, but
  67. doesn't actually generate code for it, so there are no side effects or
  68. expensive checks at run time, just the constant size of what x WOULD be,
  69. which presumably gets optimized out as unused.
  70. This also solves the problem of...
  71. int somevalue = blah();
  72. SDL_assert(somevalue == 1);
  73. ...which would cause compiles to complain that somevalue is unused if we
  74. disable assertions.
  75. */
  76. #define SDL_disabled_assert(condition) \
  77. do { (void) sizeof ((condition)); } while (0)
  78. #if (SDL_ASSERT_LEVEL > 0)
  79. typedef enum
  80. {
  81. SDL_ASSERTION_RETRY, /**< Retry the assert immediately. */
  82. SDL_ASSERTION_BREAK, /**< Make the debugger trigger a breakpoint. */
  83. SDL_ASSERTION_ABORT, /**< Terminate the program. */
  84. SDL_ASSERTION_IGNORE, /**< Ignore the assert. */
  85. SDL_ASSERTION_ALWAYS_IGNORE, /**< Ignore the assert from now on. */
  86. } SDL_assert_state;
  87. typedef struct SDL_assert_data
  88. {
  89. int always_ignore;
  90. unsigned int trigger_count;
  91. const char *condition;
  92. const char *filename;
  93. int linenum;
  94. const char *function;
  95. const struct SDL_assert_data *next;
  96. } SDL_assert_data;
  97. /* Never call this directly. Use the SDL_assert* macros. */
  98. extern DECLSPEC SDL_assert_state SDLCALL SDL_ReportAssertion(SDL_assert_data *,
  99. const char *,
  100. const char *, int);
  101. /* the do {} while(0) avoids dangling else problems:
  102. if (x) SDL_assert(y); else blah();
  103. ... without the do/while, the "else" could attach to this macro's "if".
  104. We try to handle just the minimum we need here in a macro...the loop,
  105. the static vars, and break points. The heavy lifting is handled in
  106. SDL_ReportAssertion(), in SDL_assert.c.
  107. */
  108. #define SDL_enabled_assert(condition) \
  109. do { \
  110. while ( !(condition) ) { \
  111. static struct SDL_assert_data assert_data = { \
  112. 0, 0, #condition, 0, 0, 0, 0 \
  113. }; \
  114. const SDL_assert_state state = SDL_ReportAssertion(&assert_data, \
  115. SDL_FUNCTION, \
  116. SDL_FILE, \
  117. SDL_LINE); \
  118. if (state == SDL_ASSERTION_RETRY) { \
  119. continue; /* go again. */ \
  120. } else if (state == SDL_ASSERTION_BREAK) { \
  121. SDL_TriggerBreakpoint(); \
  122. } \
  123. break; /* not retrying. */ \
  124. } \
  125. } while (0)
  126. #endif /* enabled assertions support code */
  127. /* Enable various levels of assertions. */
  128. #if SDL_ASSERT_LEVEL == 0 /* assertions disabled */
  129. # define SDL_assert(condition) SDL_disabled_assert(condition)
  130. # define SDL_assert_release(condition) SDL_disabled_assert(condition)
  131. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  132. #elif SDL_ASSERT_LEVEL == 1 /* release settings. */
  133. # define SDL_assert(condition) SDL_disabled_assert(condition)
  134. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  135. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  136. #elif SDL_ASSERT_LEVEL == 2 /* normal settings. */
  137. # define SDL_assert(condition) SDL_enabled_assert(condition)
  138. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  139. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  140. #elif SDL_ASSERT_LEVEL == 3 /* paranoid settings. */
  141. # define SDL_assert(condition) SDL_enabled_assert(condition)
  142. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  143. # define SDL_assert_paranoid(condition) SDL_enabled_assert(condition)
  144. #else
  145. # error Unknown assertion level.
  146. #endif
  147. typedef SDL_assert_state (SDLCALL *SDL_AssertionHandler)(
  148. const SDL_assert_data *, void *userdata);
  149. /**
  150. * \brief Set an application-defined assertion handler.
  151. *
  152. * This allows an app to show its own assertion UI and/or force the
  153. * response to an assertion failure. If the app doesn't provide this, SDL
  154. * will try to do the right thing, popping up a system-specific GUI dialog,
  155. * and probably minimizing any fullscreen windows.
  156. *
  157. * This callback may fire from any thread, but it runs wrapped in a mutex, so
  158. * it will only fire from one thread at a time.
  159. *
  160. * Setting the callback to NULL restores SDL's original internal handler.
  161. *
  162. * This callback is NOT reset to SDL's internal handler upon SDL_Quit()!
  163. *
  164. * \return SDL_assert_state value of how to handle the assertion failure.
  165. *
  166. * \param handler Callback function, called when an assertion fails.
  167. * \param userdata A pointer passed to the callback as-is.
  168. */
  169. extern DECLSPEC void SDLCALL SDL_SetAssertionHandler(
  170. SDL_AssertionHandler handler,
  171. void *userdata);
  172. /**
  173. * \brief Get a list of all assertion failures.
  174. *
  175. * Get all assertions triggered since last call to SDL_ResetAssertionReport(),
  176. * or the start of the program.
  177. *
  178. * The proper way to examine this data looks something like this:
  179. *
  180. * <code>
  181. * const SDL_assert_data *item = SDL_GetAssertionReport();
  182. * while (item->condition) {
  183. * printf("'%s', %s (%s:%d), triggered %u times, always ignore: %s.\n",
  184. * item->condition, item->function, item->filename,
  185. * item->linenum, item->trigger_count,
  186. * item->always_ignore ? "yes" : "no");
  187. * item = item->next;
  188. * }
  189. * </code>
  190. *
  191. * \return List of all assertions. This never returns NULL,
  192. * even if there are no items.
  193. * \sa SDL_ResetAssertionReport
  194. */
  195. extern DECLSPEC const SDL_assert_data * SDLCALL SDL_GetAssertionReport(void);
  196. /**
  197. * \brief Reset the list of all assertion failures.
  198. *
  199. * Reset list of all assertions triggered.
  200. *
  201. * \sa SDL_GetAssertionReport
  202. */
  203. extern DECLSPEC void SDLCALL SDL_ResetAssertionReport(void);
  204. /* Ends C function definitions when using C++ */
  205. #ifdef __cplusplus
  206. /* *INDENT-OFF* */
  207. }
  208. /* *INDENT-ON* */
  209. #endif
  210. #include "close_code.h"
  211. #endif /* _SDL_assert_h */
  212. /* vi: set ts=4 sw=4 expandtab: */