esp_assert.h 1.4 KB

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