From 7b565e4b2512ad2e02ca2838187e035c0425e457 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 12 Jul 2017 12:57:44 +0800 Subject: [PATCH] assertions: Add "silent" option Reduces assertion codesize overhead by approximately 35% --- Kconfig | 37 +++++++++++++++++---- components/newlib/component.mk | 2 +- components/newlib/platform_include/assert.h | 28 ++++++++++++++++ make/project.mk | 2 +- 4 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 components/newlib/platform_include/assert.h diff --git a/Kconfig b/Kconfig index bac2f8c63..9611cf773 100644 --- a/Kconfig +++ b/Kconfig @@ -47,14 +47,39 @@ config OPTIMIZATION_LEVEL_RELEASE bool "Release (-Os)" endchoice -config OPTIMIZATION_ASSERTIONS - prompt "Enable assertions" - bool - default y +choice OPTIMIZATION_ASSERTION_LEVEL + prompt "Assertion level" + default OPTIMIZATION_ASSERTIONS_ENABLED help - Enable assertions. + Assertions can be: + - Enabled. Failure will print verbose assertion details. This is the default. - If disabled, -DNDEBUG is added to CFLAGS. + - Set to "silent" to save code size (failed assertions will abort() but user + needs to use the aborting address to find the line number with the failed assertion.) + + - Disabled entirely (not recommended for most configurations.) -DNDEBUG is added + to CPPFLAGS in this case. + +config OPTIMIZATION_ASSERTIONS_ENABLED + prompt "Enabled" + bool + help + Enable assertions. Assertion content and line number will be printed on failure. + +config OPTIMIZATION_ASSERTIONS_SILENT + prompt "Silent (saves code size)" + bool + help + Enable silent assertions. Failed assertions will abort(), user needs to + use the aborting address to find the line number with the failed assertion. + +config OPTIMIZATION_ASSERTIONS_DISABLED + prompt "Disabled (sets -DNDEBUG)" + bool + help + If assertions are disabled, -DNDEBUG is added to CPPFLAGS. + +endchoice # assertions endmenu # Optimization level diff --git a/components/newlib/component.mk b/components/newlib/component.mk index 8715c475a..d6b73f944 100644 --- a/components/newlib/component.mk +++ b/components/newlib/component.mk @@ -11,4 +11,4 @@ COMPONENT_ADD_LDFLAGS := $(LIBC_PATH) $(LIBM_PATH) -lnewlib COMPONENT_ADD_LINKER_DEPS := $(LIBC_PATH) $(LIBM_PATH) -COMPONENT_ADD_INCLUDEDIRS := include platform_include +COMPONENT_ADD_INCLUDEDIRS := platform_include include diff --git a/components/newlib/platform_include/assert.h b/components/newlib/platform_include/assert.h new file mode 100644 index 000000000..afbea986e --- /dev/null +++ b/components/newlib/platform_include/assert.h @@ -0,0 +1,28 @@ +// Copyright 2017 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +/* This header file wraps newlib's own unmodified assert.h and adds + support for silent assertion failure. +*/ +#pragma once +#include +#include + +#include_next + +#if defined(CONFIG_OPTIMIZATION_ASSERTIONS_SILENT) && !defined(NDEBUG) +#undef assert +#define assert(__e) ((__e) ? (void)0 : abort()) +#endif diff --git a/make/project.mk b/make/project.mk index a0779b464..93529b8b6 100644 --- a/make/project.mk +++ b/make/project.mk @@ -239,7 +239,7 @@ else OPTIMIZATION_FLAGS = -Og endif -ifeq ("$(CONFIG_OPTIMIZATION_ASSERTIONS)", "") +ifeq ("$(CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED)", "y") CPPFLAGS += -DNDEBUG endif