compiler warning cleanup: is_signed_type: compare -1 to 1
[babeltrace.git] / src / compat / bitfield.h
index 5e5a046f033d600ce67821c8f8ae4062b3ea7ef2..e0a3885cb28d36996558529dfc76e88b63785a9e 100644 (file)
@@ -1,28 +1,12 @@
-#ifndef _BABELTRACE_BITFIELD_H
-#define _BABELTRACE_BITFIELD_H
-
 /*
- * Copyright 2010-2019 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
+ * SPDX-License-Identifier: MIT
  *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
+ * Copyright 2010-2019 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  */
 
+#ifndef _BABELTRACE_BITFIELD_H
+#define _BABELTRACE_BITFIELD_H
+
 #include <stdint.h>    /* C99 5.2.4.2 Numerical limits */
 #include <stdbool.h>   /* C99 7.16 bool type */
 #include "compat/limits.h"     /* C99 5.2.4.2 Numerical limits */
 #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
-# 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.
  * Evaluates as a size_t expression.
  */
+#ifdef __cplusplus
+#define _BT_BUILD_ASSERT(cond) ([]{static_assert((cond), "");}, 0)
+#else
 #define _BT_BUILD_ASSERT(cond)                                 \
        sizeof(struct { int f:(2 * !!(cond) - 1); })
+#endif
 
 /*
  * Cast value `v` to an unsigned integer of the same size as `v`.
@@ -375,13 +342,12 @@ do {                                                                      \
 do {                                                                   \
        __typeof__(*(vptr)) *_vptr = (vptr);                            \
        __typeof__(*_vptr) _v;                                          \
-       type *_ptr = (void *) (ptr);                                    \
+       type *_ptr = (type *) (ptr);                                    \
        unsigned long _start = (start), _length = (length);             \
        type _mask, _cmask;                                             \
        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;                                             \
@@ -393,11 +359,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                                                            \
@@ -453,7 +415,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;                                             \
@@ -465,11 +426,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                                                            \
This page took 0.024943 seconds and 4 git commands to generate.