ValueType: fix type of integer literals

This commit is contained in:
Daniel Marjamäki 2016-10-16 11:36:22 +02:00
parent 7fb812f415
commit c70987b727
3 changed files with 41 additions and 4 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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"));