gcc warning fixes: -Wsign-compare and -Wextra
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 8 Oct 2013 21:22:42 +0000 (17:22 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 8 Oct 2013 21:22:42 +0000 (17:22 -0400)
When compiling code using the rcu_xchg_pointer() family of functions,
with the following define:


prior to including urcu headers, when compiling with gcc with
-Wsign-compare and -Wextra, gcc warns about:

urcu-xchg.c: In function â€˜reload’:
urcu-xchg.c:19:1: warning: ordered comparison of pointer with integer zero [-Wextra]
urcu-xchg.c:19:1: warning: signed and unsigned type in conditional expression [-Wsign-compare]

For the "ordered comparison of pointer with integer zero" warning, fix
this by comparing (type) -1 against (type) 0 instead of just 0, so if
"type" is a pointer type, this pointer type will be applied to the right
operand too, thus fixing the warning.

For the "signed and unsigned type in conditional expression" warning, we
need caa_cast_long_keep_sign() to always evaluate to the same type
signedness. In order to do so, when we need to sign-extend the value,
cast it to unsigned long after first casting it to long.

Reported-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

index c4ade90920dcf3ed02b46e5d098a432147aa5c93..19534f08522ac3cc28a3557d93f7a16baab6e56e 100644 (file)
 #define URCU_FORCE_CAST(type, arg)     ((type) (arg))
-#define caa_is_signed_type(type)       (((type) (-1)) < 0)
+#define caa_is_signed_type(type)       ((type) -1 < (type) 0)
-#define caa_cast_long_keep_sign(v)     \
-       (caa_is_signed_type(__typeof__(v)) ? (long) (v) : (unsigned long) (v))
+ * Sign-extend to long if needed, and output type is unsigned long.
+ */
+#define caa_cast_long_keep_sign(v)             \
+       (caa_is_signed_type(__typeof__(v)) ?    \
+               (unsigned long) (long) (v) :    \
+               (unsigned long) (v))
 #if defined (__GNUC__) \
        && ((__GNUC_MAJOR__ == 4) && (__GNUC_MINOR__ >= 5)      \
This page took 0.025242 seconds and 4 git commands to generate.