1 /* 2 * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 #ifndef __ESP_ASSERT_H__ 7 #define __ESP_ASSERT_H__ 8 9 #include "assert.h" 10 11 /* Since IDF v5.0, C17 standard is used, which supports both _Static_assert and static_assert syntax */ 12 #define ESP_STATIC_ASSERT(EXPR, MSG...) 13 14 /* Assert at compile time if possible, runtime otherwise */ 15 #ifndef __cplusplus 16 /* __builtin_choose_expr() is only in C, makes this a lot cleaner */ 17 #define TRY_STATIC_ASSERT(CONDITION, MSG) do { \ 18 ESP_STATIC_ASSERT(__builtin_choose_expr(__builtin_constant_p(CONDITION), (CONDITION), 1), #MSG); \ 19 assert(#MSG && (CONDITION)); \ 20 } while(0) 21 #else 22 /* for C++, use __attribute__((error)) - works almost as well as ESP_STATIC_ASSERT */ 23 #define TRY_STATIC_ASSERT(CONDITION, MSG) do { \ 24 if (__builtin_constant_p(CONDITION) && !(CONDITION)) { \ 25 extern __attribute__((error(#MSG))) void failed_compile_time_assert(void); \ 26 failed_compile_time_assert(); \ 27 } \ 28 assert(#MSG && (CONDITION)); \ 29 } while(0) 30 #endif /* __cplusplus */ 31 32 #endif /* __ESP_ASSERT_H__ */ 33