esp_assert.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef __ESP_ASSERT_H__
  14. #define __ESP_ASSERT_H__
  15. #include "assert.h"
  16. /* Assert at compile time if possible, runtime otherwise */
  17. #ifndef __cplusplus
  18. /* __builtin_choose_expr() is only in C, makes this a lot cleaner */
  19. #define TRY_STATIC_ASSERT(CONDITION, MSG) do { \
  20. _Static_assert(__builtin_choose_expr(__builtin_constant_p(CONDITION), (CONDITION), 1), #MSG); \
  21. assert(#MSG && (CONDITION)); \
  22. } while(0)
  23. #else
  24. /* for C++, use __attribute__((error)) - works almost as well as _Static_assert */
  25. #define TRY_STATIC_ASSERT(CONDITION, MSG) do { \
  26. if (__builtin_constant_p(CONDITION) && !(CONDITION)) { \
  27. extern __attribute__((error(#MSG))) void failed_compile_time_assert(void); \
  28. failed_compile_time_assert(); \
  29. } \
  30. assert(#MSG && (CONDITION)); \
  31. } while(0)
  32. #endif /* __cplusplus */
  33. #endif /* __ESP_ASSERT_H__ */