Skip to content

Commit 6557657

Browse files
committed
debug/assert: decouple configuration of show file name feature
add new config CONFIG_ASSERTIONS_FILENAME to library call assert(3) to decouple with CONFIG_DEBUG_ASSERTIONS | Function |CONFIG | Show filename/line | | --- | --- | --- | |assert(), ASSERT()|CONFIG_ASSERTIONS_FILENAME=y | Yes | |assert(), ASSERT()|CONFIG_ASSERTIONS_FILENAME=n | No | |DEBUGASSERT() |CONFIG_DEBUG_ASSERTIONS_FILENAME=y| Yes | |DEBUGASSERT() |CONFIG_DEBUG_ASSERTIONS_FILENAME=n| No | Signed-off-by: chao an <anchao@xiaomi.com>
1 parent eed42a1 commit 6557657

File tree

2 files changed

+61
-40
lines changed

2 files changed

+61
-40
lines changed

Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,15 @@ config NDEBUG
638638
bool "Define NDEBUG globally"
639639
default !DEBUG_ASSERTIONS
640640

641+
config ASSERTIONS_FILENAME
642+
bool "Enable library call assert(3) show the file name"
643+
default y if DEBUG_ASSERTIONS_FILENAME
644+
default !DEFAULT_SMALL
645+
---help---
646+
This option can display the file information of the library call assert(3)
647+
function when it is enabled. This option maybe will take up a lot
648+
of space from applications.
649+
641650
config DEBUG_ALERT
642651
bool
643652
default n

include/assert.h

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -42,60 +42,68 @@
4242
#undef DEBUGASSERT /* Like ASSERT, but only if CONFIG_DEBUG_ASSERTIONS is defined */
4343
#undef DEBUGVERIFY /* Like VERIFY, but only if CONFIG_DEBUG_ASSERTIONS is defined */
4444

45-
#if !defined(CONFIG_HAVE_FILENAME) || !defined(CONFIG_DEBUG_ASSERTIONS_FILENAME)
45+
/* Macro to define the assertions file name and file line
46+
* | Function |CONFIG | Show name/line |
47+
* | --- | --- | --- |
48+
* |assert(), ASSERT()|CONFIG_ASSERTIONS_FILENAME=y | Yes |
49+
* |assert(), ASSERT()|CONFIG_ASSERTIONS_FILENAME=n | No |
50+
* |DEBUGASSERT() |CONFIG_DEBUG_ASSERTIONS_FILENAME=y| Yes |
51+
* |DEBUGASSERT() |CONFIG_DEBUG_ASSERTIONS_FILENAME=n| No |
52+
*/
53+
54+
#ifdef CONFIG_HAVE_FILENAME
55+
# ifdef CONFIG_DEBUG_ASSERTIONS_FILENAME
56+
# define __DEBUG_ASSERT_FILE__ __FILE__
57+
# define __DEBUG_ASSERT_LINE__ __LINE__
58+
# endif
59+
# ifdef CONFIG_ASSERTIONS_FILENAME
60+
# define __ASSERT_FILE__ __FILE__
61+
# define __ASSERT_LINE__ __LINE__
62+
# endif
63+
#endif
64+
65+
#ifndef __DEBUG_ASSERT_FILE__
66+
# define __DEBUG_ASSERT_FILE__ 0
67+
# define __DEBUG_ASSERT_LINE__ 0
68+
#endif
69+
70+
#ifndef __ASSERT_FILE__
4671
# define __ASSERT_FILE__ 0
4772
# define __ASSERT_LINE__ 0
48-
#else
49-
# define __ASSERT_FILE__ __FILE__
50-
# define __ASSERT_LINE__ __LINE__
5173
#endif
5274

5375
#define PANIC() __assert(__ASSERT_FILE__, __ASSERT_LINE__, "panic")
5476
#define PANIC_WITH_REGS(msg, regs) _assert(__ASSERT_FILE__, \
5577
__ASSERT_LINE__, msg, regs)
5678

57-
#ifdef CONFIG_DEBUG_ASSERTIONS_EXPRESSION
58-
# define ASSERT(f) \
59-
do \
60-
{ \
61-
if (predict_false(!(f))) \
62-
__assert(__ASSERT_FILE__, \
63-
__ASSERT_LINE__, #f); \
64-
} \
79+
#define __ASSERT(f, file, line, _f) \
80+
do \
81+
{ \
82+
if (predict_false(!(f))) \
83+
__assert(file, line, _f); \
84+
} \
6585
while (0)
6686

67-
# define VERIFY(f) \
68-
do \
69-
{ \
70-
if (predict_false((f) < 0)) \
71-
__assert(__ASSERT_FILE__, \
72-
__ASSERT_LINE__, #f); \
73-
} \
74-
while (0)
75-
#else
76-
# define ASSERT(f) \
77-
do \
78-
{ \
79-
if (predict_false(!(f))) \
80-
__assert(__ASSERT_FILE__, \
81-
__ASSERT_LINE__, 0); \
82-
} \
87+
#define __VERIFY(f, file, line, _f) \
88+
do \
89+
{ \
90+
if (predict_false((f) < 0)) \
91+
__assert(file, line, _f); \
92+
} \
8393
while (0)
8494

85-
# define VERIFY(f) \
86-
do \
87-
{ \
88-
if (predict_false((f) < 0)) \
89-
__assert(__ASSERT_FILE__, \
90-
__ASSERT_LINE__, 0); \
91-
} \
92-
while (0)
95+
#ifdef CONFIG_DEBUG_ASSERTIONS_EXPRESSION
96+
# define _ASSERT(f,file,line) __ASSERT(f, file, line, #f)
97+
# define _VERIFY(f,file,line) __VERIFY(f, file, line, #f)
98+
#else
99+
# define _ASSERT(f,file,line) __ASSERT(f, file, line, NULL)
100+
# define _VERIFY(f,file,line) __VERIFY(f, file, line, NULL)
93101
#endif
94102

95103
#ifdef CONFIG_DEBUG_ASSERTIONS
96-
# define DEBUGPANIC() PANIC()
97-
# define DEBUGASSERT(f) ASSERT(f)
98-
# define DEBUGVERIFY(f) VERIFY(f)
104+
# define DEBUGPANIC() __assert(__DEBUG_ASSERT_FILE__, __DEBUG_ASSERT_LINE__, "panic")
105+
# define DEBUGASSERT(f) _ASSERT(f, __DEBUG_ASSERT_FILE__, __DEBUG_ASSERT_LINE__)
106+
# define DEBUGVERIFY(f) _VERIFY(f, __DEBUG_ASSERT_FILE__, __DEBUG_ASSERT_LINE__)
99107
#else
100108
# define DEBUGPANIC()
101109
# define DEBUGASSERT(f) ((void)(1 || (f)))
@@ -109,10 +117,14 @@
109117

110118
#ifdef NDEBUG
111119
# define assert(f) ((void)(1 || (f)))
120+
# define VERIFY(f) assert(f)
112121
#else
113-
# define assert(f) ASSERT(f)
122+
# define assert(f) _ASSERT(f, __ASSERT_FILE__, __ASSERT_LINE__)
123+
# define VERIFY(f) _VERIFY(f, __ASSERT_FILE__, __ASSERT_LINE__)
114124
#endif
115125

126+
#define ASSERT(f) assert(f)
127+
116128
/* Suppress 3rd party library redefine _assert/__assert */
117129

118130
#define _assert _assert

0 commit comments

Comments
 (0)