You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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