Bug hunting; Better handling of constant

This commit is contained in:
Daniel Marjamäki 2020-10-08 17:24:19 +02:00
parent bb6b4f9f41
commit 65721dd7a9
2 changed files with 19 additions and 0 deletions

View File

@ -464,6 +464,17 @@ namespace {
return it->second;
if (!valueType)
return ExprEngine::ValuePtr();
// constant value..
const Variable *var = tokenizer->getSymbolDatabase()->getVariableFromVarId(varId);
if (var && valueType->constness == 1 && Token::Match(var->nameToken(), "%var% =")) {
const Token *initExpr = var->nameToken()->next()->astOperand2();
if (initExpr && initExpr->hasKnownIntValue()) {
auto intval = initExpr->getKnownIntValue();
return std::make_shared<ExprEngine::IntRange>(std::to_string(intval), intval, intval);
}
}
ExprEngine::ValuePtr value = getValueRangeFromValueType(getNewSymbolName(), valueType, *settings);
if (value) {
if (tok->variable() && tok->variable()->nameToken())

View File

@ -51,6 +51,8 @@ private:
TEST_CASE(exprAssign1);
TEST_CASE(exprAssign2); // Truncation
TEST_CASE(getValueConst1);
TEST_CASE(inc1);
TEST_CASE(inc2);
@ -348,6 +350,12 @@ private:
ASSERT_EQUALS("2", getRange("void f(unsigned char x) { x = 258; int a = x }", "a=x"));
}
void getValueConst1() { // Data::getValue
ASSERT_EQUALS("512", getRange("const int x=512; void func() { x=x }", "x=x"));
}
void inc1() {
ASSERT_EQUALS("(and (>= $1 (- 2147483648)) (<= $1 2147483647))\n"
"(= (+ $1 1) $1)\n"