Force usage of ARGPAR_ASSERT() condition when NDEBUG is defined
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Fri, 20 Aug 2021 18:39:02 +0000 (14:39 -0400)
committerFrancis Deslauriers <francis.deslauriers@efficios.com>
Tue, 31 Aug 2021 14:00:47 +0000 (10:00 -0400)
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 <eeppeliteloop@gmail.com>
  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 <francis.deslauriers@efficios.com>
Change-Id: Id71f1f67013cfcf4799ef95a6830babb2dcea973

argpar/argpar.c

index 10f6ee8d42e93a780a65ad4c5cf66347d667c42d..82b561ceaae707002f36754c55e828d35b882206 100644 (file)
@@ -5,7 +5,6 @@
  * Copyright (c) 2020-2021 Simon Marchi <simon.marchi@efficios.com>
  */
 
-#include <assert.h>
 #include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
 
 #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 <assert.h>
+# define ARGPAR_ASSERT(_cond) assert(_cond)
+#endif
 
 /*
  * An argpar iterator.
This page took 0.023573 seconds and 4 git commands to generate.