From c70987b72750caffb6ea446941f3b4370e755408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 16 Oct 2016 11:36:22 +0200 Subject: [PATCH] ValueType: fix type of integer literals --- lib/platform.h | 20 ++++++++++++++++++++ lib/symboldatabase.cpp | 11 ++++++++++- test/testsymboldatabase.cpp | 14 +++++++++++--- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/platform.h b/lib/platform.h index 339480357..9c15c5462 100644 --- a/lib/platform.h +++ b/lib/platform.h @@ -33,10 +33,30 @@ namespace cppcheck { * @brief Platform settings */ class CPPCHECKLIB Platform { + private: + long long min_value(int sz) const { + if (sz >= 64) + return 1LL << 63; + return -(1LL << (sz-1)); + } + + long long max_value(int sz) const { + if (sz >= 64) + return (~0ULL) >> 1; + return (1LL << (sz-1)) - 1LL; + } public: Platform(); virtual ~Platform() {} + bool isIntValue(long long value) const { + return value >= min_value(sizeof_int) && value <= max_value(sizeof_int); + } + + bool isLongValue(long long value) const { + return value >= min_value(sizeof_long) && value <= max_value(sizeof_long); + } + unsigned int char_bit; /// bits in char unsigned int short_bit; /// bits in short unsigned int int_bit; /// bits in int diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 8f394d925..48a102ae7 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -4535,7 +4535,16 @@ void SymbolDatabase::setValueTypeInTokenList(Token *tokens, bool cpp, const Sett ::setValueType(tok, ValueType(ValueType::Sign::UNKNOWN_SIGN, type, 0U), cpp, defsign, settings); } else if (MathLib::isInt(tok->str())) { ValueType::Sign sign = ValueType::Sign::SIGNED; - ValueType::Type type = ValueType::Type::INT; + ValueType::Type type; + const MathLib::bigint value = MathLib::toLongNumber(tok->str()); + if (settings->platformType == cppcheck::Platform::Unspecified) + type = ValueType::Type::INT; + else if (settings->isIntValue(value)) + type = ValueType::Type::INT; + else if (settings->isLongValue(value)) + type = ValueType::Type::LONG; + else + type = ValueType::Type::LONGLONG; if (MathLib::isIntHex(tok->str())) sign = ValueType::Sign::UNSIGNED; for (std::size_t pos = tok->str().size() - 1U; pos > 0U; --pos) { diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index d41735c88..d14cd4bd9 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -3846,8 +3846,8 @@ private: } } - std::string typeOf(const char code[], const char pattern[], const char filename[] = "test.cpp") { - Tokenizer tokenizer(&settings2, this); + std::string typeOf(const char code[], const char pattern[], const char filename[] = "test.cpp", const Settings *settings = nullptr) { + Tokenizer tokenizer(settings ? settings : &settings2, this); std::istringstream istr(code); tokenizer.tokenize(istr, filename); const Token* tok; @@ -3861,8 +3861,16 @@ private: // stringification ASSERT_EQUALS("", ValueType().str()); + Settings s; + s.sizeof_int = 16; + s.sizeof_long = 32; + s.sizeof_long_long = 64; + // numbers - ASSERT_EQUALS("signed int", typeOf("1", "1")); + ASSERT_EQUALS("signed int", typeOf("1", "1", "test.c", &s)); + ASSERT_EQUALS("signed int", typeOf("32767", "32767", "test.c", &s)); + ASSERT_EQUALS("signed long", typeOf("32768", "32768", "test.c", &s)); + ASSERT_EQUALS("signed long long", typeOf("2147483648", "2147483648", "test.c", &s)); ASSERT_EQUALS("unsigned int", typeOf("1U", "1U")); ASSERT_EQUALS("signed long", typeOf("1L", "1L")); ASSERT_EQUALS("unsigned long", typeOf("1UL", "1UL"));