ValueType: fix type of integer literals
This commit is contained in:
parent
7fb812f415
commit
c70987b727
|
@ -33,10 +33,30 @@ namespace cppcheck {
|
||||||
* @brief Platform settings
|
* @brief Platform settings
|
||||||
*/
|
*/
|
||||||
class CPPCHECKLIB Platform {
|
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:
|
public:
|
||||||
Platform();
|
Platform();
|
||||||
virtual ~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 char_bit; /// bits in char
|
||||||
unsigned int short_bit; /// bits in short
|
unsigned int short_bit; /// bits in short
|
||||||
unsigned int int_bit; /// bits in int
|
unsigned int int_bit; /// bits in int
|
||||||
|
|
|
@ -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);
|
::setValueType(tok, ValueType(ValueType::Sign::UNKNOWN_SIGN, type, 0U), cpp, defsign, settings);
|
||||||
} else if (MathLib::isInt(tok->str())) {
|
} else if (MathLib::isInt(tok->str())) {
|
||||||
ValueType::Sign sign = ValueType::Sign::SIGNED;
|
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()))
|
if (MathLib::isIntHex(tok->str()))
|
||||||
sign = ValueType::Sign::UNSIGNED;
|
sign = ValueType::Sign::UNSIGNED;
|
||||||
for (std::size_t pos = tok->str().size() - 1U; pos > 0U; --pos) {
|
for (std::size_t pos = tok->str().size() - 1U; pos > 0U; --pos) {
|
||||||
|
|
|
@ -3846,8 +3846,8 @@ private:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string typeOf(const char code[], const char pattern[], const char filename[] = "test.cpp") {
|
std::string typeOf(const char code[], const char pattern[], const char filename[] = "test.cpp", const Settings *settings = nullptr) {
|
||||||
Tokenizer tokenizer(&settings2, this);
|
Tokenizer tokenizer(settings ? settings : &settings2, this);
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
tokenizer.tokenize(istr, filename);
|
tokenizer.tokenize(istr, filename);
|
||||||
const Token* tok;
|
const Token* tok;
|
||||||
|
@ -3861,8 +3861,16 @@ private:
|
||||||
// stringification
|
// stringification
|
||||||
ASSERT_EQUALS("", ValueType().str());
|
ASSERT_EQUALS("", ValueType().str());
|
||||||
|
|
||||||
|
Settings s;
|
||||||
|
s.sizeof_int = 16;
|
||||||
|
s.sizeof_long = 32;
|
||||||
|
s.sizeof_long_long = 64;
|
||||||
|
|
||||||
// numbers
|
// 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("unsigned int", typeOf("1U", "1U"));
|
||||||
ASSERT_EQUALS("signed long", typeOf("1L", "1L"));
|
ASSERT_EQUALS("signed long", typeOf("1L", "1L"));
|
||||||
ASSERT_EQUALS("unsigned long", typeOf("1UL", "1UL"));
|
ASSERT_EQUALS("unsigned long", typeOf("1UL", "1UL"));
|
||||||
|
|
Loading…
Reference in New Issue