From 1c098e2758d1bd55c48ffa26dab389dbbe96a666 Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Thu, 15 Dec 2016 20:09:19 -0800 Subject: [PATCH] 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 --- folly/Benchmark.cpp | 6 +++--- folly/Benchmark.h | 6 +++--- folly/Bits.h | 2 +- folly/Checksum.cpp | 2 +- folly/Conv.cpp | 12 ++++++------ folly/Conv.h | 4 ++-- folly/FBString.h | 2 +- folly/FileUtil.cpp | 22 +++++++++++----------- folly/Format.cpp | 2 +- folly/GroupVarint.h | 4 ++-- folly/IndexedMemPool.h | 7 ++++--- folly/MPMCQueue.h | 2 +- folly/MemoryMapping.cpp | 10 +++++----- folly/SparseByteSet.h | 2 +- folly/SpookyHashV1.cpp | 2 +- folly/SpookyHashV2.cpp | 2 +- folly/String.cpp | 2 +- folly/Uri.cpp | 2 +- folly/Varint.h | 2 +- folly/detail/CacheLocality.cpp | 4 ++-- folly/detail/CacheLocality.h | 15 ++++++++------- folly/detail/MemoryIdler.h | 2 +- folly/detail/RangeSse42.cpp | 18 +++++++++++------- folly/detail/Stats.h | 4 ++-- folly/detail/TurnSequencer.h | 2 +- folly/experimental/FunctionScheduler.cpp | 2 +- folly/experimental/JSONSchema.cpp | 2 +- folly/fibers/EventBaseLoopController-inl.h | 4 ++-- folly/io/RecordIO-inl.h | 2 +- folly/io/RecordIO.cpp | 6 +++--- folly/io/ShutdownSocketSet.cpp | 12 ++++++------ folly/io/ShutdownSocketSet.h | 4 ++-- folly/json.cpp | 9 +++++---- 33 files changed, 93 insertions(+), 86 deletions(-) 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 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 str_to_floating(StringPiece* src) noexcept { src->assign(b, e); - return result; + return Tgt(result); } template Expected str_to_floating( @@ -570,7 +570,7 @@ inline Expected 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 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 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(v % 10); + auto const r = static_cast(v % 10); buffer[pos--] = '0' + r; v = q; } // Last digit is trivial to handle - buffer[pos] = static_cast(v) + '0'; + buffer[pos] = static_cast(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 : public detail::GroupVarintBase { 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 : public detail::GroupVarintBase { 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( reinterpret_cast(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> : 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 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(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