SDL_assert.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
  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. /**
  19. * # CategoryAssert
  20. *
  21. * A helpful assertion macro!
  22. *
  23. * SDL assertions operate like your usual `assert` macro, but with some added
  24. * features:
  25. *
  26. * - It uses a trick with the `sizeof` operator, so disabled assertions
  27. * vaporize out of the compiled code, but variables only referenced in the
  28. * assertion won't trigger compiler warnings about being unused.
  29. * - It is safe to use with a dangling-else: `if (x) SDL_assert(y); else
  30. * do_something();`
  31. * - It works the same everywhere, instead of counting on various platforms'
  32. * compiler and C runtime to behave.
  33. * - It provides multiple levels of assertion (SDL_assert, SDL_assert_release,
  34. * SDL_assert_paranoid) instead of a single all-or-nothing option.
  35. * - It offers a variety of responses when an assertion fails (retry, trigger
  36. * the debugger, abort the program, ignore the failure once, ignore it for
  37. * the rest of the program's run).
  38. * - It tries to show the user a dialog by default, if possible, but the app
  39. * can provide a callback to handle assertion failures however they like.
  40. * - It lets failed assertions be retried. Perhaps you had a network failure
  41. * and just want to retry the test after plugging your network cable back
  42. * in? You can.
  43. * - It lets the user ignore an assertion failure, if there's a harmless
  44. * problem that one can continue past.
  45. * - It lets the user mark an assertion as ignored for the rest of the
  46. * program's run; if there's a harmless problem that keeps popping up.
  47. * - It provides statistics and data on all failed assertions to the app.
  48. * - It allows the default assertion handler to be controlled with environment
  49. * variables, in case an automated script needs to control it.
  50. * - It can be used as an aid to Clang's static analysis; it will treat SDL
  51. * assertions as universally true (under the assumption that you are serious
  52. * about the asserted claims and that your debug builds will detect when
  53. * these claims were wrong). This can help the analyzer avoid false
  54. * positives.
  55. *
  56. * To use it: compile a debug build and just sprinkle around tests to check
  57. * your code!
  58. */
  59. #ifndef SDL_assert_h_
  60. #define SDL_assert_h_
  61. #include <SDL3/SDL_stdinc.h>
  62. #include <SDL3/SDL_begin_code.h>
  63. /* Set up for C function definitions, even when using C++ */
  64. #ifdef __cplusplus
  65. extern "C" {
  66. #endif
  67. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  68. /**
  69. * The level of assertion aggressiveness.
  70. *
  71. * This value changes depending on compiler options and other preprocessor
  72. * defines.
  73. *
  74. * It is currently one of the following values, but future SDL releases might
  75. * add more:
  76. *
  77. * - 0: All SDL assertion macros are disabled.
  78. * - 1: Release settings: SDL_assert disabled, SDL_assert_release enabled.
  79. * - 2: Debug settings: SDL_assert and SDL_assert_release enabled.
  80. * - 3: Paranoid settings: All SDL assertion macros enabled, including
  81. * SDL_assert_paranoid.
  82. *
  83. * \since This macro is available since SDL 3.2.0.
  84. */
  85. #define SDL_ASSERT_LEVEL SomeNumberBasedOnVariousFactors
  86. #elif !defined(SDL_ASSERT_LEVEL)
  87. #ifdef SDL_DEFAULT_ASSERT_LEVEL
  88. #define SDL_ASSERT_LEVEL SDL_DEFAULT_ASSERT_LEVEL
  89. #elif defined(_DEBUG) || defined(DEBUG) || \
  90. (defined(__GNUC__) && !defined(__OPTIMIZE__))
  91. #define SDL_ASSERT_LEVEL 2
  92. #else
  93. #define SDL_ASSERT_LEVEL 1
  94. #endif
  95. #endif
  96. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  97. /**
  98. * Attempt to tell an attached debugger to pause.
  99. *
  100. * This allows an app to programmatically halt ("break") the debugger as if it
  101. * had hit a breakpoint, allowing the developer to examine program state, etc.
  102. *
  103. * This is a macro--not a function--so that the debugger breaks on the source
  104. * code line that used SDL_TriggerBreakpoint and not in some random guts of
  105. * SDL. SDL_assert uses this macro for the same reason.
  106. *
  107. * If the program is not running under a debugger, SDL_TriggerBreakpoint will
  108. * likely terminate the app, possibly without warning. If the current platform
  109. * isn't supported, this macro is left undefined.
  110. *
  111. * \threadsafety It is safe to call this macro from any thread.
  112. *
  113. * \since This macro is available since SDL 3.2.0.
  114. */
  115. #define SDL_TriggerBreakpoint() TriggerABreakpointInAPlatformSpecificManner
  116. #elif defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER >= 1310)
  117. /* Don't include intrin.h here because it contains C++ code */
  118. extern void __cdecl __debugbreak(void);
  119. #define SDL_TriggerBreakpoint() __debugbreak()
  120. #elif defined(_MSC_VER) && defined(_M_IX86)
  121. #define SDL_TriggerBreakpoint() { _asm { int 0x03 } }
  122. #elif SDL_HAS_BUILTIN(__builtin_debugtrap)
  123. #define SDL_TriggerBreakpoint() __builtin_debugtrap()
  124. #elif SDL_HAS_BUILTIN(__builtin_trap)
  125. #define SDL_TriggerBreakpoint() __builtin_trap()
  126. #elif (defined(__GNUC__) || defined(__clang__) || defined(__TINYC__) || defined(__slimcc__)) && (defined(__i386__) || defined(__x86_64__))
  127. #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "int $3\n\t" )
  128. #elif (defined(__GNUC__) || defined(__clang__)) && defined(__riscv)
  129. #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "ebreak\n\t" )
  130. #elif ( defined(SDL_PLATFORM_APPLE) && (defined(__arm64__) || defined(__aarch64__)) ) /* this might work on other ARM targets, but this is a known quantity... */
  131. #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "brk #22\n\t" )
  132. #elif defined(SDL_PLATFORM_APPLE) && defined(__arm__)
  133. #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "bkpt #22\n\t" )
  134. #elif defined(_WIN32) && ((defined(__GNUC__) || defined(__clang__)) && (defined(__arm64__) || defined(__aarch64__)) )
  135. #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "brk #0xF000\n\t" )
  136. #elif defined(__GNUC__) || defined(__clang__)
  137. #define SDL_TriggerBreakpoint() __builtin_trap() /* older gcc may not support SDL_HAS_BUILTIN(__builtin_trap) above */
  138. #elif defined(__386__) && defined(__WATCOMC__)
  139. #define SDL_TriggerBreakpoint() { _asm { int 0x03 } }
  140. #elif defined(HAVE_SIGNAL_H) && !defined(__WATCOMC__)
  141. #include <signal.h>
  142. #define SDL_TriggerBreakpoint() raise(SIGTRAP)
  143. #else
  144. /* SDL_TriggerBreakpoint is intentionally left undefined on unknown platforms. */
  145. #endif
  146. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  147. /**
  148. * A constant that contains the current function being compiled.
  149. *
  150. * If SDL can't figure how the compiler reports this, it will use "???".
  151. *
  152. * \since This macro is available since SDL 3.2.0.
  153. */
  154. #define SDL_FUNCTION __FUNCTION__
  155. #elif !defined(SDL_FUNCTION)
  156. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */
  157. # define SDL_FUNCTION __func__
  158. #elif ((defined(__GNUC__) && (__GNUC__ >= 2)) || defined(_MSC_VER) || defined (__WATCOMC__))
  159. # define SDL_FUNCTION __FUNCTION__
  160. #else
  161. # define SDL_FUNCTION "???"
  162. #endif
  163. #endif
  164. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  165. /**
  166. * A macro that reports the current file being compiled.
  167. *
  168. * This macro is only defined if it isn't already defined, so to override it
  169. * (perhaps with something that doesn't provide path information at all, so
  170. * build machine information doesn't leak into public binaries), apps can
  171. * define this macro before including SDL.h or SDL_assert.h.
  172. *
  173. * \since This macro is available since SDL 3.2.0.
  174. */
  175. #define SDL_FILE __FILE_NAME__
  176. #elif !defined(SDL_FILE)
  177. #ifdef __FILE_NAME__
  178. #define SDL_FILE __FILE_NAME__
  179. #else
  180. #define SDL_FILE __FILE__
  181. #endif
  182. #endif
  183. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  184. /**
  185. * A macro that reports the current file being compiled, for use in
  186. * assertions.
  187. *
  188. * This macro is only defined if it isn't already defined, so to override it
  189. * (perhaps with something that doesn't provide path information at all, so
  190. * build machine information doesn't leak into public binaries), apps can
  191. * define this macro before including SDL_assert.h. For example, defining this
  192. * to `""` will make sure no source path information is included in asserts.
  193. *
  194. * \since This macro is available since SDL 3.4.0.
  195. */
  196. #define SDL_ASSERT_FILE SDL_FILE
  197. #elif !defined(SDL_ASSERT_FILE)
  198. #define SDL_ASSERT_FILE SDL_FILE
  199. #endif
  200. /**
  201. * A macro that reports the current line number of the file being compiled.
  202. *
  203. * \since This macro is available since SDL 3.2.0.
  204. */
  205. #define SDL_LINE __LINE__
  206. /*
  207. sizeof (x) makes the compiler still parse the expression even without
  208. assertions enabled, so the code is always checked at compile time, but
  209. doesn't actually generate code for it, so there are no side effects or
  210. expensive checks at run time, just the constant size of what x WOULD be,
  211. which presumably gets optimized out as unused.
  212. This also solves the problem of...
  213. int somevalue = blah();
  214. SDL_assert(somevalue == 1);
  215. ...which would cause compiles to complain that somevalue is unused if we
  216. disable assertions.
  217. */
  218. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  219. /**
  220. * A macro for wrapping code in `do {} while (0);` without compiler warnings.
  221. *
  222. * Visual Studio with really aggressive warnings enabled needs this to avoid
  223. * compiler complaints.
  224. *
  225. * the `do {} while (0);` trick is useful for wrapping code in a macro that
  226. * may or may not be a single statement, to avoid various C language
  227. * accidents.
  228. *
  229. * To use:
  230. *
  231. * ```c
  232. * do { SomethingOnce(); } while (SDL_NULL_WHILE_LOOP_CONDITION (0));
  233. * ```
  234. *
  235. * \since This macro is available since SDL 3.2.0.
  236. */
  237. #define SDL_NULL_WHILE_LOOP_CONDITION (0)
  238. #elif defined(_MSC_VER) /* Avoid /W4 warnings. */
  239. /* "while (0,0)" fools Microsoft's compiler's /W4 warning level into thinking
  240. this condition isn't constant. And looks like an owl's face! */
  241. #define SDL_NULL_WHILE_LOOP_CONDITION (0,0)
  242. #else
  243. #define SDL_NULL_WHILE_LOOP_CONDITION (0)
  244. #endif
  245. /**
  246. * The macro used when an assertion is disabled.
  247. *
  248. * This isn't for direct use by apps, but this is the code that is inserted
  249. * when an SDL_assert is disabled (perhaps in a release build).
  250. *
  251. * The code does nothing, but wraps `condition` in a sizeof operator, which
  252. * generates no code and has no side effects, but avoid compiler warnings
  253. * about unused variables.
  254. *
  255. * \param condition the condition to assert (but not actually run here).
  256. *
  257. * \since This macro is available since SDL 3.2.0.
  258. */
  259. #define SDL_disabled_assert(condition) \
  260. do { (void) sizeof ((condition)); } while (SDL_NULL_WHILE_LOOP_CONDITION)
  261. /**
  262. * Possible outcomes from a triggered assertion.
  263. *
  264. * When an enabled assertion triggers, it may call the assertion handler
  265. * (possibly one provided by the app via SDL_SetAssertionHandler), which will
  266. * return one of these values, possibly after asking the user.
  267. *
  268. * Then SDL will respond based on this outcome (loop around to retry the
  269. * condition, try to break in a debugger, kill the program, or ignore the
  270. * problem).
  271. *
  272. * \since This enum is available since SDL 3.2.0.
  273. */
  274. typedef enum SDL_AssertState
  275. {
  276. SDL_ASSERTION_RETRY, /**< Retry the assert immediately. */
  277. SDL_ASSERTION_BREAK, /**< Make the debugger trigger a breakpoint. */
  278. SDL_ASSERTION_ABORT, /**< Terminate the program. */
  279. SDL_ASSERTION_IGNORE, /**< Ignore the assert. */
  280. SDL_ASSERTION_ALWAYS_IGNORE /**< Ignore the assert from now on. */
  281. } SDL_AssertState;
  282. /**
  283. * Information about an assertion failure.
  284. *
  285. * This structure is filled in with information about a triggered assertion,
  286. * used by the assertion handler, then added to the assertion report. This is
  287. * returned as a linked list from SDL_GetAssertionReport().
  288. *
  289. * \since This struct is available since SDL 3.2.0.
  290. */
  291. typedef struct SDL_AssertData
  292. {
  293. bool always_ignore; /**< true if app should always continue when assertion is triggered. */
  294. unsigned int trigger_count; /**< Number of times this assertion has been triggered. */
  295. const char *condition; /**< A string of this assert's test code. */
  296. const char *filename; /**< The source file where this assert lives. */
  297. int linenum; /**< The line in `filename` where this assert lives. */
  298. const char *function; /**< The name of the function where this assert lives. */
  299. const struct SDL_AssertData *next; /**< next item in the linked list. */
  300. } SDL_AssertData;
  301. /**
  302. * Never call this directly.
  303. *
  304. * Use the SDL_assert macros instead.
  305. *
  306. * \param data assert data structure.
  307. * \param func function name.
  308. * \param file file name.
  309. * \param line line number.
  310. * \returns assert state.
  311. *
  312. * \threadsafety It is safe to call this function from any thread.
  313. *
  314. * \since This function is available since SDL 3.2.0.
  315. */
  316. extern SDL_DECLSPEC SDL_AssertState SDLCALL SDL_ReportAssertion(SDL_AssertData *data,
  317. const char *func,
  318. const char *file, int line) SDL_ANALYZER_NORETURN;
  319. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  320. /**
  321. * The macro used when an assertion triggers a breakpoint.
  322. *
  323. * This isn't for direct use by apps; use SDL_assert or SDL_TriggerBreakpoint
  324. * instead.
  325. *
  326. * \since This macro is available since SDL 3.2.0.
  327. */
  328. #define SDL_AssertBreakpoint() SDL_TriggerBreakpoint()
  329. #elif !defined(SDL_AssertBreakpoint)
  330. # if defined(ANDROID) && defined(assert)
  331. /* Define this as empty in case assert() is defined as SDL_assert */
  332. # define SDL_AssertBreakpoint()
  333. # else
  334. # define SDL_AssertBreakpoint() SDL_TriggerBreakpoint()
  335. # endif
  336. #endif /* !SDL_AssertBreakpoint */
  337. /**
  338. * The macro used when an assertion is enabled.
  339. *
  340. * This isn't for direct use by apps, but this is the code that is inserted
  341. * when an SDL_assert is enabled.
  342. *
  343. * The `do {} while(0)` avoids dangling else problems:
  344. *
  345. * ```c
  346. * if (x) SDL_assert(y); else blah();
  347. * ```
  348. *
  349. * ... without the do/while, the "else" could attach to this macro's "if". We
  350. * try to handle just the minimum we need here in a macro...the loop, the
  351. * static vars, and break points. The heavy lifting is handled in
  352. * SDL_ReportAssertion().
  353. *
  354. * \param condition the condition to assert.
  355. *
  356. * \since This macro is available since SDL 3.2.0.
  357. */
  358. #define SDL_enabled_assert(condition) \
  359. do { \
  360. while ( !(condition) ) { \
  361. static struct SDL_AssertData sdl_assert_data = { false, 0, #condition, NULL, 0, NULL, NULL }; \
  362. const SDL_AssertState sdl_assert_state = SDL_ReportAssertion(&sdl_assert_data, SDL_FUNCTION, SDL_ASSERT_FILE, SDL_LINE); \
  363. if (sdl_assert_state == SDL_ASSERTION_RETRY) { \
  364. continue; /* go again. */ \
  365. } else if (sdl_assert_state == SDL_ASSERTION_BREAK) { \
  366. SDL_AssertBreakpoint(); \
  367. } \
  368. break; /* not retrying. */ \
  369. } \
  370. } while (SDL_NULL_WHILE_LOOP_CONDITION)
  371. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  372. /**
  373. * An assertion test that is normally performed only in debug builds.
  374. *
  375. * This macro is enabled when the SDL_ASSERT_LEVEL is >= 2, otherwise it is
  376. * disabled. This is meant to only do these tests in debug builds, so they can
  377. * tend to be more expensive, and they are meant to bring everything to a halt
  378. * when they fail, with the programmer there to assess the problem.
  379. *
  380. * In short: you can sprinkle these around liberally and assume they will
  381. * evaporate out of the build when building for end-users.
  382. *
  383. * When assertions are disabled, this wraps `condition` in a `sizeof`
  384. * operator, which means any function calls and side effects will not run, but
  385. * the compiler will not complain about any otherwise-unused variables that
  386. * are only referenced in the assertion.
  387. *
  388. * One can set the environment variable "SDL_ASSERT" to one of several strings
  389. * ("abort", "break", "retry", "ignore", "always_ignore") to force a default
  390. * behavior, which may be desirable for automation purposes. If your platform
  391. * requires GUI interfaces to happen on the main thread but you're debugging
  392. * an assertion in a background thread, it might be desirable to set this to
  393. * "break" so that your debugger takes control as soon as assert is triggered,
  394. * instead of risking a bad UI interaction (deadlock, etc) in the application.
  395. *
  396. * \param condition boolean value to test.
  397. *
  398. * \threadsafety It is safe to call this macro from any thread.
  399. *
  400. * \since This macro is available since SDL 3.2.0.
  401. */
  402. #define SDL_assert(condition) if (assertion_enabled && (condition)) { trigger_assertion; }
  403. /**
  404. * An assertion test that is performed even in release builds.
  405. *
  406. * This macro is enabled when the SDL_ASSERT_LEVEL is >= 1, otherwise it is
  407. * disabled. This is meant to be for tests that are cheap to make and
  408. * extremely unlikely to fail; generally it is frowned upon to have an
  409. * assertion failure in a release build, so these assertions generally need to
  410. * be of more than life-and-death importance if there's a chance they might
  411. * trigger. You should almost always consider handling these cases more
  412. * gracefully than an assert allows.
  413. *
  414. * When assertions are disabled, this wraps `condition` in a `sizeof`
  415. * operator, which means any function calls and side effects will not run, but
  416. * the compiler will not complain about any otherwise-unused variables that
  417. * are only referenced in the assertion.
  418. *
  419. * One can set the environment variable "SDL_ASSERT" to one of several strings
  420. * ("abort", "break", "retry", "ignore", "always_ignore") to force a default
  421. * behavior, which may be desirable for automation purposes. If your platform
  422. * requires GUI interfaces to happen on the main thread but you're debugging
  423. * an assertion in a background thread, it might be desirable to set this to
  424. * "break" so that your debugger takes control as soon as assert is triggered,
  425. * instead of risking a bad UI interaction (deadlock, etc) in the application.
  426. * *
  427. *
  428. * \param condition boolean value to test.
  429. *
  430. * \threadsafety It is safe to call this macro from any thread.
  431. *
  432. * \since This macro is available since SDL 3.2.0.
  433. */
  434. #define SDL_assert_release(condition) SDL_disabled_assert(condition)
  435. /**
  436. * An assertion test that is performed only when built with paranoid settings.
  437. *
  438. * This macro is enabled when the SDL_ASSERT_LEVEL is >= 3, otherwise it is
  439. * disabled. This is a higher level than both release and debug, so these
  440. * tests are meant to be expensive and only run when specifically looking for
  441. * extremely unexpected failure cases in a special build.
  442. *
  443. * When assertions are disabled, this wraps `condition` in a `sizeof`
  444. * operator, which means any function calls and side effects will not run, but
  445. * the compiler will not complain about any otherwise-unused variables that
  446. * are only referenced in the assertion.
  447. *
  448. * One can set the environment variable "SDL_ASSERT" to one of several strings
  449. * ("abort", "break", "retry", "ignore", "always_ignore") to force a default
  450. * behavior, which may be desirable for automation purposes. If your platform
  451. * requires GUI interfaces to happen on the main thread but you're debugging
  452. * an assertion in a background thread, it might be desirable to set this to
  453. * "break" so that your debugger takes control as soon as assert is triggered,
  454. * instead of risking a bad UI interaction (deadlock, etc) in the application.
  455. *
  456. * \param condition boolean value to test.
  457. *
  458. * \threadsafety It is safe to call this macro from any thread.
  459. *
  460. * \since This macro is available since SDL 3.2.0.
  461. */
  462. #define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  463. /* Enable various levels of assertions. */
  464. #elif SDL_ASSERT_LEVEL == 0 /* assertions disabled */
  465. # define SDL_assert(condition) SDL_disabled_assert(condition)
  466. # define SDL_assert_release(condition) SDL_disabled_assert(condition)
  467. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  468. #elif SDL_ASSERT_LEVEL == 1 /* release settings. */
  469. # define SDL_assert(condition) SDL_disabled_assert(condition)
  470. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  471. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  472. #elif SDL_ASSERT_LEVEL == 2 /* debug settings. */
  473. # define SDL_assert(condition) SDL_enabled_assert(condition)
  474. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  475. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  476. #elif SDL_ASSERT_LEVEL == 3 /* paranoid settings. */
  477. # define SDL_assert(condition) SDL_enabled_assert(condition)
  478. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  479. # define SDL_assert_paranoid(condition) SDL_enabled_assert(condition)
  480. #else
  481. # error Unknown assertion level.
  482. #endif
  483. /**
  484. * An assertion test that is always performed.
  485. *
  486. * This macro is always enabled no matter what SDL_ASSERT_LEVEL is set to. You
  487. * almost never want to use this, as it could trigger on an end-user's system,
  488. * crashing your program.
  489. *
  490. * One can set the environment variable "SDL_ASSERT" to one of several strings
  491. * ("abort", "break", "retry", "ignore", "always_ignore") to force a default
  492. * behavior, which may be desirable for automation purposes. If your platform
  493. * requires GUI interfaces to happen on the main thread but you're debugging
  494. * an assertion in a background thread, it might be desirable to set this to
  495. * "break" so that your debugger takes control as soon as assert is triggered,
  496. * instead of risking a bad UI interaction (deadlock, etc) in the application.
  497. *
  498. * \param condition boolean value to test.
  499. *
  500. * \threadsafety It is safe to call this macro from any thread.
  501. *
  502. * \since This macro is available since SDL 3.2.0.
  503. */
  504. #define SDL_assert_always(condition) SDL_enabled_assert(condition)
  505. /**
  506. * A callback that fires when an SDL assertion fails.
  507. *
  508. * \param data a pointer to the SDL_AssertData structure corresponding to the
  509. * current assertion.
  510. * \param userdata what was passed as `userdata` to SDL_SetAssertionHandler().
  511. * \returns an SDL_AssertState value indicating how to handle the failure.
  512. *
  513. * \threadsafety This callback may be called from any thread that triggers an
  514. * assert at any time.
  515. *
  516. * \since This datatype is available since SDL 3.2.0.
  517. */
  518. typedef SDL_AssertState (SDLCALL *SDL_AssertionHandler)(
  519. const SDL_AssertData *data, void *userdata);
  520. /**
  521. * Set an application-defined assertion handler.
  522. *
  523. * This function allows an application to show its own assertion UI and/or
  524. * force the response to an assertion failure. If the application doesn't
  525. * provide this, SDL will try to do the right thing, popping up a
  526. * system-specific GUI dialog, and probably minimizing any fullscreen windows.
  527. *
  528. * This callback may fire from any thread, but it runs wrapped in a mutex, so
  529. * it will only fire from one thread at a time.
  530. *
  531. * This callback is NOT reset to SDL's internal handler upon SDL_Quit()!
  532. *
  533. * \param handler the SDL_AssertionHandler function to call when an assertion
  534. * fails or NULL for the default handler.
  535. * \param userdata a pointer that is passed to `handler`.
  536. *
  537. * \threadsafety It is safe to call this function from any thread.
  538. *
  539. * \since This function is available since SDL 3.2.0.
  540. *
  541. * \sa SDL_GetAssertionHandler
  542. */
  543. extern SDL_DECLSPEC void SDLCALL SDL_SetAssertionHandler(
  544. SDL_AssertionHandler handler,
  545. void *userdata);
  546. /**
  547. * Get the default assertion handler.
  548. *
  549. * This returns the function pointer that is called by default when an
  550. * assertion is triggered. This is an internal function provided by SDL, that
  551. * is used for assertions when SDL_SetAssertionHandler() hasn't been used to
  552. * provide a different function.
  553. *
  554. * \returns the default SDL_AssertionHandler that is called when an assert
  555. * triggers.
  556. *
  557. * \threadsafety It is safe to call this function from any thread.
  558. *
  559. * \since This function is available since SDL 3.2.0.
  560. *
  561. * \sa SDL_GetAssertionHandler
  562. */
  563. extern SDL_DECLSPEC SDL_AssertionHandler SDLCALL SDL_GetDefaultAssertionHandler(void);
  564. /**
  565. * Get the current assertion handler.
  566. *
  567. * This returns the function pointer that is called when an assertion is
  568. * triggered. This is either the value last passed to
  569. * SDL_SetAssertionHandler(), or if no application-specified function is set,
  570. * is equivalent to calling SDL_GetDefaultAssertionHandler().
  571. *
  572. * The parameter `puserdata` is a pointer to a void*, which will store the
  573. * "userdata" pointer that was passed to SDL_SetAssertionHandler(). This value
  574. * will always be NULL for the default handler. If you don't care about this
  575. * data, it is safe to pass a NULL pointer to this function to ignore it.
  576. *
  577. * \param puserdata pointer which is filled with the "userdata" pointer that
  578. * was passed to SDL_SetAssertionHandler().
  579. * \returns the SDL_AssertionHandler that is called when an assert triggers.
  580. *
  581. * \threadsafety It is safe to call this function from any thread.
  582. *
  583. * \since This function is available since SDL 3.2.0.
  584. *
  585. * \sa SDL_SetAssertionHandler
  586. */
  587. extern SDL_DECLSPEC SDL_AssertionHandler SDLCALL SDL_GetAssertionHandler(void **puserdata);
  588. /**
  589. * Get a list of all assertion failures.
  590. *
  591. * This function gets all assertions triggered since the last call to
  592. * SDL_ResetAssertionReport(), or the start of the program.
  593. *
  594. * The proper way to examine this data looks something like this:
  595. *
  596. * ```c
  597. * const SDL_AssertData *item = SDL_GetAssertionReport();
  598. * while (item) {
  599. * printf("'%s', %s (%s:%d), triggered %u times, always ignore: %s.\\n",
  600. * item->condition, item->function, item->filename,
  601. * item->linenum, item->trigger_count,
  602. * item->always_ignore ? "yes" : "no");
  603. * item = item->next;
  604. * }
  605. * ```
  606. *
  607. * \returns a list of all failed assertions or NULL if the list is empty. This
  608. * memory should not be modified or freed by the application. This
  609. * pointer remains valid until the next call to SDL_Quit() or
  610. * SDL_ResetAssertionReport().
  611. *
  612. * \threadsafety This function is not thread safe. Other threads calling
  613. * SDL_ResetAssertionReport() simultaneously, may render the
  614. * returned pointer invalid.
  615. *
  616. * \since This function is available since SDL 3.2.0.
  617. *
  618. * \sa SDL_ResetAssertionReport
  619. */
  620. extern SDL_DECLSPEC const SDL_AssertData * SDLCALL SDL_GetAssertionReport(void);
  621. /**
  622. * Clear the list of all assertion failures.
  623. *
  624. * This function will clear the list of all assertions triggered up to that
  625. * point. Immediately following this call, SDL_GetAssertionReport will return
  626. * no items. In addition, any previously-triggered assertions will be reset to
  627. * a trigger_count of zero, and their always_ignore state will be false.
  628. *
  629. * \threadsafety This function is not thread safe. Other threads triggering an
  630. * assertion, or simultaneously calling this function may cause
  631. * memory leaks or crashes.
  632. *
  633. * \since This function is available since SDL 3.2.0.
  634. *
  635. * \sa SDL_GetAssertionReport
  636. */
  637. extern SDL_DECLSPEC void SDLCALL SDL_ResetAssertionReport(void);
  638. /* Ends C function definitions when using C++ */
  639. #ifdef __cplusplus
  640. }
  641. #endif
  642. #include <SDL3/SDL_close_code.h>
  643. #endif /* SDL_assert_h_ */