| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright (c) 2018-2019 EfficiOS Inc. and Linux Foundation |
| 5 | * Copyright (c) 2018-2019 Philippe Proulx <pproulx@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #ifndef BABELTRACE_COMMON_ASSERT_H |
| 9 | #define BABELTRACE_COMMON_ASSERT_H |
| 10 | |
| 11 | #include <assert.h> |
| 12 | #include <glib.h> |
| 13 | |
| 14 | #include "common/macros.h" |
| 15 | |
| 16 | #ifdef __cplusplus |
| 17 | extern "C" { |
| 18 | #endif |
| 19 | |
| 20 | extern void bt_common_assert_failed(const char *file, int line, |
| 21 | const char *func, const char *assertion) |
| 22 | __attribute__((noreturn)); |
| 23 | |
| 24 | /* |
| 25 | * Internal assertion (to detect logic errors on which the library user |
| 26 | * has no influence). Use BT_ASSERT_PRE*() or BT_ASSERT_POST*() to check |
| 27 | * preconditions or postconditions which must be directly or indirectly |
| 28 | * satisfied by the library user. |
| 29 | * |
| 30 | * BT_ASSERT() is enabled in both debug and non-debug modes. |
| 31 | */ |
| 32 | #define BT_ASSERT(_cond) \ |
| 33 | do { \ |
| 34 | if (!(_cond)) { \ |
| 35 | bt_common_assert_failed(__FILE__, __LINE__, __func__, \ |
| 36 | G_STRINGIFY(_cond)); \ |
| 37 | } \ |
| 38 | } while (0) |
| 39 | |
| 40 | /* |
| 41 | * Marks a function as being only used within a BT_ASSERT() context. |
| 42 | */ |
| 43 | #define BT_ASSERT_FUNC |
| 44 | |
| 45 | #ifdef BT_DEBUG_MODE |
| 46 | |
| 47 | /* |
| 48 | * Debug mode internal assertion. |
| 49 | */ |
| 50 | #define BT_ASSERT_DBG(_cond) BT_ASSERT(_cond) |
| 51 | |
| 52 | /* |
| 53 | * Marks a function as being only used within a BT_ASSERT_DBG() context. |
| 54 | */ |
| 55 | #define BT_ASSERT_DBG_FUNC |
| 56 | |
| 57 | #else /* BT_DEBUG_MODE */ |
| 58 | # define BT_ASSERT_DBG(_cond) BT_USE_EXPR(_cond) |
| 59 | # define BT_ASSERT_DBG_FUNC __attribute__((unused)) |
| 60 | #endif /* BT_DEBUG_MODE */ |
| 61 | |
| 62 | #ifdef __cplusplus |
| 63 | } |
| 64 | #endif |
| 65 | |
| 66 | #endif /* BABELTRACE_COMMON_ASSERT_H */ |