Skip to contentSkip to conversation

Silence compiler "always false comparison" warning

PATCH New Mathieu Desnoyers 1 participant 2 messages May 14, 2019 – May 15, 2019 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 2/2 Silence compiler "always false comparison" warning

Compiling the bitfield test with gcc -Wextra generates those warnings:

../../include/babeltrace/bitfield-internal.h:38:45: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits] #define _bt_is_signed_type(type) ((type) -1 < (type) 0)

This is the intent of the macro. Disable compiler warnings around use of that macro.

---
 include/lttng/bitfield.h | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
include/lttng/bitfield.h +32 -2
index 35b382b2..ab74a8dd 100644
@@ -40,6 +40,26 @@
 #error "bitfield.h requires the compiler representation of signed integers to be two's complement."
 #endif
 
+/*
+ * _bt_is_signed_type() willingly generates comparison of unsigned
+ * expression < 0, which is always false. Silence compiler warnings.
+ */
+#ifdef __GNUC__
+# define _BT_DIAG_PUSH			_Pragma("GCC diagnostic push")
+# define _BT_DIAG_POP			_Pragma("GCC diagnostic pop")
+
+# define _BT_DIAG_STRINGIFY_1(x)	#x
+# define _BT_DIAG_STRINGIFY(x)		_BT_DIAG_STRINGIFY_1(x)
+
+# define _BT_DIAG_IGNORE(option)	\
+	_Pragma(_BT_DIAG_STRINGIFY(GCC diagnostic ignored option))
+# define _BT_DIAG_IGNORE_TYPE_LIMITS	_BT_DIAG_IGNORE("-Wtype-limits")
+#else
+# define _BT_DIAG_PUSH
+# define _BT_DIAG_POP
+# define _BT_DIAG_IGNORE
+#endif
+
 #define _bt_is_signed_type(type)	((type) -1 < (type) 0)
 
 /*
@@ -347,6 +367,7 @@ do {									\
 	unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */	\
 	unsigned long start_unit, end_unit, this_unit;			\
 	unsigned long end, cshift; /* cshift is "complement shift" */	\
+	int is_signed_type;						\
 									\
 	if (!__length) {						\
 		*__vptr = 0;						\
@@ -358,7 +379,11 @@ do {									\
 	end_unit = (end + (ts - 1)) / ts;				\
 									\
 	this_unit = end_unit - 1;					\
-	if (_bt_is_signed_type(__typeof__(__v))				\
+	_BT_DIAG_PUSH							\
+	_BT_DIAG_IGNORE_TYPE_LIMITS					\
+	is_signed_type = _bt_is_signed_type(__typeof__(__v));		\
+	_BT_DIAG_POP							\
+	if (is_signed_type						\
 	    && (__ptr[this_unit] & _bt_lshift((type) 1, (end % ts ? end % ts : ts) - 1))) \
 		__v = ~(__typeof__(__v)) 0;				\
 	else								\
@@ -414,6 +439,7 @@ do {									\
 	unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */	\
 	unsigned long start_unit, end_unit, this_unit;			\
 	unsigned long end, cshift; /* cshift is "complement shift" */	\
+	int is_signed_type;						\
 									\
 	if (!__length) {						\
 		*__vptr = 0;						\
@@ -425,7 +451,11 @@ do {									\
 	end_unit = (end + (ts - 1)) / ts;				\
 									\
 	this_unit = start_unit;						\
-	if (_bt_is_signed_type(__typeof__(__v))				\
+	_BT_DIAG_PUSH							\
+	_BT_DIAG_IGNORE_TYPE_LIMITS					\
+	is_signed_type = _bt_is_signed_type(__typeof__(__v));		\
+	_BT_DIAG_POP							\
+	if (is_signed_type						\
 	    && (__ptr[this_unit] & _bt_lshift((type) 1, ts - (__start % ts) - 1))) \
 		__v = ~(__typeof__(__v)) 0;				\
 	else								\
Signature · 1 line
2.11.0
#2 lore raw

Re: [PATCH lttng-ust 1/2] Fix: bitfield: shift undefined/implementation defined behaviors

In reply to a message not in the archive

----- On May 14, 2019, at 4:36 PM, Mathieu Desnoyers [email protected] wrote:

[...]

							\
Quoted 43 lines

#define _bt_bitfield_write_be(_ptr, type, _start, _length, _v) \
do { \

(quoted diff) +1 -1
-	__typeof__(_v) __v = (_v);						\
+	__typeof__(_v) __v = (_v);					\
	type *__ptr = (void *) (_ptr);					\
	unsigned long __start = (_start), __length = (_length);		\
	type mask, cmask;						\
(quoted diff) +7 -7
@@ -161,15 +263,15 @@ do {									\
 									\
 	/* Trim v high bits */						\
 	if (__length < sizeof(__v) * CHAR_BIT)				\
-		__v &= ~((~(__typeof__(__v)) 0) << __length);		\
+		__v &= _bt_make_mask(__typeof__(__v), __length);	\
 									\
 	/* We can now append v with a simple "or", shift it piece-wise */ \
 	this_unit = end_unit - 1;					\
 	if (start_unit == end_unit - 1) {				\
-		mask = ~((~(type) 0) << ((ts - (end % ts)) % ts));	\
+		mask = _bt_make_mask(type, (ts - (end % ts)) % ts);	\
 		if (__start % ts)					\
-			mask |= (~((type) 0)) << (ts - (__start % ts));	\
-		cmask = (type) __v << ((ts - (end % ts)) % ts);		\
+			mask |= _bt_make_mask_complement(type, ts - (__start % ts)); \
+		cmask = _bt_lshift((type) (__v), (ts - (end % ts)) % ts); \
 		cmask &= ~mask;						\
 		__ptr[this_unit] &= mask;				\
 		__ptr[this_unit] |= cmask;				\
@@ -177,22 +279,22 @@ do {									\
 	}								\
 	if (end % ts) {							\
 		cshift = end % ts;					\
-		mask = ~((~(type) 0) << (ts - cshift));			\
-		cmask = (type) __v << (ts - cshift);			\
+		mask = _bt_make_mask(type, ts - cshift);		\
+		cmask = _bt_lshift((type) (__v), ts - cshift);		\
 		cmask &= ~mask;						\
 		__ptr[this_unit] &= mask;				\
 		__ptr[this_unit] |= cmask;				\
-		__v = _bt_piecewise_rshift(__v, cshift);		\
+		_bt_piecewise_rshift(__v, cshift);			\
 		end -= cshift;						\
 		this_unit--;						\

Review do { \ · line 297

Quoted 10 lines
(quoted diff) +2 -2
 	}								\
 	for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
 		__ptr[this_unit] = (type) __v;				\
-		__v = _bt_piecewise_rshift(__v, ts);			\
+		_bt_piecewise_rshift(__v, ts);				\
 		end -= ts;						\
 	}								\
 	if (__start % ts) {						\
-		mask = (~(type) 0) << (ts - (__start % ts));		\
+		mask = _bt_make_mask(type, ts - (__start % ts));	\

Should be:

+ mask = _bt_make_mask_complement(type, ts - (__start % ts)); \

Will fix before merging.

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com