compiler warning cleanup: is_signed_type: compare -1 to 1
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 25 Mar 2021 18:28:20 +0000 (14:28 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 25 Mar 2021 18:35:28 +0000 (14:35 -0400)
Comparing -1 to 0 triggers compiler warnings (gcc -Wtype-limits and
-Wbool-compare) and Coverity warning "Macro compares unsigned to 0".

Comparing -1 to 1 instead takes care of silencing those warnings while
keeping the same behavior.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: I5003ce1f80d34ca6713bab43e8114a23f2d8c1d4

include/lttng/bitfield.h
include/lttng/ust-tracer.h

index 5ed629381ac6ff9508b4e432d8d784660a776d9b..d97e2b9a5d3aca8b8fe7beaa5c0f85f5842d11a2 100644 (file)
 #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.
- * GCC versions lower than 4.6.0 do not accept diagnostic pragma inside
- * functions.
- */
-#if defined (__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
-# 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
-# define _BT_DIAG_IGNORE_TYPE_LIMITS
-#endif
-
-#define _bt_is_signed_type(type)       ((type) -1 < (type) 0)
+#define _bt_is_signed_type(type)       ((type) -1 < (type) 1)
 
 /*
  * Produce a build-time error if the condition `cond` is non-zero.
@@ -383,7 +360,6 @@ 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;                                             \
@@ -395,11 +371,7 @@ do {                                                                       \
        _end_unit = (_end + (_ts - 1)) / _ts;                           \
                                                                        \
        _this_unit = _end_unit - 1;                                     \
-       _BT_DIAG_PUSH                                                   \
-       _BT_DIAG_IGNORE_TYPE_LIMITS                                     \
-       _is_signed_type = _bt_is_signed_type(__typeof__(_v));           \
-       _BT_DIAG_POP                                                    \
-       if (_is_signed_type                                             \
+       if (_bt_is_signed_type(__typeof__(_v))                          \
            && (_ptr[_this_unit] & _bt_lshift((type) 1, (_end % _ts ? _end % _ts : _ts) - 1))) \
                _v = ~(__typeof__(_v)) 0;                               \
        else                                                            \
@@ -455,7 +427,6 @@ 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;                                             \
@@ -467,11 +438,7 @@ do {                                                                       \
        _end_unit = (_end + (_ts - 1)) / _ts;                           \
                                                                        \
        _this_unit = _start_unit;                                       \
-       _BT_DIAG_PUSH                                                   \
-       _BT_DIAG_IGNORE_TYPE_LIMITS                                     \
-       _is_signed_type = _bt_is_signed_type(__typeof__(_v));           \
-       _BT_DIAG_POP                                                    \
-       if (_is_signed_type                                             \
+       if (_bt_is_signed_type(__typeof__(_v))                          \
            && (_ptr[_this_unit] & _bt_lshift((type) 1, _ts - (_start % _ts) - 1))) \
                _v = ~(__typeof__(_v)) 0;                               \
        else                                                            \
index ec30db7446a52608b6d5b0a9267f3184f33b516f..2513ab2acfca73e175973509171046529fcf8326 100644 (file)
@@ -45,6 +45,6 @@
 #define lttng_alignof(type)    1
 #endif
 
-#define lttng_is_signed_type(type)           ((type) -1 < (type) 0)
+#define lttng_is_signed_type(type)           ((type) -1 < (type) 1)
 
 #endif /* _LTTNG_UST_TRACER_H */
This page took 0.026444 seconds and 4 git commands to generate.