Allow folly to compile cleanly with most of the rest of MSVC's sign mismatch warnings
authorChristopher Dykes <cdykes@fb.com>
Fri, 16 Dec 2016 04:09:21 +0000 (20:09 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Fri, 16 Dec 2016 04:18:06 +0000 (20:18 -0800)
Summary:
This allows folly to compile with warnings 4245, 4287 and 4365 enabled, all of which are sign mismatch warnings.
This is labeled as 'mostly' because I'll probably have to clean up a few more of these.

Reviewed By: yfeldblum

Differential Revision: D4263259

fbshipit-source-id: 0db618f0405817503a63094edd75b24ec1e5c9ad

31 files changed:
folly/Bits.h
folly/Conv.h
folly/CpuId.h
folly/FBString.h
folly/FBVector.h
folly/Format-inl.h
folly/Format.h
folly/FormatArg.h
folly/GroupVarint.h
folly/MPMCQueue.h
folly/RWSpinLock.h
folly/Random.cpp
folly/Range.h
folly/SpookyHashV1.h
folly/SpookyHashV2.h
folly/String.cpp
folly/detail/BitsDetail.h
folly/detail/FileUtilDetail.h
folly/detail/GroupVarintDetail.h
folly/detail/RangeCommon.cpp
folly/detail/RangeCommon.h
folly/detail/TurnSequencer.h
folly/dynamic-inl.h
folly/experimental/Instructions.h
folly/experimental/JSONSchema.cpp
folly/fibers/Baton.cpp
folly/fibers/Semaphore.h
folly/gen/File-inl.h
folly/io/IOBuf.h
folly/io/RecordIO.cpp
folly/json.cpp

index 6c794b1..04a1b8e 100644 (file)
@@ -263,8 +263,11 @@ our_bswap16(Int16 x) {
 
 #endif
 
-#define FB_GEN(t, fn) \
-template<> inline t EndianIntBase<t>::swap(t x) { return fn(x); }
+#define FB_GEN(t, fn)                             \
+  template <>                                     \
+  inline t EndianIntBase<t>::swap(t x) {          \
+    return t(fn(std::make_unsigned<t>::type(x))); \
+  }
 
 // fn(x) expands to (x) if the second argument is empty, which is exactly
 // what we want for [u]int8_t. Also, gcc 4.7 on Intel doesn't have
index 37764d2..b034e2e 100644 (file)
@@ -557,9 +557,10 @@ toAppend(Src value, Tgt * result) {
   char buffer[20];
   if (value < 0) {
     result->push_back('-');
-    result->append(buffer, uint64ToBufferUnsafe(-uint64_t(value), buffer));
+    result->append(
+        buffer, uint64ToBufferUnsafe(uint64_t(-uint64_t(value)), buffer));
   } else {
-    result->append(buffer, uint64ToBufferUnsafe(value, buffer));
+    result->append(buffer, uint64ToBufferUnsafe(uint64_t(value), buffer));
   }
 }
 
@@ -681,14 +682,14 @@ toAppend(
       conv.ToShortest(value, &builder);
       break;
     case DoubleToStringConverter::FIXED:
-      conv.ToFixed(value, numDigits, &builder);
+      conv.ToFixed(value, int(numDigits), &builder);
       break;
     default:
       CHECK(mode == DoubleToStringConverter::PRECISION);
-      conv.ToPrecision(value, numDigits, &builder);
+      conv.ToPrecision(value, int(numDigits), &builder);
       break;
   }
-  const size_t length = builder.position();
+  const size_t length = size_t(builder.position());
   builder.Finalize();
   result->append(buffer, length);
 }
@@ -730,7 +731,9 @@ estimateSpaceNeeded(Src value) {
       // so 21 is the longest non-exponential number > 1.
       detail::kConvMaxDecimalInShortestHigh
     });
