Browse Source

assert: Allow SDL_FILE (and the new SDL_ASSERT_FILE) to be overridden.

This allows changing out `__FILE__` in general and the file path reported in
SDL_assert specifically to not leak build machine path info, or just simply
not include the absolute path if you swap it out with `FILE_NAME` on Clang/GCC.

Fixes #14290.
Ryan C. Gordon 1 month ago
parent
commit
801889dafa
1 changed files with 26 additions and 1 deletions
  1. 26 1
      include/SDL3/SDL_assert.h

+ 26 - 1
include/SDL3/SDL_assert.h

@@ -176,12 +176,37 @@ extern "C" {
 #   define SDL_FUNCTION "???"
 #endif
 
+#ifndef SDL_FILE
 /**
  * A macro that reports the current file being compiled.
  *
+ * This macro is only defined if it isn't already defined, so to override
+ * it (perhaps with something that doesn't provide path information at all,
+ * so build machine information doesn't leak into public binaries), apps can
+ * define this macro before including SDL_assert.h. For example, Clang and GCC
+ * can define this to `FILE_NAME` to get just the source filename instead of
+ * the full path.
+ *
  * \since This macro is available since SDL 3.2.0.
  */
 #define SDL_FILE    __FILE__
+#endif
+
+#ifndef SDL_ASSERT_FILE
+/**
+ * A macro that reports the current file being compiled, for use in assertions.
+ *
+ * This macro is only defined if it isn't already defined, so to override
+ * it (perhaps with something that doesn't provide path information at all,
+ * so build machine information doesn't leak into public binaries), apps can
+ * define this macro before including SDL_assert.h. For example, defining this
+ * to `""` will make sure no source path information is included in asserts.
+ *
+ * \since This macro is available since SDL 3.4.0.
+ */
+#define SDL_ASSERT_FILE SDL_FILE
+#endif
+
 
 /**
  * A macro that reports the current line number of the file being compiled.
@@ -360,7 +385,7 @@ extern SDL_DECLSPEC SDL_AssertState SDLCALL SDL_ReportAssertion(SDL_AssertData *
     do { \
         while ( !(condition) ) { \
             static struct SDL_AssertData sdl_assert_data = { false, 0, #condition, NULL, 0, NULL, NULL }; \
-            const SDL_AssertState sdl_assert_state = SDL_ReportAssertion(&sdl_assert_data, SDL_FUNCTION, SDL_FILE, SDL_LINE); \
+            const SDL_AssertState sdl_assert_state = SDL_ReportAssertion(&sdl_assert_data, SDL_FUNCTION, SDL_ASSERT_FILE, SDL_LINE); \
             if (sdl_assert_state == SDL_ASSERTION_RETRY) { \
                 continue; /* go again. */ \
             } else if (sdl_assert_state == SDL_ASSERTION_BREAK) { \