SDL_assert.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  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. extern "C" {
  25. #endif
  26. #ifndef SDL_ASSERT_LEVEL
  27. #ifdef SDL_DEFAULT_ASSERT_LEVEL
  28. #define SDL_ASSERT_LEVEL SDL_DEFAULT_ASSERT_LEVEL
  29. #elif defined(_DEBUG) || defined(DEBUG) || \
  30. (defined(__GNUC__) && !defined(__OPTIMIZE__))
  31. #define SDL_ASSERT_LEVEL 2
  32. #else
  33. #define SDL_ASSERT_LEVEL 1
  34. #endif
  35. #endif /* SDL_ASSERT_LEVEL */
  36. /*
  37. These are macros and not first class functions so that the debugger breaks
  38. on the assertion line and not in some random guts of SDL, and so each
  39. assert can have unique static variables associated with it.
  40. */
  41. #if defined(_MSC_VER)
  42. /* Don't include intrin.h here because it contains C++ code */
  43. extern void __cdecl __debugbreak(void);
  44. #define SDL_TriggerBreakpoint() __debugbreak()
  45. #elif (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)))
  46. #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "int $3\n\t" )
  47. #elif defined(HAVE_SIGNAL_H)
  48. #include <signal.h>
  49. #define SDL_TriggerBreakpoint() raise(SIGTRAP)
  50. #else
  51. /* How do we trigger breakpoints on this platform? */
  52. #define SDL_TriggerBreakpoint()
  53. #endif
  54. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */
  55. # define SDL_FUNCTION __func__
  56. #elif ((__GNUC__ >= 2) || defined(_MSC_VER))
  57. # define SDL_FUNCTION __FUNCTION__
  58. #else
  59. # define SDL_FUNCTION "???"
  60. #endif
  61. #define SDL_FILE __FILE__
  62. #define SDL_LINE __LINE__
  63. /*
  64. sizeof (x) makes the compiler still parse the expression even without
  65. assertions enabled, so the code is always checked at compile time, but
  66. doesn't actually generate code for it, so there are no side effects or
  67. expensive checks at run time, just the constant size of what x WOULD be,
  68. which presumably gets optimized out as unused.
  69. This also solves the problem of...
  70. int somevalue = blah();
  71. SDL_assert(somevalue == 1);
  72. ...which would cause compiles to complain that somevalue is unused if we
  73. disable assertions.
  74. */
  75. #ifdef _MSC_VER /* stupid /W4 warnings. */
  76. #define SDL_NULL_WHILE_LOOP_CONDITION (-1 == __LINE__)
  77. #else
  78. #define SDL_NULL_WHILE_LOOP_CONDITION (0)
  79. #endif
  80. #define SDL_disabled_assert(condition) \
  81. do { (void) sizeof ((condition)); } while (SDL_NULL_WHILE_LOOP_CONDITION)
  82. typedef enum
  83. {
  84. SDL_ASSERTION_RETRY, /**< Retry the assert immediately. */
  85. SDL_ASSERTION_BREAK, /**< Make the debugger trigger a breakpoint. */
  86. SDL_ASSERTION_ABORT, /**< Terminate the program. */
  87. SDL_ASSERTION_IGNORE, /**< Ignore the assert. */
  88. SDL_ASSERTION_ALWAYS_IGNORE /**< Ignore the assert from now on. */
  89. } SDL_assert_state;
  90. typedef struct SDL_assert_data
  91. {
  92. int always_ignore;
  93. unsigned int trigger_count;
  94. const char *condition;
  95. const char *filename;
  96. int linenum;
  97. const char *function;
  98. const struct SDL_assert_data *next;
  99. } SDL_assert_data;
  100. #if (SDL_ASSERT_LEVEL > 0)
  101. /* Never call this directly. Use the SDL_assert* macros. */
  102. extern DECLSPEC SDL_assert_state SDLCALL SDL_ReportAssertion(SDL_assert_data *,
  103. const char *,
  104. const char *, int)
  105. #if defined(__clang__)
  106. #if __has_feature(attribute_analyzer_noreturn)
  107. /* this tells Clang's static analysis that we're a custom assert function,
  108. and that the analyzer should assume the condition was always true past this
  109. SDL_assert test. */
  110. __attribute__((analyzer_noreturn))
  111. #endif
  112. #endif
  113. ;
  114. /* the do {} while(0) avoids dangling else problems:
  115. if (x) SDL_assert(y); else blah();
  116. ... without the do/while, the "else" could attach to this macro's "if".
  117. We try to handle just the minimum we need here in a macro...the loop,
  118. the static vars, and break points. The heavy lifting is handled in
  119. SDL_ReportAssertion(), in SDL_assert.c.
  120. */
  121. #define SDL_enabled_assert(condition) \
  122. do { \
  123. while ( !(condition) ) { \
  124. static struct SDL_assert_data assert_data = { \
  125. 0, 0, #condition, 0, 0, 0, 0 \
  126. }; \
  127. const SDL_assert_state state = SDL_ReportAssertion(&assert_data, \
  128. SDL_FUNCTION, \
  129. SDL_FILE, \
  130. SDL_LINE); \
  131. if (state == SDL_ASSERTION_RETRY) { \
  132. continue; /* go again. */ \
  133. } else if (state == SDL_ASSERTION_BREAK) { \
  134. SDL_TriggerBreakpoint(); \
  135. } \
  136. break; /* not retrying. */ \
  137. } \
  138. } while (SDL_NULL_WHILE_LOOP_CONDITION)
  139. #endif /* enabled assertions support code */
  140. /* Enable various levels of assertions. */
  141. #if SDL_ASSERT_LEVEL == 0 /* assertions disabled */
  142. # define SDL_assert(condition) SDL_disabled_assert(condition)
  143. # define SDL_assert_release(condition) SDL_disabled_assert(condition)
  144. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  145. #elif SDL_ASSERT_LEVEL == 1 /* release settings. */
  146. # define SDL_assert(condition) SDL_disabled_assert(condition)
  147. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  148. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  149. #elif SDL_ASSERT_LEVEL == 2 /* normal settings. */
  150. # define SDL_assert(condition) SDL_enabled_assert(condition)
  151. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  152. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  153. #elif SDL_ASSERT_LEVEL == 3 /* paranoid settings. */
  154. # define SDL_assert(condition) SDL_enabled_assert(condition)
  155. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  156. # define SDL_assert_paranoid(condition) SDL_enabled_assert(condition)
  157. #else
  158. # error Unknown assertion level.
  159. #endif
  160. /* this assertion is never disabled at any level. */
  161. #define SDL_assert_always(condition) SDL_enabled_assert(condition)
  162. typedef SDL_assert_state (SDLCALL *SDL_AssertionHandler)(
  163. const SDL_assert_data* data, void* userdata);
  164. /**
  165. * \brief Set an application-defined assertion handler.
  166. *
  167. * This allows an app to show its own assertion UI and/or force the
  168. * response to an assertion failure. If the app doesn't provide this, SDL
  169. * will try to do the right thing, popping up a system-specific GUI dialog,
  170. * and probably minimizing any fullscreen windows.
  171. *
  172. * This callback may fire from any thread, but it runs wrapped in a mutex, so
  173. * it will only fire from one thread at a time.
  174. *
  175. * Setting the callback to NULL restores SDL's original internal handler.
  176. *
  177. * This callback is NOT reset to SDL's internal handler upon SDL_Quit()!
  178. *
  179. * \return SDL_assert_state value of how to handle the assertion failure.
  180. *
  181. * \param handler Callback function, called when an assertion fails.
  182. * \param userdata A pointer passed to the callback as-is.
  183. */
  184. extern DECLSPEC void SDLCALL SDL_SetAssertionHandler(
  185. SDL_AssertionHandler handler,
  186. void *userdata);
  187. /**
  188. * \brief Get the default assertion handler.
  189. *
  190. * This returns the function pointer that is called by default when an
  191. * assertion is triggered. This is an internal function provided by SDL,
  192. * that is used for assertions when SDL_SetAssertionHandler() hasn't been
  193. * used to provide a different function.
  194. *
  195. * \return The default SDL_AssertionHandler that is called when an assert triggers.
  196. */
  197. extern DECLSPEC SDL_AssertionHandler SDLCALL SDL_GetDefaultAssertionHandler(void);
  198. /**
  199. * \brief Get the current assertion handler.
  200. *
  201. * This returns the function pointer that is called when an assertion is
  202. * triggered. This is either the value last passed to
  203. * SDL_SetAssertionHandler(), or if no application-specified function is
  204. * set, is equivalent to calling SDL_GetDefaultAssertionHandler().
  205. *
  206. * \param puserdata Pointer to a void*, which will store the "userdata"
  207. * pointer that was passed to SDL_SetAssertionHandler().
  208. * This value will always be NULL for the default handler.
  209. * If you don't care about this data, it is safe to pass
  210. * a NULL pointer to this function to ignore it.
  211. * \return The SDL_AssertionHandler that is called when an assert triggers.
  212. */
  213. extern DECLSPEC SDL_AssertionHandler SDLCALL SDL_GetAssertionHandler(void **puserdata);
  214. /**
  215. * \brief Get a list of all assertion failures.
  216. *
  217. * Get all assertions triggered since last call to SDL_ResetAssertionReport(),
  218. * or the start of the program.
  219. *
  220. * The proper way to examine this data looks something like this:
  221. *
  222. * <code>
  223. * const SDL_assert_data *item = SDL_GetAssertionReport();
  224. * while (item) {
  225. * printf("'%s', %s (%s:%d), triggered %u times, always ignore: %s.\n",
  226. * item->condition, item->function, item->filename,
  227. * item->linenum, item->trigger_count,
  228. * item->always_ignore ? "yes" : "no");
  229. * item = item->next;
  230. * }
  231. * </code>
  232. *
  233. * \return List of all assertions.
  234. * \sa SDL_ResetAssertionReport
  235. */
  236. extern DECLSPEC const SDL_assert_data * SDLCALL SDL_GetAssertionReport(void);
  237. /**
  238. * \brief Reset the list of all assertion failures.
  239. *
  240. * Reset list of all assertions triggered.
  241. *
  242. * \sa SDL_GetAssertionReport
  243. */
  244. extern DECLSPEC void SDLCALL SDL_ResetAssertionReport(void);
  245. /* Ends C function definitions when using C++ */
  246. #ifdef __cplusplus
  247. }
  248. #endif
  249. #include "close_code.h"
  250. #endif /* _SDL_assert_h */
  251. /* vi: set ts=4 sw=4 expandtab: */