]> granicus.if.org Git - libatomic_ops/commitdiff
Eliminate 'condition sizeof(long)>4 is always true' cppcheck style warning
authorIvan Maidanski <ivmai@mail.ru>
Mon, 20 Feb 2017 19:16:54 +0000 (22:16 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Mon, 20 Feb 2017 19:16:54 +0000 (22:16 +0300)
* src/atomic_ops_malloc.c (msb): Cast (s >> 32) to unsigned int type
before assigning the result to v; check __SIZEOF_SIZE_T__ value (if
defined) to decide whether (s >> 32) is needed; move condition
(sizeof(size_t) > 4) to SIZEOF_SIZE_T_GT_4 macro (to eliminate cppcheck
false report that the condition is always true).

src/atomic_ops_malloc.c

index aae21d07e257520a4cd14146e27c2f850803b9c4..499b1451edb09dce4fadfca42f55fa7a97d5728e 100644 (file)
@@ -251,14 +251,27 @@ static unsigned msb(size_t s)
 {
   unsigned result = 0;
   if ((s & 0xff) != s) {
-    unsigned v;
-    /* The following is a tricky code ought to be equivalent to         */
-    /* "(v = s >> 32) != 0" but suppresses warnings on 32-bit arch's.   */
-    if (sizeof(size_t) > 4 && (v = s >> (sizeof(size_t) > 4 ? 32 : 0)) != 0)
-      {
-        s = v;
-        result += 32;
-      }
+#   if __SIZEOF_SIZE_T__ == 8
+      unsigned v = (unsigned)(s >> 32);
+      if (v != 0)
+        {
+          s = v;
+          result += 32;
+        }
+#   elif __SIZEOF_SIZE_T__ == 4
+      /* No op. */
+#   else
+      unsigned v;
+      /* The following is a tricky code ought to be equivalent to       */
+      /* "(v = s >> 32) != 0" but suppresses warnings on 32-bit arch's. */
+#     define SIZEOF_SIZE_T_GT_4 (sizeof(size_t) > 4)
+      if (SIZEOF_SIZE_T_GT_4
+          && (v = (unsigned)(s >> (SIZEOF_SIZE_T_GT_4 ? 32 : 0))) != 0)
+        {
+          s = v;
+          result += 32;
+        }
+#   endif /* !defined(__SIZEOF_SIZE_T__) */
     if ((s >> 16) != 0)
       {
         s >>= 16;