JavaScriptCore/wtf/Assertions.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2003, 2006, 2007 Apple Inc.  All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     7  * 1. Redistributions of source code must retain the above copyright
       
     8  *    notice, this list of conditions and the following disclaimer.
       
     9  * 2. Redistributions in binary form must reproduce the above copyright
       
    10  *    notice, this list of conditions and the following disclaimer in the
       
    11  *    documentation and/or other materials provided with the distribution.
       
    12  *
       
    13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
       
    14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
       
    17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
       
    21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
       
    24  */
       
    25 
       
    26 #ifndef WTF_Assertions_h
       
    27 #define WTF_Assertions_h
       
    28 
       
    29 /*
       
    30    no namespaces because this file has to be includable from C and Objective-C
       
    31 
       
    32    Note, this file uses many GCC extensions, but it should be compatible with
       
    33    C, Objective C, C++, and Objective C++.
       
    34 
       
    35    For non-debug builds, everything is disabled by default.
       
    36    Defining any of the symbols explicitly prevents this from having any effect.
       
    37    
       
    38    MSVC7 note: variadic macro support was added in MSVC8, so for now we disable
       
    39    those macros in MSVC7. For more info, see the MSDN document on variadic 
       
    40    macros here:
       
    41    
       
    42    http://msdn2.microsoft.com/en-us/library/ms177415(VS.80).aspx
       
    43 */
       
    44 
       
    45 #include "Platform.h"
       
    46 
       
    47 #if COMPILER(MSVC)
       
    48 #include <stddef.h>
       
    49 #else
       
    50 #include <inttypes.h>
       
    51 #endif
       
    52 
       
    53 #if OS(SYMBIAN)
       
    54 #include <e32def.h>
       
    55 #include <e32debug.h>
       
    56 #endif
       
    57 
       
    58 #if PLATFORM(BREWMP)
       
    59 #include <AEEStdLib.h>
       
    60 #endif
       
    61 
       
    62 #ifdef NDEBUG
       
    63 /* Disable ASSERT* macros in release mode. */
       
    64 #define ASSERTIONS_DISABLED_DEFAULT 1
       
    65 #else
       
    66 #define ASSERTIONS_DISABLED_DEFAULT 0
       
    67 #endif
       
    68 
       
    69 #if COMPILER(MSVC7_OR_LOWER) || COMPILER(WINSCW)
       
    70 #define HAVE_VARIADIC_MACRO 0
       
    71 #else
       
    72 #define HAVE_VARIADIC_MACRO 1
       
    73 #endif
       
    74 
       
    75 #ifndef ASSERT_DISABLED
       
    76 #define ASSERT_DISABLED ASSERTIONS_DISABLED_DEFAULT
       
    77 #endif
       
    78 
       
    79 #ifndef ASSERT_MSG_DISABLED
       
    80 #if HAVE(VARIADIC_MACRO)
       
    81 #define ASSERT_MSG_DISABLED ASSERTIONS_DISABLED_DEFAULT
       
    82 #else
       
    83 #define ASSERT_MSG_DISABLED 1
       
    84 #endif
       
    85 #endif
       
    86 
       
    87 #ifndef ASSERT_ARG_DISABLED
       
    88 #define ASSERT_ARG_DISABLED ASSERTIONS_DISABLED_DEFAULT
       
    89 #endif
       
    90 
       
    91 #ifndef FATAL_DISABLED
       
    92 #if HAVE(VARIADIC_MACRO)
       
    93 #define FATAL_DISABLED ASSERTIONS_DISABLED_DEFAULT
       
    94 #else
       
    95 #define FATAL_DISABLED 1
       
    96 #endif
       
    97 #endif
       
    98 
       
    99 #ifndef ERROR_DISABLED
       
   100 #if HAVE(VARIADIC_MACRO)
       
   101 #define ERROR_DISABLED ASSERTIONS_DISABLED_DEFAULT
       
   102 #else
       
   103 #define ERROR_DISABLED 1
       
   104 #endif
       
   105 #endif
       
   106 
       
   107 #ifndef LOG_DISABLED
       
   108 #if HAVE(VARIADIC_MACRO)
       
   109 #define LOG_DISABLED ASSERTIONS_DISABLED_DEFAULT
       
   110 #else
       
   111 #define LOG_DISABLED 1
       
   112 #endif
       
   113 #endif
       
   114 
       
   115 #if COMPILER(GCC)
       
   116 #define WTF_PRETTY_FUNCTION __PRETTY_FUNCTION__
       
   117 #else
       
   118 #define WTF_PRETTY_FUNCTION __FUNCTION__
       
   119 #endif
       
   120 
       
   121 /* WTF logging functions can process %@ in the format string to log a NSObject* but the printf format attribute
       
   122    emits a warning when %@ is used in the format string.  Until <rdar://problem/5195437> is resolved we can't include
       
   123    the attribute when being used from Objective-C code in case it decides to use %@. */
       
   124 #if COMPILER(GCC) && !defined(__OBJC__)
       
   125 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments) __attribute__((__format__(printf, formatStringArgument, extraArguments)))
       
   126 #else
       
   127 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments) 
       
   128 #endif
       
   129 
       
   130 /* These helper functions are always declared, but not necessarily always defined if the corresponding function is disabled. */
       
   131 
       
   132 #ifdef __cplusplus
       
   133 extern "C" {
       
   134 #endif
       
   135 
       
   136 typedef enum { WTFLogChannelOff, WTFLogChannelOn } WTFLogChannelState;
       
   137 
       
   138 typedef struct {
       
   139     unsigned mask;
       
   140     const char *defaultName;
       
   141     WTFLogChannelState state;
       
   142 } WTFLogChannel;
       
   143 
       
   144 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion);
       
   145 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
       
   146 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion);
       
   147 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
       
   148 void WTFReportError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
       
   149 void WTFLog(WTFLogChannel* channel, const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3);
       
   150 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
       
   151 
       
   152 #ifdef __cplusplus
       
   153 }
       
   154 #endif
       
   155 
       
   156 /* CRASH() - Raises a fatal error resulting in program termination and triggering either the debugger or the crash reporter.
       
   157 
       
   158    Use CRASH() in response to known, unrecoverable errors like out-of-memory.
       
   159    Macro is enabled in both debug and release mode.
       
   160    To test for unknown errors and verify assumptions, use ASSERT instead, to avoid impacting performance in release builds.
       
   161 
       
   162    Signals are ignored by the crash reporter on OS X so we must do better.
       
   163 */
       
   164 #ifndef CRASH
       
   165 #if OS(SYMBIAN)
       
   166 #define CRASH() do { \
       
   167     __DEBUGGER(); \
       
   168     User::Panic(_L("Webkit CRASH"),0); \
       
   169     } while(false)
       
   170 #elif PLATFORM(BREWMP)
       
   171 #define CRASH() do { \
       
   172     DBGPRINTF_FATAL("WebKit CRASH"); \
       
   173     *(int *)(uintptr_t)0xbbadbeef = 0; \
       
   174     ((void(*)())0)(); /* More reliable, but doesn't say BBADBEEF */ \
       
   175 } while(false)
       
   176 #else
       
   177 #define CRASH() do { \
       
   178     *(int *)(uintptr_t)0xbbadbeef = 0; \
       
   179     ((void(*)())0)(); /* More reliable, but doesn't say BBADBEEF */ \
       
   180 } while(false)
       
   181 #endif
       
   182 #endif
       
   183 
       
   184 /* ASSERT, ASSERT_NOT_REACHED, ASSERT_UNUSED
       
   185 
       
   186   These macros are compiled out of release builds.
       
   187   Expressions inside them are evaluated in debug builds only.
       
   188 */
       
   189 
       
   190 #if OS(WINCE) && !PLATFORM(TORCHMOBILE)
       
   191 /* FIXME: We include this here only to avoid a conflict with the ASSERT macro. */
       
   192 #include <windows.h>
       
   193 #undef min
       
   194 #undef max
       
   195 #undef ERROR
       
   196 #endif
       
   197 
       
   198 #if OS(WINDOWS) || OS(SYMBIAN)
       
   199 /* FIXME: Change to use something other than ASSERT to avoid this conflict with the underlying platform */
       
   200 #undef ASSERT
       
   201 #endif
       
   202 
       
   203 #if PLATFORM(BREWMP)
       
   204 /* FIXME: We include this here only to avoid a conflict with the COMPILE_ASSERT macro. */
       
   205 #include <AEEClassIDs.h>
       
   206 
       
   207 /* FIXME: Change to use something other than COMPILE_ASSERT to avoid this conflict with the underlying platform */
       
   208 #undef COMPILE_ASSERT
       
   209 #endif
       
   210 
       
   211 #if ASSERT_DISABLED
       
   212 
       
   213 #define ASSERT(assertion) ((void)0)
       
   214 #define ASSERT_NOT_REACHED() ((void)0)
       
   215 #define ASSERT_UNUSED(variable, assertion) ((void)variable)
       
   216 
       
   217 #else
       
   218 
       
   219 #define ASSERT(assertion) do \
       
   220     if (!(assertion)) { \
       
   221         WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion); \
       
   222         CRASH(); \
       
   223     } \
       
   224 while (0)
       
   225 
       
   226 #define ASSERT_NOT_REACHED() do { \
       
   227     WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, 0); \
       
   228     CRASH(); \
       
   229 } while (0)
       
   230 
       
   231 #define ASSERT_UNUSED(variable, assertion) ASSERT(assertion)
       
   232 
       
   233 #endif
       
   234 
       
   235 /* ASSERT_WITH_MESSAGE */
       
   236 
       
   237 #if COMPILER(MSVC7_OR_LOWER)
       
   238 #define ASSERT_WITH_MESSAGE(assertion) ((void)0)
       
   239 #elif COMPILER(WINSCW)
       
   240 #define ASSERT_WITH_MESSAGE(assertion, arg...) ((void)0)
       
   241 #elif ASSERT_MSG_DISABLED
       
   242 #define ASSERT_WITH_MESSAGE(assertion, ...) ((void)0)
       
   243 #else
       
   244 #define ASSERT_WITH_MESSAGE(assertion, ...) do \
       
   245     if (!(assertion)) { \
       
   246         WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
       
   247         CRASH(); \
       
   248     } \
       
   249 while (0)
       
   250 #endif
       
   251                         
       
   252                         
       
   253 /* ASSERT_ARG */
       
   254 
       
   255 #if ASSERT_ARG_DISABLED
       
   256 
       
   257 #define ASSERT_ARG(argName, assertion) ((void)0)
       
   258 
       
   259 #else
       
   260 
       
   261 #define ASSERT_ARG(argName, assertion) do \
       
   262     if (!(assertion)) { \
       
   263         WTFReportArgumentAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #argName, #assertion); \
       
   264         CRASH(); \
       
   265     } \
       
   266 while (0)
       
   267 
       
   268 #endif
       
   269 
       
   270 /* COMPILE_ASSERT */
       
   271 #ifndef COMPILE_ASSERT
       
   272 #define COMPILE_ASSERT(exp, name) typedef int dummy##name [(exp) ? 1 : -1]
       
   273 #endif
       
   274 
       
   275 /* FATAL */
       
   276 
       
   277 #if COMPILER(MSVC7_OR_LOWER)
       
   278 #define FATAL() ((void)0)
       
   279 #elif COMPILER(WINSCW)
       
   280 #define FATAL(arg...) ((void)0)
       
   281 #elif FATAL_DISABLED
       
   282 #define FATAL(...) ((void)0)
       
   283 #else
       
   284 #define FATAL(...) do { \
       
   285     WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__); \
       
   286     CRASH(); \
       
   287 } while (0)
       
   288 #endif
       
   289 
       
   290 /* LOG_ERROR */
       
   291 
       
   292 #if COMPILER(MSVC7_OR_LOWER)
       
   293 #define LOG_ERROR() ((void)0)
       
   294 #elif COMPILER(WINSCW)
       
   295 #define LOG_ERROR(arg...)  ((void)0)
       
   296 #elif ERROR_DISABLED
       
   297 #define LOG_ERROR(...) ((void)0)
       
   298 #else
       
   299 #define LOG_ERROR(...) WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__)
       
   300 #endif
       
   301 
       
   302 /* LOG */
       
   303 
       
   304 #if COMPILER(MSVC7_OR_LOWER)
       
   305 #define LOG() ((void)0)
       
   306 #elif COMPILER(WINSCW)
       
   307 #define LOG(arg...) ((void)0)
       
   308 #elif LOG_DISABLED
       
   309 #define LOG(channel, ...) ((void)0)
       
   310 #else
       
   311 #define LOG(channel, ...) WTFLog(&JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
       
   312 #define JOIN_LOG_CHANNEL_WITH_PREFIX(prefix, channel) JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel)
       
   313 #define JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel) prefix ## channel
       
   314 #endif
       
   315 
       
   316 /* LOG_VERBOSE */
       
   317 
       
   318 #if COMPILER(MSVC7_OR_LOWER)
       
   319 #define LOG_VERBOSE(channel) ((void)0)
       
   320 #elif COMPILER(WINSCW)
       
   321 #define LOG_VERBOSE(channel, arg...) ((void)0)
       
   322 #elif LOG_DISABLED
       
   323 #define LOG_VERBOSE(channel, ...) ((void)0)
       
   324 #else
       
   325 #define LOG_VERBOSE(channel, ...) WTFLogVerbose(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, &JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
       
   326 #endif
       
   327 
       
   328 #endif /* WTF_Assertions_h */