1 /* 2 * Copyright (c) Meta Platforms, Inc. and affiliates. 3 * All rights reserved. 4 * 5 * This source code is licensed under the BSD-style license found in the 6 * LICENSE file in the root directory of this source tree. 7 */ 8 9 #pragma once 10 11 #include <executorch/runtime/platform/abort.h> 12 #include <executorch/runtime/platform/compiler.h> 13 #include <executorch/runtime/platform/log.h> 14 15 /** 16 * Assertion failure message emit method. 17 * 18 * @param[in] _format Printf-style message format string. 19 * @param[in] ... Format string arguments. 20 */ 21 #define ET_ASSERT_MESSAGE_EMIT(_format, ...) \ 22 ET_LOG( \ 23 Fatal, \ 24 "In function %s(), assert failed" _format, \ 25 ET_FUNCTION, \ 26 ##__VA_ARGS__) 27 28 /** 29 * Abort the runtime if the condition is not true. 30 * This check will be performed even in release builds. 31 * 32 * @param[in] _cond Condition asserted as true. 33 * @param[in] _format Printf-style message format string. 34 * @param[in] ... Format string arguments. 35 */ 36 #define ET_CHECK_MSG(_cond, _format, ...) \ 37 do { \ 38 if ET_UNLIKELY (!(_cond)) { \ 39 ET_ASSERT_MESSAGE_EMIT(" (%s): " _format, #_cond, ##__VA_ARGS__); \ 40 ::executorch::runtime::runtime_abort(); \ 41 } \ 42 } while (0) 43 44 /** 45 * Abort the runtime if the condition is not true. 46 * This check will be performed even in release builds. 47 * 48 * @param[in] _cond Condition asserted as true. 49 */ 50 #define ET_CHECK(_cond) \ 51 do { \ 52 if ET_UNLIKELY (!(_cond)) { \ 53 ET_ASSERT_MESSAGE_EMIT(": %s", #_cond); \ 54 ::executorch::runtime::runtime_abort(); \ 55 } \ 56 } while (0) 57 58 #ifdef NDEBUG 59 60 /** 61 * Abort the runtime if the condition is not true. 62 * This check will be performed in debug builds, but not release builds. 63 * 64 * @param[in] _cond Condition asserted as true. 65 * @param[in] _format Printf-style message format string. 66 * @param[in] ... Format string arguments. 67 */ 68 #define ET_DCHECK_MSG(_cond, _format, ...) ((void)0) 69 70 /** 71 * Abort the runtime if the condition is not true. 72 * This check will be performed in debug builds, but not release builds. 73 * 74 * @param[in] _cond Condition asserted as true. 75 */ 76 #define ET_DCHECK(_cond) ((void)0) 77 #define ET_DEBUG_ONLY [[maybe_unused]] 78 79 #else // NDEBUG 80 81 /** 82 * Abort the runtime if the condition is not true. 83 * This check will be performed in debug builds, but not release builds. 84 * 85 * @param[in] _cond Condition asserted as true. 86 * @param[in] _format Printf-style message format string. 87 * @param[in] ... Format string arguments. 88 */ 89 #define ET_DCHECK_MSG(_cond, _format, ...) \ 90 ET_CHECK_MSG(_cond, _format, ##__VA_ARGS__) 91 92 /** 93 * Abort the runtime if the condition is not true. 94 * This check will be performed in debug builds, but not release builds. 95 * 96 * @param[in] _cond Condition asserted as true. 97 */ 98 #define ET_DCHECK(_cond) ET_CHECK(_cond) 99 #define ET_DEBUG_ONLY 100 101 #endif // NDEBUG 102 103 /** 104 * Assert that this code location is unreachable during execution. 105 */ 106 #define ET_ASSERT_UNREACHABLE() \ 107 do { \ 108 ET_CHECK_MSG(false, "Execution should not reach this point"); \ 109 ET_UNREACHABLE(); \ 110 } while (0) 111 112 /** 113 * Assert that this code location is unreachable during execution. 114 * 115 * @param[in] _message Message on how to avoid this assertion error. 116 */ 117 #define ET_ASSERT_UNREACHABLE_MSG(_format, ...) \ 118 do { \ 119 ET_CHECK_MSG( \ 120 false, \ 121 "Execution should not reach this point. " _format, \ 122 ##__VA_ARGS__); \ 123 ET_UNREACHABLE(); \ 124 } while (0) 125