From d52988d94697b0d661fb85387b900baf1b7c1453 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 13 Jan 2010 08:25:16 +0000 Subject: [PATCH] Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE --- include/SDL_assert.h | 76 +++++++++++++++++++++++++++++--------------- src/SDL_assert.c | 8 +++-- 2 files changed, 55 insertions(+), 29 deletions(-) diff --git a/include/SDL_assert.h b/include/SDL_assert.h index de4dd8640..483ae2353 100644 --- a/include/SDL_assert.h +++ b/include/SDL_assert.h @@ -19,11 +19,20 @@ Sam Lantinga slouken@libsdl.org */ -#include "SDL_config.h" #ifndef _SDL_assert_h #define _SDL_assert_h +#include "SDL_config.h" + +#include "begin_code.h" +/* Set up for C function definitions, even when using C++ */ +#ifdef __cplusplus +/* *INDENT-OFF* */ +extern "C" { +/* *INDENT-ON* */ +#endif + #ifndef SDL_ASSERT_LEVEL #ifdef SDL_DEFAULT_ASSERT_LEVEL #define SDL_ASSERT_LEVEL SDL_DEFAULT_ASSERT_LEVEL @@ -35,26 +44,6 @@ #endif #endif /* SDL_ASSERT_LEVEL */ -/* -sizeof (x) makes the compiler still parse the expression even without -assertions enabled, so the code is always checked at compile time, but -doesn't actually generate code for it, so there are no side effects or -expensive checks at run time, just the constant size of what x WOULD be, -which presumably gets optimized out as unused. -This also solves the problem of... - - int somevalue = blah(); - SDL_assert(somevalue == 1); - -...which would cause compiles to complain that somevalue is unused if we -disable assertions. -*/ - -#define SDL_disabled_assert(condition) \ - do { (void) sizeof ((condition)); } while (0) - -#if (SDL_ASSERT_LEVEL > 0) - /* These are macros and not first class functions so that the debugger breaks on the assertion line and not in some random guts of SDL, and so each @@ -69,7 +58,8 @@ macro can have unique static variables associated with it. #include #define SDL_TriggerBreakpoint() raise(SIGTRAP) #else - #error Please define your platform or set SDL_ASSERT_LEVEL to 0. + /* How do we trigger breakpoints on this platform? */ + #define SDL_TriggerBreakpoint() #endif #if (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */ @@ -79,6 +69,29 @@ macro can have unique static variables associated with it. #else # define SDL_FUNCTION "???" #endif +#define SDL_FILE __FILE__ +#define SDL_LINE __LINE__ + +/* +sizeof (x) makes the compiler still parse the expression even without +assertions enabled, so the code is always checked at compile time, but +doesn't actually generate code for it, so there are no side effects or +expensive checks at run time, just the constant size of what x WOULD be, +which presumably gets optimized out as unused. +This also solves the problem of... + + int somevalue = blah(); + SDL_assert(somevalue == 1); + +...which would cause compiles to complain that somevalue is unused if we +disable assertions. +*/ + +#define SDL_disabled_assert(condition) \ + do { (void) sizeof ((condition)); } while (0) + +#if (SDL_ASSERT_LEVEL > 0) + typedef enum { @@ -100,7 +113,9 @@ typedef struct SDL_assert_data struct SDL_assert_data *next; } SDL_assert_data; -SDL_assert_state SDL_ReportAssertion(SDL_assert_data *, const char *, int); +extern DECLSPEC SDL_assert_state SDLCALL SDL_ReportAssertion(SDL_assert_data *, + const char *, + const char *, int); /* the do {} while(0) avoids dangling else problems: if (x) SDL_assert(y); else blah(); @@ -113,11 +128,12 @@ SDL_assert_state SDL_ReportAssertion(SDL_assert_data *, const char *, int); do { \ while ( !(condition) ) { \ static struct SDL_assert_data assert_data = { \ - 0, 0, #condition, __FILE__, 0, 0, 0 \ + 0, 0, #condition, 0, 0, 0, 0 \ }; \ const SDL_assert_state state = SDL_ReportAssertion(&assert_data, \ SDL_FUNCTION, \ - __LINE__); \ + SDL_FILE, \ + SDL_LINE); \ if (state == SDL_ASSERTION_RETRY) { \ continue; /* go again. */ \ } else if (state == SDL_ASSERTION_BREAK) { \ @@ -147,8 +163,16 @@ SDL_assert_state SDL_ReportAssertion(SDL_assert_data *, const char *, int); # define SDL_assert_release(condition) SDL_enabled_assert(condition) # define SDL_assert_paranoid(condition) SDL_enabled_assert(condition) #else -# error Unknown assertion level. Please fix your SDL_config.h. +# error Unknown assertion level. +#endif + +/* Ends C function definitions when using C++ */ +#ifdef __cplusplus +/* *INDENT-OFF* */ +} +/* *INDENT-ON* */ #endif +#include "close_code.h" #endif /* _SDL_assert_h */ diff --git a/src/SDL_assert.c b/src/SDL_assert.c index b6e470908..687f51067 100644 --- a/src/SDL_assert.c +++ b/src/SDL_assert.c @@ -265,7 +265,7 @@ static SDL_assert_state SDL_PromptAssertion(const SDL_assert_data *data) data->trigger_count, (data->trigger_count == 1) ? "" : "s", data->condition); - /* let env. variable override, so unit tests won't block in a GUI. */ + /* let env. variable override, so unit tests won't block in a GUI. */ envr = SDL_getenv("SDL_ASSERT"); if (envr != NULL) { if (SDL_strcmp(envr, "abort") == 0) { @@ -327,7 +327,8 @@ static SDL_assert_state SDL_PromptAssertion(const SDL_assert_data *data) static SDL_mutex *assertion_mutex = NULL; SDL_assert_state -SDL_ReportAssertion(SDL_assert_data *data, const char *func, int line) +SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file, + int line) { SDL_assert_state state; @@ -338,7 +339,8 @@ SDL_ReportAssertion(SDL_assert_data *data, const char *func, int line) /* doing this because Visual C is upset over assigning in the macro. */ if (data->trigger_count == 0) { data->function = func; - data->linenum = line; + data->filename = file; + data->linenum = line; } SDL_AddAssertionToReport(data);