-  return kMaxPositiveSpace + (value < 0);  // +1 for minus sign, if negative
+  return size_t(
+      kMaxPositiveSpace +
+      (value < 0 ? 1 : 0)); // +1 for minus sign, if negative
 }
 
 /**
index 9084804..26fa197 100644 (file)
@@ -45,13 +45,13 @@ class CpuId {
     const int n = reg[0];
     if (n >= 1) {
       __cpuid(static_cast<int*>(reg), 1);
-      f1c_ = reg[2];
-      f1d_ = reg[3];
+      f1c_ = uint32_t(reg[2]);
+      f1d_ = uint32_t(reg[3]);
     }
     if (n >= 7) {
       __cpuidex(static_cast<int*>(reg), 7, 0);
-      f7b_ = reg[1];
-      f7c_ = reg[2];
+      f7b_ = uint32_t(reg[1]);
+      f7c_ = uint32_t(reg[2]);
     }
 #elif defined(__i386__) && defined(__PIC__) && !defined(__clang__) && \
     defined(__GNUC__)
index 7a9252d..43f4e33 100644 (file)
@@ -136,7 +136,7 @@ template <class Pod, class T>
 inline void podFill(Pod* b, Pod* e, T c) {
   FBSTRING_ASSERT(b && e && b <= e);
   /*static*/ if (sizeof(T) == 1) {
-    memset(b, c, e - b);
+    memset(b, c, size_t(e - b));
   } else {
     auto const ee = b + ((e - b) & ~7u);
     for (; b != ee; b += 8) {
@@ -1184,7 +1184,7 @@ public:
   // Specialization for const char*, const char*
   FOLLY_MALLOC_NOINLINE
   basic_fbstring(const value_type* b, const value_type* e, const A& /*a*/ = A())
-      : store_(b, e - b) {
+      : store_(b, size_type(e - b)) {
   }
 
   // Nonstandard constructor
@@ -2705,7 +2705,7 @@ operator<<(
   }
 #elif defined(_MSC_VER)
   // MSVC doesn't define __ostream_insert
-  os.write(str.data(), str.size());
+  os.write(str.data(), std::streamsize(str.size()));
 #else
   std::__ostream_insert(os, str.data(), str.size());
 #endif
index 0979f88..1bcc765 100644 (file)
@@ -149,7 +149,7 @@ private:
           S_destroy_range_a(*this, b_, e_);
         }
 
-        D_deallocate(b_, z_ - b_);
+        D_deallocate(b_, size_type(z_ - b_));
       }
     }
 
@@ -915,7 +915,7 @@ public:
 public:
 
   size_type size() const noexcept {
-    return impl_.e_ - impl_.b_;
+    return size_type(impl_.e_ - impl_.b_);
   }
 
   size_type max_size() const noexcept {
@@ -946,7 +946,7 @@ public:
   }
 
   size_type capacity() const noexcept {
-    return impl_.z_ - impl_.b_;
+    return size_type(impl_.z_ - impl_.b_);
   }
 
   bool empty() const noexcept {
@@ -966,7 +966,7 @@ public:
       throw;
     }
     if (impl_.b_)
-      M_deallocate(impl_.b_, impl_.z_ - impl_.b_);
+      M_deallocate(impl_.b_, size_type(impl_.z_ - impl_.b_));
     impl_.z_ = newB + newCap;
     impl_.e_ = newB + (impl_.e_ - impl_.b_);
     impl_.b_ = newB;
index 7b71736..d116f5a 100644 (file)
@@ -174,7 +174,7 @@ void BaseFormatter<Derived, containerMode, Args...>::operator()(Output& out)
     auto p = s.begin();
     auto end = s.end();
     while (p != end) {
-      auto q = static_cast<const char*>(memchr(p, '}', end - p));
+      auto q = static_cast<const char*>(memchr(p, '}', size_t(end - p)));
       if (!q) {
         out(StringPiece(p, end));
         break;
@@ -197,7 +197,7 @@ void BaseFormatter<Derived, containerMode, Args...>::operator()(Output& out)
   bool hasDefaultArgIndex = false;
   bool hasExplicitArgIndex = false;
   while (p != end) {
-    auto q = static_cast<const char*>(memchr(p, '{', end - p));
+    auto q = static_cast<const char*>(memchr(p, '{', size_t(end - p)));
     if (!q) {
       outputString(StringPiece(p, end));
       break;
@@ -217,7 +217,7 @@ void BaseFormatter<Derived, containerMode, Args...>::operator()(Output& out)
     }
 
     // Format string
-    q = static_cast<const char*>(memchr(p, '}', end - p));
+    q = static_cast<const char*>(memchr(p, '}', size_t(end - p)));
     if (q == nullptr) {
       throw BadFormatArg("folly::format: missing ending '}'");
     }
@@ -242,7 +242,7 @@ void BaseFormatter<Derived, containerMode, Args...>::operator()(Output& out)
           arg.enforce(arg.widthIndex == FormatArg::kNoIndex,
                       "cannot provide width arg index without value arg index");
           int sizeArg = nextArg++;
-          arg.width = getSizeArg(sizeArg, arg);
+          arg.width = getSizeArg(size_t(sizeArg), arg);
         }
 
         argIndex = nextArg++;
@@ -251,7 +251,7 @@ void BaseFormatter<Derived, containerMode, Args...>::operator()(Output& out)
         if (arg.width == FormatArg::kDynamicWidth) {
           arg.enforce(arg.widthIndex != FormatArg::kNoIndex,
                       "cannot provide value arg index without width arg index");
-          arg.width = getSizeArg(arg.widthIndex, arg);
+          arg.width = getSizeArg(size_t(arg.widthIndex), arg);
         }
 
         try {
@@ -269,7 +269,7 @@ void BaseFormatter<Derived, containerMode, Args...>::operator()(Output& out)
           "folly::format: may not have both default and explicit arg indexes");
     }
 
-    doFormat(argIndex, arg, out);
+    doFormat(size_t(argIndex), arg, out);
   }
 }
 
