esp_assert.h 1.6 KB

1234567891011121314151617181920212223242526272829303132
  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. #define ESP_STATIC_ASSERT static_assert
  11. /* Assert at compile time if possible, runtime otherwise */
  12. #ifndef __cplusplus
  13. /* __builtin_choose_expr() is only in C, makes this a lot cleaner */
  14. #define TRY_STATIC_ASSERT(CONDITION, MSG) do { \
  15. ESP_STATIC_ASSERT(__builtin_choose_expr(__builtin_constant_p(CONDITION), (CONDITION), 1), #MSG); \
  16. assert(#MSG && (CONDITION)); \
  17. } while(0)
  18. #else
  19. /* for C++, use __attribute__((error)) - works almost as well as ESP_STATIC_ASSERT */
  20. #define TRY_STATIC_ASSERT(CONDITION, MSG) do { \
  21. if (__builtin_constant_p(CONDITION) && !(CONDITION)) { \
  22. extern __attribute__((error(#MSG))) void failed_compile_time_assert(void); \
  23. failed_compile_time_assert(); \
  24. } \
  25. assert(#MSG && (CONDITION)); \
  26. } while(0)
  27. #endif /* __cplusplus */
  28. #endif /* __ESP_ASSERT_H__ */