Home | History | Annotate | Download | only in common
      1 /****************************************************************************
      2 * Copyright (C) 2014-2015 Intel Corporation.   All Rights Reserved.
      3 *
      4 * Permission is hereby granted, free of charge, to any person obtaining a
      5 * copy of this software and associated documentation files (the "Software"),
      6 * to deal in the Software without restriction, including without limitation
      7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8 * and/or sell copies of the Software, and to permit persons to whom the
      9 * Software is furnished to do so, subject to the following conditions:
     10 *
     11 * The above copyright notice and this permission notice (including the next
     12 * paragraph) shall be included in all copies or substantial portions of the
     13 * Software.
     14 *
     15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     21 * IN THE SOFTWARE.
     22 ****************************************************************************/
     23 
     24 #ifndef __SWR_ASSERT_H__
     25 #define __SWR_ASSERT_H__
     26 
     27 #if !defined(__SWR_OS_H__)
     28 #error swr_assert.h should not be included directly, please include "common/os.h" instead.
     29 #endif
     30 
     31 //=============================================================================
     32 //
     33 // MACROS defined in this file:
     34 //
     35 // - SWR_ASSUME(expression, ...):   Tell compiler that the expression is true.
     36 //                                  Helps with static code analysis as well.
     37 //                                  DO NOT USE if code after this dynamically
     38 //                                  checks for errors and handles them.  The
     39 //                                  compiler may optimize out the error check.
     40 //
     41 // - SWR_ASSERT(expression, ...):   Inform the user is expression is false.
     42 //                                  This check is only conditionally made,
     43 //                                  usually only in debug mode.
     44 //
     45 // - SWR_REL_ASSERT(expression, ...): Unconditionally enabled version of SWR_ASSERT
     46 //
     47 // - SWR_ASSUME_ASSERT(expression, ...): Conditionally enabled SWR_ASSERT.  Uses
     48 //                                       SWR_ASSUME if SWR_ASSERT is disabled.
     49 //                                       DO NOT USE in combination with actual
     50 //                                       error checking (see SWR_ASSUME)
     51 //
     52 // - SWR_REL_ASSUME_ASSERT(expression, ...): Same as SWR_REL_ASSERT.
     53 //
     54 //=============================================================================
     55 
     56 #if defined(_WIN32)
     57 #define SWR_ASSUME(e, ...) __assume(e)
     58 #elif defined(__clang__)
     59 #define SWR_ASSUME(e, ...) __builtin_assume(e)
     60 #elif defined(__GNUC__)
     61 #define SWR_ASSUME(e, ...) ((e) ? ((void)0) : __builtin_unreachable())
     62 #else
     63 #define SWR_ASSUME(e, ...) ASSUME(e)
     64 #endif
     65 
     66 #if !defined(SWR_ENABLE_ASSERTS)
     67 
     68 #if !defined(NDEBUG)
     69 #define SWR_ENABLE_ASSERTS 1
     70 #else
     71 #define SWR_ENABLE_ASSERTS 0
     72 #endif // _DEBUG
     73 
     74 #endif // SWR_ENABLE_ASSERTS
     75 
     76 #if !defined(SWR_ENABLE_REL_ASSERTS)
     77 #define SWR_ENABLE_REL_ASSERTS 1
     78 #endif
     79 
     80 #if SWR_ENABLE_ASSERTS || SWR_ENABLE_REL_ASSERTS
     81 #include "assert.h"
     82 
     83 #if !defined(__cplusplus)
     84 
     85 #pragma message("C++ is required for SWR Asserts, falling back to assert.h")
     86 
     87 #if SWR_ENABLE_ASSERTS
     88 #define SWR_ASSERT(e, ...) assert(e)
     89 #endif
     90 
     91 #if SWR_ENABLE_REL_ASSERTS
     92 #define SWR_REL_ASSERT(e, ...) assert(e)
     93 #endif
     94 
     95 #else
     96 
     97 bool SwrAssert(
     98     bool        chkDebugger,
     99     bool&       enabled,
    100     const char* pExpression,
    101     const char* pFileName,
    102     uint32_t    lineNum,
    103     const char* function,
    104     const char* pFmtString = nullptr,
    105     ...);
    106 
    107 void SwrTrace(
    108     const char* pFileName,
    109     uint32_t    lineNum,
    110     const char* function,
    111     const char* pFmtString,
    112     ...);
    113 
    114 #define _SWR_ASSERT(chkDebugger, e, ...) {\
    115     bool expFailed = !(e);\
    116     if (expFailed) {\
    117         static bool swrAssertEnabled = true;\
    118         expFailed = SwrAssert(chkDebugger, swrAssertEnabled, #e, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);\
    119         if (expFailed) { DEBUGBREAK; }\
    120     }\
    121 }
    122 
    123 #define _SWR_TRACE(_fmtstr, ...) \
    124     SwrTrace(__FILE__, __LINE__, __FUNCTION__, _fmtstr, ##__VA_ARGS__);
    125 
    126 #if SWR_ENABLE_ASSERTS
    127 #define SWR_ASSERT(e, ...)              _SWR_ASSERT(true, e, ##__VA_ARGS__)
    128 #define SWR_ASSUME_ASSERT(e, ...)       SWR_ASSERT(e, ##__VA_ARGS__)
    129 #define SWR_TRACE(_fmtstr, ...)         _SWR_TRACE(_fmtstr, ##__VA_ARGS__)
    130 
    131 #if defined(assert)
    132 #undef assert
    133 #endif
    134 #define assert(exp) SWR_ASSERT(exp)
    135 
    136 #endif // SWR_ENABLE_ASSERTS
    137 
    138 #if SWR_ENABLE_REL_ASSERTS
    139 #define SWR_REL_ASSERT(e, ...)          _SWR_ASSERT(false, e, ##__VA_ARGS__)
    140 #define SWR_REL_ASSUME_ASSERT(e, ...)   SWR_REL_ASSERT(e, ##__VA_ARGS__)
    141 #define SWR_REL_TRACE(_fmtstr, ...)     _SWR_TRACE(_fmtstr, ##__VA_ARGS__)
    142 #endif
    143 
    144 #endif // C++
    145 
    146 #endif // SWR_ENABLE_ASSERTS || SWR_ENABLE_REL_ASSERTS
    147 
    148 #if !SWR_ENABLE_ASSERTS
    149 #define SWR_ASSERT(e, ...)              (void)(0)
    150 #define SWR_ASSUME_ASSERT(e, ...)       SWR_ASSUME(e, ##__VA_ARGS__)
    151 #define SWR_TRACE(_fmtstr, ...)         (void)(0)
    152 #endif
    153 
    154 #if !SWR_ENABLE_REL_ASSERTS
    155 #define SWR_REL_ASSERT(e, ...)          (void)(0)
    156 #define SWR_REL_ASSUME_ASSERT(e, ...)   SWR_ASSUME(e, ##__VA_ARGS__)
    157 #define SWR_REL_TRACE(_fmtstr, ...)     (void)(0)
    158 #endif
    159 
    160 #define SWR_NOT_IMPL SWR_ASSERT(0, "%s not implemented", __FUNCTION__)
    161 
    162 #endif//__SWR_ASSERT_H__
    163