|
Message-Id: <c29c3d9b7955d95617350b1f91d97ab8bc23ec48.1685522953.git.Jens.Gustedt@inria.fr> Date: Wed, 31 May 2023 11:22:59 +0200 From: Jens Gustedt <Jens.Gustedt@...ia.fr> To: musl@...ts.openwall.com Subject: [C23 divers headers 12/17] C23: change the assert macro to ... arguments Macro arguments are ambiguate fellows. A single C expression such as (struct toto){ 1, 2}.a appears as two macro arguments. This mandatory change has the macro accept an unlimited number of macro-parameters, but which then must resolve to a single C expression. This is achieved by squeezing the argument list through a trivial function call to a static function. By that we ensure that lists that represent more than one expression (such as `1, 2`) and empty lists are diagnosed. --- include/assert.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/include/assert.h b/include/assert.h index 71007a10..034c4aa7 100644 --- a/include/assert.h +++ b/include/assert.h @@ -2,10 +2,17 @@ #undef assert +// Ensure that this version only accepts arguments that resolve to one +// C expression after preprocessing. +#ifndef __ASSERT_EXPRESSION +#define __ASSERT_EXPRESSION(...) __assert_expression(__VA_ARGS__) +static inline _Bool __assert_expression(_Bool __x) { return __x; } +#endif + #ifdef NDEBUG -#define assert(x) (void)0 +#define assert(...) (void)0 #else -#define assert(x) ((void)((x) || (__assert_fail(#x, __FILE__, __LINE__, __func__),0))) +#define assert(...) ((void)(__ASSERT_EXPRESSION(__VA_ARGS__) || (__assert_fail(#__VA_ARGS__, __FILE__, __LINE__, __func__),0))) #endif #if __STDC_VERSION__ >= 201112L && __STDC_VERSION__ < 202311L && !defined(__cplusplus) -- 2.34.1
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.