assert.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 INRIA
3  * Copyright (C) 2016 Freie Universität Berlin
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
10 #pragma once
11 
24 #include <stdint.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #ifdef DOXYGEN
47 # define DEBUG_ASSERT_VERBOSE
48 
61 # define DEBUG_ASSERT_BREAKPOINT
62 #else
63 /* we should not include custom headers in standard headers */
64 # define _likely(x) __builtin_expect((uintptr_t)(x), 1)
65 #endif
66 
67 #ifndef __NORETURN
68 # if defined(__GNUC__) || defined(DOXYGEN)
76 # define __NORETURN __attribute__((noreturn))
77 # else
78 # define __NORETURN
79 # endif
80 #endif
81 
93 
104 __NORETURN void _assert_failure(const char *file, unsigned line);
105 
106 #ifdef NDEBUG
107 # define assert(ignore)((void)0)
108 #elif defined(DEBUG_ASSERT_VERBOSE)
146 # define assert(cond) (_likely(cond) ? (void)0 : _assert_failure(__FILE__, __LINE__))
147 #else /* DEBUG_ASSERT_VERBOSE */
148 # define assert(cond) (_likely(cond) ? (void)0 : _assert_panic())
149 #endif /* DEBUG_ASSERT_VERBOSE */
150 
151 #if !defined __cplusplus
152 # if __STDC_VERSION__ >= 201112L
156 # define static_assert(...) _Static_assert(__VA_ARGS__)
157 # else
163 # define static_assert(cond, ...) \
164  { enum { static_assert_failed_on_div_by_0 = 1 / (!!(cond)) }; }
165 # endif
166 #endif
167 
173 #ifndef DEBUG_ASSERT_NO_PANIC
174 # define DEBUG_ASSERT_NO_PANIC (1)
175 #endif
176 
177 #ifdef __cplusplus
178 }
179 #endif
180 
__NORETURN void _assert_failure(const char *file, unsigned line)
Function to handle failed assertion.
__NORETURN void _assert_panic(void)
Internal function to trigger a panic with a failed assertion as identifying cause.
#define __NORETURN
Same as NORETURN.
Definition: assert.h:76