-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathassert.h
124 lines (110 loc) · 4.12 KB
/
assert.h
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <executorch/runtime/platform/abort.h>
#include <executorch/runtime/platform/compiler.h>
#include <executorch/runtime/platform/log.h>
/**
* Assertion failure message emit method.
*
* @param[in] _format Printf-style message format string.
* @param[in] ... Format string arguments.
*/
#define ET_ASSERT_MESSAGE_EMIT(_format, ...) \
ET_LOG( \
Fatal, \
"In function %s(), assert failed" _format, \
ET_FUNCTION, \
##__VA_ARGS__)
/**
* Abort the runtime if the condition is not true.
* This check will be performed even in release builds.
*
* @param[in] _cond Condition asserted as true.
* @param[in] _format Printf-style message format string.
* @param[in] ... Format string arguments.
*/
#define ET_CHECK_MSG(_cond, _format, ...) \
do { \
if ET_UNLIKELY (!(_cond)) { \
ET_ASSERT_MESSAGE_EMIT(" (%s): " _format, #_cond, ##__VA_ARGS__); \
::executorch::runtime::runtime_abort(); \
} \
} while (0)
/**
* Abort the runtime if the condition is not true.
* This check will be performed even in release builds.
*
* @param[in] _cond Condition asserted as true.
*/
#define ET_CHECK(_cond) \
do { \
if ET_UNLIKELY (!(_cond)) { \
ET_ASSERT_MESSAGE_EMIT(": %s", #_cond); \
::executorch::runtime::runtime_abort(); \
} \
} while (0)
#ifdef NDEBUG
/**
* Abort the runtime if the condition is not true.
* This check will be performed in debug builds, but not release builds.
*
* @param[in] _cond Condition asserted as true.
* @param[in] _format Printf-style message format string.
* @param[in] ... Format string arguments.
*/
#define ET_DCHECK_MSG(_cond, _format, ...) ((void)0)
/**
* Abort the runtime if the condition is not true.
* This check will be performed in debug builds, but not release builds.
*
* @param[in] _cond Condition asserted as true.
*/
#define ET_DCHECK(_cond) ((void)0)
#define ET_DEBUG_ONLY [[maybe_unused]]
#else // NDEBUG
/**
* Abort the runtime if the condition is not true.
* This check will be performed in debug builds, but not release builds.
*
* @param[in] _cond Condition asserted as true.
* @param[in] _format Printf-style message format string.
* @param[in] ... Format string arguments.
*/
#define ET_DCHECK_MSG(_cond, _format, ...) \
ET_CHECK_MSG(_cond, _format, ##__VA_ARGS__)
/**
* Abort the runtime if the condition is not true.
* This check will be performed in debug builds, but not release builds.
*
* @param[in] _cond Condition asserted as true.
*/
#define ET_DCHECK(_cond) ET_CHECK(_cond)
#define ET_DEBUG_ONLY
#endif // NDEBUG
/**
* Assert that this code location is unreachable during execution.
*/
#define ET_ASSERT_UNREACHABLE() \
do { \
ET_CHECK_MSG(false, "Execution should not reach this point"); \
ET_UNREACHABLE(); \
} while (0)
/**
* Assert that this code location is unreachable during execution.
*
* @param[in] _message Message on how to avoid this assertion error.
*/
#define ET_ASSERT_UNREACHABLE_MSG(_format, ...) \
do { \
ET_CHECK_MSG( \
false, \
"Execution should not reach this point. " _format, \
##__VA_ARGS__); \
ET_UNREACHABLE(); \
} while (0)