DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH] eal: force evaluation of RTE_ASSERT expression
@ 2025-02-04 16:55 Stephen Hemminger
  2025-02-04 19:46 ` [PATCH v2] eal: force compilation " Stephen Hemminger
  0 siblings, 1 reply; 2+ messages in thread
From: Stephen Hemminger @ 2025-02-04 16:55 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

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(-)

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); \
-- 
2.47.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH v2] eal: force compilation of RTE_ASSERT expression
  2025-02-04 16:55 [PATCH] eal: force evaluation of RTE_ASSERT expression Stephen Hemminger
@ 2025-02-04 19:46 ` Stephen Hemminger
  0 siblings, 0 replies; 2+ messages in thread
From: Stephen Hemminger @ 2025-02-04 19:46 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

Even if RTE_ENABLE_ASSERT is not enabled, the expression used should
still be checked 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.

This was motivated by detection of problems in a driver submission
that was not detected until compiled with non default flags.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
v2 - reduce checkpatch complaints and reword comment

 lib/eal/include/rte_debug.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/lib/eal/include/rte_debug.h b/lib/eal/include/rte_debug.h
index 74593cd4d4..204e6db017 100644
--- a/lib/eal/include/rte_debug.h
+++ b/lib/eal/include/rte_debug.h
@@ -46,8 +46,16 @@ void rte_dump_stack(void);
 #ifdef RTE_ENABLE_ASSERT
 #define RTE_ASSERT(exp)	RTE_VERIFY(exp)
 #else
-#define RTE_ASSERT(exp) do {} while (0)
+/*
+ * If RTE_ENABLE_ASSERT is not set, the exp is not checked but not evaluated because
+ * of the use of sizeof(). The ternary operator is to allow function pointers
+ * and bit fields, and to suppress the evaluation of any variable length arrays.
+ */
+#define RTE_ASSERT(exp)	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); \
-- 
2.47.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-02-04 19:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-02-04 16:55 [PATCH] eal: force evaluation of RTE_ASSERT expression Stephen Hemminger
2025-02-04 19:46 ` [PATCH v2] eal: force compilation " Stephen Hemminger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).