include/SDL_assert.h
author Sam Lantinga <slouken@libsdl.org>
Wed, 13 Jan 2010 07:25:28 +0000
changeset 3649 1e74d7984d0b
parent 3647 c5925cd41955
child 3650 91460fdfe83a
permissions -rw-r--r--
The config sets the default assertion level so people can override it for their own use in application code.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #ifndef _SDL_assert_h
    25 #define _SDL_assert_h
    26 
    27 #ifndef SDL_ASSERT_LEVEL
    28 #define SDL_ASSERT_LEVEL SDL_DEFAULT_ASSERT_LEVEL
    29 #endif
    30 
    31 /*
    32 sizeof (x) makes the compiler still parse the expression even without
    33 assertions enabled, so the code is always checked at compile time, but
    34 doesn't actually generate code for it, so there are no side effects or
    35 expensive checks at run time, just the constant size of what x WOULD be,
    36 which presumably gets optimized out as unused.
    37 This also solves the problem of...
    38 
    39     int somevalue = blah();
    40     SDL_assert(somevalue == 1);
    41 
    42 ...which would cause compiles to complain that somevalue is unused if we
    43 disable assertions.
    44 */
    45 
    46 #define SDL_disabled_assert(condition) \
    47     do { (void) sizeof ((condition)); } while (0)
    48 
    49 #if (SDL_ASSERT_LEVEL > 0)
    50 
    51 /*
    52 These are macros and not first class functions so that the debugger breaks
    53 on the assertion line and not in some random guts of SDL, and so each
    54 macro can have unique static variables associated with it.
    55 */
    56 
    57 #if (defined(_MSC_VER) && ((_M_IX86) || (_M_X64)))
    58     #define SDL_TriggerBreakpoint() __asm { int 3 }
    59 #elif (defined(__GNUC__) && ((__i386__) || (__x86_64__)))
    60     #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "int $3\n\t" )
    61 #elif defined(unix)
    62     #include <signal.h>
    63     #define SDL_TriggerBreakpoint() raise(SIGTRAP)
    64 #else
    65     #error Please define your platform or set SDL_ASSERT_LEVEL to 0.
    66 #endif
    67 
    68 #if (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */
    69 #   define SDL_FUNCTION __func__
    70 #elif ((__GNUC__ >= 2) || defined(_MSC_VER))
    71 #   define SDL_FUNCTION __FUNCTION__
    72 #else
    73 #   define SDL_FUNCTION "???"
    74 #endif
    75 
    76 typedef enum
    77 {
    78     SDL_ASSERTION_RETRY,  /**< Retry the assert immediately. */
    79     SDL_ASSERTION_BREAK,  /**< Make the debugger trigger a breakpoint. */
    80     SDL_ASSERTION_ABORT,  /**< Terminate the program. */
    81     SDL_ASSERTION_IGNORE,  /**< Ignore the assert. */
    82     SDL_ASSERTION_ALWAYS_IGNORE,  /**< Ignore the assert from now on. */
    83 } SDL_assert_state;
    84 
    85 typedef struct SDL_assert_data
    86 {
    87     int always_ignore;
    88     unsigned int trigger_count;
    89     const char *condition;
    90     const char *filename;
    91     int linenum;
    92     const char *function;
    93     struct SDL_assert_data *next;
    94 } SDL_assert_data;
    95 
    96 SDL_assert_state SDL_ReportAssertion(SDL_assert_data *, const char *, int);
    97 
    98 /* the do {} while(0) avoids dangling else problems:
    99     if (x) SDL_assert(y); else blah();
   100        ... without the do/while, the "else" could attach to this macro's "if".
   101    We try to handle just the minimum we need here in a macro...the loop,
   102    the static vars, and break points. The heavy lifting is handled in
   103    SDL_ReportAssertion(), in SDL_assert.c.
   104 */
   105 #define SDL_enabled_assert(condition) \
   106     do { \
   107         while ( !(condition) ) { \
   108             static struct SDL_assert_data assert_data = { \
   109                 0, 0, #condition, __FILE__, 0, 0, 0 \
   110             }; \
   111             const SDL_assert_state state = SDL_ReportAssertion(&assert_data, \
   112                                                                SDL_FUNCTION, \
   113                                                                __LINE__); \
   114             if (state == SDL_ASSERTION_RETRY) { \
   115                 continue; /* go again. */ \
   116             } else if (state == SDL_ASSERTION_BREAK) { \
   117                 SDL_TriggerBreakpoint(); \
   118             } \
   119             break; /* not retrying. */ \
   120         } \
   121     } while (0)
   122 
   123 #endif  /* enabled assertions support code */
   124 
   125 /* Enable various levels of assertions. */
   126 #if SDL_ASSERT_LEVEL == 0   /* assertions disabled */
   127 #   define SDL_assert(condition) SDL_disabled_assert(condition)
   128 #   define SDL_assert_release(condition) SDL_disabled_assert(condition)
   129 #   define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
   130 #elif SDL_ASSERT_LEVEL == 1  /* release settings. */
   131 #   define SDL_assert(condition) SDL_enabled_assert(condition)
   132 #   define SDL_assert_release(condition) SDL_enabled_assert(condition)
   133 #   define SDL_assert_paranoid(condition) SDL_enabled_assert(condition)
   134 #elif SDL_ASSERT_LEVEL == 2  /* normal settings. */
   135 #   define SDL_assert(condition) SDL_enabled_assert(condition)
   136 #   define SDL_assert_release(condition) SDL_enabled_assert(condition)
   137 #   define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
   138 #elif SDL_ASSERT_LEVEL == 3  /* paranoid settings. */
   139 #   define SDL_assert(condition) SDL_enabled_assert(condition)
   140 #   define SDL_assert_release(condition) SDL_enabled_assert(condition)
   141 #   define SDL_assert_paranoid(condition) SDL_enabled_assert(condition)
   142 #else
   143 #   error Unknown assertion level. Please fix your SDL_config.h.
   144 #endif
   145 
   146 #endif /* _SDL_assert_h */
   147 
   148 /* vi: set ts=4 sw=4 expandtab: */