summaryrefslogtreecommitdiff
path: root/include/libc/assert.h
blob: de54b8a79ca809679bfa2ce90194a55537e58692 (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
#ifndef ASSERT_H
#define ASSERT_H

#if !defined(__GNUC__) && !defined(__attribute__)
#define __attribute__(x)
#endif

// Runtime assertions

__attribute__((noreturn)) void __assert(const char* assertion, const char* file, int line);

// assert for matching
#ifndef NDEBUG
# ifndef NON_MATCHING
#  define ASSERT(cond, msg, file, line) ((cond) ? ((void)0) : __assert(msg, file, line))
# else
#  define ASSERT(cond, msg, file, line) ((cond) ? ((void)0) : __assert(#cond, __FILE__, __LINE__))
# endif
#else
# define ASSERT(cond, msg, file, line) ((void)0)
#endif

// standard assert macro
#ifndef NDEBUG
# define assert(cond) ASSERT(cond, #cond, __FILE__, __LINE__)
#else
# define assert(cond) ((void)0)
#endif

// Static/compile-time assertions

#if !defined(__sgi) && (__GNUC__ >= 5 || __STDC_VERSION__ >= 201112L)
# define static_assert(cond, msg) _Static_assert(cond, msg)
#else
# ifndef GLUE
#  define GLUE(a, b) a##b
# endif
# ifndef GLUE2
#  define GLUE2(a, b) GLUE(a, b)
# endif

# define static_assert(cond, msg) typedef char GLUE2(static_assertion_failed, __LINE__)[(cond) ? 1 : -1]
#endif

#endif