Commit 89f83fd9 authored by Sam Lantinga's avatar Sam Lantinga

Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%404406
parent 803375d5
...@@ -19,11 +19,20 @@ ...@@ -19,11 +19,20 @@
Sam Lantinga Sam Lantinga
slouken@libsdl.org slouken@libsdl.org
*/ */
#include "SDL_config.h"
#ifndef _SDL_assert_h #ifndef _SDL_assert_h
#define _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 #ifndef SDL_ASSERT_LEVEL
#ifdef SDL_DEFAULT_ASSERT_LEVEL #ifdef SDL_DEFAULT_ASSERT_LEVEL
#define SDL_ASSERT_LEVEL SDL_DEFAULT_ASSERT_LEVEL #define SDL_ASSERT_LEVEL SDL_DEFAULT_ASSERT_LEVEL
...@@ -35,26 +44,6 @@ ...@@ -35,26 +44,6 @@
#endif #endif
#endif /* SDL_ASSERT_LEVEL */ #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 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 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. ...@@ -69,7 +58,8 @@ macro can have unique static variables associated with it.
#include <signal.h> #include <signal.h>
#define SDL_TriggerBreakpoint() raise(SIGTRAP) #define SDL_TriggerBreakpoint() raise(SIGTRAP)
#else #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 #endif
#if (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */ #if (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */
...@@ -79,6 +69,29 @@ macro can have unique static variables associated with it. ...@@ -79,6 +69,29 @@ macro can have unique static variables associated with it.
#else #else
# define SDL_FUNCTION "???" # define SDL_FUNCTION "???"
#endif #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 typedef enum
{ {
...@@ -100,7 +113,9 @@ typedef struct SDL_assert_data ...@@ -100,7 +113,9 @@ typedef struct SDL_assert_data
struct SDL_assert_data *next; struct SDL_assert_data *next;
} SDL_assert_data; } 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: /* the do {} while(0) avoids dangling else problems:
if (x) SDL_assert(y); else blah(); if (x) SDL_assert(y); else blah();
...@@ -113,11 +128,12 @@ SDL_assert_state SDL_ReportAssertion(SDL_assert_data *, const char *, int); ...@@ -113,11 +128,12 @@ SDL_assert_state SDL_ReportAssertion(SDL_assert_data *, const char *, int);
do { \ do { \
while ( !(condition) ) { \ while ( !(condition) ) { \
static struct SDL_assert_data assert_data = { \ 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, \ const SDL_assert_state state = SDL_ReportAssertion(&assert_data, \
SDL_FUNCTION, \ SDL_FUNCTION, \
__LINE__); \ SDL_FILE, \
SDL_LINE); \
if (state == SDL_ASSERTION_RETRY) { \ if (state == SDL_ASSERTION_RETRY) { \
continue; /* go again. */ \ continue; /* go again. */ \
} else if (state == SDL_ASSERTION_BREAK) { \ } else if (state == SDL_ASSERTION_BREAK) { \
...@@ -147,8 +163,16 @@ SDL_assert_state SDL_ReportAssertion(SDL_assert_data *, const char *, int); ...@@ -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_release(condition) SDL_enabled_assert(condition)
# define SDL_assert_paranoid(condition) SDL_enabled_assert(condition) # define SDL_assert_paranoid(condition) SDL_enabled_assert(condition)
#else #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 #endif
#include "close_code.h"
#endif /* _SDL_assert_h */ #endif /* _SDL_assert_h */
......
...@@ -327,7 +327,8 @@ static SDL_assert_state SDL_PromptAssertion(const SDL_assert_data *data) ...@@ -327,7 +327,8 @@ static SDL_assert_state SDL_PromptAssertion(const SDL_assert_data *data)
static SDL_mutex *assertion_mutex = NULL; static SDL_mutex *assertion_mutex = NULL;
SDL_assert_state 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; SDL_assert_state state;
...@@ -338,6 +339,7 @@ SDL_ReportAssertion(SDL_assert_data *data, const char *func, int line) ...@@ -338,6 +339,7 @@ SDL_ReportAssertion(SDL_assert_data *data, const char *func, int line)
/* doing this because Visual C is upset over assigning in the macro. */ /* doing this because Visual C is upset over assigning in the macro. */
if (data->trigger_count == 0) { if (data->trigger_count == 0) {
data->function = func; data->function = func;
data->filename = file;
data->linenum = line; data->linenum = line;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment