assert.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /* This header file wraps newlib's own unmodified assert.h and adds
  7. support for silent assertion failure.
  8. */
  9. #pragma once
  10. #include <sdkconfig.h>
  11. #include <stdlib.h>
  12. #include <stdint.h>
  13. #include_next <assert.h>
  14. /* moved part of libc provided assert to here allows
  15. * tweaking the assert macro to use __builtin_expect()
  16. * and reduce jumps in the "asserts OK" code path
  17. *
  18. * Note: using __builtin_expect() not likely() to avoid defining the likely
  19. * macro in namespace of non-IDF code that may include this standard header.
  20. */
  21. #undef assert
  22. /* __FILENAME__ points to the file name instead of path + filename
  23. * e.g __FILE__ points to "/apps/test.c" where as __FILENAME__ points to "test.c"
  24. */
  25. #define __FILENAME__ (__builtin_strrchr( "/" __FILE__, '/') + 1)
  26. #if defined(NDEBUG)
  27. #define assert(__e) ((void)(__e))
  28. #elif CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT
  29. #define assert(__e) (__builtin_expect(!!(__e), 1) ? (void)0 : __assert_func(NULL, 0, NULL, NULL))
  30. #else // !CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT
  31. #define assert(__e) (__builtin_expect(!!(__e), 1) ? (void)0 : __assert_func (__FILENAME__, __LINE__, \
  32. __ASSERT_FUNC, #__e))
  33. #endif