@@ -302,7 +302,7 @@ void formatString(StringPiece val, FormatArg& arg, FormatCallback& cb) {
 
   if (arg.precision != FormatArg::kDefaultPrecision &&
       val.size() > static_cast<size_t>(arg.precision)) {
-    val.reset(val.data(), arg.precision);
+    val.reset(val.data(), size_t(arg.precision));
   }
 
   constexpr int padBufSize = 128;
@@ -312,7 +312,7 @@ void formatString(StringPiece val, FormatArg& arg, FormatCallback& cb) {
   auto pad = [&padBuf, &cb, padBufSize] (int chars) {
     while (chars) {
       int n = std::min(chars, padBufSize);
-      cb(StringPiece(padBuf, n));
+      cb(StringPiece(padBuf, size_t(n)));
       chars -= n;
     }
   };
@@ -322,7 +322,7 @@ void formatString(StringPiece val, FormatArg& arg, FormatCallback& cb) {
       val.size() < static_cast<size_t>(arg.width)) {
     char fill = arg.fill == FormatArg::kDefaultFill ? ' ' : arg.fill;
     int padChars = static_cast<int> (arg.width - val.size());
-    memset(padBuf, fill, std::min(padBufSize, padChars));
+    memset(padBuf, fill, size_t(std::min(padBufSize, padChars)));
 
     switch (arg.align) {
     case FormatArg::Align::DEFAULT:
@@ -359,8 +359,8 @@ void formatNumber(StringPiece val, int prefixLen, FormatArg& arg,
     arg.align = FormatArg::Align::RIGHT;
   } else if (prefixLen && arg.align == FormatArg::Align::PAD_AFTER_SIGN) {
     // Split off the prefix, then do any padding if necessary
-    cb(val.subpiece(0, prefixLen));
-    val.advance(prefixLen);
+    cb(val.subpiece(0, size_t(prefixLen)));
+    val.advance(size_t(prefixLen));
     arg.width = std::max(arg.width - prefixLen, 0);
   }
   format_value::formatString(val, arg, cb);
@@ -444,7 +444,7 @@ class FormatValue<
     char sign;
     if (std::is_signed<T>::value) {
       if (folly::is_negative(val_)) {
-        uval = -static_cast<UT>(val_);
+        uval = UT(-static_cast<UT>(val_));
         sign = '-';
       } else {
         uval = static_cast<UT>(val_);
@@ -461,7 +461,7 @@ class FormatValue<
         }
       }
     } else {
-      uval = val_;
+      uval = static_cast<UT>(val_);
       sign = '\0';
 
       arg.enforce(arg.sign == FormatArg::Sign::DEFAULT,
@@ -496,8 +496,11 @@ class FormatValue<
       int len = snprintf(valBufBegin, (valBuf + valBufSize) - valBufBegin,
                          "%" PRIuMAX, static_cast<uintmax_t>(uval));
 #else
-      int len = snprintf(valBufBegin, (valBuf + valBufSize) - valBufBegin,
-                         "%ju", static_cast<uintmax_t>(uval));
+      int len = snprintf(
+          valBufBegin,
+          size_t((valBuf + valBufSize) - valBufBegin),
+          "%ju",
+          static_cast<uintmax_t>(uval));
 #endif
       // valBufSize should always be big enough, so this should never
       // happen.
@@ -673,7 +676,7 @@ class FormatValue<
                   "invalid specifier '", arg.presentation, "'");
       format_value::formatString(val_, arg, cb);
     } else {
-      FormatValue<char>(val_.at(arg.splitIntKey())).format(arg, cb);
+      FormatValue<char>(val_.at(size_t(arg.splitIntKey()))).format(arg, cb);
     }
   }
 
index c34f333..1b6c871 100644 (file)
@@ -211,7 +211,9 @@ class Formatter : public BaseFormatter<Formatter<containerMode, Args...>,
 template<bool containerMode, class... Args>
 std::ostream& operator<<(std::ostream& out,
                          const Formatter<containerMode, Args...>& formatter) {
-  auto writer = [&out] (StringPiece sp) { out.write(sp.data(), sp.size()); };
+  auto writer = [&out](StringPiece sp) {
+    out.write(sp.data(), std::streamsize(sp.size()));
+  };
   formatter(writer);
   return out;
 }
index 89c7e93..33eb262 100644 (file)
@@ -243,10 +243,10 @@ inline StringPiece FormatArg::doSplitKey() {
   const char* p;
   if (e[-1] == ']') {
     --e;
-    p = static_cast<const char*>(memchr(b, '[', e - b));
+    p = static_cast<const char*>(memchr(b, '[', size_t(e - b)));
     enforce(p, "unmatched ']'");
   } else {
-    p = static_cast<const char*>(memchr(b, '.', e - b));
+    p = static_cast<const char*>(memchr(b, '.', size_t(e - b)));
   }
   if (p) {
     key_.assign(p + 1, e);
index 70a6fc3..ae3afdb 100644 (file)
@@ -102,7 +102,7 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
    * buffer of size bytes.
    */
   static size_t partialCount(const char* p, size_t size) {
-    char v = *p;
+    uint8_t v = uint8_t(*p);
     size_t s = kHeaderSize;
     s += 1 + b0key(v);
     if (s > size) return 0;
@@ -120,8 +120,9 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
    * return the number of bytes used by the encoding.
    */
   static size_t encodedSize(const char* p) {
-    return (kHeaderSize + kGroupSize +
-            b0key(*p) + b1key(*p) + b2key(*p) + b3key(*p));
+    return kHeaderSize + kGroupSize +
+           b0key(uint8_t(*p)) + b1key(uint8_t(*p)) +
+           b2key(uint8_t(*p)) + b3key(uint8_t(*p));
   }
 
   /**
@@ -194,7 +195,7 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
    * that we must be able to read at least 17 bytes from the input pointer, p.
    */
   static const char* decode(const char* p, uint32_t* dest) {
-    uint8_t key = p[0];
+    uint8_t key = uint8_t(p[0]);
     __m128i val = _mm_loadu_si128((const __m128i*)(p+1));
     __m128i mask =
         _mm_load_si128((const __m128i*)&detail::groupVarintSSEMasks[key * 2]);
@@ -209,7 +210,7 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
    */
   static const char* decode(const char* p, uint32_t* a, uint32_t* b,
                             uint32_t* c, uint32_t* d) {
-    uint8_t key = p[0];
+    uint8_t key = uint8_t(p[0]);
     __m128i val = _mm_loadu_si128((const __m128i*)(p+1));
     __m128i mask =
         _mm_load_si128((const __m128i*)&detail::groupVarintSSEMasks[key * 2]);
@@ -217,10 +218,10 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
 
     // Extracting 32 bits at a time out of an XMM register is a SSE4 feature
 #if FOLLY_SSE >= 4
-    *a = _mm_extract_epi32(r, 0);
-    *b = _mm_extract_epi32(r, 1);
-    *c = _mm_extract_epi32(r, 2);
-    *d = _mm_extract_epi32(r, 3);
+    *a = uint32_t(_mm_extract_epi32(r, 0));
+    *b = uint32_t(_mm_extract_epi32(r, 1));
+    *c = uint32_t(_mm_extract_epi32(r, 2));
+    *d = uint32_t(_mm_extract_epi32(r, 3));
 #else  /* !__SSE4__ */
     *a = _mm_extract_epi16(r, 0) + (_mm_extract_epi16(r, 1) << 16);
     *b = _mm_extract_epi16(r, 2) + (_mm_extract_epi16(r, 3) << 16);
@@ -274,8 +275,8 @@ class GroupVarint<uint64_t> : public detail::GroupVarintBase<uint64_t> {
    */
   static size_t size(uint64_t a, uint64_t b, uint64_t c, uint64_t d,
                      uint64_t e) {
-    return (kHeaderSize + kGroupSize +
-            key(a) + key(b) + key(c) + key(d) + key(e));
+    return kHeaderSize + kGroupSize +
+           key(a) + key(b) + key(c) + key(d) + key(e);
   }
 
   /**
@@ -327,8 +328,8 @@ class GroupVarint<uint64_t> : public detail::GroupVarintBase<uint64_t> {
    */
   static size_t encodedSize(const char* p) {
     uint16_t n = loadUnaligned<uint16_t>(p);
-    return (kHeaderSize + kGroupSize +
-            b0key(n) + b1key(n) + b2key(n) + b3key(n) + b4key(n));
+    return kHeaderSize + kGroupSize +
+            b0key(n) + b1key(n) + b2key(n) + b3key(n) + b4key(n);
   }
 
   /**
@@ -338,14 +339,19 @@ class GroupVarint<uint64_t> : public detail::GroupVarintBase<uint64_t> {
    */
   static char* encode(char* p, uint64_t a, uint64_t b, uint64_t c,
                       uint64_t d, uint64_t e) {
-    uint8_t b0key = key(a);
-    uint8_t b1key = key(b);
-    uint8_t b2key = key(c);
-    uint8_t b3key = key(d);
-    uint8_t b4key = key(e);
+    uint16_t b0key = key(a);
+    uint16_t b1key = key(b);
+    uint16_t b2key = key(c);
+    uint16_t b3key = key(d);
+    uint16_t b4key = key(e);
     storeUnaligned<uint16_t>(
         p,
-        (b4key << 12) | (b3key << 9) | (b2key << 6) | (b1key << 3) | b0key);
+        uint16_t(
+            (b4key << 12) |
+            (b3key << 9) |
+            (b2key << 6) |
+            (b1key << 3) |
+            b0key));
     p += 2;
     storeUnaligned(p, a);
     p += b0key+1;
@@ -412,11 +418,11 @@ class GroupVarint<uint64_t> : public detail::GroupVarintBase<uint64_t> {
     return uint8_t(7 - (__builtin_clzll(x | 1) / 8));
   }
 
-  static uint8_t b0key(uint16_t x) { return x & 7; }
-  static uint8_t b1key(uint16_t x) { return (x >> 3) & 7; }
-  static uint8_t b2key(uint16_t x) { return (x >> 6) & 7; }
-  static uint8_t b3key(uint16_t x) { return (x >> 9) & 7; }
-  static uint8_t b4key(uint16_t x) { return (x >> 12) & 7; }
+  static uint8_t b0key(uint16_t x) { return x & 7u; }
+  static uint8_t b1key(uint16_t x) { return (x >> 3) & 7u; }
+  static uint8_t b2key(uint16_t x) { return (x >> 6) & 7u; }
+  static uint8_t b3key(uint16_t x) { return (x >> 9) & 7u; }
+  static uint8_t b4key(uint16_t x) { return (x >> 12) & 7u; }
 
   static const uint64_t kMask[];
 };
index ce6e54a..74899ae 100644 (file)
@@ -729,14 +729,14 @@ class MPMCQueueBase<Derived<T, Atom, Dynamic>> : boost::noncopyable {
       if (pushes == nextPushes) {
         // pushTicket_ didn't change from A (or the previous C) to C,
         // so we can linearize at B (or D)
-        return pushes - pops;
+        return ssize_t(pushes - pops);
       }
       pushes = nextPushes;
       uint64_t nextPops = popTicket_.load(std::memory_order_acquire); // D
       if (pops == nextPops) {
         // popTicket_ didn't chance from B (or the previous D), so we
         // can linearize at C
-        return pushes - pops;
+        return ssize_t(pushes - pops);
       }
       pops = nextPops;
     }
index eea433c..b5effd7 100644 (file)
@@ -479,13 +479,14 @@ struct RWTicketIntTrait<64> {
 
 #ifdef RW_SPINLOCK_USE_SSE_INSTRUCTIONS_
   static __m128i make128(const uint16_t v[4]) {
-    return _mm_set_epi16(0, 0, 0, 0, v[3], v[2], v[1], v[0]);
+    return _mm_set_epi16(0, 0, 0, 0,
+        short(v[3]), short(v[2]), short(v[1]), short(v[0]));
   }
   static inline __m128i fromInteger(uint64_t from) {
-    return _mm_cvtsi64_si128(from);
+    return _mm_cvtsi64_si128(int64_t(from));
   }
   static inline uint64_t toInteger(__m128i in) {
-    return _mm_cvtsi128_si64(in);
+    return uint64_t(_mm_cvtsi128_si64(in));
   }
   static inline uint64_t addParallel(__m128i in, __m128i kDelta) {
     return toInteger(_mm_add_epi16(in, kDelta));
@@ -501,14 +502,17 @@ struct RWTicketIntTrait<32> {
 
 #ifdef RW_SPINLOCK_USE_SSE_INSTRUCTIONS_
   static __m128i make128(const uint8_t v[4]) {
-    return _mm_set_epi8(0, 0, 0, 0, 0, 0, 0, 0,
-        0, 0, 0, 0, v[3], v[2], v[1], v[0]);
+    return _mm_set_epi8(
+        0, 0, 0, 0,
+        0, 0, 0, 0,
+        0, 0, 0, 0,
+        char(v[3]), char(v[2]), char(v[1]), char(v[0]));
   }
   static inline __m128i fromInteger(uint32_t from) {
-    return _mm_cvtsi32_si128(from);
+    return _mm_cvtsi32_si128(int32_t(from));
   }
   static inline uint32_t toInteger(__m128i in) {
-    return _mm_cvtsi128_si32(in);
+    return uint32_t(_mm_cvtsi128_si32(in));
   }
   static inline uint32_t addParallel(__m128i in, __m128i kDelta) {
     return toInteger(_mm_add_epi8(in, kDelta));
index d991990..8d172fe 100644 (file)
@@ -89,7 +89,7 @@ class BufferedRandomDevice {
   void getSlow(unsigned char* data, size_t size);
 
   inline size_t remaining() const {
-    return buffer_.get() + bufferSize_ - ptr_;
+    return size_t(buffer_.get() + bufferSize_ - ptr_);
   }
 
   const size_t bufferSize_;
index 8ab8ae5..e5af94a 100644 (file)
@@ -361,7 +361,7 @@ public:
     // Unfortunately current gcc versions have a bug causing it to reject
     // this check in a constexpr function:
     // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71448
-    return e_ - b_;
+    return size_type(e_ - b_);
   }
   constexpr size_type walk_size() const {
     return std::distance(b_, e_);
@@ -912,13 +912,13 @@ typedef Range<unsigned char*> MutableByteRange;
 
 inline std::ostream& operator<<(std::ostream& os,
                                 const StringPiece piece) {
-  os.write(piece.start(), piece.size());
+  os.write(piece.start(), std::streamsize(piece.size()));
   return os;
 }
 
 inline std::ostream& operator<<(std::ostream& os,
                                 const MutableStringPiece piece) {
-  os.write(piece.start(), piece.size());
+  os.write(piece.start(), std::streamsize(piece.size()));
   return os;
 }
 
@@ -1058,7 +1058,7 @@ size_t qfind(const Range<T>& haystack,
       // Check if done searching
       if (++j == nsize) {
         // Yay
-        return i - haystack.begin();
+        return size_t(i - haystack.begin());
       }
     }
   }
index 2a3d867..9cfe029 100644 (file)
@@ -291,7 +291,7 @@ private:
     //  * is a not-very-regular mix of 1's and 0's
     //  * does not need any other special mathematical properties
     //
-    static const uint64_t sc_const = 0xdeadbeefdeadbeefLL;
+    static const uint64_t sc_const = 0xdeadbeefdeadbeefULL;
 
     uint64_t m_data[2*sc_numVars];  // unhashed data, for partial messages
     uint64_t m_state[sc_numVars];   // internal state of the hash
index 804191d..7d14069 100644 (file)
@@ -296,7 +296,7 @@ private:
     //  * is a not-very-regular mix of 1's and 0's
     //  * does not need any other special mathematical properties
     //
-    static constexpr uint64_t sc_const = 0xdeadbeefdeadbeefLL;
+    static constexpr uint64_t sc_const = 0xdeadbeefdeadbeefULL;
 
     uint64_t m_data[2*sc_numVars];   // unhashed data, for partial messages
     uint64_t m_state[sc_numVars];  // internal state of the hash
index 06a7d96..37bc231 100644 (file)
@@ -575,7 +575,7 @@ std::string stripLeftMargin(std::string s) {
                           piece->end(),
                           [](char c) { return c != ' ' && c != '\t'; });
     if (needle != piece->end()) {
-      indent = std::min<size_t>(indent, needle - piece->begin());
+      indent = std::min<size_t>(indent, size_t(needle - piece->begin()));
     } else {
       max_length = std::max<size_t>(piece->size(), max_length);
     }
index 0613654..19b0ecb 100644 (file)
@@ -25,7 +25,7 @@ namespace detail {
 // (see Bits.cpp)
 #ifdef _MSC_VER
 inline int popcount(unsigned int x) {
-  return __popcnt(x);
+  return int(__popcnt(x));
 }
 inline int popcountll(unsigned long long x) {
   return int(__popcnt64(x));
index 5b70d65..7ce1f67 100644 (file)
@@ -73,10 +73,10 @@ ssize_t wrapFull(F f, int fd, void* buf, size_t count, Offset... offset) {
 template <class F, class... Offset>
 ssize_t wrapvFull(F f, int fd, iovec* iov, int count, Offset... offset) {
   ssize_t totalBytes = 0;
-  size_t r;
+  ssize_t r;
   do {
     r = f(fd, iov, std::min<int>(count, kIovMax), offset...);
-    if (r == (size_t)-1) {
+    if (r == -1) {
       if (errno == EINTR) {
         continue;
       }
@@ -90,8 +90,8 @@ ssize_t wrapvFull(F f, int fd, iovec* iov, int count, Offset... offset) {
     totalBytes += r;
     incr(r, offset...);
     while (r != 0 && count != 0) {
-      if (r >= iov->iov_len) {
-        r -= iov->iov_len;
+      if (r >= ssize_t(iov->iov_len)) {
+        r -= ssize_t(iov->iov_len);
         ++iov;
         --count;
       } else {
index d44e799..4a94c7d 100644 (file)
@@ -30,7 +30,7 @@ struct GroupVarintTraits;
 
 template <>
 struct GroupVarintTraits<uint32_t> {
-  enum {
+  enum : uint32_t {
     kGroupSize = 4,
     kHeaderSize = 1,
   };
@@ -38,7 +38,7 @@ struct GroupVarintTraits<uint32_t> {
 
 template <>
 struct GroupVarintTraits<uint64_t> {
-  enum {
+  enum : uint32_t {
     kGroupSize = 5,
     kHeaderSize = 2,
   };
@@ -48,7 +48,7 @@ template <typename T>
 class GroupVarintBase {
  protected:
   typedef GroupVarintTraits<T> Traits;
-  enum { kHeaderSize = Traits::kHeaderSize };
+  enum : uint32_t { kHeaderSize = Traits::kHeaderSize };
 
  public:
   typedef T type;
@@ -56,12 +56,12 @@ class GroupVarintBase {
   /**
    * Number of integers encoded / decoded in one pass.
    */
-  enum { kGroupSize = Traits::kGroupSize };
+  enum : uint32_t { kGroupSize = Traits::kGroupSize };
 
   /**
    * Maximum encoded size.
    */
-  enum { kMaxSize = kHeaderSize + sizeof(type) * kGroupSize };
+  enum : uint32_t { kMaxSize = kHeaderSize + sizeof(type) * kGroupSize };
 
   /**
    * Maximum size for n values.
index bcab002..95c66dc 100644 (file)
@@ -41,10 +41,10 @@ size_t qfind_first_byte_of_byteset(const StringPieceLite haystack,
                                    const StringPieceLite needles) {
   SparseByteSet s;
   for (auto needle: needles) {
-    s.add(needle);
+    s.add(uint8_t(needle));
   }
   for (size_t index = 0; index < haystack.size(); ++index) {
-    if (s.contains(haystack[index])) {
+    if (s.contains(uint8_t(haystack[index]))) {
       return index;
     }
   }
index f7c8a4a..8eba63e 100644 (file)
@@ -41,7 +41,7 @@ class StringPieceLite {
   const char* data() const { return b_; }
   const char* begin() const { return b_; }
   const char* end() const { return e_; }
-  size_t size() const { return e_ - b_; }
+  size_t size() const { return size_t(e_ - b_); }
   bool empty() const { return size() == 0; }
   const char& operator[](size_t i) const { DCHECK_GT(size(), i); return b_[i]; }
   template <typename Range>
index 536c2a4..9845eab 100644 (file)
@@ -245,7 +245,9 @@ struct TurnSequencer {
 
   /// Returns the bitmask to pass futexWait or futexWake when communicating
   /// about the specified turn
-  int futexChannel(uint32_t turn) const noexcept { return 1 << (turn & 31); }
+  uint32_t futexChannel(uint32_t turn) const noexcept {
+    return 1u << (turn & 31);
+  }
 
   uint32_t decodeCurrentSturn(uint32_t state) const noexcept {
     return state & ~kWaitersMask;
index c1be95b..09c1b6a 100644 (file)
@@ -288,7 +288,7 @@ struct dynamic::NumericTypeHelper<double> {
 template<class T, class NumericType /* = typename NumericTypeHelper<T>::type */>
 dynamic::dynamic(T t) {
   type_ = TypeInfo<NumericType>::type;
-  new (getAddress<NumericType>()) NumericType(t);
+  new (getAddress<NumericType>()) NumericType(NumericType(t));
 }
 
 template <class Iterator>
@@ -507,7 +507,7 @@ inline bool dynamic::empty() const {
 }
 
 inline std::size_t dynamic::count(dynamic const& key) const {
-  return find(key) != items().end();
+  return find(key) != items().end() ? 1u : 0u;
 }
 
 inline dynamic::const_item_iterator dynamic::find(dynamic const& key) const {
index e9ce723..cc768d5 100644 (file)
@@ -69,7 +69,7 @@ struct Nehalem : public Default {
     asm ("popcntq %1, %0" : "=r" (result) : "r" (value));
     return result;
 #else
-    return _mm_popcnt_u64(value);
+    return uint64_t(_mm_popcnt_u64(value));
 #endif
   }
 };
index d73d76b..81c708d 100644 (file)
@@ -219,7 +219,7 @@ struct SizeValidator final : IValidator {
     if (value.type() != type_) {
       return none;
     }
-    if (!Comparison()(length_, value.size())) {
+    if (!Comparison()(size_t(length_), value.size())) {
       return makeError("different length string/array/object", value);
     }
     return none;
index caaaa7f..01eaf4d 100644 (file)
@@ -57,7 +57,8 @@ void Baton::waitThread() {
           fiber == NO_WAITER &&
           waitingFiber_.compare_exchange_strong(fiber, THREAD_WAITING))) {
     do {
-      folly::detail::MemoryIdler::futexWait(futex_.futex, THREAD_WAITING);
+      folly::detail::MemoryIdler::futexWait(
+          futex_.futex, uint32_t(THREAD_WAITING));
       fiber = waitingFiber_.load(std::memory_order_acquire);
     } while (fiber == THREAD_WAITING);
   }
@@ -109,7 +110,7 @@ bool Baton::timedWaitThread(TimeoutController::Duration timeout) {
     auto deadline = TimeoutController::Clock::now() + timeout;
     do {
       const auto wait_rv =
-          futex_.futex.futexWaitUntil(THREAD_WAITING, deadline);
+          futex_.futex.futexWaitUntil(uint32_t(THREAD_WAITING), deadline);
       if (wait_rv == folly::detail::FutexResult::TIMEDOUT) {
         return false;
       }
index 047abee..51cca2c 100644 (file)
@@ -28,7 +28,7 @@ namespace fibers {
 class Semaphore {
  public:
   explicit Semaphore(size_t tokenCount)
-      : capacity_(tokenCount), tokens_(capacity_) {}
+      : capacity_(tokenCount), tokens_(int64_t(capacity_)) {}
 
   Semaphore(const Semaphore&) = delete;
   Semaphore(Semaphore&&) = delete;
index 98981ef..3d94f8a 100644 (file)
@@ -47,7 +47,7 @@ class FileReader : public GenImpl<ByteRange, FileReader> {
       if (n == 0) {
         return true;
       }
-      if (!body(ByteRange(buffer_->tail(), n))) {
+      if (!body(ByteRange(buffer_->tail(), size_t(n)))) {
         return false;
       }
     }
@@ -105,7 +105,7 @@ class FileWriter : public Operator<FileWriter> {
         throw std::system_error(errno, std::system_category(),
                                 "write() failed");
       }
-      v.advance(n);
+      v.advance(size_t(n));
     }
   }
 
index 66c7cb1..6d9424c 100644 (file)
@@ -507,7 +507,7 @@ class IOBuf {
    * Returns the number of bytes in the buffer before the start of the data.
    */
   uint64_t headroom() const {
-    return data_ - buffer();
+    return uint64_t(data_ - buffer());
   }
 
   /**
@@ -516,7 +516,7 @@ class IOBuf {
    * Returns the number of bytes in the buffer after the end of the data.
    */
   uint64_t tailroom() const {
-    return bufferEnd() - tail();
+    return uint64_t(bufferEnd() - tail());
   }
 
   /**
index 37efc51..8cb081c 100644 (file)
@@ -95,7 +95,7 @@ void RecordIOReader::Iterator::advanceToValid() {
     recordAndPos_ = std::make_pair(ByteRange(), off_t(-1));
     range_.clear();  // at end
   } else {
-    size_t skipped = record.begin() - range_.begin();
+    size_t skipped = size_t(record.begin() - range_.begin());
     DCHECK_GE(skipped, headerSize());
     skipped -= headerSize();
     range_.advance(skipped);
index 6e80722..882d3bf 100644 (file)
@@ -735,7 +735,7 @@ void escapeString(
       out.push_back(hexDigit(v & 0x0f));
     } else if (*p == '\\' || *p == '\"') {
       out.push_back('\\');
-      out.push_back(*p++);
+      out.push_back(char(*p++));
     } else if (*p <= 0x1f) {
       switch (*p) {
         case '\b': out.append("\\b"); p++; break;