SDL_assert.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 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(__NACL__) && 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. /* "while (0,0)" fools Microsoft's compiler's /W4 warning level into thinking
  76. this condition isn't constant. And looks like an owl's face! */
  77. #ifdef _MSC_VER /* stupid /W4 warnings. */
  78. #define SDL_NULL_WHILE_LOOP_CONDITION (0,0)
  79. #else
  80. #define SDL_NULL_WHILE_LOOP_CONDITION (0)
  81. #endif
  82. #define SDL_disabled_assert(condition) \
  83. do { (void) sizeof ((condition)); } while (SDL_NULL_WHILE_LOOP_CONDITION)
  84. typedef enum
  85. {
  86. SDL_ASSERTION_RETRY, /**< Retry the assert immediately. */
  87. SDL_ASSERTION_BREAK, /**< Make the debugger trigger a breakpoint. */
  88. SDL_ASSERTION_ABORT, /**< Terminate the program. */
  89. SDL_ASSERTION_IGNORE, /**< Ignore the assert. */
  90. SDL_ASSERTION_ALWAYS_IGNORE /**< Ignore the assert from now on. */
  91. } SDL_AssertState;
  92. typedef struct SDL_AssertData
  93. {
  94. int always_ignore;
  95. unsigned int trigger_count;
  96. const char *condition;
  97. const char *filename;
  98. int linenum;
  99. const char *function;
  100. const struct SDL_AssertData *next;
  101. } SDL_AssertData;
  102. #if (SDL_ASSERT_LEVEL > 0)
  103. /* Never call this directly. Use the SDL_assert* macros. */
  104. extern DECLSPEC SDL_AssertState SDLCALL SDL_ReportAssertion(SDL_AssertData *,
  105. const char *,
  106. const char *, int)
  107. #if defined(__clang__)
  108. #if __has_feature(attribute_analyzer_noreturn)
  109. /* this tells Clang's static analysis that we're a custom assert function,
  110. and that the analyzer should assume the condition was always true past this
  111. SDL_assert test. */
  112. __attribute__((analyzer_noreturn))
  113. #endif
  114. #endif
  115. ;
  116. /* the do {} while(0) avoids dangling else problems:
  117. if (x) SDL_assert(y); else blah();
  118. ... without the do/while, the "else" could attach to this macro's "if".
  119. We try to handle just the minimum we need here in a macro...the loop,
  120. the static vars, and break points. The heavy lifting is handled in
  121. SDL_ReportAssertion(), in SDL_assert.c.
  122. */
  123. #define SDL_enabled_assert(condition) \
  124. do { \
  125. while ( !(condition) ) { \
  126. static struct SDL_AssertData sdl_assert_data = { \
  127. 0, 0, #condition, 0, 0, 0, 0 \
  128. }; \
  129. const SDL_AssertState sdl_assert_state = SDL_ReportAssertion(&sdl_assert_data, SDL_FUNCTION, SDL_FILE, SDL_LINE); \
  130. if (sdl_assert_state == SDL_ASSERTION_RETRY) { \
  131. continue; /* go again. */ \
  132. } else if (sdl_assert_state == SDL_ASSERTION_BREAK) { \
  133. SDL_TriggerBreakpoint(); \
  134. } \
  135. break; /* not retrying. */ \
  136. } \
  137. } while (SDL_NULL_WHILE_LOOP_CONDITION)
  138. #endif /* enabled assertions support code */
  139. /* Enable various levels of assertions. */
  140. #if SDL_ASSERT_LEVEL == 0 /* assertions disabled */
  141. # define SDL_assert(condition) SDL_disabled_assert(condition)
  142. # define SDL_assert_release(condition) SDL_disabled_assert(condition)
  143. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  144. #elif SDL_ASSERT_LEVEL == 1 /* release settings. */
  145. # define SDL_assert(condition) SDL_disabled_assert(condition)
  146. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  147. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  148. #elif SDL_ASSERT_LEVEL == 2 /* normal settings. */
  149. # define SDL_assert(condition) SDL_enabled_assert(condition)
  150. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  151. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  152. #elif SDL_ASSERT_LEVEL == 3 /* paranoid settings. */
  153. # define SDL_assert(condition) SDL_enabled_assert(condition)
  154. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  155. # define SDL_assert_paranoid(condition) SDL_enabled_assert(condition)
  156. #else
  157. # error Unknown assertion level.
  158. #endif
  159. /* this assertion is never disabled at any level. */
  160. #define SDL_assert_always(condition) SDL_enabled_assert(condition)
  161. typedef SDL_AssertState (SDLCALL *SDL_AssertionHandler)(
  162. const SDL_AssertData* data, void* userdata);
  163. /**
  164. * \brief Set an application-defined assertion handler.
  165. *
  166. * This allows an app to show its own assertion UI and/or force the
  167. * response to an assertion failure. If the app doesn't provide this, SDL
  168. * will try to do the right thing, popping up a system-specific GUI dialog,
  169. * and probably minimizing any fullscreen windows.
  170. *
  171. * This callback may fire from any thread, but it runs wrapped in a mutex, so
  172. * it will only fire from one thread at a time.
  173. *
  174. * Setting the callback to NULL restores SDL's original internal handler.
  175. *
  176. * This callback is NOT reset to SDL's internal handler upon SDL_Quit()!
  177. *
  178. * \return SDL_AssertState value of how to handle the assertion failure.
  179. *
  180. * \param handler Callback function, called when an assertion fails.
  181. * \param userdata A pointer passed to the callback as-is.
  182. */
  183. extern DECLSPEC void SDLCALL SDL_SetAssertionHandler(
  184. SDL_AssertionHandler handler,
  185. void *userdata);
  186. /**
  187. * \brief Get the default assertion handler.
  188. *
  189. * This returns the function pointer that is called by default when an
  190. * assertion is triggered. This is an internal function provided by SDL,
  191. * that is used for assertions when SDL_SetAssertionHandler() hasn't been
  192. * used to provide a different function.
  193. *
  194. * \return The default SDL_AssertionHandler that is called when an assert triggers.
  195. */
  196. extern DECLSPEC SDL_AssertionHandler SDLCALL SDL_GetDefaultAssertionHandler(void);
  197. /**
  198. * \brief Get the current assertion handler.
  199. *
  200. * This returns the function pointer that is called when an assertion is
  201. * triggered. This is either the value last passed to
  202. * SDL_SetAssertionHandler(), or if no application-specified function is
  203. * set, is equivalent to calling SDL_GetDefaultAssertionHandler().
  204. *
  205. * \param puserdata Pointer to a void*, which will store the "userdata"
  206. * pointer that was passed to SDL_SetAssertionHandler().
  207. * This value will always be NULL for the default handler.
  208. * If you don't care about this data, it is safe to pass
  209. * a NULL pointer to this function to ignore it.
  210. * \return The SDL_AssertionHandler that is called when an assert triggers.
  211. */
  212. extern DECLSPEC SDL_AssertionHandler SDLCALL SDL_GetAssertionHandler(void **puserdata);
  213. /**
  214. * \brief Get a list of all assertion failures.
  215. *
  216. * Get all assertions triggered since last call to SDL_ResetAssertionReport(),
  217. * or the start of the program.
  218. *
  219. * The proper way to examine this data looks something like this:
  220. *
  221. * <code>
  222. * const SDL_AssertData *item = SDL_GetAssertionReport();
  223. * while (item) {
  224. * printf("'%s', %s (%s:%d), triggered %u times, always ignore: %s.\n",
  225. * item->condition, item->function, item->filename,
  226. * item->linenum, item->trigger_count,
  227. * item->always_ignore ? "yes" : "no");
  228. * item = item->next;
  229. * }
  230. * </code>
  231. *
  232. * \return List of all assertions.
  233. * \sa SDL_ResetAssertionReport
  234. */
  235. extern DECLSPEC const SDL_AssertData * SDLCALL SDL_GetAssertionReport(void);
  236. /**
  237. * \brief Reset the list of all assertion failures.
  238. *
  239. * Reset list of all assertions triggered.
  240. *
  241. * \sa SDL_GetAssertionReport
  242. */
  243. extern DECLSPEC void SDLCALL SDL_ResetAssertionReport(void);
  244. /* these had wrong naming conventions until 2.0.4. Please update your app! */
  245. #define SDL_assert_state SDL_AssertState
  246. #define SDL_assert_data SDL_AssertData
  247. /* Ends C function definitions when using C++ */
  248. #ifdef __cplusplus
  249. }
  250. #endif
  251. #include "close_code.h"
  252. #endif /* _SDL_assert_h */
  253. /* vi: set ts=4 sw=4 expandtab: */