Path: blob/21.2-virgl/src/gallium/drivers/swr/rasterizer/common/swr_assert.h
4574 views
/****************************************************************************1* Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21****************************************************************************/2223#ifndef __SWR_ASSERT_H__24#define __SWR_ASSERT_H__2526#if !defined(__SWR_OS_H__)27#error swr_assert.h should not be included directly, please include "common/os.h" instead.28#endif2930//=============================================================================31//32// MACROS defined in this file:33//34// - SWR_ASSUME(expression, ...): Tell compiler that the expression is true.35// Helps with static code analysis as well.36// DO NOT USE if code after this dynamically37// checks for errors and handles them. The38// compiler may optimize out the error check.39//40// - SWR_ASSERT(expression, ...): Inform the user is expression is false.41// This check is only conditionally made,42// usually only in debug mode.43//44// - SWR_REL_ASSERT(expression, ...): Unconditionally enabled version of SWR_ASSERT45//46// - SWR_ASSUME_ASSERT(expression, ...): Conditionally enabled SWR_ASSERT. Uses47// SWR_ASSUME if SWR_ASSERT is disabled.48// DO NOT USE in combination with actual49// error checking (see SWR_ASSUME)50//51// - SWR_REL_ASSUME_ASSERT(expression, ...): Same as SWR_REL_ASSERT.52//53//=============================================================================5455// Stupid preprocessor tricks to avoid -Wall / -W4 warnings56#if defined(_MSC_VER)57#define _SWR_WARN_DISABLE __pragma(warning(push)) __pragma(warning(disable : 4127))58#define _SWR_WARN_RESTORE __pragma(warning(pop))59#else // ! MSVC compiler60#define _SWR_WARN_DISABLE61#define _SWR_WARN_RESTORE62#endif6364#define _SWR_MACRO_START \65do \66{67#define _SWR_MACRO_END \68_SWR_WARN_DISABLE \69} \70while (0) \71_SWR_WARN_RESTORE7273#if defined(_MSC_VER)74#define SWR_ASSUME(e, ...) \75_SWR_MACRO_START __assume(e); \76_SWR_MACRO_END77#elif defined(__clang__)78#define SWR_ASSUME(e, ...) \79_SWR_MACRO_START __builtin_assume(e); \80_SWR_MACRO_END81#elif defined(__GNUC__)82#define SWR_ASSUME(e, ...) \83_SWR_MACRO_START((e) ? ((void)0) : __builtin_unreachable()); \84_SWR_MACRO_END85#else86#define SWR_ASSUME(e, ...) \87_SWR_MACRO_START ASSUME(e); \88_SWR_MACRO_END89#endif9091#if !defined(SWR_ENABLE_ASSERTS)9293#if !defined(NDEBUG)94#define SWR_ENABLE_ASSERTS 195#else96#define SWR_ENABLE_ASSERTS 097#endif // _DEBUG9899#endif // SWR_ENABLE_ASSERTS100101#if !defined(SWR_ENABLE_REL_ASSERTS)102#define SWR_ENABLE_REL_ASSERTS 1103#endif104105#if SWR_ENABLE_ASSERTS || SWR_ENABLE_REL_ASSERTS106#include "assert.h"107108#if !defined(__cplusplus)109110#pragma message("C++ is required for SWR Asserts, falling back to assert.h")111112#if SWR_ENABLE_ASSERTS113#define SWR_ASSERT(e, ...) assert(e)114#endif115116#if SWR_ENABLE_REL_ASSERTS117#define SWR_REL_ASSERT(e, ...) assert(e)118#endif119120#else121122bool SwrAssert(bool chkDebugger,123bool& enabled,124const char* pExpression,125const char* pFileName,126uint32_t lineNum,127const char* function,128const char* pFmtString = nullptr,129...);130131void SwrTrace(132const char* pFileName, uint32_t lineNum, const char* function, const char* pFmtString, ...);133134#define _SWR_ASSERT(chkDebugger, e, ...) \135_SWR_MACRO_START \136bool expFailed = !(e); \137if (expFailed) \138{ \139static bool swrAssertEnabled = true; \140expFailed = SwrAssert( \141chkDebugger, swrAssertEnabled, #e, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \142if (expFailed) \143{ \144DEBUGBREAK; \145} \146} \147_SWR_MACRO_END148149#define _SWR_INVALID(chkDebugger, ...) \150_SWR_MACRO_START \151static bool swrAssertEnabled = true; \152bool expFailed = SwrAssert( \153chkDebugger, swrAssertEnabled, "", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \154if (expFailed) \155{ \156DEBUGBREAK; \157} \158_SWR_MACRO_END159160#define _SWR_TRACE(_fmtstr, ...) SwrTrace(__FILE__, __LINE__, __FUNCTION__, _fmtstr, ##__VA_ARGS__);161162#if SWR_ENABLE_ASSERTS163#define SWR_ASSERT(e, ...) _SWR_ASSERT(true, e, ##__VA_ARGS__)164#define SWR_ASSUME_ASSERT(e, ...) SWR_ASSERT(e, ##__VA_ARGS__)165#define SWR_TRACE(_fmtstr, ...) _SWR_TRACE(_fmtstr, ##__VA_ARGS__)166#endif // SWR_ENABLE_ASSERTS167168#if SWR_ENABLE_REL_ASSERTS169#define SWR_REL_ASSERT(e, ...) _SWR_ASSERT(false, e, ##__VA_ARGS__)170#define SWR_REL_ASSUME_ASSERT(e, ...) SWR_REL_ASSERT(e, ##__VA_ARGS__)171#define SWR_REL_TRACE(_fmtstr, ...) _SWR_TRACE(_fmtstr, ##__VA_ARGS__)172173// SWR_INVALID is always enabled174// Funky handling to allow 0 arguments with g++/gcc175// This is needed because you can't "swallow commas" with ##_VA_ARGS__ unless176// there is a first argument to the macro. So having a macro that can optionally177// accept 0 arguments is tricky.178#define _SWR_INVALID_0() _SWR_INVALID(false)179#define _SWR_INVALID_1(...) _SWR_INVALID(false, ##__VA_ARGS__)180#define _SWR_INVALID_VARGS_(_10, _9, _8, _7, _6, _5, _4, _3, _2, _1, N, ...) N181#define _SWR_INVALID_VARGS(...) _SWR_INVALID_VARGS_(__VA_ARGS__, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1)182#define _SWR_INVALID_VARGS_0() 1, 2, 3, 4, 5, 6, 7, 9, 9, 10183#define _SWR_INVALID_CONCAT_(a, b) a##b184#define _SWR_INVALID_CONCAT(a, b) _SWR_INVALID_CONCAT_(a, b)185#define SWR_INVALID(...) \186_SWR_INVALID_CONCAT(_SWR_INVALID_, _SWR_INVALID_VARGS(_SWR_INVALID_VARGS_0 __VA_ARGS__())) \187(__VA_ARGS__)188189#define SWR_STATIC_ASSERT(expression, ...) \190static_assert((expression), "Failed:\n " #expression "\n " __VA_ARGS__);191192#endif // SWR_ENABLE_REL_ASSERTS193194#endif // C++195196#endif // SWR_ENABLE_ASSERTS || SWR_ENABLE_REL_ASSERTS197198// Needed to allow passing bitfield members to sizeof() in disabled asserts199template <typename T>200static bool SwrSizeofWorkaround(T)201{202return false;203}204205#if !SWR_ENABLE_ASSERTS206#define SWR_ASSERT(e, ...) \207_SWR_MACRO_START(void) sizeof(SwrSizeofWorkaround(e)); \208_SWR_MACRO_END209#define SWR_ASSUME_ASSERT(e, ...) SWR_ASSUME(e, ##__VA_ARGS__)210#define SWR_TRACE(_fmtstr, ...) \211_SWR_MACRO_START(void)(0); \212_SWR_MACRO_END213#endif214215#if !SWR_ENABLE_REL_ASSERTS216#define SWR_REL_ASSERT(e, ...) \217_SWR_MACRO_START(void) sizeof(SwrSizeofWorkaround(e)); \218_SWR_MACRO_END219#define SWR_INVALID(...) \220_SWR_MACRO_START(void)(0); \221_SWR_MACRO_END222#define SWR_REL_ASSUME_ASSERT(e, ...) SWR_ASSUME(e, ##__VA_ARGS__)223#define SWR_REL_TRACE(_fmtstr, ...) \224_SWR_MACRO_START(void)(0); \225_SWR_MACRO_END226#define SWR_STATIC_ASSERT(e, ...) \227_SWR_MACRO_START(void) sizeof(SwrSizeofWorkaround(e)); \228_SWR_MACRO_END229#endif230231#if defined(_MSC_VER)232#define SWR_FUNCTION_DECL __FUNCSIG__233#elif (defined(__GNUC__) || defined(__clang__))234#define SWR_FUNCTION_DECL __PRETTY_FUNCTION__235#else236#define SWR_FUNCTION_DECL __FUNCTION__237#endif238239#define SWR_NOT_IMPL SWR_INVALID("%s not implemented", SWR_FUNCTION_DECL)240241#endif //__SWR_ASSERT_H__242243244