summaryrefslogtreecommitdiff
path: root/src/debug.h
blob: 4cd1b5835148948f36614d7693d47c416211991c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#pragma once

/* SPDX-License-Identifier: Unlicense
 */

#ifdef NDEBUG
#define __DEBUG 0
#else
#define __DEBUG 1
#endif

#ifndef __TRACE
#define __TRACE 0
#endif

#define TRACE(format, ...) \
  if (__TRACE) fprintf(stderr, "%s:%d %s: " format "\n", \
          __FILE__, __LINE__, __func__ __VA_OPT__(,) __VA_ARGS__)

#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__)
#define __SANITIZER_PRINT_STACK_TRACE() __sanitizer_print_stack_trace()
#include <sanitizer/common_interface_defs.h>
#else
#define __SANITIZER_PRINT_STACK_TRACE()
#endif

#if __DEBUG
#define ASSERT(x) ((x) ? (void)(x) : (__SANITIZER_PRINT_STACK_TRACE(), assert(x)))
#define ASSERT_CONSUME(x) ASSERT(x)
#define UNREACHABLE_BODY() (ASSERT(!"unreachable code reached"), abort())
#else
#define ASSERT(x) ((void)0)
#define ASSERT_CONSUME(x) ((void)(x))
#define UNREACHABLE_BODY() ((void)0)
#endif

#include <cassert>

#ifdef __GNUC__
#define BUILTIN_UNREACHABLE() __builtin_unreachable()
#else
#define BUILTIN_UNREACHABLE() ((void)0)
#endif

#define UNREACHABLE() (UNREACHABLE_BODY(), BUILTIN_UNREACHABLE())