Move to kernel style SPDX license identifiers
[babeltrace.git] / src / ctf-writer / assert-pre.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
6 */
7
8 #ifndef BABELTRACE_CTF_WRITER_ASSERT_PRE_INTERNAL_H
9 #define BABELTRACE_CTF_WRITER_ASSERT_PRE_INTERNAL_H
10
11 /*
12 * The macros in this header use macros defined in "logging/log.h". We
13 * don't want this header to automatically include "logging/log.h"
14 * because you need to manually define BT_LOG_TAG before including
15 * "logging/log.h" and it is unexpected that you also need to define it
16 * before including this header.
17 *
18 * This is a reminder that in order to use "ctf-writer/assert-pre.h",
19 * you also need to use logging explicitly.
20 */
21
22 #ifndef BT_LOG_SUPPORTED
23 # error Include "logging/log.h" before this header.
24 #endif
25
26 #include <stdbool.h>
27 #include <stdlib.h>
28 #include <inttypes.h>
29 #include "common/macros.h"
30
31 #ifdef BT_DEV_MODE
32 /*
33 * Prints the details of an unsatisfied precondition without immediately
34 * aborting. You should use this within a function which checks
35 * preconditions, but which is called from a BT_CTF_ASSERT_PRE()
36 * context, so that the function can still return its result for
37 * BT_CTF_ASSERT_PRE() to evaluate it.
38 *
39 * Example:
40 *
41 * BT_CTF_ASSERT_PRE_FUNC
42 * static inline bool check_complex_precond(...)
43 * {
44 * ...
45 *
46 * if (...) {
47 * BT_CTF_ASSERT_PRE_MSG("Invalid object: ...", ...);
48 * return false;
49 * }
50 *
51 * ...
52 * }
53 *
54 * ...
55 *
56 * BT_CTF_ASSERT_PRE(check_complex_precond(...),
57 * "Precondition is not satisfied: ...", ...);
58 */
59 # define BT_CTF_ASSERT_PRE_MSG(_fmt, ...) \
60 do { \
61 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION, __FILE__, \
62 __LINE__, BT_LOG_FATAL, BT_LOG_TAG, (_fmt), \
63 ##__VA_ARGS__); \
64 } while (0)
65
66 /*
67 * Asserts that the library precondition _cond is satisfied.
68 *
69 * If _cond is false, log a fatal statement using _fmt and the optional
70 * arguments using BT_LOGF(), and abort.
71 *
72 * To assert that a postcondition is satisfied or that some internal
73 * object/context/value is in the expected state, use BT_ASSERT_DBG().
74 */
75 # define BT_CTF_ASSERT_PRE(_cond, _fmt, ...) \
76 do { \
77 if (!(_cond)) { \
78 BT_CTF_ASSERT_PRE_MSG("CTF writer precondition not satisfied; error is:"); \
79 BT_CTF_ASSERT_PRE_MSG((_fmt), ##__VA_ARGS__); \
80 BT_CTF_ASSERT_PRE_MSG("Aborting..."); \
81 bt_common_abort(); \
82 } \
83 } while (0)
84
85 /*
86 * Marks a function as being only used within a BT_CTF_ASSERT_PRE() context.
87 */
88 # define BT_CTF_ASSERT_PRE_FUNC
89
90 /*
91 * Prints the details of an unsatisfied precondition without immediately
92 * aborting. You should use this within a function which checks
93 * preconditions, but which is called from a BT_CTF_ASSERT_PRE() context, so
94 * that the function can still return its result for BT_CTF_ASSERT_PRE() to
95 * evaluate it.
96 *
97 * Example:
98 *
99 * BT_CTF_ASSERT_PRE_FUNC
100 * static inline bool check_complex_precond(...)
101 * {
102 * ...
103 *
104 * if (...) {
105 * BT_CTF_ASSERT_PRE_MSG("Invalid object: ...", ...);
106 * return false;
107 * }
108 *
109 * ...
110 * }
111 *
112 * ...
113 *
114 * BT_CTF_ASSERT_PRE(check_complex_precond(...),
115 * "Precondition is not satisfied: ...", ...);
116 */
117 #else
118 # define BT_CTF_ASSERT_PRE(_cond, _fmt, ...) ((void) sizeof((void) (_cond), 0))
119 # define BT_CTF_ASSERT_PRE_FUNC __attribute__((unused))
120 # define BT_CTF_ASSERT_PRE_MSG(_fmt, ...)
121 #endif /* BT_DEV_MODE */
122
123 /*
124 * Developer mode: asserts that a given variable is not NULL.
125 */
126 #define BT_CTF_ASSERT_PRE_NON_NULL(_obj, _obj_name) \
127 BT_CTF_ASSERT_PRE((_obj), "%s is NULL: ", _obj_name)
128
129 /*
130 * Developer mode: asserts that a given object is NOT frozen. This macro
131 * checks the `frozen` field of _obj.
132 */
133 #define BT_CTF_ASSERT_PRE_HOT(_obj, _obj_name, _fmt, ...) \
134 BT_CTF_ASSERT_PRE(!(_obj)->frozen, "%s is frozen" _fmt, _obj_name, \
135 ##__VA_ARGS__)
136
137 /*
138 * Developer mode: asserts that a given index is less than a given size.
139 */
140 #define BT_CTF_ASSERT_PRE_VALID_INDEX(_index, _length) \
141 BT_CTF_ASSERT_PRE((_index) < (_length), \
142 "Index is out of bounds: index=%" PRIu64 ", " \
143 "count=%" PRIu64, (uint64_t) (_index), (uint64_t) (_length))
144
145 #endif /* BABELTRACE_CTF_WRITER_ASSERT_PRE_INTERNAL_H */
This page took 0.031538 seconds and 4 git commands to generate.