From: Christopher Dykes <cdykes@fb.com> Date: Fri, 16 Dec 2016 04:09:19 +0000 (-0800) Subject: More implicit truncation warning fixes X-Git-Tag: v2016.12.19.00~11 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=1c098e2758d1bd55c48ffa26dab389dbbe96a666;p=folly.git More implicit truncation warning fixes Summary: This makes the changes required to compile Folly cleanly with the rest of MSVC's truncation warnings, 4244 & 4267. Only another 2800 sign mismatch warnings left to go. Reviewed By: yfeldblum Differential Revision: D4257094 fbshipit-source-id: 1651eca875a31f53774d36c682f5e2745ddfcda5 --- diff --git a/folly/Benchmark.cpp b/folly/Benchmark.cpp index 060c4bf4..ccae0344 100644 --- a/folly/Benchmark.cpp +++ b/folly/Benchmark.cpp @@ -84,7 +84,7 @@ BENCHMARK(FB_FOLLY_GLOBAL_BENCHMARK_BASELINE) { #endif } -int getGlobalBenchmarkBaselineIndex() { +size_t getGlobalBenchmarkBaselineIndex() { const char *global = FB_STRINGIZE_X2(FB_FOLLY_GLOBAL_BENCHMARK_BASELINE); auto it = std::find_if( benchmarks().begin(), @@ -94,7 +94,7 @@ int getGlobalBenchmarkBaselineIndex() { } ); CHECK(it != benchmarks().end()); - return it - benchmarks().begin(); + return size_t(std::distance(benchmarks().begin(), it)); } #undef FB_STRINGIZE_X2 @@ -465,7 +465,7 @@ void runBenchmarks() { // PLEASE KEEP QUIET. MEASUREMENTS IN PROGRESS. - unsigned int baselineIndex = getGlobalBenchmarkBaselineIndex(); + size_t baselineIndex = getGlobalBenchmarkBaselineIndex(); auto const globalBaseline = runBenchmarkGetNSPerIteration(get<2>(benchmarks()[baselineIndex]), 0); diff --git a/folly/Benchmark.h b/folly/Benchmark.h index 4c1e2806..0b193c65 100644 --- a/folly/Benchmark.h +++ b/folly/Benchmark.h @@ -116,7 +116,7 @@ struct BenchmarkSuspender { BenchmarkSuspender(const BenchmarkSuspender &) = delete; BenchmarkSuspender(BenchmarkSuspender && rhs) noexcept { start = rhs.start; - rhs.start.tv_nsec = rhs.start.tv_sec = 0; + rhs.start = {0, 0}; } BenchmarkSuspender& operator=(const BenchmarkSuspender &) = delete; @@ -125,7 +125,7 @@ struct BenchmarkSuspender { tally(); } start = rhs.start; - rhs.start.tv_nsec = rhs.start.tv_sec = 0; + rhs.start = {0, 0}; return *this; } @@ -138,7 +138,7 @@ struct BenchmarkSuspender { void dismiss() { assert(start.tv_nsec > 0 || start.tv_sec > 0); tally(); - start.tv_nsec = start.tv_sec = 0; + start = {0, 0}; } void rehire() { diff --git a/folly/Bits.h b/folly/Bits.h index 9742a066..6c794b12 100644 --- a/folly/Bits.h +++ b/folly/Bits.h @@ -459,7 +459,7 @@ class BitIterator other.bitOffset_ - bitOffset_; } - unsigned int bitOffset_; + size_t bitOffset_; }; /** diff --git a/folly/Checksum.cpp b/folly/Checksum.cpp index c8dbec10..49853bd4 100644 --- a/folly/Checksum.cpp +++ b/folly/Checksum.cpp @@ -52,7 +52,7 @@ uint32_t crc32c_hw(const uint8_t *data, size_t nbytes, // Process 8 bytes at a time until we have fewer than 8 bytes left. while (offset + sizeof(uint64_t) <= nbytes) { const uint64_t* src = (const uint64_t*)(data + offset); - sum = _mm_crc32_u64(sum, *src); + sum = uint32_t(_mm_crc32_u64(sum, *src)); offset += sizeof(uint64_t); } diff --git a/folly/Conv.cpp b/folly/Conv.cpp index 3e5214a8..65713646 100644 --- a/folly/Conv.cpp +++ b/folly/Conv.cpp @@ -235,7 +235,7 @@ using IsAscii = std:: // The code in this file that uses tolower() really only cares about // 7-bit ASCII characters, so we can take a nice shortcut here. inline char tolower_ascii(char in) { - return IsAscii::value ? in | 0x20 : std::tolower(in); + return IsAscii::value ? in | 0x20 : char(std::tolower(in)); } inline bool bool_str_cmp(const char** b, size_t len, const char* value) { @@ -365,7 +365,7 @@ Expected<Tgt, ConversionCode> str_to_floating(StringPiece* src) noexcept { return makeUnexpected(ConversionCode::EMPTY_INPUT_STRING); } src->advance(length); - return result; + return Tgt(result); } auto* e = src->end(); @@ -423,7 +423,7 @@ Expected<Tgt, ConversionCode> str_to_floating(StringPiece* src) noexcept { src->assign(b, e); - return result; + return Tgt(result); } template Expected<float, ConversionCode> str_to_floating<float>( @@ -570,7 +570,7 @@ inline Expected<Tgt, ConversionCode> digits_to( if (sum >= OOR) { goto outOfRange; } - result = 1000 * result + sum; + result = UT(1000 * result + sum); break; } case 2: { @@ -580,7 +580,7 @@ inline Expected<Tgt, ConversionCode> digits_to( if (sum >= OOR) { goto outOfRange; } - result = 100 * result + sum; + result = UT(100 * result + sum); break; } case 1: { @@ -588,7 +588,7 @@ inline Expected<Tgt, ConversionCode> digits_to( if (sum >= OOR) { goto outOfRange; } - result = 10 * result + sum; + result = UT(10 * result + sum); break; } default: diff --git a/folly/Conv.h b/folly/Conv.h index af2235e3..37764d24 100644 --- a/folly/Conv.h +++ b/folly/Conv.h @@ -383,12 +383,12 @@ inline uint32_t uint64ToBufferUnsafe(uint64_t v, char *const buffer) { // Keep these together so a peephole optimization "sees" them and // computes them in one shot. auto const q = v / 10; - auto const r = static_cast<uint32_t>(v % 10); + auto const r = static_cast<char>(v % 10); buffer[pos--] = '0' + r; v = q; } // Last digit is trivial to handle - buffer[pos] = static_cast<uint32_t>(v) + '0'; + buffer[pos] = static_cast<char>(v) + '0'; return result; } diff --git a/folly/FBString.h b/folly/FBString.h index bb3d339b..7a9252d7 100644 --- a/folly/FBString.h +++ b/folly/FBString.h @@ -655,7 +655,7 @@ private: // small_[maxSmallSize]. FBSTRING_ASSERT(s <= maxSmallSize); constexpr auto shift = kIsLittleEndian ? 0 : 2; - small_[maxSmallSize] = (maxSmallSize - s) << shift; + small_[maxSmallSize] = char((maxSmallSize - s) << shift); small_[s] = '\0'; FBSTRING_ASSERT(category() == Category::isSmall && size() == s); } diff --git a/folly/FileUtil.cpp b/folly/FileUtil.cpp index 70a8e095..0ec03b3c 100644 --- a/folly/FileUtil.cpp +++ b/folly/FileUtil.cpp @@ -29,7 +29,7 @@ namespace folly { using namespace fileutil_detail; int openNoInt(const char* name, int flags, mode_t mode) { - return wrapNoInt(open, name, flags, mode); + return int(wrapNoInt(open, name, flags, mode)); } int closeNoInt(int fd) { @@ -50,41 +50,41 @@ int closeNoInt(int fd) { } int fsyncNoInt(int fd) { - return wrapNoInt(fsync, fd); + return int(wrapNoInt(fsync, fd)); } int dupNoInt(int fd) { - return wrapNoInt(dup, fd); + return int(wrapNoInt(dup, fd)); } int dup2NoInt(int oldfd, int newfd) { - return wrapNoInt(dup2, oldfd, newfd); + return int(wrapNoInt(dup2, oldfd, newfd)); } int fdatasyncNoInt(int fd) { #if defined(__APPLE__) - return wrapNoInt(fcntl, fd, F_FULLFSYNC); + return int(wrapNoInt(fcntl, fd, F_FULLFSYNC)); #elif defined(__FreeBSD__) || defined(_MSC_VER) - return wrapNoInt(fsync, fd); + return int(wrapNoInt(fsync, fd)); #else - return wrapNoInt(fdatasync, fd); + return int(wrapNoInt(fdatasync, fd)); #endif } int ftruncateNoInt(int fd, off_t len) { - return wrapNoInt(ftruncate, fd, len); + return int(wrapNoInt(ftruncate, fd, len)); } int truncateNoInt(const char* path, off_t len) { - return wrapNoInt(truncate, path, len); + return int(wrapNoInt(truncate, path, len)); } int flockNoInt(int fd, int operation) { - return wrapNoInt(flock, fd, operation); + return int(wrapNoInt(flock, fd, operation)); } int shutdownNoInt(int fd, int how) { - return wrapNoInt(portability::sockets::shutdown, fd, how); + return int(wrapNoInt(portability::sockets::shutdown, fd, how)); } ssize_t readNoInt(int fd, void* buf, size_t count) { diff --git a/folly/Format.cpp b/folly/Format.cpp index e6df1141..3b11efe5 100644 --- a/folly/Format.cpp +++ b/folly/Format.cpp @@ -294,7 +294,7 @@ void FormatArg::validate(Type type) const { namespace detail { void insertThousandsGroupingUnsafe(char* start_buffer, char** end_buffer) { - uint32_t remaining_digits = *end_buffer - start_buffer; + uint32_t remaining_digits = uint32_t(*end_buffer - start_buffer); uint32_t separator_size = (remaining_digits - 1) / 3; uint32_t result_size = remaining_digits + separator_size; *end_buffer = *end_buffer + separator_size; diff --git a/folly/GroupVarint.h b/folly/GroupVarint.h index 0e8f5edd..70a6fc3c 100644 --- a/folly/GroupVarint.h +++ b/folly/GroupVarint.h @@ -245,7 +245,7 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> { private: static uint8_t key(uint32_t x) { // __builtin_clz is undefined for the x==0 case - return 3 - (__builtin_clz(x|1) / 8); + return uint8_t(3 - (__builtin_clz(x | 1) / 8)); } static size_t b0key(size_t x) { return x & 3; } static size_t b1key(size_t x) { return (x >> 2) & 3; } @@ -409,7 +409,7 @@ class GroupVarint<uint64_t> : public detail::GroupVarintBase<uint64_t> { static uint8_t key(uint64_t x) { // __builtin_clzll is undefined for the x==0 case - return 7 - (__builtin_clzll(x|1) / 8); + return uint8_t(7 - (__builtin_clzll(x | 1) / 8)); } static uint8_t b0key(uint16_t x) { return x & 7; } diff --git a/folly/IndexedMemPool.h b/folly/IndexedMemPool.h index bf35b559..d1723039 100644 --- a/folly/IndexedMemPool.h +++ b/folly/IndexedMemPool.h @@ -112,8 +112,9 @@ struct IndexedMemPool : boost::noncopyable { static constexpr uint32_t maxIndexForCapacity(uint32_t capacity) { // index of uint32_t(-1) == UINT32_MAX is reserved for isAllocated tracking - return std::min(uint64_t(capacity) + (NumLocalLists - 1) * LocalListLimit, - uint64_t(uint32_t(-1) - 1)); + return uint32_t(std::min( + uint64_t(capacity) + (NumLocalLists - 1) * LocalListLimit, + uint64_t(uint32_t(-1) - 1))); } static constexpr uint32_t capacityForMaxIndex(uint32_t maxIndex) { @@ -217,7 +218,7 @@ struct IndexedMemPool : boost::noncopyable { auto slot = reinterpret_cast<const Slot*>( reinterpret_cast<const char*>(elem) - offsetof(Slot, elem)); - auto rv = slot - slots_; + auto rv = uint32_t(slot - slots_); // this assert also tests that rv is in range assert(elem == &(*this)[rv]); diff --git a/folly/MPMCQueue.h b/folly/MPMCQueue.h index e706a1f4..ce6e54a0 100644 --- a/folly/MPMCQueue.h +++ b/folly/MPMCQueue.h @@ -1067,7 +1067,7 @@ class MPMCQueueBase<Derived<T, Atom, Dynamic>> : boost::noncopyable { /// corresponding SingleElementQueue uint32_t turn(uint64_t ticket, size_t cap) noexcept { assert(cap != 0); - return ticket / cap; + return uint32_t(ticket / cap); } /// Tries to obtain a push ticket for which SingleElementQueue::enqueue diff --git a/folly/MemoryMapping.cpp b/folly/MemoryMapping.cpp index 23fe52b2..d1f6997f 100644 --- a/folly/MemoryMapping.cpp +++ b/folly/MemoryMapping.cpp @@ -129,7 +129,7 @@ void MemoryMapping::init(off_t offset, off_t length) { } if (pageSize == 0) { - pageSize = sysconf(_SC_PAGESIZE); + pageSize = off_t(sysconf(_SC_PAGESIZE)); } CHECK_GT(pageSize, 0); @@ -137,7 +137,7 @@ void MemoryMapping::init(off_t offset, off_t length) { CHECK_GE(offset, 0); // Round down the start of the mapped region - size_t skipStart = offset % pageSize; + off_t skipStart = offset % pageSize; offset -= skipStart; mapLength_ = length; @@ -206,7 +206,7 @@ off_t memOpChunkSize(off_t length, off_t pageSize) { return chunkSize; } - chunkSize = FLAGS_mlock_chunk_size; + chunkSize = off_t(FLAGS_mlock_chunk_size); off_t r = chunkSize % pageSize; if (r) { chunkSize += (pageSize - r); @@ -231,7 +231,7 @@ bool memOpInChunks(std::function<int(void*, size_t)> op, // chunks breaks the locking into intervals and lets other threads do memory // operations of their own. - size_t chunkSize = memOpChunkSize(bufSize, pageSize); + size_t chunkSize = memOpChunkSize(off_t(bufSize), pageSize); char* addr = static_cast<char*>(mem); amountSucceeded = 0; @@ -377,7 +377,7 @@ void mmapFileCopy(const char* src, const char* dest, mode_t mode) { MemoryMapping destMap( File(dest, O_RDWR | O_CREAT | O_TRUNC, mode), 0, - srcMap.range().size(), + off_t(srcMap.range().size()), MemoryMapping::writable()); alignedForwardMemcpy(destMap.writableRange().data(), diff --git a/folly/SparseByteSet.h b/folly/SparseByteSet.h index 2dc55e87..05304daa 100644 --- a/folly/SparseByteSet.h +++ b/folly/SparseByteSet.h @@ -62,7 +62,7 @@ class SparseByteSet { if (r) { DCHECK_LT(size_, kCapacity); dense_[size_] = i; - sparse_[i] = size_; + sparse_[i] = uint8_t(size_); size_++; } return r; diff --git a/folly/SpookyHashV1.cpp b/folly/SpookyHashV1.cpp index 9a13fca8..ba6a91d1 100644 --- a/folly/SpookyHashV1.cpp +++ b/folly/SpookyHashV1.cpp @@ -196,7 +196,7 @@ void SpookyHashV1::Hash128( remainder = (length - ((const uint8_t *)end-(const uint8_t *)message)); memcpy(buf, end, remainder); memset(((uint8_t *)buf)+remainder, 0, sc_blockSize-remainder); - ((uint8_t *)buf)[sc_blockSize-1] = remainder; + ((uint8_t*)buf)[sc_blockSize - 1] = uint8_t(remainder); Mix(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); // do some final mixing diff --git a/folly/SpookyHashV2.cpp b/folly/SpookyHashV2.cpp index b945bc6f..cd251870 100644 --- a/folly/SpookyHashV2.cpp +++ b/folly/SpookyHashV2.cpp @@ -198,7 +198,7 @@ void SpookyHashV2::Hash128( remainder = (length - ((const uint8_t *)end-(const uint8_t *)message)); memcpy(buf, end, remainder); memset(((uint8_t *)buf)+remainder, 0, sc_blockSize-remainder); - ((uint8_t *)buf)[sc_blockSize-1] = remainder; + ((uint8_t*)buf)[sc_blockSize - 1] = uint8_t(remainder); // do some final mixing End(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); diff --git a/folly/String.cpp b/folly/String.cpp index 94763881..06a7d968 100644 --- a/folly/String.cpp +++ b/folly/String.cpp @@ -285,7 +285,7 @@ double prettyToDouble(folly::StringPiece *const prettyString, bestPrefixId = j; } } else if (prettyString->startsWith(suffixes[j].suffix)) { - int suffixLen = strlen(suffixes[j].suffix); + int suffixLen = int(strlen(suffixes[j].suffix)); //We are looking for a longest suffix matching prefix of the string //after numeric value. We need this in case suffixes have common prefix. if (suffixLen > longestPrefixLen) { diff --git a/folly/Uri.cpp b/folly/Uri.cpp index f278619b..9a955b54 100644 --- a/folly/Uri.cpp +++ b/folly/Uri.cpp @@ -23,7 +23,7 @@ namespace folly { namespace { -fbstring submatch(const boost::cmatch& m, size_t idx) { +fbstring submatch(const boost::cmatch& m, int idx) { auto& sub = m[idx]; return fbstring(sub.first, sub.second); } diff --git a/folly/Varint.h b/folly/Varint.h index 85953573..07968799 100644 --- a/folly/Varint.h +++ b/folly/Varint.h @@ -87,7 +87,7 @@ inline size_t encodeVarint(uint64_t val, uint8_t* buf) { *p++ = 0x80 | (val & 0x7f); val >>= 7; } - *p++ = val; + *p++ = uint8_t(val); return p - buf; } diff --git a/folly/detail/CacheLocality.cpp b/folly/detail/CacheLocality.cpp index c62e0ac8..b514c606 100644 --- a/folly/detail/CacheLocality.cpp +++ b/folly/detail/CacheLocality.cpp @@ -43,7 +43,7 @@ static CacheLocality getSystemLocalityInfo() { } #endif - long numCpus = sysconf(_SC_NPROCESSORS_CONF); + size_t numCpus = sysconf(_SC_NPROCESSORS_CONF); if (numCpus <= 0) { // This shouldn't happen, but if it does we should try to keep // going. We are probably not going to be able to parse /sys on @@ -156,7 +156,7 @@ CacheLocality CacheLocality::readFromSysfsTree( // a sub-optimal ordering, but it won't crash auto& lhsEquiv = equivClassesByCpu[lhs]; auto& rhsEquiv = equivClassesByCpu[rhs]; - for (int i = std::min(lhsEquiv.size(), rhsEquiv.size()) - 1; + for (int i = int(std::min(lhsEquiv.size(), rhsEquiv.size())) - 1; i >= 0; --i) { if (lhsEquiv[i] != rhsEquiv[i]) { diff --git a/folly/detail/CacheLocality.h b/folly/detail/CacheLocality.h index c9baf528..bf043011 100644 --- a/folly/detail/CacheLocality.h +++ b/folly/detail/CacheLocality.h @@ -148,7 +148,7 @@ template <template <typename> class Atom> struct SequentialThreadId { /// Returns the thread id assigned to the current thread - static size_t get() { + static unsigned get() { auto rv = currentId; if (UNLIKELY(rv == 0)) { rv = currentId = ++prevId; @@ -157,16 +157,16 @@ struct SequentialThreadId { } private: - static Atom<size_t> prevId; + static Atom<unsigned> prevId; - static FOLLY_TLS size_t currentId; + static FOLLY_TLS unsigned currentId; }; template <template <typename> class Atom> -Atom<size_t> SequentialThreadId<Atom>::prevId(0); +Atom<unsigned> SequentialThreadId<Atom>::prevId(0); template <template <typename> class Atom> -FOLLY_TLS size_t SequentialThreadId<Atom>::currentId(0); +FOLLY_TLS unsigned SequentialThreadId<Atom>::currentId(0); // Suppress this instantiation in other translation units. It is // instantiated in CacheLocality.cpp @@ -174,7 +174,7 @@ extern template struct SequentialThreadId<std::atomic>; #endif struct HashingThreadId { - static size_t get() { + static unsigned get() { pthread_t pid = pthread_self(); uint64_t id = 0; memcpy(&id, &pid, std::min(sizeof(pid), sizeof(id))); @@ -328,7 +328,8 @@ struct AccessSpreader { assert(index < n); // as index goes from 0..n, post-transform value goes from // 0..numStripes - widthAndCpuToStripe[width][cpu] = (index * numStripes) / n; + widthAndCpuToStripe[width][cpu] = + CompactStripe((index * numStripes) / n); assert(widthAndCpuToStripe[width][cpu] < numStripes); } for (size_t cpu = n; cpu < kMaxCpus; ++cpu) { diff --git a/folly/detail/MemoryIdler.h b/folly/detail/MemoryIdler.h index bc155e91..13d4a4c4 100644 --- a/folly/detail/MemoryIdler.h +++ b/folly/detail/MemoryIdler.h @@ -93,7 +93,7 @@ struct MemoryIdler { // multiplying the duration by a floating point doesn't work, grr.. auto extraFrac = timeoutVariationFrac / std::numeric_limits<uint64_t>::max() * h; - uint64_t tics = idleTimeout.count() * (1 + extraFrac); + auto tics = uint64_t(idleTimeout.count() * (1 + extraFrac)); idleTimeout = typename Clock::duration(tics); } diff --git a/folly/detail/RangeSse42.cpp b/folly/detail/RangeSse42.cpp index 83ead386..4bd75be4 100644 --- a/folly/detail/RangeSse42.cpp +++ b/folly/detail/RangeSse42.cpp @@ -105,8 +105,8 @@ size_t qfind_first_byte_of_needles16(const StringPieceLite haystack, // do an unaligned load for first block of haystack auto arr1 = _mm_loadu_si128( reinterpret_cast<const __m128i*>(haystack.data())); - auto index = _mm_cmpestri(arr2, needles.size(), - arr1, haystack.size(), 0); + auto index = + _mm_cmpestri(arr2, int(needles.size()), arr1, int(haystack.size()), 0); if (index < 16) { return index; } @@ -116,7 +116,8 @@ size_t qfind_first_byte_of_needles16(const StringPieceLite haystack, for (; i < haystack.size(); i+= 16) { arr1 = _mm_load_si128(reinterpret_cast<const __m128i*>(haystack.data() + i)); - index = _mm_cmpestri(arr2, needles.size(), arr1, haystack.size() - i, 0); + index = _mm_cmpestri( + arr2, int(needles.size()), arr1, int(haystack.size() - i), 0); if (index < 16) { return i + index; } @@ -159,8 +160,8 @@ size_t scanHaystackBlock(const StringPieceLite haystack, // This load is safe because needles.size() >= 16 auto arr2 = _mm_loadu_si128( reinterpret_cast<const __m128i*>(needles.data())); - size_t b = _mm_cmpestri( - arr2, 16, arr1, haystack.size() - blockStartIdx, 0); + size_t b = + _mm_cmpestri(arr2, 16, arr1, int(haystack.size() - blockStartIdx), 0); size_t j = nextAlignedIndex(needles.data()); for (; j < needles.size(); j += 16) { @@ -168,8 +169,11 @@ size_t scanHaystackBlock(const StringPieceLite haystack, reinterpret_cast<const __m128i*>(needles.data() + j)); auto index = _mm_cmpestri( - arr2, needles.size() - j, - arr1, haystack.size() - blockStartIdx, 0); + arr2, + int(needles.size() - j), + arr1, + int(haystack.size() - blockStartIdx), + 0); b = std::min<size_t>(index, b); } diff --git a/folly/detail/Stats.h b/folly/detail/Stats.h index d99ac008..ad331d3b 100644 --- a/folly/detail/Stats.h +++ b/folly/detail/Stats.h @@ -44,8 +44,8 @@ typename std::enable_if<!std::is_same<typename std::remove_cv<ValueType>::type, ReturnType>::type avgHelper(ValueType sum, uint64_t count) { if (count == 0) { return ReturnType(0); } - const double sumf = sum; - const double countf = count; + const double sumf = double(sum); + const double countf = double(count); return static_cast<ReturnType>(sumf / countf); } diff --git a/folly/detail/TurnSequencer.h b/folly/detail/TurnSequencer.h index b70f258b..536c2a49 100644 --- a/folly/detail/TurnSequencer.h +++ b/folly/detail/TurnSequencer.h @@ -215,7 +215,7 @@ struct TurnSequencer { /// Returns the least-most significant byte of the current uncompleted /// turn. The full 32 bit turn cannot be recovered. uint8_t uncompletedTurnLSB() const noexcept { - return state_.load(std::memory_order_acquire) >> kTurnShift; + return uint8_t(state_.load(std::memory_order_acquire) >> kTurnShift); } private: diff --git a/folly/experimental/FunctionScheduler.cpp b/folly/experimental/FunctionScheduler.cpp index 98bd9ed7..d2476c6e 100644 --- a/folly/experimental/FunctionScheduler.cpp +++ b/folly/experimental/FunctionScheduler.cpp @@ -63,7 +63,7 @@ struct PoissonDistributionFunctor { struct UniformDistributionFunctor { std::default_random_engine generator; - std::uniform_int_distribution<> dist; + std::uniform_int_distribution<milliseconds::rep> dist; UniformDistributionFunctor(milliseconds minInterval, milliseconds maxInterval) : generator(Random::rand32()), diff --git a/folly/experimental/JSONSchema.cpp b/folly/experimental/JSONSchema.cpp index e2db5c45..d73d76b3 100644 --- a/folly/experimental/JSONSchema.cpp +++ b/folly/experimental/JSONSchema.cpp @@ -616,7 +616,7 @@ struct AnyOfValidator final : IValidator { errors.emplace_back(*se); } } - const int success = validators_.size() - errors.size(); + const auto success = validators_.size() - errors.size(); if (success == 0) { return makeError("at least one valid schema", value); } else if (success > 1 && type_ == Type::EXACTLY_ONE) { diff --git a/folly/fibers/EventBaseLoopController-inl.h b/folly/fibers/EventBaseLoopController-inl.h index b23fb45c..74317701 100644 --- a/folly/fibers/EventBaseLoopController-inl.h +++ b/folly/fibers/EventBaseLoopController-inl.h @@ -118,8 +118,8 @@ inline void EventBaseLoopControllerT<EventBaseT>::timedSchedule( .count() + 1; // If clock is not monotonic - delay_ms = std::max<decltype(delay_ms)>(delay_ms, 0L); - eventBase_->tryRunAfterDelay(func, delay_ms); + delay_ms = std::max<decltype(delay_ms)>(delay_ms, 0); + eventBase_->tryRunAfterDelay(func, uint32_t(delay_ms)); } } } // folly::fibers diff --git a/folly/io/RecordIO-inl.h b/folly/io/RecordIO-inl.h index 4eef3650..9638f580 100644 --- a/folly/io/RecordIO-inl.h +++ b/folly/io/RecordIO-inl.h @@ -37,7 +37,7 @@ class RecordIOReader::Iterator : public boost::iterator_facade< bool equal(const Iterator& other) const { return range_ == other.range_; } void increment() { size_t skip = recordio_helpers::headerSize() + recordAndPos_.first.size(); - recordAndPos_.second += skip; + recordAndPos_.second += off_t(skip); range_.advance(skip); advanceToValid(); } diff --git a/folly/io/RecordIO.cpp b/folly/io/RecordIO.cpp index f279ae47..37efc51a 100644 --- a/folly/io/RecordIO.cpp +++ b/folly/io/RecordIO.cpp @@ -54,7 +54,7 @@ void RecordIOWriter::write(std::unique_ptr<IOBuf> buf) { DCHECK_EQ(buf->computeChainDataLength(), totalLength); // We're going to write. Reserve space for ourselves. - off_t pos = filePos_.fetch_add(totalLength); + off_t pos = filePos_.fetch_add(off_t(totalLength)); #if FOLLY_HAVE_PWRITEV auto iov = buf->getIov(); @@ -100,7 +100,7 @@ void RecordIOReader::Iterator::advanceToValid() { skipped -= headerSize(); range_.advance(skipped); recordAndPos_.first = record; - recordAndPos_.second += skipped; + recordAndPos_.second += off_t(skipped); } } @@ -165,7 +165,7 @@ size_t prependHeader(std::unique_ptr<IOBuf>& buf, uint32_t fileId) { memset(header, 0, sizeof(Header)); header->magic = detail::Header::kMagic; header->fileId = fileId; - header->dataLength = lengthAndHash.first; + header->dataLength = uint32_t(lengthAndHash.first); header->dataHash = lengthAndHash.second; header->headerHash = headerHash(*header); diff --git a/folly/io/ShutdownSocketSet.cpp b/folly/io/ShutdownSocketSet.cpp index 9114c8f0..215024e9 100644 --- a/folly/io/ShutdownSocketSet.cpp +++ b/folly/io/ShutdownSocketSet.cpp @@ -29,7 +29,7 @@ namespace folly { -ShutdownSocketSet::ShutdownSocketSet(size_t maxFd) +ShutdownSocketSet::ShutdownSocketSet(int maxFd) : maxFd_(maxFd), data_(static_cast<std::atomic<uint8_t>*>( folly::checkedCalloc(maxFd, sizeof(std::atomic<uint8_t>)))), @@ -39,7 +39,7 @@ ShutdownSocketSet::ShutdownSocketSet(size_t maxFd) void ShutdownSocketSet::add(int fd) { // Silently ignore any fds >= maxFd_, very unlikely DCHECK_GE(fd, 0); - if (size_t(fd) >= maxFd_) { + if (fd >= maxFd_) { return; } @@ -53,7 +53,7 @@ void ShutdownSocketSet::add(int fd) { void ShutdownSocketSet::remove(int fd) { DCHECK_GE(fd, 0); - if (size_t(fd) >= maxFd_) { + if (fd >= maxFd_) { return; } @@ -81,7 +81,7 @@ retry: int ShutdownSocketSet::close(int fd) { DCHECK_GE(fd, 0); - if (size_t(fd) >= maxFd_) { + if (fd >= maxFd_) { return folly::closeNoInt(fd); } @@ -113,7 +113,7 @@ retry: void ShutdownSocketSet::shutdown(int fd, bool abortive) { DCHECK_GE(fd, 0); - if (fd >= 0 && size_t(fd) >= maxFd_) { + if (fd >= maxFd_) { doShutdown(fd, abortive); return; } @@ -147,7 +147,7 @@ void ShutdownSocketSet::shutdown(int fd, bool abortive) { } void ShutdownSocketSet::shutdownAll(bool abortive) { - for (size_t i = 0; i < maxFd_; ++i) { + for (int i = 0; i < maxFd_; ++i) { auto& sref = data_[i]; if (sref.load(std::memory_order_acquire) == IN_USE) { shutdown(i, abortive); diff --git a/folly/io/ShutdownSocketSet.h b/folly/io/ShutdownSocketSet.h index c3beda32..17f1c050 100644 --- a/folly/io/ShutdownSocketSet.h +++ b/folly/io/ShutdownSocketSet.h @@ -37,7 +37,7 @@ class ShutdownSocketSet : private boost::noncopyable { * applications, even if you increased the number of file descriptors * on your system. */ - explicit ShutdownSocketSet(size_t maxFd = 1 << 18); + explicit ShutdownSocketSet(int maxFd = 1 << 18); /** * Add an already open socket to the list of sockets managed by @@ -112,7 +112,7 @@ class ShutdownSocketSet : private boost::noncopyable { } }; - const size_t maxFd_; + const int maxFd_; std::unique_ptr<std::atomic<uint8_t>[], Free> data_; folly::File nullFile_; }; diff --git a/folly/json.cpp b/folly/json.cpp index 0e6b0136..6e807229 100644 --- a/folly/json.cpp +++ b/folly/json.cpp @@ -553,10 +553,11 @@ dynamic parseNumber(Input& in) { std::string decodeUnicodeEscape(Input& in) { auto hexVal = [&] (int c) -> uint16_t { - return c >= '0' && c <= '9' ? c - '0' : + return uint16_t( + c >= '0' && c <= '9' ? c - '0' : c >= 'a' && c <= 'f' ? c - 'a' + 10 : c >= 'A' && c <= 'F' ? c - 'A' + 10 : - (in.error("invalid hex digit"), 0); + (in.error("invalid hex digit"), 0)); }; auto readHex = [&]() -> uint16_t { @@ -689,7 +690,7 @@ void escapeString( StringPiece input, std::string& out, const serialization_opts& opts) { - auto hexDigit = [] (int c) -> char { + auto hexDigit = [] (uint8_t c) -> char { return c < 10 ? c + '0' : c - 10 + 'a'; }; @@ -728,7 +729,7 @@ void escapeString( // with value > 127, so size > 1 byte char32_t v = decodeUtf8(p, e, opts.skip_invalid_utf8); out.append("\\u"); - out.push_back(hexDigit(v >> 12)); + out.push_back(hexDigit(uint8_t(v >> 12))); out.push_back(hexDigit((v >> 8) & 0x0f)); out.push_back(hexDigit((v >> 4) & 0x0f)); out.push_back(hexDigit(v & 0x0f));