diff --git a/lib/checktype.cpp b/lib/checktype.cpp index a191660a6..ec39223b6 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -60,11 +60,11 @@ void CheckType::checkTooBigBitwiseShift() continue; int lhsbits = 0; if (lhstype->type <= ValueType::Type::INT) - lhsbits = 8 * _settings->sizeof_int; + lhsbits = _settings->int_bit; else if (lhstype->type == ValueType::Type::LONG) - lhsbits = 8 * _settings->sizeof_long; + lhsbits = _settings->long_bit; else if (lhstype->type == ValueType::Type::LONGLONG) - lhsbits = 8 * _settings->sizeof_long_long; + lhsbits = _settings->long_long_bit; else continue; @@ -105,7 +105,7 @@ void CheckType::checkIntegerOverflow() return; // max int value according to platform settings. - const MathLib::bigint maxint = (1LL << (8 * _settings->sizeof_int - 1)) - 1; + const MathLib::bigint maxint = (1LL << (_settings->int_bit - 1)) - 1; const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const std::size_t functions = symbolDatabase->functionScopes.size(); diff --git a/lib/settings.cpp b/lib/settings.cpp index da5633154..db56c008b 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -154,6 +154,11 @@ bool Settings::platform(PlatformType type) sizeof_size_t = sizeof(std::size_t); sizeof_pointer = sizeof(void *); defaultSign = '\0'; + char_bit = 8; + short_bit = char_bit * sizeof_short; + int_bit = char_bit * sizeof_int; + long_bit = char_bit * sizeof_long; + long_long_bit = char_bit * sizeof_long_long; return true; case Native: // same as system this code was compile on platformType = type; @@ -172,6 +177,11 @@ bool Settings::platform(PlatformType type) int x = 2; defaultSign = (-10 / x == -5) ? 's' : 'u'; } + char_bit = 8; + short_bit = char_bit * sizeof_short; + int_bit = char_bit * sizeof_int; + long_bit = char_bit * sizeof_long; + long_long_bit = char_bit * sizeof_long_long; return true; case Win32W: case Win32A: @@ -188,6 +198,11 @@ bool Settings::platform(PlatformType type) sizeof_size_t = 4; sizeof_pointer = 4; defaultSign = '\0'; + char_bit = 8; + short_bit = char_bit * sizeof_short; + int_bit = char_bit * sizeof_int; + long_bit = char_bit * sizeof_long; + long_long_bit = char_bit * sizeof_long_long; return true; case Win64: platformType = type; @@ -203,6 +218,11 @@ bool Settings::platform(PlatformType type) sizeof_size_t = 8; sizeof_pointer = 8; defaultSign = '\0'; + char_bit = 8; + short_bit = char_bit * sizeof_short; + int_bit = char_bit * sizeof_int; + long_bit = char_bit * sizeof_long; + long_long_bit = char_bit * sizeof_long_long; return true; case Unix32: platformType = type; @@ -218,6 +238,11 @@ bool Settings::platform(PlatformType type) sizeof_size_t = 4; sizeof_pointer = 4; defaultSign = '\0'; + char_bit = 8; + short_bit = char_bit * sizeof_short; + int_bit = char_bit * sizeof_int; + long_bit = char_bit * sizeof_long; + long_long_bit = char_bit * sizeof_long_long; return true; case Unix64: platformType = type; @@ -233,6 +258,11 @@ bool Settings::platform(PlatformType type) sizeof_size_t = 8; sizeof_pointer = 8; defaultSign = '\0'; + char_bit = 8; + short_bit = char_bit * sizeof_short; + int_bit = char_bit * sizeof_int; + long_bit = char_bit * sizeof_long; + long_long_bit = char_bit * sizeof_long_long; return true; } diff --git a/lib/settings.h b/lib/settings.h index 0c4195e59..13532e351 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -243,6 +243,12 @@ public: /** Struct contains standards settings */ Standards standards; + unsigned int char_bit; /// bits in char + unsigned int short_bit; /// bits in short + unsigned int int_bit; /// bits in int + unsigned int long_bit; /// bits in long + unsigned int long_long_bit; /// bits in long long + /** size of standard types */ unsigned int sizeof_bool; unsigned int sizeof_short; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 7802cc930..a0181f87b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4909,7 +4909,7 @@ void Tokenizer::simplifyCasts() // #4164 : ((unsigned char)1) => (1) if (Token::Match(tok->next(), "( %type% ) %num%") && tok->next()->link()->previous()->isStandardType()) { const MathLib::bigint value = MathLib::toLongNumber(tok->next()->link()->next()->str()); - unsigned int bits = 8 * _typeSize[tok->next()->link()->previous()->str()]; + unsigned int bits = _settings->char_bit * _typeSize[tok->next()->link()->previous()->str()]; if (!tok->tokAt(2)->isUnsigned() && bits > 0) bits--; if (bits < 31 && value >= 0 && value < (1LL << bits)) {