From 143cec42e14e050571be640cb2dfa5c5e0198d59 Mon Sep 17 00:00:00 2001 From: Francis Deslauriers Date: Fri, 20 Aug 2021 14:39:02 -0400 Subject: [PATCH] Force usage of ARGPAR_ASSERT() condition when NDEBUG is defined MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This preventive fix is added to prevent future `-Wunused` warnings in `assert()` statement when building with NDEBUG defined. There are currently no such warning in the argpar project but there used to be one. Because the LTTng-Tools project currently (commit 4f002736e) uses an older argpar version I am seeing this warning: argpar.c: In function ‘argpar_vasprintf’: argpar.c:63:12: error: variable ‘len2’ set but not used [-Werror=unused-but-set-variable] 63 | int len1, len2; Reuse the BT2 approach to force the usage of the assertion condition even when assert() are removed by the NDEBUG define. See `BT_USE_EXPR()` macro and documentation in Babeltrace commit[0]: commit 1778c2a4134647150b199b2b57130817144446b0 Author: Philippe Proulx Date: Tue Apr 21 11:15:42 2020 -0400 lib: assign a unique ID to each pre/postcond. and report it on failure 0: https://github.com/efficios/babeltrace/commit/1778c2a4134647150b199b2b57130817144446b0 Signed-off-by: Francis Deslauriers Change-Id: Id71f1f67013cfcf4799ef95a6830babb2dcea973 --- argpar/argpar.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/argpar/argpar.c b/argpar/argpar.c index 10f6ee8..82b561c 100644 --- a/argpar/argpar.c +++ b/argpar/argpar.c @@ -5,7 +5,6 @@ * Copyright (c) 2020-2021 Simon Marchi */ -#include #include #include #include @@ -22,7 +21,16 @@ #define ARGPAR_ZALLOC(_type) ARGPAR_CALLOC(_type, 1) -#define ARGPAR_ASSERT(_cond) assert(_cond) +#ifdef NDEBUG +/* + * Force usage of the assertion condition to prevent unused variable warnings + * when `assert()` are disabled by the `NDEBUG` definition. + */ +# define ARGPAR_ASSERT(_cond) ((void) sizeof((void) (_cond), 0)) +#else +# include +# define ARGPAR_ASSERT(_cond) assert(_cond) +#endif /* * An argpar iterator. -- 2.34.1