Skip to content

Commit 942bb46

Browse files
authored
Merge pull request Tencent#827 from lichray/fix-signed
Treat signed-unsigned conversions as errors.
2 parents 265fb6e + 3cc77d5 commit 942bb46

File tree

16 files changed

+37
-35
lines changed

16 files changed

+37
-35
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
5757
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
5858
endif()
5959
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror")
60+
set(EXTRA_CXX_FLAGS -Weffc++ -Wswitch-default -Wfloat-equal -Wconversion -Wsign-conversion)
6061
if (RAPIDJSON_BUILD_CXX11)
6162
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.7.0")
6263
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
@@ -86,6 +87,7 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
8687
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
8788
endif()
8889
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -Wno-missing-field-initializers")
90+
set(EXTRA_CXX_FLAGS -Weffc++ -Wswitch-default -Wfloat-equal -Wconversion -Wimplicit-fallthrough -Weverything)
8991
if (RAPIDJSON_BUILD_CXX11)
9092
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
9193
endif()

example/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ set(EXAMPLES
2424
include_directories("../include/")
2525

2626
add_definitions(-D__STDC_FORMAT_MACROS)
27+
set_property(DIRECTORY PROPERTY COMPILE_OPTIONS ${EXTRA_CXX_FLAGS})
2728

2829
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
29-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -Werror -Wall -Wextra -Weffc++ -Wswitch-default")
30-
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
31-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal -Wimplicit-fallthrough -Weverything")
30+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
3231
endif()
3332

3433
foreach (example ${EXAMPLES})

include/rapidjson/encodedstream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class AutoUTFInputStream {
200200
// xx xx xx xx UTF-8
201201

202202
if (!hasBOM_) {
203-
unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0);
203+
int pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0);
204204
switch (pattern) {
205205
case 0x08: type_ = kUTF32BE; break;
206206
case 0x0A: type_ = kUTF16BE; break;

include/rapidjson/encodings.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ struct UTF8 {
157157
if (type >= 32) {
158158
*codepoint = 0;
159159
} else {
160-
*codepoint = (0xFF >> type) & static_cast<unsigned char>(c);
160+
*codepoint = (0xFFu >> type) & static_cast<unsigned char>(c);
161161
}
162162
bool result = true;
163163
switch (type) {
@@ -283,7 +283,7 @@ struct UTF16 {
283283
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF);
284284
unsigned v = codepoint - 0x10000;
285285
os.Put(static_cast<typename OutputStream::Ch>((v >> 10) | 0xD800));
286-
os.Put((v & 0x3FF) | 0xDC00);
286+
os.Put(static_cast<typename OutputStream::Ch>((v & 0x3FF) | 0xDC00));
287287
}
288288
}
289289

@@ -299,7 +299,7 @@ struct UTF16 {
299299
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF);
300300
unsigned v = codepoint - 0x10000;
301301
PutUnsafe(os, static_cast<typename OutputStream::Ch>((v >> 10) | 0xD800));
302-
PutUnsafe(os, (v & 0x3FF) | 0xDC00);
302+
PutUnsafe(os, static_cast<typename OutputStream::Ch>((v & 0x3FF) | 0xDC00));
303303
}
304304
}
305305

include/rapidjson/internal/dtoa.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uin
4141
}
4242
}
4343

44-
inline unsigned CountDecimalDigit32(uint32_t n) {
44+
inline int CountDecimalDigit32(uint32_t n) {
4545
// Simple pure C++ implementation was faster than __builtin_clz version in this situation.
4646
if (n < 10) return 1;
4747
if (n < 100) return 2;
@@ -63,7 +63,7 @@ inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buff
6363
const DiyFp wp_w = Mp - W;
6464
uint32_t p1 = static_cast<uint32_t>(Mp.f >> -one.e);
6565
uint64_t p2 = Mp.f & (one.f - 1);
66-
unsigned kappa = CountDecimalDigit32(p1); // kappa in [0, 9]
66+
int kappa = CountDecimalDigit32(p1); // kappa in [0, 9]
6767
*len = 0;
6868

6969
while (kappa > 0) {
@@ -102,8 +102,8 @@ inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buff
102102
kappa--;
103103
if (p2 < delta) {
104104
*K += kappa;
105-
int index = -static_cast<int>(kappa);
106-
GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[-static_cast<int>(kappa)] : 0));
105+
int index = -kappa;
106+
GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[index] : 0));
107107
return;
108108
}
109109
}

