diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 6fc8ccf46b2..91249c1b700 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -1944,7 +1944,7 @@ void CheckCondition::checkCompareValueOutOfTypeRange() continue; if (valueTok->valueType() && valueTok->valueType()->isTypeEqual(typeTok->valueType())) continue; - int bits = 0; + std::uint8_t bits = 0; switch (typeTok->valueType()->type) { case ValueType::Type::BOOL: bits = 1; diff --git a/lib/checktype.cpp b/lib/checktype.cpp index de0a69cf109..27b71d2add2 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -88,7 +88,7 @@ void CheckType::checkTooBigBitwiseShift() // C11 Standard, section 6.5.7 Bitwise shift operators, states: // The integer promotions are performed on each of the operands. // The type of the result is that of the promoted left operand. - int lhsbits; + std::uint8_t lhsbits; if ((lhstype->type == ValueType::Type::CHAR) || (lhstype->type == ValueType::Type::SHORT) || (lhstype->type == ValueType::Type::WCHAR_T) || diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index fa4c205110f..61bd3fd4cbf 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -391,11 +391,11 @@ static void createDumpFile(const Settings& settings, fdump << "\n"; fdump << " (settings.platform.char_bit) << '\"' + << " short_bit=\"" << static_cast(settings.platform.short_bit) << '\"' + << " int_bit=\"" << static_cast(settings.platform.int_bit) << '\"' + << " long_bit=\"" << static_cast(settings.platform.long_bit) << '\"' + << " long_long_bit=\"" << static_cast(settings.platform.long_long_bit) << '\"' << " pointer_bit=\"" << (settings.platform.sizeof_pointer * settings.platform.char_bit) << '\"' << " wchar_t_bit=\"" << (settings.platform.sizeof_wchar_t * settings.platform.char_bit) << '\"' << " size_t_bit=\"" << (settings.platform.sizeof_size_t * settings.platform.char_bit) << '\"' diff --git a/lib/platform.h b/lib/platform.h index 63dd321cb4a..098405ae1f4 100644 --- a/lib/platform.h +++ b/lib/platform.h @@ -25,6 +25,7 @@ #include "mathlib.h" #include "standards.h" +#include #include #include #include @@ -44,19 +45,22 @@ namespace tinyxml2 { */ class CPPCHECKLIB Platform { private: - static long long min_value(int bit) { + static long long min_value(std::uint8_t bit) { + assert(bit > 0); if (bit >= 64) return LLONG_MIN; return -(1LL << (bit-1)); } - static long long max_value(int bit) { + static long long max_value(std::uint8_t bit) { + assert(bit > 0); if (bit >= 64) return (~0ULL) >> 1; return (1LL << (bit-1)) - 1LL; } - static unsigned long long max_value_unsigned(int bit) { + static unsigned long long max_value_unsigned(std::uint8_t bit) { + assert(bit > 0); if (bit >= 64) return ~0ULL; return (1ULL << bit) - 1ULL; @@ -90,11 +94,11 @@ class CPPCHECKLIB Platform { return value <= longLongMax; } - nonneg int char_bit; /// bits in char - nonneg int short_bit; /// bits in short - nonneg int int_bit; /// bits in int - nonneg int long_bit; /// bits in long - nonneg int long_long_bit; /// bits in long long + std::uint8_t char_bit; /// bits in char + std::uint8_t short_bit; /// bits in short + std::uint8_t int_bit; /// bits in int + std::uint8_t long_bit; /// bits in long + std::uint8_t long_long_bit; /// bits in long long /** size of standard types */ std::size_t sizeof_bool; diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 65c70b12b8b..2570c819f5e 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1848,7 +1848,7 @@ void SymbolDatabase::setArrayDimensionsUsingValueFlow() } if (dimension.tok->valueType() && dimension.tok->valueType()->pointer == 0) { - int bits = 0; + std::uint8_t bits = 0; switch (dimension.tok->valueType()->type) { case ValueType::Type::CHAR: bits = mSettings.platform.char_bit; diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 9f5254b02c7..6186b90d4e6 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -959,13 +959,13 @@ static void valueFlowRightShift(TokenList& tokenList, const Settings& settings) continue; if (lhsmax < 0) continue; - int lhsbits; + std::uint8_t lhsbits; if ((tok->astOperand1()->valueType()->type == ValueType::Type::CHAR) || (tok->astOperand1()->valueType()->type == ValueType::Type::SHORT) || (tok->astOperand1()->valueType()->type == ValueType::Type::WCHAR_T) || (tok->astOperand1()->valueType()->type == ValueType::Type::BOOL) || (tok->astOperand1()->valueType()->type == ValueType::Type::INT)) - lhsbits = settings.platform.int_bit; + lhsbits = settings.platform.int_bit; // TODO: needs to use the proper *_bit fo each type else if (tok->astOperand1()->valueType()->type == ValueType::Type::LONG) lhsbits = settings.platform.long_bit; else if (tok->astOperand1()->valueType()->type == ValueType::Type::LONGLONG) diff --git a/lib/vf_common.cpp b/lib/vf_common.cpp index 0717cb6db66..ad47b3d93d0 100644 --- a/lib/vf_common.cpp +++ b/lib/vf_common.cpp @@ -44,7 +44,7 @@ namespace ValueFlow if (!vt || !vt->isIntegral() || vt->pointer) return false; - int bits; + std::uint8_t bits; switch (vt->type) { case ValueType::Type::BOOL: bits = 1; diff --git a/lib/vf_settokenvalue.cpp b/lib/vf_settokenvalue.cpp index 6a8eea7820c..4ed1d59b2be 100644 --- a/lib/vf_settokenvalue.cpp +++ b/lib/vf_settokenvalue.cpp @@ -608,7 +608,7 @@ namespace ValueFlow continue; Value v(val); v.intvalue = ~v.intvalue; - int bits = 0; + std::uint8_t bits = 0; if (tok->valueType() && tok->valueType()->sign == ValueType::Sign::UNSIGNED && tok->valueType()->pointer == 0) { @@ -618,7 +618,7 @@ namespace ValueFlow bits = settings.platform.long_bit; } if (bits > 0 && bits < MathLib::bigint_bits) - v.intvalue &= (((MathLib::biguint)1)<