🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

293 lines
11 KiB

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