From 9c5bd98a0f93a48393f454e165e5edc2103585d0 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Sun, 18 Aug 2024 20:53:07 -0400
Subject: [PATCH] assert: Added SDL_HINT_ASSERT.
Same environment variable, but now accessible as a formal SDL hint.
Reference PR #10171.
---
include/SDL3/SDL_assert.h | 14 +-------------
include/SDL3/SDL_hints.h | 23 +++++++++++++++++++++++
src/SDL_assert.c | 15 +++++++--------
3 files changed, 31 insertions(+), 21 deletions(-)
diff --git a/include/SDL3/SDL_assert.h b/include/SDL3/SDL_assert.h
index 346d1e3a45f80..6e4f92428e519 100644
--- a/include/SDL3/SDL_assert.h
+++ b/include/SDL3/SDL_assert.h
@@ -303,9 +303,6 @@ extern SDL_DECLSPEC SDL_AssertState SDLCALL SDL_ReportAssertion(SDL_AssertData *
* "break" so that your debugger takes control as soon as assert is triggered,
* instead of risking a bad UI interaction (deadlock, etc) in the application.
*
- * Note that SDL_ASSERT is an _environment variable_ and not an SDL hint!
- * Please refer to your platform's documentation for how to set it!
- *
* \param condition boolean value to test.
*
* \since This macro is available since SDL 3.0.0.
@@ -335,10 +332,7 @@ extern SDL_DECLSPEC SDL_AssertState SDLCALL SDL_ReportAssertion(SDL_AssertData *
* an assertion in a background thread, it might be desirable to set this to
* "break" so that your debugger takes control as soon as assert is triggered,
* instead of risking a bad UI interaction (deadlock, etc) in the application.
- *
- * Note that SDL_ASSERT is an _environment variable_ and not an SDL hint!
- * Please refer to your platform's documentation for how to set it!
- *
+ * *
* \param condition boolean value to test.
*
* \since This macro is available since SDL 3.0.0.
@@ -366,9 +360,6 @@ extern SDL_DECLSPEC SDL_AssertState SDLCALL SDL_ReportAssertion(SDL_AssertData *
* "break" so that your debugger takes control as soon as assert is triggered,
* instead of risking a bad UI interaction (deadlock, etc) in the application.
*
- * Note that SDL_ASSERT is an _environment variable_ and not an SDL hint!
- * Please refer to your platform's documentation for how to set it!
- *
* \param condition boolean value to test.
*
* \since This macro is available since SDL 3.0.0.
@@ -412,9 +403,6 @@ extern SDL_DECLSPEC SDL_AssertState SDLCALL SDL_ReportAssertion(SDL_AssertData *
* "break" so that your debugger takes control as soon as assert is triggered,
* instead of risking a bad UI interaction (deadlock, etc) in the application.
*
- * Note that SDL_ASSERT is an _environment variable_ and not an SDL hint!
- * Please refer to your platform's documentation for how to set it!
- *
* \param condition boolean value to test.
*
* \since This macro is available since SDL 3.0.0.
diff --git a/include/SDL3/SDL_hints.h b/include/SDL3/SDL_hints.h
index 57893a383f80d..22938f3b5f358 100644
--- a/include/SDL3/SDL_hints.h
+++ b/include/SDL3/SDL_hints.h
@@ -4120,6 +4120,29 @@ extern "C" {
*/
#define SDL_HINT_XINPUT_ENABLED "SDL_XINPUT_ENABLED"
+/**
+ * A variable controlling response to SDL_assert failures.
+ *
+ * The variable can be set to the following case-sensitive values:
+ *
+ * - "abort": Program terminates immediately.
+ * - "break": Program triggers a debugger breakpoint.
+ * - "retry": Program reruns the SDL_assert's test again.
+ * - "ignore": Program continues on, ignoring this assertion failure this time.
+ * - "always_ignore": Program continues on, ignoring this assertion failure for the rest of the run.
+ *
+ * Note that SDL_SetAssertionHandler offers a programmatic means to deal with
+ * assertion failures through a callback, and this hint is largely intended to
+ * be used via environment variables by end users and automated tools.
+ *
+ * This hint should be set before an assertion failure is triggered and can
+ * be changed at any time.
+ *
+ * \since This hint is available since SDL 3.0.0.
+ */
+#define SDL_HINT_ASSERT "SDL_ASSERT"
+
+
/**
* An enumeration of hint priorities.
*
diff --git a/src/SDL_assert.c b/src/SDL_assert.c
index 938008c00f7aa..1fa62409f8daf 100644
--- a/src/SDL_assert.c
+++ b/src/SDL_assert.c
@@ -149,7 +149,6 @@ static SDL_NORETURN void SDL_AbortAssertion(void)
static SDL_AssertState SDLCALL SDL_PromptAssertion(const SDL_AssertData *data, void *userdata)
{
- const char *envr;
SDL_AssertState state = SDL_ASSERTION_ABORT;
SDL_Window *window;
SDL_MessageBoxData messagebox;
@@ -197,21 +196,21 @@ static SDL_AssertState SDLCALL SDL_PromptAssertion(const SDL_AssertData *data, v
debug_print("\n\n%s\n\n", message);
/* let env. variable override, so unit tests won't block in a GUI. */
- envr = SDL_getenv("SDL_ASSERT");
- if (envr) {
+ const char *hint = SDL_GetHint(SDL_HINT_ASSERT);
+ if (hint) {
if (message != stack_buf) {
SDL_free(message);
}
- if (SDL_strcmp(envr, "abort") == 0) {
+ if (SDL_strcmp(hint, "abort") == 0) {
return SDL_ASSERTION_ABORT;
- } else if (SDL_strcmp(envr, "break") == 0) {
+ } else if (SDL_strcmp(hint, "break") == 0) {
return SDL_ASSERTION_BREAK;
- } else if (SDL_strcmp(envr, "retry") == 0) {
+ } else if (SDL_strcmp(hint, "retry") == 0) {
return SDL_ASSERTION_RETRY;
- } else if (SDL_strcmp(envr, "ignore") == 0) {
+ } else if (SDL_strcmp(hint, "ignore") == 0) {
return SDL_ASSERTION_IGNORE;
- } else if (SDL_strcmp(envr, "always_ignore") == 0) {
+ } else if (SDL_strcmp(hint, "always_ignore") == 0) {
return SDL_ASSERTION_ALWAYS_IGNORE;
} else {
return SDL_ASSERTION_ABORT; /* oh well. */