This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 562db43781f include/assert: stringify assert expressions at the first 
macro level
562db43781f is described below

commit 562db43781f5de6b76d69799010f5586512cf6e4
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Thu Nov 12 15:18:25 2026 +0800

    include/assert: stringify assert expressions at the first macro level
    
    The chain DEBUGASSERT(f) -> _ASSERT(f) -> #f macro-replaces f before the
    #f stringification (the DEBUGASSERT replacement list has no # or ##), so
    the assert strings held the expanded token sequences.  With
    __INT128_DEFINED, FLS() expands to both FLS64() and FLS128() (a full
    ternary tree each), so one DEBUGASSERT(nodesize >= MM_MIN_CHUNK) in
    mm/mm_heap/mm.h reached 67,165 bytes -- the +49 KB .rodata on
    qemu-intel64 with CONFIG_DEBUG_ASSERTIONS_EXPRESSION.
    
    Stringify at the first macro level instead: _ASSERT()/_VERIFY() take the
    expression string as a parameter, and the five assert entry points
    (DEBUGASSERT/DEBUGVERIFY/ASSERT/VERIFY/assert) pass #f themselves.  The
    strings now hold the source text, and the largest string in the
    qemu-intel64 image drops from 67,165 to 223 bytes.
    
    _ASSERT()/_VERIFY() gain a 4th parameter; the one in-tree caller outside
    assert.h (ostest's wdtest_assert() in nuttx-apps) is updated by a
    companion PR.
    
    Verified on qemu-intel64 with CONFIG_DEBUG_ASSERTIONS_EXPRESSION: the
    image links clean, strings -n 1000 finds no large string, and the assert
    strings read "nodesize >= MM_MIN_CHUNK".
    
    Assisted-By: mimo-v2.6-pro
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 include/assert.h | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/include/assert.h b/include/assert.h
index 65b17631e65..cbe7f68577d 100644
--- a/include/assert.h
+++ b/include/assert.h
@@ -96,12 +96,17 @@
 #define __VERIFY__(f, file, line, _f) \
   (predict_false((f) < 0)) ? __assert(file, line, _f) : ((void)0)
 
+/* The expression string is passed in so #f stringifies at the caller's
+ * first macro level; in a two-level chain the argument would be
+ * macro-expanded first and bloat the .rodata assert strings.
+ */
+
 #ifdef CONFIG_DEBUG_ASSERTIONS_EXPRESSION
-#  define _ASSERT(f,file,line) __ASSERT__(f, file, line, #f)
-#  define _VERIFY(f,file,line) __VERIFY__(f, file, line, #f)
+#  define _ASSERT(f,file,line,s) __ASSERT__(f, file, line, s)
+#  define _VERIFY(f,file,line,s) __VERIFY__(f, file, line, s)
 #else
-#  define _ASSERT(f,file,line) __ASSERT__(f, file, line, NULL)
-#  define _VERIFY(f,file,line) __VERIFY__(f, file, line, NULL)
+#  define _ASSERT(f,file,line,s) __ASSERT__(f, file, line, NULL)
+#  define _VERIFY(f,file,line,s) __VERIFY__(f, file, line, NULL)
 #endif
 
 #ifndef DEBUGPANIC
@@ -116,7 +121,7 @@
 #ifndef DEBUGASSERT
 #  ifdef CONFIG_DEBUG_ASSERTIONS
 #    define DEBUGASSERT(f) _ASSERT(f, __DEBUG_ASSERT_FILE__, \
-                                   __DEBUG_ASSERT_LINE__)
+                                   __DEBUG_ASSERT_LINE__, #f)
 #  else
 #    define DEBUGASSERT(f) ((void)(1 || (f)))
 #  endif
@@ -125,7 +130,7 @@
 #ifndef DEBUGVERIFY
 #  ifdef CONFIG_DEBUG_ASSERTIONS
 #    define DEBUGVERIFY(f) _VERIFY(f, __DEBUG_ASSERT_FILE__, \
-                                   __DEBUG_ASSERT_LINE__)
+                                   __DEBUG_ASSERT_LINE__, #f)
 #  else
 #    define DEBUGVERIFY(f) ((void)(f))
 #  endif
@@ -137,8 +142,8 @@
  */
 
 #ifndef NUTTX_NO_EXTENSIONS
-#define ASSERT(f) _ASSERT(f, __ASSERT_FILE__, __ASSERT_LINE__)
-#define VERIFY(f) _VERIFY(f, __ASSERT_FILE__, __ASSERT_LINE__)
+#define ASSERT(f) _ASSERT(f, __ASSERT_FILE__, __ASSERT_LINE__, #f)
+#define VERIFY(f) _VERIFY(f, __ASSERT_FILE__, __ASSERT_LINE__, #f)
 #endif
 
 /* Suppress 3rd party library redefine _assert/__assert */
@@ -236,6 +241,6 @@ void __assert(FAR const char *filename, int linenum,
 #  ifdef NDEBUG
 #    define assert(f) ((void)0)
 #  else
-#    define assert(f) _ASSERT(f, __ASSERT_FILE__, __ASSERT_LINE__)
+#    define assert(f) _ASSERT(f, __ASSERT_FILE__, __ASSERT_LINE__, #f)
 #  endif
 #endif

Reply via email to