From ff8265b6b3df8e3c9d424385a33a90d251574217 Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Thu, 15 Aug 2024 16:40:19 +0800 Subject: [PATCH] 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 --- Kconfig | 15 +++++++++++++++ components/newlib/platform_include/assert.h | 4 ++++ .../system/startup/sdkconfig.ci.no_asserts | 2 ++ 3 files changed, 21 insertions(+) create mode 100644 tools/test_apps/system/startup/sdkconfig.ci.no_asserts diff --git a/Kconfig b/Kconfig index ca09ec94bc..2f04cac80c 100644 --- a/Kconfig +++ b/Kconfig @@ -388,6 +388,21 @@ mainmenu "Espressif IoT Development Framework Configuration" endchoice # assertions + config COMPILER_ASSERT_NDEBUG_EVALUATE + bool "Enable to evaluate the expression inside assert(X) when NDEBUG is set" + default y + help + When NDEBUG is set, assert(X) will not cause code to trigger an assertion. + With this option set assert(X) will still evaluate the expression X, though + the result will never cause an assertion. This means that if X is a function + then the function will be called. + + This is not according to the standard, which states that the assert(X) should + be replaced with ((void)0) if NDEBUG is defined. + + In ESP-IDF v6.0 the default behavior will change to "no" to be inline with the + standard. + choice COMPILER_FLOAT_LIB_FROM prompt "Compiler float lib source" default COMPILER_FLOAT_LIB_FROM_RVFPLIB if ESP_ROM_HAS_RVFPLIB diff --git a/components/newlib/platform_include/assert.h b/components/newlib/platform_include/assert.h index 72a4d2149b..1834290fc6 100644 --- a/components/newlib/platform_include/assert.h +++ b/components/newlib/platform_include/assert.h @@ -30,7 +30,11 @@ #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 diff --git a/tools/test_apps/system/startup/sdkconfig.ci.no_asserts b/tools/test_apps/system/startup/sdkconfig.ci.no_asserts new file mode 100644 index 0000000000..6f843adfec --- /dev/null +++ b/tools/test_apps/system/startup/sdkconfig.ci.no_asserts @@ -0,0 +1,2 @@ +CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE=y +CONFIG_COMPILER_ASSERT_NDEBUG_EVALUATE=n