Bug hunting: Handle not better

This commit is contained in:
Daniel Marjamäki 2020-12-13 13:00:04 +01:00
parent 0ccc5c695b
commit b18dc0fdbb
2 changed files with 18 additions and 0 deletions

View File

@ -2267,6 +2267,17 @@ static ExprEngine::ValuePtr executeDeref(const Token *tok, Data &data)
return ExprEngine::ValuePtr(); return ExprEngine::ValuePtr();
} }
static ExprEngine::ValuePtr executeNot(const Token *tok, Data &data)
{
ExprEngine::ValuePtr v = executeExpression(tok->astOperand1(), data);
if (!v)
return v;
ExprEngine::ValuePtr zero = std::make_shared<ExprEngine::IntRange>("0", 0, 0);
auto result = simplifyValue(std::make_shared<ExprEngine::BinOpResult>("==", v, zero));
call(data.callbacks, tok, result, &data);
return result;
}
static ExprEngine::ValuePtr executeVariable(const Token *tok, Data &data) static ExprEngine::ValuePtr executeVariable(const Token *tok, Data &data)
{ {
auto val = data.getValue(tok->varId(), tok->valueType(), tok); auto val = data.getValue(tok->varId(), tok->valueType(), tok);
@ -2345,6 +2356,9 @@ static ExprEngine::ValuePtr executeExpression1(const Token *tok, Data &data)
if (tok->isUnaryOp("*")) if (tok->isUnaryOp("*"))
return executeDeref(tok, data); return executeDeref(tok, data);
if (tok->isUnaryOp("!"))
return executeNot(tok, data);
if (tok->varId()) if (tok->varId())
return executeVariable(tok, data); return executeVariable(tok, data);

View File

@ -50,6 +50,7 @@ private:
TEST_CASE(expr9); TEST_CASE(expr9);
TEST_CASE(exprAssign1); TEST_CASE(exprAssign1);
TEST_CASE(exprAssign2); // Truncation TEST_CASE(exprAssign2); // Truncation
TEST_CASE(exprNot);
TEST_CASE(getValueConst1); TEST_CASE(getValueConst1);
@ -352,6 +353,9 @@ private:
ASSERT_EQUALS("2", getRange("void f(unsigned char x) { x = 258; int a = x }", "a=x")); ASSERT_EQUALS("2", getRange("void f(unsigned char x) { x = 258; int a = x }", "a=x"));
} }
void exprNot() {
ASSERT_EQUALS("($1)==(0)", getRange("void f(unsigned char a) { return !a; }", "!a"));
}
void getValueConst1() { // Data::getValue void getValueConst1() { // Data::getValue
ASSERT_EQUALS("512", getRange("const int x=512; void func() { x=x }", "x=x")); ASSERT_EQUALS("512", getRange("const int x=512; void func() { x=x }", "x=x"));