Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Moved SDL_FUNCTION out so it's always available, and added SDL_FILE a…
Browse files Browse the repository at this point in the history
…nd SDL_LINE
  • Loading branch information
slouken committed Jan 13, 2010
1 parent 179e447 commit d52988d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
76 changes: 50 additions & 26 deletions include/SDL_assert.h
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -69,7 +58,8 @@ macro can have unique static variables associated with it.
#include <signal.h>
#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. */
Expand All @@ -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
{
Expand All @@ -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();
Expand All @@ -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) { \
Expand Down Expand Up @@ -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 */

Expand Down
8 changes: 5 additions & 3 deletions src/SDL_assert.c
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit d52988d

Please sign in to comment.