include/SDL_assert.h
author Sam Lantinga <slouken@libsdl.org>
Wed, 13 Jan 2010 08:06:32 +0000
changeset 3654 336f3df1578d
parent 3653 1cd9f7117b98
child 3655 1cc7f0143c12
permissions -rw-r--r--
Allow configure to override assertion settings, but by default this comes from the build optimization level.
     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 #ifdef SDL_DEFAULT_ASSERT_LEVEL
    29 #define SDL_ASSERT_LEVEL SDL_DEFAULT_ASSERT_LEVEL
    30 #elif defined(_DEBUG) || defined(DEBUG) || \
    31       (defined(__GNUC__) && !defined(__OPTIMIZE__))
    32 #define SDL_ASSERT_LEVEL 2
    33 #else
    34 #define SDL_ASSERT_LEVEL 1
    35 #endif
    36 #endif /* SDL_ASSERT_LEVEL */
    37 
    38 /*
    39 sizeof (x) makes the compiler still parse the expression even without
    40 assertions enabled, so the code is always checked at compile time, but
    41 doesn't actually generate code for it, so there are no side effects or
    42 expensive checks at run time, just the constant size of what x WOULD be,
    43 which presumably gets optimized out as unused.
    44 This also solves the problem of...
    45 
    46     int somevalue = blah();
    47     SDL_assert(somevalue == 1);
    48 
    49 ...which would cause compiles to complain that somevalue is unused if we
    50 disable assertions.
    51 */
    52 
    53 #define SDL_disabled_assert(condition) \
    54     do { (void) sizeof ((condition)); } while (0)
    55 
    56 #if (SDL_ASSERT_LEVEL > 0)
    57 
    58 /*
    59 These are macros and not first class functions so that the debugger breaks
    60 on the assertion line and not in some random guts of SDL, and so each
    61 macro can have unique static variables associated with it.
    62 */
    63 
    64 #if (defined(_MSC_VER) && ((_M_IX86) || (_M_X64)))
    65     #define SDL_TriggerBreakpoint() __asm { int 3 }
    66 #elif (defined(__GNUC__) && ((__i386__) || (__x86_64__)))
    67     #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "int $3\n\t" )
    68 #elif defined(HAVE_SIGNAL_H)
    69     #include <signal.h>
    70     #define SDL_TriggerBreakpoint() raise(SIGTRAP)
    71 #else
    72     #error Please define your platform or set SDL_ASSERT_LEVEL to 0.
    73 #endif
    74 
    75 #if (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */
    76 #   define SDL_FUNCTION __func__
    77 #elif ((__GNUC__ >= 2) || defined(_MSC_VER))
    78 #   define SDL_FUNCTION __FUNCTION__
    79 #else
    80 #   define SDL_FUNCTION "???"
    81 #endif
    82 
    83 typedef enum
    84 {
    85     SDL_ASSERTION_RETRY,  /**< Retry the assert immediately. */
    86     SDL_ASSERTION_BREAK,  /**< Make the debugger trigger a breakpoint. */
    87     SDL_ASSERTION_ABORT,  /**< Terminate the program. */
    88     SDL_ASSERTION_IGNORE,  /**< Ignore the assert. */
    89     SDL_ASSERTION_ALWAYS_IGNORE,  /**< Ignore the assert from now on. */
    90 } SDL_assert_state;
    91 
    92 typedef struct SDL_assert_data
    93 {
    94     int always_ignore;
    95     unsigned int trigger_count;
    96     const char *condition;
    97     const char *filename;
    98     int linenum;
    99     const char *function;
   100     struct SDL_assert_data *next;
   101 } SDL_assert_data;
   102 
   103 SDL_assert_state SDL_ReportAssertion(SDL_assert_data *, const char *, int);
   104 
   105 /* the do {} while(0) avoids dangling else problems:
   106     if (x) SDL_assert(y); else blah();
   107        ... without the do/while, the "else" could attach to this macro's "if".
   108    We try to handle just the minimum we need here in a macro...the loop,
   109    the static vars, and break points. The heavy lifting is handled in
   110    SDL_ReportAssertion(), in SDL_assert.c.
   111 */
   112 #define SDL_enabled_assert(condition) \
   113     do { \
   114         while ( !(condition) ) { \
   115             static struct SDL_assert_data assert_data = { \
   116                 0, 0, #condition, __FILE__, 0, 0, 0 \
   117             }; \
   118             const SDL_assert_state state = SDL_ReportAssertion(&assert_data, \
   119                                                                SDL_FUNCTION, \
   120                                                                __LINE__); \
   121             if (state == SDL_ASSERTION_RETRY) { \
   122                 continue; /* go again. */ \
   123             } else if (state == SDL_ASSERTION_BREAK) { \
   124                 SDL_TriggerBreakpoint(); \
   125             } \
   126             break; /* not retrying. */ \
   127         } \
   128     } while (0)
   129 
   130 #endif  /* enabled assertions support code */
   131 
   132 /* Enable various levels of assertions. */
   133 #if SDL_ASSERT_LEVEL == 0   /* assertions disabled */
   134 #   define SDL_assert(condition) SDL_disabled_assert(condition)
   135 #   define SDL_assert_release(condition) SDL_disabled_assert(condition)
   136 #   define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
   137 #elif SDL_ASSERT_LEVEL == 1  /* release settings. */
   138 #   define SDL_assert(condition) SDL_disabled_assert(condition)
   139 #   define SDL_assert_release(condition) SDL_enabled_assert(condition)
   140 #   define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
   141 #elif SDL_ASSERT_LEVEL == 2  /* normal settings. */
   142 #   define SDL_assert(condition) SDL_enabled_assert(condition)
   143 #   define SDL_assert_release(condition) SDL_enabled_assert(condition)
   144 #   define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
   145 #elif SDL_ASSERT_LEVEL == 3  /* paranoid settings. */
   146 #   define SDL_assert(condition) SDL_enabled_assert(condition)
   147 #   define SDL_assert_release(condition) SDL_enabled_assert(condition)
   148 #   define SDL_assert_paranoid(condition) SDL_enabled_assert(condition)
   149 #else
   150 #   error Unknown assertion level. Please fix your SDL_config.h.
   151 #endif
   152 
   153 #endif /* _SDL_assert_h */
   154 
   155 /* vi: set ts=4 sw=4 expandtab: */