lttng-dev.lists.lttng.org archive mirror
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: jgalar@efficios.com, simon.marchi@efficios.com
Cc: lttng-dev@lists.lttng.org
Subject: [PATCH v2 babeltrace 2/4] Silence compiler "always false comparison" warning
Date: Thu, 16 May 2019 16:58:46 -0400	[thread overview]
Message-ID: <20190516205848.20409-2-mathieu.desnoyers__37728.3504045167$1558040384$gmane$org@efficios.com> (raw)
In-Reply-To: <20190516205848.20409-1-mathieu.desnoyers@efficios.com>

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.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: I2dd980e11ebd6cd37ae71b013925a6fca5d7b483
---
Changes since v1:
- Use stdbool.h.
---
 include/babeltrace/bitfield-internal.h | 35 ++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

diff --git a/include/babeltrace/bitfield-internal.h b/include/babeltrace/bitfield-internal.h
index 5ecde05e..bb0283a3 100644
--- a/include/babeltrace/bitfield-internal.h
+++ b/include/babeltrace/bitfield-internal.h
@@ -24,6 +24,7 @@
  */
 
 #include <stdint.h>	/* C99 5.2.4.2 Numerical limits */
+#include <stdbool.h>	/* C99 7.16 bool type */
 #include <babeltrace/compat/limits-internal.h>	/* C99 5.2.4.2 Numerical limits */
 #include <babeltrace/endian-internal.h>	/* Non-standard BIG_ENDIAN, LITTLE_ENDIAN, BYTE_ORDER */
 
@@ -40,6 +41,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)
 
 /*
@@ -359,6 +380,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" */	\
+	bool is_signed_type;						\
 									\
 	if (!__length) {						\
 		*__vptr = 0;						\
@@ -370,7 +392,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								\
@@ -426,6 +452,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" */	\
+	bool is_signed_type;						\
 									\
 	if (!__length) {						\
 		*__vptr = 0;						\
@@ -437,7 +464,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								\
-- 
2.11.0

       reply	other threads:[~2019-05-16 20:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20190516205848.20409-1-mathieu.desnoyers@efficios.com>
2019-05-16 20:58 ` Mathieu Desnoyers [this message]
2019-05-16 20:58 ` [PATCH babeltrace 3/4] Cleanup: bitfields: streamline use of underscores Mathieu Desnoyers
2019-05-16 20:58 ` [PATCH v2 babeltrace 4/4] Extend test_bitfield coverage Mathieu Desnoyers
2019-05-17 20:44 ` [PATCH v8 babeltrace 1/4] Fix: bitfield: shift undefined/implementation defined behaviors Jérémie Galarneau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='20190516205848.20409-2-mathieu.desnoyers__37728.3504045167$1558040384$gmane$org@efficios.com' \
    --to=mathieu.desnoyers@efficios.com \
    --cc=jgalar@efficios.com \
    --cc=lttng-dev@lists.lttng.org \
    --cc=simon.marchi@efficios.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).