esp_assert.h 1.6 KB

123456789101112131415161718192021222324252627282930313233
  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. #include "assert.h"
  9. /* Since IDF v5.0, C17 standard is used, which supports both _Static_assert and static_assert syntax */
  10. /* Please do not use `_Static_assert` for C++ compatibility */
  11. #define ESP_STATIC_ASSERT static_assert
  12. /* Assert at compile time if possible, runtime otherwise */
  13. #ifndef __cplusplus
  14. /* __builtin_choose_expr() is only in C, makes this a lot cleaner */
  15. #define TRY_STATIC_ASSERT(CONDITION, MSG) do { \
  16. ESP_STATIC_ASSERT(__builtin_choose_expr(__builtin_constant_p(CONDITION), (CONDITION), 1), #MSG); \
  17. assert(#MSG && (CONDITION)); \
  18. } while(0)
  19. #else
  20. /* for C++, use __attribute__((error)) - works almost as well as ESP_STATIC_ASSERT */
  21. #define TRY_STATIC_ASSERT(CONDITION, MSG) do { \
  22. if (__builtin_constant_p(CONDITION) && !(CONDITION)) { \
  23. extern __attribute__((error(#MSG))) void failed_compile_time_assert(void); \
  24. failed_compile_time_assert(); \
  25. } \
  26. assert(#MSG && (CONDITION)); \
  27. } while(0)
  28. #endif /* __cplusplus */
  29. #endif /* __ESP_ASSERT_H__ */