浏览代码

assertions: Add "silent" option

Reduces assertion codesize overhead by approximately 35%
Angus Gratton 8 年之前
父节点
当前提交
7b565e4b25
共有 4 个文件被更改,包括 60 次插入7 次删除
  1. 30 5
      Kconfig
  2. 1 1
      components/newlib/component.mk
  3. 28 0
      components/newlib/platform_include/assert.h
  4. 1 1
      make/project.mk

+ 30 - 5
Kconfig

@@ -47,14 +47,39 @@ config OPTIMIZATION_LEVEL_RELEASE
     bool "Release (-Os)"
 endchoice
 
-config OPTIMIZATION_ASSERTIONS
-    prompt "Enable assertions"
+choice OPTIMIZATION_ASSERTION_LEVEL
+    prompt "Assertion level"
+    default OPTIMIZATION_ASSERTIONS_ENABLED
+    help
+        Assertions can be:
+        - Enabled. Failure will print verbose assertion details. This is the default.
+
+        - 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
-    default y
     help
-        Enable assertions.
+        If assertions are disabled, -DNDEBUG is added to CPPFLAGS.
 
-        If disabled, -DNDEBUG is added to CFLAGS.
+endchoice # assertions
 
 endmenu # Optimization level
 

+ 1 - 1
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

+ 28 - 0
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 <sdkconfig.h>
+#include <stdlib.h>
+
+#include_next <assert.h>
+
+#if defined(CONFIG_OPTIMIZATION_ASSERTIONS_SILENT) && !defined(NDEBUG)
+#undef assert
+#define assert(__e) ((__e) ? (void)0 : abort())
+#endif

+ 1 - 1
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