Skip to content

Commit 3345a1f

Browse files
committed
diag: Don't leave unused assertion messages in the binary
Rather than hiding them with %.0s, use a ternary to replace them with an empty string if they would be unused.
1 parent 5c5ae39 commit 3345a1f

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/diag.h

+19-6
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,44 @@ void bfs_abortf(const char *format, ...);
8888
# define bfs_ebug bfs_eabort
8989
#endif
9090

91+
/**
92+
* Get the default assertion message, if no format string was specified.
93+
*/
94+
#define BFS_DIAG_MSG_(format, str) \
95+
(sizeof(format) > 1 ? "" : str)
96+
9197
/**
9298
* Unconditional assert.
9399
*/
94100
#define bfs_verify(...) \
95101
bfs_verify_(#__VA_ARGS__, __VA_ARGS__, "", )
96102

97103
#define bfs_verify_(str, cond, format, ...) \
98-
((cond) ? (void)0 : bfs_abortf( \
104+
((cond) ? (void)0 : bfs_verify__(format, BFS_DIAG_MSG_(format, str), __VA_ARGS__))
105+
106+
#define bfs_verify__(format, ...) \
107+
bfs_abortf( \
99108
sizeof(format) > 1 \
100-
? BFS_DIAG_FORMAT_("%.0s" format "%s") \
109+
? BFS_DIAG_FORMAT_("%s" format "%s") \
101110
: BFS_DIAG_FORMAT_("Assertion failed: `%s`"), \
102-
BFS_DIAG_ARGS_(str, __VA_ARGS__)))
111+
BFS_DIAG_ARGS_(__VA_ARGS__))
103112

104113
/**
105114
* Unconditional assert, including the last error.
106115
*/
107116
#define bfs_everify(...) \
108117
bfs_everify_(#__VA_ARGS__, __VA_ARGS__, "", )
109118

119+
110120
#define bfs_everify_(str, cond, format, ...) \
111-
((cond) ? (void)0 : bfs_abortf( \
121+
((cond) ? (void)0 : bfs_everify__(format, BFS_DIAG_MSG_(format, str), __VA_ARGS__))
122+
123+
#define bfs_everify__(format, ...) \
124+
bfs_abortf( \
112125
sizeof(format) > 1 \
113-
? BFS_DIAG_FORMAT_("%.0s" format "%s: %s") \
126+
? BFS_DIAG_FORMAT_("%s" format "%s: %s") \
114127
: BFS_DIAG_FORMAT_("Assertion failed: `%s`: %s"), \
115-
BFS_DIAG_ARGS_(str, __VA_ARGS__ errstr(), )))
128+
BFS_DIAG_ARGS_(__VA_ARGS__ errstr(), ))
116129

117130
/**
118131
* Assert in debug builds; no-op in release builds.

tests/tests.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ bool bfs_check_impl(bool result);
4848
bfs_check_(#__VA_ARGS__, __VA_ARGS__, "", )
4949

5050
#define bfs_check_(str, cond, format, ...) \
51-
bfs_check_impl((cond) || (bfs_check__(str, format, __VA_ARGS__), false))
51+
bfs_check_impl((cond) || (bfs_check__(format, BFS_DIAG_MSG_(format, str), __VA_ARGS__), false))
5252

53-
#define bfs_check__(str, format, ...) \
53+
#define bfs_check__(format, ...) \
5454
bfs_diagf(sizeof(format) > 1 \
55-
? BFS_DIAG_FORMAT_("%.0s" format "%s") \
55+
? BFS_DIAG_FORMAT_("%s" format "%s") \
5656
: BFS_DIAG_FORMAT_("Check failed: `%s`"), \
57-
BFS_DIAG_ARGS_(str, __VA_ARGS__))
57+
BFS_DIAG_ARGS_(__VA_ARGS__))
5858

5959
/**
6060
* Check a condition, logging the current error string on failure.
@@ -63,12 +63,12 @@ bool bfs_check_impl(bool result);
6363
bfs_echeck_(#__VA_ARGS__, __VA_ARGS__, "", )
6464

6565
#define bfs_echeck_(str, cond, format, ...) \
66-
bfs_check_impl((cond) || (bfs_echeck__(str, format, __VA_ARGS__), false))
66+
bfs_check_impl((cond) || (bfs_echeck__(format, BFS_DIAG_MSG_(format, str), __VA_ARGS__), false))
6767

68-
#define bfs_echeck__(str, format, ...) \
68+
#define bfs_echeck__(format, ...) \
6969
bfs_diagf(sizeof(format) > 1 \
70-
? BFS_DIAG_FORMAT_("%.0s" format "%s: %s") \
70+
? BFS_DIAG_FORMAT_("%s" format "%s: %s") \
7171
: BFS_DIAG_FORMAT_("Check failed: `%s`: %s"), \
72-
BFS_DIAG_ARGS_(str, __VA_ARGS__ errstr(), ))
72+
BFS_DIAG_ARGS_(__VA_ARGS__ errstr(), ))
7373

7474
#endif // BFS_TESTS_H

0 commit comments

Comments
 (0)