esp-idf/components/newlib/platform_include/assert.h
Marius Vikhammer ff8265b6b3 feat(newlib): add option to disable eval of expression in assert() when NDEBUG set
According to the standard assert(X) should be replaced by a void expression when
NDEBUG is set. IDF's behavior was to not trigger an assertion, but we would still
evaluate X, e.g. if X was a function it would be ran.

This MR adds a kconfig option CONFIG_COMPILER_ASSERT_NDEBUG_EVALUATE which allows us
revert the behavior to be inline with the standard.

With IDF v6.0 the plan is to make CONFIG_COMPILER_ASSERT_NDEBUG_EVALUATE=n the default
behavior.

Closes https://github.com/espressif/esp-idf/issues/10136
Closes https://github.com/espressif/esp-idf/issues/2758
2024-08-20 09:26:54 +08:00

48 lines
1.5 KiB
C

/*
* SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/* This header file wraps newlib's own unmodified assert.h and adds
support for silent assertion failure.
*/
#pragma once
#include <sdkconfig.h>
#include <stdlib.h>
#include <stdint.h>
#include_next <assert.h>
/* moved part of libc provided assert to here allows
* tweaking the assert macro to use __builtin_expect()
* and reduce jumps in the "asserts OK" code path
*
* Note: using __builtin_expect() not likely() to avoid defining the likely
* macro in namespace of non-IDF code that may include this standard header.
*/
#undef assert
/* __FILENAME__ points to the file name instead of path + filename
* e.g __FILE__ points to "/apps/test.c" where as __FILENAME__ points to "test.c"
*/
#define __FILENAME__ (__builtin_strrchr( "/" __FILE__, '/') + 1)
#if defined(NDEBUG)
#if CONFIG_COMPILER_ASSERT_NDEBUG_EVALUATE
#define assert(__e) ((void)(__e))
#else
#define assert(__e) ((void)0)
#endif //CONFIG_COMPILER_ASSERT_NDEBUG_EVALUATE
#elif CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT
#define assert(__e) (__builtin_expect(!!(__e), 1) ? (void)0 : __assert_func(NULL, 0, NULL, NULL))
#else // !CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT
#define assert(__e) (__builtin_expect(!!(__e), 1) ? (void)0 : __assert_func (__FILENAME__, __LINE__, \
__ASSERT_FUNC, #__e))
#endif