eal: force evaluation of RTE_ASSERT expression

Message ID 20250204165516.168106-1-stephen@networkplumber.org (mailing list archive)
State Superseded
Delegated to: Thomas Monjalon
Headers
Series eal: force evaluation of RTE_ASSERT expression |

Checks

Context Check Description
ci/checkpatch warning coding style issues
ci/Intel-compilation success Compilation OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/intel-Testing success Testing PASS
ci/intel-Functional success Functional PASS

Commit Message

Stephen Hemminger Feb. 4, 2025, 4:55 p.m. UTC
Even if RTE_ENABLE_ASSERT is not enabled, the expression used should
still be evaluated to check for compiler warnings. Use sizeof
and ternary operator in same manner as the assert() macro to
cause the expression to be evaluated but not generate code.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/include/rte_debug.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
  

Patch

diff --git a/lib/eal/include/rte_debug.h b/lib/eal/include/rte_debug.h
index 74593cd4d4..357dbb09ec 100644
--- a/lib/eal/include/rte_debug.h
+++ b/lib/eal/include/rte_debug.h
@@ -43,11 +43,17 @@  void rte_dump_stack(void);
 #define rte_panic(...) rte_panic_(__func__, __VA_ARGS__, "dummy")
 #define rte_panic_(func, format, ...) __rte_panic(func, format "%.0s", __VA_ARGS__)
 
+/* RTE_ASSERT is optional checks that only get evaluted if RTE_ENABLE_ASSERT is enabled. */
 #ifdef RTE_ENABLE_ASSERT
 #define RTE_ASSERT(exp)	RTE_VERIFY(exp)
 #else
-#define RTE_ASSERT(exp) do {} while (0)
+#define RTE_ASSERT(exp)					\
+	/* Evaluate expression to trigger warnings */	\
+	do {						\
+		(void)sizeof((exp) ? 1 : 0);		\
+	} while (0)
 #endif
+
 #define	RTE_VERIFY(exp)	do {                                                  \
 	if (unlikely(!(exp)))                                                           \
 		rte_panic("line %d\tassert \"%s\" failed\n", __LINE__, #exp); \