include/rapidjson/internal/ieee754.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ class Double {
4848
int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; }
4949
uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; }
5050

51-
static unsigned EffectiveSignificandSize(int order) {
51+
static int EffectiveSignificandSize(int order) {
5252
if (order >= -1021)
5353
return 53;
5454
else if (order <= -1074)
5555
return 0;
5656
else
57-
return static_cast<unsigned>(order) + 1074;
57+
return order + 1074;
5858
}
5959

6060
private:

include/rapidjson/internal/regex.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,8 @@ class GenericRegexSearch {
688688
bool matched = AddState(l, s.out);
689689
return AddState(l, s.out1) || matched;
690690
}
691-
else if (!(stateSet_[index >> 5] & (1 << (index & 31)))) {
692-
stateSet_[index >> 5] |= (1 << (index & 31));
691+
else if (!(stateSet_[index >> 5] & (1u << (index & 31)))) {
692+
stateSet_[index >> 5] |= (1u << (index & 31));
693693
*l.template PushUnsafe<SizeType>() = index;
694694
}
695695
return s.out == kRegexInvalidState; // by using PushUnsafe() above, we can ensure s is not validated due to reallocation.

include/rapidjson/internal/strtod.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosit
140140
significand++;
141141

142142
size_t remaining = length - i;
143-
const unsigned kUlpShift = 3;
144-
const unsigned kUlp = 1 << kUlpShift;
143+
const int kUlpShift = 3;
144+
const int kUlp = 1 << kUlpShift;
145145
int64_t error = (remaining == 0) ? 0 : kUlp / 2;
146146

147147
DiyFp v(significand, 0);
@@ -177,17 +177,17 @@ inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosit
177177
v = v.Normalize();
178178
error <<= oldExp - v.e;
179179

180-
const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e);
181-
unsigned precisionSize = 64 - effectiveSignificandSize;
180+
const int effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e);
181+
int precisionSize = 64 - effectiveSignificandSize;
182182
if (precisionSize + kUlpShift >= 64) {
183-
unsigned scaleExp = (precisionSize + kUlpShift) - 63;
183+
int scaleExp = (precisionSize + kUlpShift) - 63;
184184
v.f >>= scaleExp;
185185
v.e += scaleExp;
186-
error = (error >> scaleExp) + 1 + static_cast<int>(kUlp);
186+
error = (error >> scaleExp) + 1 + kUlp;
187187
precisionSize -= scaleExp;
188188
}
189189

190-
DiyFp rounded(v.f >> precisionSize, v.e + static_cast<int>(precisionSize));
190+
DiyFp rounded(v.f >> precisionSize, v.e + precisionSize);
191191
const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp;
192192
const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp;
193193
if (precisionBits >= halfWay + static_cast<unsigned>(error)) {

include/rapidjson/istreamwrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class BasicIStreamWrapper {
5454

5555
Ch Peek() const {
5656
typename StreamType::int_type c = stream_.peek();
57-
return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast<Ch>(c) : '\0';
57+
return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast<Ch>(c) : static_cast<Ch>('\0');
5858
}
5959

6060
Ch Take() {

include/rapidjson/pointer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class GenericPointer {
274274
else {
275275
Ch name[21];
276276
for (size_t i = 0; i <= length; i++)
277-
name[i] = buffer[i];
277+
name[i] = static_cast<Ch>(buffer[i]);
278278
Token token = { name, length, index };
279279
return Append(token, allocator);
280280
}
@@ -1029,8 +1029,8 @@ class GenericPointer {
10291029
unsigned char u = static_cast<unsigned char>(c);
10301030
static const char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
10311031
os_.Put('%');
1032-
os_.Put(hexDigits[u >> 4]);
1033-
os_.Put(hexDigits[u & 15]);
1032+
os_.Put(static_cast<typename OutputStream::Ch>(hexDigits[u >> 4]));
1033+
os_.Put(static_cast<typename OutputStream::Ch>(hexDigits[u & 15]));
10341034
}
10351035
private:
10361036
OutputStream& os_;

0 commit comments

Comments
 (0)