/* - | Copyright | ------------------------------------------------------------ Copyright (c) 2026 Randy Jordan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * --------------------------------------------------------------------------*/ #ifndef CI2_ASSERT_H #define CI2_ASSERT_H #include /* Force Inline */ #if defined(_MSC_VER) || defined(__INTEL__LLVM_COMPILER) #define CI2_INLINE __forceinline #elif defined(__GNUC__) || defined(__clang__) #define CI2_INLINE __attribute__((always_inline)) inline #else #define CI2_INLINE inline #endif /* Use __has_builtin if available (Clang and newer GCC). */ #if defined(__has_builtin) /* Check builtins in preferred order */ #if __has_builtin(__builtin_debugtrap) #define CI2_DEBUG_TRAP() __builtin_debugtrap() #elif __has_builtin(__builtin_trap) #define CI2_DEBUG_TRAP() __builtin_trap() #elif __has_builtin(__builtin_break) #define CI2_DEBUG_TRAP() __builtin_break() #endif #elif defined(_MSC_VER) || defined(__INTEL__LLVM_COMPILER) #define CI2_DEBUG_TRAP() __debugbreak() #else #define CI2_DEBUG_TRAP() (*(volatile int*)0 = 0) #endif /* Static Asserts */ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */ #define CI2_STATIC_ASSERT(expr) _Static_assert((expr), #expr) #else /* Abuse a typedef of a negative-size array */ #define CI2_STATIC_ASSERT(expr) typedef char _static_assert_[(expr) ? 1 : -1] #endif /* Current __func__ */ #if defined(__FUNC__) #define CI2_FUNC __FUNCTION__ #else #define CI2_FUNC __func__ #endif /* Assert Fail */ static CI2_INLINE void ci2_assert_fail(const char* expr, const char* msg, /* may be NULL */ const char* file, int line, const char* func) { if (msg) fprintf(stderr, "\n[ASSERT FAILED]\n" " Expression : %s\n" " Message : %s\n" " Location : %s:%d\n" " Function : %s\n\n", expr, msg, file, line, func); else fprintf(stderr, "\n[ASSERT FAILED]\n" " Expression : %s\n" " Location : %s:%d\n" " Function : %s\n\n", expr, file, line, func); /* Flush so the message is visible before we halt */ fflush(stderr); CI2_DEBUG_TRAP(); } #ifndef NDEBUG #define CI2_ASSERT(expr) \ do { \ if (!(expr)) { \ ci2_assert_fail(#expr, NULL, __FILE__, __LINE__, CI2_FUNC); \ } \ } while (0) #define CI2_ASSERT_MSG(expr, msg) \ do { \ if (!(expr)) { \ ci2_assert_fail(#expr, (msg), __FILE__, __LINE__, CI2_FUNC); \ } \ } while (0) #else /* NDEBUG — strip all runtime checks */ #define ASSERT(expr) ((void)(expr)) #define ASSERT_MSG(expr, msg) ((void)(expr)) #endif /* NDEBUG */ #endif // ci2_assert.h