ValueFlow: Improved value for cast

This commit is contained in:
Daniel Marjamäki 2016-12-20 21:37:25 +01:00
parent 606bb78297
commit 44ccbdedec
2 changed files with 5 additions and 1 deletions

View File

@ -323,6 +323,9 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value, const Setti
setTokenValue(parent, castValue(value, valueType.sign, settings->long_bit), settings);
else if (valueType.type == ValueType::Type::LONGLONG)
setTokenValue(parent, castValue(value, valueType.sign, settings->long_long_bit), settings);
else if (value.isIntValue() && value.intvalue < (1 << (settings->char_bit - 1)) && value.intvalue > -(1 << (settings->char_bit - 1)))
// unknown type, but value is small so there should be no truncation etc
setTokenValue(parent,value,settings);
}
else if (parent->str() == ":") {

View File

@ -377,7 +377,8 @@ private:
ASSERT_EQUALS(0, valueOfTok("3 < (a ? b : 2);", "<").intvalue);
ASSERT_EQUALS(0, valueOfTok("3 <= (a ? b : 2);", "<=").intvalue);
ASSERT(tokenValues("(UNKNOWN_TYPE)123;","(").empty());
ASSERT_EQUALS(1, valueOfTok("(UNKNOWN_TYPE)1;","(").intvalue);
ASSERT(tokenValues("(UNKNOWN_TYPE)1000;","(").empty()); // don't know if there is truncation, sign extension
ASSERT_EQUALS(255, valueOfTok("(unsigned char)~0;", "(").intvalue);
ASSERT_EQUALS(0, valueOfTok("(int)0;", "(").intvalue);
ASSERT_EQUALS(0, valueOfTok("(UNKNOWN_TYPE*)0;","(").intvalue);