Commit | Line | Data |
---|---|---|
464ebc31 | 1 | /* |
0235b0db MJ |
2 | * SPDX-License-Identifier: MIT |
3 | * | |
98b15851 PP |
4 | * Copyright (c) 2018-2019 EfficiOS Inc. and Linux Foundation |
5 | * Copyright (c) 2018-2019 Philippe Proulx <pproulx@efficios.com> | |
464ebc31 PP |
6 | */ |
7 | ||
0235b0db MJ |
8 | #ifndef BABELTRACE_ASSERT_INTERNAL_H |
9 | #define BABELTRACE_ASSERT_INTERNAL_H | |
10 | ||
464ebc31 | 11 | #include <assert.h> |
91d81473 MJ |
12 | #include <glib.h> |
13 | ||
14 | #include "common/macros.h" | |
464ebc31 | 15 | |
3b3873c8 | 16 | BT_HIDDEN |
4ee41178 | 17 | extern void bt_common_assert_failed(const char *file, int line, |
98b15851 PP |
18 | const char *func, const char *assertion) |
19 | __attribute__((noreturn)); | |
4ee41178 | 20 | |
464ebc31 PP |
21 | /* |
22 | * Internal assertion (to detect logic errors on which the library user | |
98b15851 | 23 | * has no influence). Use BT_ASSERT_PRE*() or BT_ASSERT_POST*() to check |
1f9f5b4d PP |
24 | * preconditions or postconditions which must be directly or indirectly |
25 | * satisfied by the library user. | |
98b15851 PP |
26 | * |
27 | * BT_ASSERT() is enabled in both debug and non-debug modes. | |
464ebc31 | 28 | */ |
4ee41178 SM |
29 | #define BT_ASSERT(_cond) \ |
30 | do { \ | |
31 | if (!(_cond)) { \ | |
32 | bt_common_assert_failed(__FILE__, __LINE__, __func__, \ | |
91d81473 | 33 | G_STRINGIFY(_cond)); \ |
4ee41178 SM |
34 | } \ |
35 | } while (0) | |
464ebc31 PP |
36 | |
37 | /* | |
38 | * Marks a function as being only used within a BT_ASSERT() context. | |
39 | */ | |
98b15851 PP |
40 | #define BT_ASSERT_FUNC |
41 | ||
42 | #ifdef BT_DEBUG_MODE | |
43 | ||
44 | /* | |
45 | * Debug mode internal assertion. | |
46 | */ | |
47 | #define BT_ASSERT_DBG(_cond) BT_ASSERT(_cond) | |
48 | ||
d7da1f66 | 49 | /* |
98b15851 PP |
50 | * Marks a function as being only used within a BT_ASSERT_DBG() context. |
51 | */ | |
52 | #define BT_ASSERT_DBG_FUNC | |
53 | ||
54 | #else /* BT_DEBUG_MODE */ | |
55 | ||
56 | /* | |
57 | * When `BT_DEBUG_MODE` is _not_ defined, define BT_ASSERT_DBG() macro | |
58 | * to the following to trick the compiler into thinking that the | |
59 | * variable passed as condition to the assertion is used. This is to | |
60 | * prevent set-but-not-used warnings from the compiler when assertions | |
61 | * are disabled. The sizeof() operator also makes sure that the `_cond` | |
62 | * expression is not evaluated, thus preventing unwanted side effects. | |
d7da1f66 | 63 | * |
98b15851 PP |
64 | * In-depth explanation: |
65 | * https://stackoverflow.com/questions/37411809/how-to-elegantly-fix-this-unused-variable-warning/37412551#37412551 | |
d7da1f66 | 66 | */ |
98b15851 PP |
67 | # define BT_ASSERT_DBG(_cond) ((void) sizeof((void) (_cond), 0)) |
68 | # define BT_ASSERT_DBG_FUNC __attribute__((unused)) | |
69 | ||
464ebc31 PP |
70 | #endif /* BT_DEBUG_MODE */ |
71 | ||
72 | #endif /* BABELTRACE_ASSERT_INTERNAL_H */ |