Program Listing for File assert.h
↰ Return to documentation for file (include\utility\assert.h
)
/******************************************************************************
(c) Intel Corporation.
This software and the related documents are Intel copyrighted materials,
and your use of them is governed by the express license under which they
were provided to you ("License"). Unless the License provides otherwise,
you may not use, modify, copy, publish, distribute, disclose or transmit
this software or the related documents without Intel's prior written
permission.
This software and the related documents are provided as is, with no express
or implied warranties, other than those that are expressly stated in the
License.
******************************************************************************/
#include "instrumentation/gpa-checkret.h"
#include <vector>
#include <assert.h>
/*
assert.h replaces use of <assert> to enable better debugging.
Unlike <assert>, assert.h provides:
- Logging to STDOUT with file, line, and condition information.
- Always breaks debugger where the assertion fires.
- Instructs compiler where to optimize out code.
*/
// MSVC triggers warning for `do {} while(0)` statements but this can be bypassed via the "owl face".
// The "owl face" makes the compiler treat the condition as a sequence-operator by evaluating the second "0" and thus prevents
// the compiler from complaining the loop is being controlled by a constant.
#if defined(_MSC_VER)
#define GPA_ASSERT_LOOP_CONDITION (0, 0)
#pragma warning(disable : 4127)
#else
#define GPA_ASSERT_LOOP_CONDITION (false)
#endif
// Tell the compiler what code cannot be reached, ever.
#if defined(_MSC_VER)
#define GPA_UNREACHABLE() __assume(false)
#elif defined(__GNUC__) // GCC 4.8+, Clang, Intel, and GCC-compatible compilers.
#define GPA_UNREACHABLE() __builtin_unreachable()
#else
#error "Unsupported compiler."
#endif
// Casting any expression to void tricks the compiler to thinking its always being used.
#define GPA_UNUSED(expr) (void)expr
#if defined(_MSC_VER)
extern void __cdecl __debugbreak(void);
#define GPA_DEBUG_BREAKPOINT() __debugbreak();
#elif defined(__i386__) || defined(__x86_64__)
#define GPA_DEBUG_BREAKPOINT() __asm__ __volatile__("int $3\n\t")
#else
#error "Unsupported architecture."
#endif
#ifndef GPA_ASSERT_RETURN
#ifdef _WIN32
#define GPA_ASSERT_RETURN 1
#else
#define GPA_ASSERT_RETURN 0
#endif
#endif
#if GPA_ASSERT_RETURN
#define GPA_ASSERT_CONDITIONAL_RETURN() GPA_EXIT_FUNCTION()
#else
#define GPA_ASSERT_CONDITIONAL_RETURN()
#endif
#define GPA_ASSERT_HELPER(cond) \
do { \
if (!(cond)) { \
GPA_REPORT("%s(%d): error: %s | \t %s\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond); \
GPA_ASSERT_CONDITIONAL_RETURN(); \
} \
} while (GPA_ASSERT_LOOP_CONDITION)
#define GPA_ASSERT_UNREACHABLE() \
do { \
GPA_ASSERT(GPA_ASSERT_LOOP_CONDITION && "Unreachable"); \
GPA_UNREACHABLE(); \
} while (GPA_ASSERT_LOOP_CONDITION)
#define GPA_ASSERT(condition) \
GPA_ASSERT_HELPER(condition)
// Short-hand macros.
#if !defined(ASSERT)
#define ASSERT GPA_ASSERT
#endif
#if !defined(UNREACHABLE)
#define UNREACHABLE GPA_ASSERT_UNREACHABLE
#endif
#if !defined(UNUSED)
#define UNUSED GPA_UNUSED
#endif