Skip to content

Commit e3e77a1

Browse files
committed
platform.h: fixed clang-analyzer-core.BitwiseShift warnings and adjusted types of *_bit members
1 parent 81f0086 commit e3e77a1

7 files changed

+20
-16
lines changed

Diff for: lib/checkcondition.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,7 @@ void CheckCondition::checkCompareValueOutOfTypeRange()
19441944
continue;
19451945
if (valueTok->valueType() && valueTok->valueType()->isTypeEqual(typeTok->valueType()))
19461946
continue;
1947-
int bits = 0;
1947+
std::uint8_t bits = 0;
19481948
switch (typeTok->valueType()->type) {
19491949
case ValueType::Type::BOOL:
19501950
bits = 1;

Diff for: lib/checktype.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void CheckType::checkTooBigBitwiseShift()
8888
// C11 Standard, section 6.5.7 Bitwise shift operators, states:
8989
// The integer promotions are performed on each of the operands.
9090
// The type of the result is that of the promoted left operand.
91-
int lhsbits;
91+
std::uint8_t lhsbits;
9292
if ((lhstype->type == ValueType::Type::CHAR) ||
9393
(lhstype->type == ValueType::Type::SHORT) ||
9494
(lhstype->type == ValueType::Type::WCHAR_T) ||

Diff for: lib/platform.h

+12-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "mathlib.h"
2626
#include "standards.h"
2727

28+
#include <cassert>
2829
#include <climits>
2930
#include <cstddef>
3031
#include <cstdint>
@@ -44,19 +45,22 @@ namespace tinyxml2 {
4445
*/
4546
class CPPCHECKLIB Platform {
4647
private:
47-
static long long min_value(int bit) {
48+
static long long min_value(std::uint8_t bit) {
49+
assert(bit > 0);
4850
if (bit >= 64)
4951
return LLONG_MIN;
5052
return -(1LL << (bit-1));
5153
}
5254

53-
static long long max_value(int bit) {
55+
static long long max_value(std::uint8_t bit) {
56+
assert(bit > 0);
5457
if (bit >= 64)
5558
return (~0ULL) >> 1;
5659
return (1LL << (bit-1)) - 1LL;
5760
}
5861

59-
static unsigned long long max_value_unsigned(int bit) {
62+
static unsigned long long max_value_unsigned(std::uint8_t bit) {
63+
assert(bit > 0);
6064
if (bit >= 64)
6165
return ~0ULL;
6266
return (1ULL << bit) - 1ULL;
@@ -90,11 +94,11 @@ class CPPCHECKLIB Platform {
9094
return value <= longLongMax;
9195
}
9296

93-
nonneg int char_bit; /// bits in char
94-
nonneg int short_bit; /// bits in short
95-
nonneg int int_bit; /// bits in int
96-
nonneg int long_bit; /// bits in long
97-
nonneg int long_long_bit; /// bits in long long
97+
std::uint8_t char_bit; /// bits in char
98+
std::uint8_t short_bit; /// bits in short
99+
std::uint8_t int_bit; /// bits in int
100+
std::uint8_t long_bit; /// bits in long
101+
std::uint8_t long_long_bit; /// bits in long long
98102

99103
/** size of standard types */
100104
std::size_t sizeof_bool;

Diff for: lib/symboldatabase.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1848,7 +1848,7 @@ void SymbolDatabase::setArrayDimensionsUsingValueFlow()
18481848
}
18491849

18501850
if (dimension.tok->valueType() && dimension.tok->valueType()->pointer == 0) {
1851-
int bits = 0;
1851+
std::uint8_t bits = 0;
18521852
switch (dimension.tok->valueType()->type) {
18531853
case ValueType::Type::CHAR:
18541854
bits = mSettings.platform.char_bit;

Diff for: lib/valueflow.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -959,13 +959,13 @@ static void valueFlowRightShift(TokenList& tokenList, const Settings& settings)
959959
continue;
960960
if (lhsmax < 0)
961961
continue;
962-
int lhsbits;
962+
std::uint8_t lhsbits;
963963
if ((tok->astOperand1()->valueType()->type == ValueType::Type::CHAR) ||
964964
(tok->astOperand1()->valueType()->type == ValueType::Type::SHORT) ||
965965
(tok->astOperand1()->valueType()->type == ValueType::Type::WCHAR_T) ||
966966
(tok->astOperand1()->valueType()->type == ValueType::Type::BOOL) ||
967967
(tok->astOperand1()->valueType()->type == ValueType::Type::INT))
968-
lhsbits = settings.platform.int_bit;
968+
lhsbits = settings.platform.int_bit; // TODO: needs to use the proper *_bit fo each type
969969
else if (tok->astOperand1()->valueType()->type == ValueType::Type::LONG)
970970
lhsbits = settings.platform.long_bit;
971971
else if (tok->astOperand1()->valueType()->type == ValueType::Type::LONGLONG)

Diff for: lib/vf_common.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace ValueFlow
4444
if (!vt || !vt->isIntegral() || vt->pointer)
4545
return false;
4646

47-
int bits;
47+
std::uint8_t bits;
4848
switch (vt->type) {
4949
case ValueType::Type::BOOL:
5050
bits = 1;

Diff for: lib/vf_settokenvalue.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ namespace ValueFlow
608608
continue;
609609
Value v(val);
610610
v.intvalue = ~v.intvalue;
611-
int bits = 0;
611+
std::uint8_t bits = 0;
612612
if (tok->valueType() &&
613613
tok->valueType()->sign == ValueType::Sign::UNSIGNED &&
614614
tok->valueType()->pointer == 0) {
@@ -618,7 +618,7 @@ namespace ValueFlow
618618
bits = settings.platform.long_bit;
619619
}
620620
if (bits > 0 && bits < MathLib::bigint_bits)
621-
v.intvalue &= (((MathLib::biguint)1)<<bits) - 1;
621+
v.intvalue &= (1ULL<<bits) - 1;
622622
setTokenValue(parent, std::move(v), settings);
623623
}
624624
}

0 commit comments

Comments
 (0)