ValueType: Improved signedness

This commit is contained in:
Daniel Marjamäki 2015-10-07 18:10:48 +02:00
parent 2e510c8733
commit db31e22316
2 changed files with 61 additions and 9 deletions

View File

@ -3697,8 +3697,29 @@ static void setValueType(Token *tok, ValueType::Sign sign, ValueType::Type type,
return;
}
if (vt1->isIntegral() && vt2->isIntegral()) {
ValueType::Type t = (vt1->type > vt2->type) ? vt1->type : vt2->type;
setValueType(parent, ValueType::Sign::UNKNOWN_SIGN, t, 0U);
ValueType::Type t;
ValueType::Sign s;
if (vt1->type == vt2->type) {
t = vt1->type;
if (vt1->sign == ValueType::Sign::UNSIGNED || vt2->sign == ValueType::Sign::UNSIGNED)
s = ValueType::Sign::UNSIGNED;
else if (vt1->sign == ValueType::Sign::UNKNOWN_SIGN || vt2->sign == ValueType::Sign::UNKNOWN_SIGN)
s = ValueType::Sign::UNKNOWN_SIGN;
else
s = ValueType::Sign::SIGNED;
} else if (vt1->type > vt2->type) {
t = vt1->type;
s = vt1->sign;
} else {
t = vt2->type;
s = vt2->sign;
}
if (t < ValueType::Type::INT) {
t = ValueType::Type::INT;
s = ValueType::Sign::SIGNED;
}
setValueType(parent, s, t, 0U);
return;
}
}
@ -3743,10 +3764,26 @@ void SymbolDatabase::setValueTypeInTokenList(Token *tokens)
for (Token *tok = tokens; tok; tok = tok->next()) {
if (tok->isNumber()) {
if (MathLib::isFloat(tok->str()))
::setValueType(tok, ValueType::Sign::UNKNOWN_SIGN, ValueType::Type::FLOAT, 0U);
if (MathLib::isInt(tok->str()))
::setValueType(tok, (tok->str()[0] == '-') ? ValueType::Sign::SIGNED : ValueType::Sign::UNKNOWN_SIGN, ValueType::Type::INT, 0U);
if (MathLib::isFloat(tok->str())) {
ValueType::Type type = ValueType::Type::DOUBLE;
if (tok->str()[tok->str().size() - 1U] == 'f')
type = ValueType::Type::FLOAT;
::setValueType(tok, ValueType::Sign::UNKNOWN_SIGN, type, 0U);
} else if (MathLib::isInt(tok->str())) {
ValueType::Sign sign = ValueType::Sign::SIGNED;
ValueType::Type type = ValueType::Type::INT;
if (MathLib::isIntHex(tok->str()))
sign = ValueType::Sign::UNSIGNED;
else if (tok->str().find_first_of("uU") != std::string::npos)
sign = ValueType::Sign::UNSIGNED;
if (tok->str()[tok->str().size() - 1U] == 'L') {
if (tok->str()[tok->str().size() - 2U] == 'L')
type = ValueType::Type::LONGLONG;
else
type = ValueType::Type::LONG;
}
::setValueType(tok, sign, type, 0U);
}
} else if (tok->isComparisonOp())
::setValueType(tok, ValueType::Sign::UNKNOWN_SIGN, ValueType::Type::BOOL, 0U);
else if (tok->tokType() == Token::eChar)

View File

@ -2956,10 +2956,20 @@ private:
}
void valuetype() {
// numbers
ASSERT_EQUALS("signed int", typeOf("1", "1"));
ASSERT_EQUALS("unsigned int", typeOf("1U", "1U"));
ASSERT_EQUALS("signed long", typeOf("1L", "1L"));
ASSERT_EQUALS("unsigned long", typeOf("1UL", "1UL"));
ASSERT_EQUALS("signed long long", typeOf("1LL", "1LL"));
ASSERT_EQUALS("unsigned long long", typeOf("1ULL", "1ULL"));
ASSERT_EQUALS("float", typeOf("1.0f", "1.0f"));
ASSERT_EQUALS("double", typeOf("1.0", "1.0"));
// Constant calculations
ASSERT_EQUALS("int", typeOf("1 + 2", "+"));
//ASSERT_EQUALS("long", typeOf("1L + 2", "+"));
//ASSERT_EQUALS("long long", typeOf("1LL + 2", "+"));
ASSERT_EQUALS("signed int", typeOf("1 + 2", "+"));
ASSERT_EQUALS("signed long", typeOf("1L + 2", "+"));
ASSERT_EQUALS("signed long long", typeOf("1LL + 2", "+"));
ASSERT_EQUALS("float", typeOf("1.2f + 3", "+"));
ASSERT_EQUALS("float", typeOf("1 + 2.3f", "+"));
@ -2973,6 +2983,11 @@ private:
ASSERT_EQUALS("float", typeOf("float x; a = x + 1;", "+"));
ASSERT_EQUALS("signed int", typeOf("signed x; a = x + 1;", "x +"));
ASSERT_EQUALS("unsigned int", typeOf("unsigned x; a = x + 1;", "x +"));
ASSERT_EQUALS("unsigned int", typeOf("unsigned int u1, u2; a = u1 + 1;", "u1 +"));
ASSERT_EQUALS("unsigned int", typeOf("unsigned int u1, u2; a = u1 + 1U;", "u1 +"));
ASSERT_EQUALS("unsigned int", typeOf("unsigned int u1, u2; a = u1 + u2;", "u1 +"));
ASSERT_EQUALS("unsigned int", typeOf("unsigned int u1, u2; a = u1 * 2;", "u1 *"));
ASSERT_EQUALS("unsigned int", typeOf("unsigned int u1, u2; a = u1 * u2;", "u1 *"));
// array..
ASSERT_EQUALS("int*", typeOf("int x[10]; a = x + 1;", "+"));