2019-10-30 17:57:46 +01:00
|
|
|
|
|
|
|
#include "programmemory.h"
|
|
|
|
#include "astutils.h"
|
2021-10-04 07:53:58 +02:00
|
|
|
#include "calculate.h"
|
|
|
|
#include "errortypes.h"
|
2021-10-30 22:13:58 +02:00
|
|
|
#include "infer.h"
|
2021-01-21 19:49:37 +01:00
|
|
|
#include "mathlib.h"
|
2021-08-23 09:03:48 +02:00
|
|
|
#include "settings.h"
|
2021-10-04 07:53:58 +02:00
|
|
|
#include "symboldatabase.h"
|
2021-01-21 19:49:37 +01:00
|
|
|
#include "token.h"
|
2021-03-30 14:02:28 +02:00
|
|
|
#include "valueflow.h"
|
2021-10-30 22:13:58 +02:00
|
|
|
#include "valueptr.h"
|
2020-05-19 08:53:38 +02:00
|
|
|
#include <algorithm>
|
2019-10-30 17:57:46 +01:00
|
|
|
#include <cassert>
|
2021-10-04 07:53:58 +02:00
|
|
|
#include <cmath>
|
2021-07-24 22:44:18 +02:00
|
|
|
#include <functional>
|
2020-12-19 21:54:19 +01:00
|
|
|
#include <limits>
|
2020-09-11 23:03:57 +02:00
|
|
|
#include <memory>
|
2019-10-30 17:57:46 +01:00
|
|
|
|
2021-01-27 19:49:13 +01:00
|
|
|
void ProgramMemory::setValue(nonneg int exprid, const ValueFlow::Value& value)
|
2021-01-22 21:47:24 +01:00
|
|
|
{
|
|
|
|
values[exprid] = value;
|
|
|
|
}
|
2021-01-28 12:37:56 +01:00
|
|
|
const ValueFlow::Value* ProgramMemory::getValue(nonneg int exprid, bool impossible) const
|
2019-10-30 17:58:19 +01:00
|
|
|
{
|
2021-01-21 19:49:37 +01:00
|
|
|
const ProgramMemory::Map::const_iterator it = values.find(exprid);
|
2021-01-28 12:37:56 +01:00
|
|
|
const bool found = it != values.end() && (impossible || !it->second.isImpossible());
|
2019-10-30 17:57:46 +01:00
|
|
|
if (found)
|
2020-09-11 23:03:57 +02:00
|
|
|
return &it->second;
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-01-27 19:49:13 +01:00
|
|
|
bool ProgramMemory::getIntValue(nonneg int exprid, MathLib::bigint* result) const
|
2020-09-11 23:03:57 +02:00
|
|
|
{
|
2021-01-21 19:49:37 +01:00
|
|
|
const ValueFlow::Value* value = getValue(exprid);
|
2020-09-11 23:03:57 +02:00
|
|
|
if (value && value->isIntValue()) {
|
|
|
|
*result = value->intvalue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
2021-10-04 07:53:58 +02:00
|
|
|
void ProgramMemory::setIntValue(nonneg int exprid, MathLib::bigint value, bool impossible)
|
2019-10-30 17:58:19 +01:00
|
|
|
{
|
2021-10-04 07:53:58 +02:00
|
|
|
ValueFlow::Value v(value);
|
|
|
|
if (impossible)
|
|
|
|
v.setImpossible();
|
|
|
|
values[exprid] = v;
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 19:49:13 +01:00
|
|
|
bool ProgramMemory::getTokValue(nonneg int exprid, const Token** result) const
|
2019-10-30 17:58:19 +01:00
|
|
|
{
|
2021-01-21 19:49:37 +01:00
|
|
|
const ValueFlow::Value* value = getValue(exprid);
|
2020-09-11 23:03:57 +02:00
|
|
|
if (value && value->isTokValue()) {
|
|
|
|
*result = value->tokvalue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 19:49:13 +01:00
|
|
|
bool ProgramMemory::getContainerSizeValue(nonneg int exprid, MathLib::bigint* result) const
|
2020-08-11 03:08:49 +02:00
|
|
|
{
|
2021-01-21 19:49:37 +01:00
|
|
|
const ValueFlow::Value* value = getValue(exprid);
|
2020-09-11 23:03:57 +02:00
|
|
|
if (value && value->isContainerSizeValue()) {
|
|
|
|
*result = value->intvalue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-08-11 03:08:49 +02:00
|
|
|
}
|
2021-01-28 12:37:56 +01:00
|
|
|
bool ProgramMemory::getContainerEmptyValue(nonneg int exprid, MathLib::bigint* result) const
|
|
|
|
{
|
|
|
|
const ValueFlow::Value* value = getValue(exprid, true);
|
|
|
|
if (value && value->isContainerSizeValue()) {
|
|
|
|
if (value->isImpossible() && value->intvalue == 0) {
|
|
|
|
*result = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!value->isImpossible()) {
|
|
|
|
*result = (value->intvalue == 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-11 03:08:49 +02:00
|
|
|
|
2021-07-16 18:49:07 +02:00
|
|
|
void ProgramMemory::setContainerSizeValue(nonneg int exprid, MathLib::bigint value, bool isEqual)
|
|
|
|
{
|
|
|
|
ValueFlow::Value v(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
|
|
|
|
if (!isEqual)
|
|
|
|
v.valueKind = ValueFlow::Value::ValueKind::Impossible;
|
|
|
|
values[exprid] = v;
|
|
|
|
}
|
|
|
|
|
2021-01-27 19:49:13 +01:00
|
|
|
void ProgramMemory::setUnknown(nonneg int exprid)
|
2020-02-13 17:04:05 +01:00
|
|
|
{
|
2021-01-21 19:49:37 +01:00
|
|
|
values[exprid].valueType = ValueFlow::Value::ValueType::UNINIT;
|
2020-02-13 17:04:05 +01:00
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2021-01-27 19:49:13 +01:00
|
|
|
bool ProgramMemory::hasValue(nonneg int exprid)
|
2021-01-22 21:47:24 +01:00
|
|
|
{
|
|
|
|
return values.find(exprid) != values.end();
|
|
|
|
}
|
2019-10-30 17:57:46 +01:00
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
void ProgramMemory::swap(ProgramMemory &pm)
|
|
|
|
{
|
2019-10-30 17:57:46 +01:00
|
|
|
values.swap(pm.values);
|
|
|
|
}
|
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
void ProgramMemory::clear()
|
|
|
|
{
|
2019-10-30 17:57:46 +01:00
|
|
|
values.clear();
|
|
|
|
}
|
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
bool ProgramMemory::empty() const
|
|
|
|
{
|
2019-10-30 17:57:46 +01:00
|
|
|
return values.empty();
|
|
|
|
}
|
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
void ProgramMemory::replace(const ProgramMemory &pm)
|
|
|
|
{
|
2021-01-21 19:49:37 +01:00
|
|
|
for (auto&& p : pm.values) {
|
2019-10-30 17:57:46 +01:00
|
|
|
values[p.first] = p.second;
|
2021-01-21 19:49:37 +01:00
|
|
|
}
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
void ProgramMemory::insert(const ProgramMemory &pm)
|
|
|
|
{
|
2019-10-30 17:57:46 +01:00
|
|
|
for (auto&& p:pm.values)
|
|
|
|
values.insert(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool conditionIsFalse(const Token *condition, const ProgramMemory &programMemory)
|
|
|
|
{
|
|
|
|
if (!condition)
|
|
|
|
return false;
|
|
|
|
if (condition->str() == "&&") {
|
|
|
|
return conditionIsFalse(condition->astOperand1(), programMemory) ||
|
|
|
|
conditionIsFalse(condition->astOperand2(), programMemory);
|
|
|
|
}
|
|
|
|
ProgramMemory progmem(programMemory);
|
|
|
|
MathLib::bigint result = 0;
|
|
|
|
bool error = false;
|
|
|
|
execute(condition, &progmem, &result, &error);
|
|
|
|
return !error && result == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool conditionIsTrue(const Token *condition, const ProgramMemory &programMemory)
|
|
|
|
{
|
|
|
|
if (!condition)
|
|
|
|
return false;
|
|
|
|
if (condition->str() == "||") {
|
|
|
|
return conditionIsTrue(condition->astOperand1(), programMemory) ||
|
|
|
|
conditionIsTrue(condition->astOperand2(), programMemory);
|
|
|
|
}
|
|
|
|
ProgramMemory progmem(programMemory);
|
|
|
|
bool error = false;
|
|
|
|
MathLib::bigint result = 0;
|
|
|
|
execute(condition, &progmem, &result, &error);
|
|
|
|
return !error && result == 1;
|
|
|
|
}
|
|
|
|
|
2021-08-25 20:33:41 +02:00
|
|
|
static bool frontIs(const std::vector<MathLib::bigint>& v, bool i)
|
|
|
|
{
|
|
|
|
if (v.empty())
|
|
|
|
return false;
|
|
|
|
if (v.front())
|
|
|
|
return i;
|
|
|
|
return !i;
|
|
|
|
}
|
|
|
|
|
2021-01-11 08:00:13 +01:00
|
|
|
void programMemoryParseCondition(ProgramMemory& pm, const Token* tok, const Token* endTok, const Settings* settings, bool then)
|
2019-10-30 17:57:46 +01:00
|
|
|
{
|
2021-08-25 20:33:41 +02:00
|
|
|
auto eval = [&](const Token* t) -> std::vector<MathLib::bigint> {
|
|
|
|
if (t->hasKnownIntValue())
|
|
|
|
return {t->values().front().intvalue};
|
|
|
|
MathLib::bigint result = 0;
|
|
|
|
bool error = false;
|
|
|
|
execute(t, &pm, &result, &error);
|
|
|
|
if (!error)
|
|
|
|
return {result};
|
|
|
|
return std::vector<MathLib::bigint>{};
|
|
|
|
};
|
2019-10-30 17:57:46 +01:00
|
|
|
if (Token::Match(tok, "==|>=|<=|<|>|!=")) {
|
|
|
|
ValueFlow::Value truevalue;
|
|
|
|
ValueFlow::Value falsevalue;
|
2021-08-25 20:33:41 +02:00
|
|
|
const Token* vartok = parseCompareInt(tok, truevalue, falsevalue, eval);
|
2019-10-30 17:57:46 +01:00
|
|
|
if (!vartok)
|
|
|
|
return;
|
2021-01-21 19:49:37 +01:00
|
|
|
if (vartok->exprId() == 0)
|
2019-10-30 17:57:46 +01:00
|
|
|
return;
|
|
|
|
if (!truevalue.isIntValue())
|
|
|
|
return;
|
2021-01-21 19:49:37 +01:00
|
|
|
if (endTok && isExpressionChanged(vartok, tok->next(), endTok, settings, true))
|
2019-10-30 17:57:46 +01:00
|
|
|
return;
|
2021-07-16 18:49:07 +02:00
|
|
|
bool impossible = (tok->str() == "==" && !then) || (tok->str() == "!=" && then);
|
2021-10-04 07:53:58 +02:00
|
|
|
pm.setIntValue(vartok->exprId(), then ? truevalue.intvalue : falsevalue.intvalue, impossible);
|
2021-08-23 09:03:48 +02:00
|
|
|
const Token* containerTok = settings->library.getContainerFromYield(vartok, Library::Container::Yield::SIZE);
|
2021-07-16 18:49:07 +02:00
|
|
|
if (containerTok)
|
|
|
|
pm.setContainerSizeValue(containerTok->exprId(), then ? truevalue.intvalue : falsevalue.intvalue, !impossible);
|
2019-10-30 17:57:46 +01:00
|
|
|
} else if (Token::simpleMatch(tok, "!")) {
|
|
|
|
programMemoryParseCondition(pm, tok->astOperand1(), endTok, settings, !then);
|
|
|
|
} else if (then && Token::simpleMatch(tok, "&&")) {
|
|
|
|
programMemoryParseCondition(pm, tok->astOperand1(), endTok, settings, then);
|
|
|
|
programMemoryParseCondition(pm, tok->astOperand2(), endTok, settings, then);
|
|
|
|
} else if (!then && Token::simpleMatch(tok, "||")) {
|
|
|
|
programMemoryParseCondition(pm, tok->astOperand1(), endTok, settings, then);
|
|
|
|
programMemoryParseCondition(pm, tok->astOperand2(), endTok, settings, then);
|
2021-08-25 20:33:41 +02:00
|
|
|
} else if (Token::Match(tok, "&&|%oror%")) {
|
|
|
|
std::vector<MathLib::bigint> lhs = eval(tok->astOperand1());
|
|
|
|
std::vector<MathLib::bigint> rhs = eval(tok->astOperand2());
|
|
|
|
if (lhs.empty() || rhs.empty()) {
|
|
|
|
if (frontIs(lhs, !then))
|
|
|
|
programMemoryParseCondition(pm, tok->astOperand2(), endTok, settings, then);
|
|
|
|
if (frontIs(rhs, !then))
|
|
|
|
programMemoryParseCondition(pm, tok->astOperand1(), endTok, settings, then);
|
|
|
|
}
|
2021-01-21 19:49:37 +01:00
|
|
|
} else if (tok->exprId() > 0) {
|
|
|
|
if (endTok && isExpressionChanged(tok, tok->next(), endTok, settings, true))
|
|
|
|
return;
|
2021-10-30 22:13:58 +02:00
|
|
|
pm.setIntValue(tok->exprId(), 0, then);
|
2021-08-23 09:03:48 +02:00
|
|
|
const Token* containerTok = settings->library.getContainerFromYield(tok, Library::Container::Yield::EMPTY);
|
2021-07-16 18:49:07 +02:00
|
|
|
if (containerTok)
|
|
|
|
pm.setContainerSizeValue(containerTok->exprId(), 0, then);
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fillProgramMemoryFromConditions(ProgramMemory& pm, const Scope* scope, const Token* endTok, const Settings* settings)
|
|
|
|
{
|
|
|
|
if (!scope)
|
|
|
|
return;
|
|
|
|
if (!scope->isLocal())
|
|
|
|
return;
|
|
|
|
assert(scope != scope->nestedIn);
|
|
|
|
fillProgramMemoryFromConditions(pm, scope->nestedIn, endTok, settings);
|
2021-01-11 08:00:13 +01:00
|
|
|
if (scope->type == Scope::eIf || scope->type == Scope::eWhile || scope->type == Scope::eElse || scope->type == Scope::eFor) {
|
|
|
|
const Token* condTok = getCondTokFromEnd(scope->bodyEnd);
|
|
|
|
if (!condTok)
|
2019-10-30 17:57:46 +01:00
|
|
|
return;
|
2021-06-09 09:20:43 +02:00
|
|
|
MathLib::bigint result = 0;
|
|
|
|
bool error = false;
|
|
|
|
execute(condTok, &pm, &result, &error);
|
|
|
|
if (error)
|
|
|
|
programMemoryParseCondition(pm, condTok, endTok, settings, scope->type != Scope::eElse);
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fillProgramMemoryFromConditions(ProgramMemory& pm, const Token* tok, const Settings* settings)
|
|
|
|
{
|
|
|
|
fillProgramMemoryFromConditions(pm, tok->scope(), tok, settings);
|
|
|
|
}
|
|
|
|
|
2021-01-16 19:05:51 +01:00
|
|
|
static void fillProgramMemoryFromAssignments(ProgramMemory& pm, const Token* tok, const ProgramMemory& state, const ProgramMemory::Map& vars)
|
2019-10-30 17:57:46 +01:00
|
|
|
{
|
|
|
|
int indentlevel = 0;
|
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->previous()) {
|
2021-01-21 19:49:37 +01:00
|
|
|
if ((Token::simpleMatch(tok2, "=") || Token::Match(tok2->previous(), "%var% (|{")) && tok2->astOperand1() &&
|
|
|
|
tok2->astOperand2()) {
|
|
|
|
bool setvar = false;
|
|
|
|
const Token* vartok = tok2->astOperand1();
|
|
|
|
const Token* valuetok = tok2->astOperand2();
|
2021-01-16 19:05:51 +01:00
|
|
|
for (const auto& p:vars) {
|
2021-01-21 19:49:37 +01:00
|
|
|
if (p.first != vartok->exprId())
|
2019-10-30 17:57:46 +01:00
|
|
|
continue;
|
2020-11-10 16:00:55 +01:00
|
|
|
if (vartok == tok)
|
|
|
|
continue;
|
2021-01-21 19:49:37 +01:00
|
|
|
pm.setValue(vartok->exprId(), p.second);
|
2019-10-30 17:57:46 +01:00
|
|
|
setvar = true;
|
|
|
|
}
|
2021-01-21 19:49:37 +01:00
|
|
|
if (!setvar) {
|
|
|
|
if (!pm.hasValue(vartok->exprId())) {
|
|
|
|
MathLib::bigint result = 0;
|
|
|
|
bool error = false;
|
|
|
|
execute(valuetok, &pm, &result, &error);
|
|
|
|
if (!error)
|
|
|
|
pm.setIntValue(vartok->exprId(), result);
|
|
|
|
else
|
|
|
|
pm.setUnknown(vartok->exprId());
|
|
|
|
}
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
2021-01-21 19:49:37 +01:00
|
|
|
} else if (tok2->exprId() > 0 && Token::Match(tok2, ".|(|[|*|%var%") && !pm.hasValue(tok2->exprId()) &&
|
|
|
|
isVariableChanged(tok2, 0, nullptr, true)) {
|
|
|
|
pm.setUnknown(tok2->exprId());
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tok2->str() == "{") {
|
2021-08-27 05:46:57 +02:00
|
|
|
if (indentlevel <= 0) {
|
|
|
|
// Keep progressing with anonymous/do scopes
|
|
|
|
if (!Token::Match(tok2->previous(), "do|; {"))
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
--indentlevel;
|
2021-06-19 13:58:57 +02:00
|
|
|
if (Token::simpleMatch(tok2->previous(), "else {"))
|
|
|
|
tok2 = tok2->linkAt(-2)->previous();
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
if (tok2->str() == "}") {
|
2021-06-19 13:58:57 +02:00
|
|
|
const Token *cond = getCondTokFromEnd(tok2);
|
|
|
|
const bool inElse = Token::simpleMatch(tok2->link()->previous(), "else {");
|
|
|
|
if (cond) {
|
|
|
|
if (conditionIsFalse(cond, state)) {
|
|
|
|
if (inElse) {
|
|
|
|
++indentlevel;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tok2 = cond->astParent()->previous();
|
2021-06-19 14:47:35 +02:00
|
|
|
} else if (conditionIsTrue(cond, state)) {
|
2021-06-19 13:58:57 +02:00
|
|
|
if (inElse)
|
|
|
|
tok2 = tok2->link()->tokAt(-2);
|
|
|
|
++indentlevel;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 16:25:33 +01:00
|
|
|
static void removeModifiedVars(ProgramMemory& pm, const Token* tok, const Token* origin)
|
|
|
|
{
|
|
|
|
for (auto i = pm.values.begin(), last = pm.values.end(); i != last;) {
|
|
|
|
if (isVariableChanged(origin, tok, i->first, false, nullptr, true)) {
|
|
|
|
i = pm.values.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ProgramMemory getInitialProgramState(const Token* tok,
|
2021-08-07 20:51:18 +02:00
|
|
|
const Token* origin,
|
|
|
|
const ProgramMemory::Map& vars = ProgramMemory::Map {})
|
2020-01-05 16:25:33 +01:00
|
|
|
{
|
|
|
|
ProgramMemory pm;
|
|
|
|
if (origin) {
|
|
|
|
fillProgramMemoryFromConditions(pm, origin, nullptr);
|
|
|
|
const ProgramMemory state = pm;
|
|
|
|
fillProgramMemoryFromAssignments(pm, tok, state, vars);
|
|
|
|
removeModifiedVars(pm, tok, origin);
|
|
|
|
}
|
|
|
|
return pm;
|
|
|
|
}
|
|
|
|
|
2021-08-23 09:03:48 +02:00
|
|
|
ProgramMemoryState::ProgramMemoryState(const Settings* s) : state(), origins(), settings(s) {}
|
|
|
|
|
2020-02-19 07:55:04 +01:00
|
|
|
void ProgramMemoryState::insert(const ProgramMemory &pm, const Token* origin)
|
|
|
|
{
|
2020-02-21 16:18:41 +01:00
|
|
|
if (origin)
|
|
|
|
for (auto&& p:pm.values)
|
2020-02-19 07:55:04 +01:00
|
|
|
origins.insert(std::make_pair(p.first, origin));
|
|
|
|
state.insert(pm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramMemoryState::replace(const ProgramMemory &pm, const Token* origin)
|
|
|
|
{
|
2020-02-21 16:18:41 +01:00
|
|
|
if (origin)
|
|
|
|
for (auto&& p:pm.values)
|
2020-02-19 07:55:04 +01:00
|
|
|
origins[p.first] = origin;
|
|
|
|
state.replace(pm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramMemoryState::addState(const Token* tok, const ProgramMemory::Map& vars)
|
|
|
|
{
|
2021-01-21 19:49:37 +01:00
|
|
|
ProgramMemory pm = state;
|
2020-02-19 07:55:04 +01:00
|
|
|
for (const auto& p:vars) {
|
2021-01-27 19:49:13 +01:00
|
|
|
nonneg int exprid = p.first;
|
2020-02-19 07:55:04 +01:00
|
|
|
const ValueFlow::Value &value = p.second;
|
2021-01-21 19:49:37 +01:00
|
|
|
pm.setValue(exprid, value);
|
2020-02-19 07:55:04 +01:00
|
|
|
if (value.varId)
|
|
|
|
pm.setIntValue(value.varId, value.varvalue);
|
|
|
|
}
|
2021-08-29 15:39:41 +02:00
|
|
|
fillProgramMemoryFromConditions(pm, tok, settings);
|
2021-01-16 19:05:51 +01:00
|
|
|
ProgramMemory local = pm;
|
2020-02-19 07:55:04 +01:00
|
|
|
fillProgramMemoryFromAssignments(pm, tok, local, vars);
|
|
|
|
replace(pm, tok);
|
|
|
|
}
|
|
|
|
|
2021-07-16 18:49:07 +02:00
|
|
|
void ProgramMemoryState::assume(const Token* tok, bool b, bool isEmpty)
|
2020-02-19 07:55:04 +01:00
|
|
|
{
|
|
|
|
ProgramMemory pm = state;
|
2021-07-16 18:49:07 +02:00
|
|
|
if (isEmpty)
|
|
|
|
pm.setContainerSizeValue(tok->exprId(), 0, b);
|
|
|
|
else
|
2021-08-23 09:03:48 +02:00
|
|
|
programMemoryParseCondition(pm, tok, nullptr, settings, b);
|
2021-06-09 09:20:43 +02:00
|
|
|
const Token* origin = tok;
|
|
|
|
const Token* top = tok->astTop();
|
|
|
|
if (top && Token::Match(top->previous(), "for|while ("))
|
|
|
|
origin = top->link();
|
|
|
|
replace(pm, origin);
|
2020-02-19 07:55:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramMemoryState::removeModifiedVars(const Token* tok)
|
|
|
|
{
|
|
|
|
for (auto i = state.values.begin(), last = state.values.end(); i != last;) {
|
2021-08-23 09:03:48 +02:00
|
|
|
const Token* start = origins[i->first];
|
|
|
|
const Token* expr = findExpression(start ? start : tok, i->first);
|
|
|
|
if (!expr || isExpressionChanged(expr, start, tok, settings, true)) {
|
2020-02-19 07:55:04 +01:00
|
|
|
origins.erase(i->first);
|
|
|
|
i = state.values.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:20:43 +02:00
|
|
|
ProgramMemory ProgramMemoryState::get(const Token* tok, const Token* ctx, const ProgramMemory::Map& vars) const
|
2020-02-19 07:55:04 +01:00
|
|
|
{
|
|
|
|
ProgramMemoryState local = *this;
|
2021-06-09 09:20:43 +02:00
|
|
|
if (ctx)
|
|
|
|
local.addState(ctx, vars);
|
|
|
|
const Token* start = previousBeforeAstLeftmostLeaf(tok);
|
|
|
|
if (!start)
|
|
|
|
start = tok;
|
|
|
|
|
|
|
|
if (!ctx || precedes(start, ctx)) {
|
|
|
|
local.removeModifiedVars(start);
|
2021-07-10 07:35:16 +02:00
|
|
|
local.addState(start, vars);
|
2021-06-09 09:20:43 +02:00
|
|
|
} else {
|
|
|
|
local.removeModifiedVars(ctx);
|
|
|
|
}
|
2020-02-19 07:55:04 +01:00
|
|
|
return local.state;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramMemory getProgramMemory(const Token *tok, const ProgramMemory::Map& vars)
|
2020-02-16 16:02:22 +01:00
|
|
|
{
|
|
|
|
ProgramMemory programMemory;
|
2020-02-16 19:58:09 +01:00
|
|
|
for (const auto& p:vars) {
|
2020-02-16 16:02:22 +01:00
|
|
|
const ValueFlow::Value &value = p.second;
|
|
|
|
programMemory.replace(getInitialProgramState(tok, value.tokvalue));
|
|
|
|
programMemory.replace(getInitialProgramState(tok, value.condition));
|
|
|
|
}
|
|
|
|
fillProgramMemoryFromConditions(programMemory, tok, nullptr);
|
|
|
|
ProgramMemory state;
|
2020-02-16 19:58:09 +01:00
|
|
|
for (const auto& p:vars) {
|
2021-01-27 19:49:13 +01:00
|
|
|
nonneg int exprid = p.first;
|
2020-02-16 16:02:22 +01:00
|
|
|
const ValueFlow::Value &value = p.second;
|
2021-01-21 19:49:37 +01:00
|
|
|
programMemory.setValue(exprid, value);
|
2020-02-16 16:02:22 +01:00
|
|
|
if (value.varId)
|
|
|
|
programMemory.setIntValue(value.varId, value.varvalue);
|
|
|
|
}
|
2020-02-19 07:55:04 +01:00
|
|
|
state = programMemory;
|
2020-02-16 16:02:22 +01:00
|
|
|
fillProgramMemoryFromAssignments(programMemory, tok, state, vars);
|
|
|
|
return programMemory;
|
|
|
|
}
|
|
|
|
|
2021-08-25 06:56:19 +02:00
|
|
|
ProgramMemory getProgramMemory(const Token* tok, nonneg int exprid, const ValueFlow::Value& value, const Settings *settings)
|
2019-10-30 17:57:46 +01:00
|
|
|
{
|
|
|
|
ProgramMemory programMemory;
|
2020-01-05 16:25:33 +01:00
|
|
|
programMemory.replace(getInitialProgramState(tok, value.tokvalue));
|
|
|
|
programMemory.replace(getInitialProgramState(tok, value.condition));
|
2021-08-25 06:56:19 +02:00
|
|
|
fillProgramMemoryFromConditions(programMemory, tok, settings);
|
2021-01-21 19:49:37 +01:00
|
|
|
programMemory.setValue(exprid, value);
|
2019-10-30 17:57:46 +01:00
|
|
|
if (value.varId)
|
|
|
|
programMemory.setIntValue(value.varId, value.varvalue);
|
|
|
|
const ProgramMemory state = programMemory;
|
2021-01-21 19:49:37 +01:00
|
|
|
fillProgramMemoryFromAssignments(programMemory, tok, state, {{exprid, value}});
|
2019-10-30 17:57:46 +01:00
|
|
|
return programMemory;
|
|
|
|
}
|
|
|
|
|
2021-10-04 07:53:58 +02:00
|
|
|
static bool isNumericValue(const ValueFlow::Value& value) {
|
|
|
|
return value.isIntValue() || value.isFloatValue();
|
2021-07-24 22:44:18 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 07:53:58 +02:00
|
|
|
static double asFloat(const ValueFlow::Value& value)
|
2021-07-24 22:44:18 +02:00
|
|
|
{
|
2021-10-04 07:53:58 +02:00
|
|
|
return value.isFloatValue() ? value.floatValue : value.intvalue;
|
2021-07-24 22:44:18 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 07:53:58 +02:00
|
|
|
static std::string removeAssign(const std::string& assign) {
|
|
|
|
return std::string{assign.begin(), assign.end() - 1};
|
2021-07-24 22:44:18 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 07:53:58 +02:00
|
|
|
struct assign {
|
|
|
|
template<class T, class U>
|
|
|
|
void operator()(T& x, const U& y) const
|
|
|
|
{
|
|
|
|
x = y;
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
2021-10-04 07:53:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static ValueFlow::Value evaluate(const std::string& op, const ValueFlow::Value& lhs, const ValueFlow::Value& rhs)
|
|
|
|
{
|
|
|
|
ValueFlow::Value result;
|
|
|
|
if (lhs.isImpossible() && rhs.isImpossible())
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
if (lhs.isImpossible() || rhs.isImpossible()) {
|
|
|
|
// noninvertible
|
|
|
|
if (contains({"%", "/", "&", "|"}, op))
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
result.setImpossible();
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
2021-10-04 07:53:58 +02:00
|
|
|
if (isNumericValue(lhs) && isNumericValue(rhs)) {
|
|
|
|
if (lhs.isFloatValue() || rhs.isFloatValue()) {
|
|
|
|
result.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
bool error = false;
|
|
|
|
result.floatValue = calculate(op, asFloat(lhs), asFloat(rhs), &error);
|
|
|
|
if (error)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
return result;
|
2021-02-10 08:11:06 +01:00
|
|
|
}
|
|
|
|
}
|
2021-10-04 07:53:58 +02:00
|
|
|
result.valueType = ValueFlow::Value::ValueType::INT;
|
|
|
|
if (op == "+") {
|
|
|
|
if (lhs.isIteratorValue())
|
|
|
|
result.valueType = lhs.valueType;
|
|
|
|
else if (rhs.isIteratorValue())
|
|
|
|
result.valueType = rhs.valueType;
|
|
|
|
} else if (lhs.valueType != rhs.valueType) {
|
|
|
|
return ValueFlow::Value::unknown();
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
2021-10-04 07:53:58 +02:00
|
|
|
bool error = false;
|
|
|
|
result.intvalue = calculate(op, lhs.intvalue, rhs.intvalue, &error);
|
|
|
|
if (error)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
if (result.isImpossible()) {
|
|
|
|
if ((result.intvalue == 0 && op == "!=") || (result.intvalue != 0 && op == "==")) {
|
|
|
|
result.setPossible();
|
|
|
|
result.intvalue = !result.intvalue;
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
}
|
2021-10-04 07:53:58 +02:00
|
|
|
return result;
|
|
|
|
}
|
2019-10-30 17:57:46 +01:00
|
|
|
|
2021-10-04 07:53:58 +02:00
|
|
|
static ValueFlow::Value execute(const Token* expr, ProgramMemory& pm)
|
|
|
|
{
|
|
|
|
ValueFlow::Value unknown = ValueFlow::Value::unknown();
|
|
|
|
const ValueFlow::Value* value = nullptr;
|
|
|
|
if (!expr)
|
|
|
|
return unknown;
|
|
|
|
else if (expr->hasKnownIntValue() && !expr->isAssignmentOp()) {
|
|
|
|
return expr->values().front();
|
|
|
|
} else if ((value = expr->getKnownValue(ValueFlow::Value::ValueType::FLOAT)) ||
|
|
|
|
(value = expr->getKnownValue(ValueFlow::Value::ValueType::ITERATOR_START)) ||
|
|
|
|
(value = expr->getKnownValue(ValueFlow::Value::ValueType::ITERATOR_END)) ||
|
|
|
|
(value = expr->getKnownValue(ValueFlow::Value::ValueType::CONTAINER_SIZE))) {
|
|
|
|
return *value;
|
|
|
|
} else if (expr->isNumber()) {
|
|
|
|
if (MathLib::isFloat(expr->str()))
|
|
|
|
return unknown;
|
|
|
|
return ValueFlow::Value{MathLib::toLongNumber(expr->str())};
|
|
|
|
} else if (Token::Match(expr->tokAt(-2), ". %name% (") && astIsContainer(expr->tokAt(-2)->astOperand1())) {
|
|
|
|
const Token* containerTok = expr->tokAt(-2)->astOperand1();
|
|
|
|
Library::Container::Yield yield = containerTok->valueType()->container->getYield(expr->strAt(-1));
|
|
|
|
if (yield == Library::Container::Yield::SIZE) {
|
|
|
|
ValueFlow::Value v = execute(containerTok, pm);
|
|
|
|
if (!v.isContainerSizeValue())
|
|
|
|
return unknown;
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::INT;
|
|
|
|
return v;
|
|
|
|
} else if (yield == Library::Container::Yield::EMPTY) {
|
|
|
|
ValueFlow::Value v = execute(containerTok, pm);
|
|
|
|
if (!v.isContainerSizeValue())
|
|
|
|
return unknown;
|
|
|
|
if (v.isImpossible() && v.intvalue == 0)
|
|
|
|
return ValueFlow::Value{0};
|
|
|
|
else if (!v.isImpossible())
|
|
|
|
return ValueFlow::Value{v.intvalue == 0};
|
2021-08-27 05:46:33 +02:00
|
|
|
}
|
2021-10-04 07:53:58 +02:00
|
|
|
} else if (expr->isAssignmentOp() && expr->astOperand1() && expr->astOperand2() && expr->astOperand1()->exprId() > 0) {
|
|
|
|
ValueFlow::Value rhs = execute(expr->astOperand2(), pm);
|
|
|
|
if (rhs.isUninitValue())
|
|
|
|
return unknown;
|
|
|
|
if (expr->str() != "=") {
|
|
|
|
if (!pm.hasValue(expr->astOperand1()->exprId()))
|
|
|
|
return unknown;
|
|
|
|
ValueFlow::Value& lhs = pm.values.at(expr->astOperand1()->exprId());
|
|
|
|
rhs = evaluate(removeAssign(expr->str()), lhs, rhs);
|
|
|
|
if (lhs.isIntValue())
|
|
|
|
ValueFlow::Value::visitValue(rhs, std::bind(assign{}, std::ref(lhs.intvalue), std::placeholders::_1));
|
|
|
|
else if (lhs.isFloatValue())
|
|
|
|
ValueFlow::Value::visitValue(rhs, std::bind(assign{}, std::ref(lhs.floatValue), std::placeholders::_1));
|
2019-10-30 17:57:46 +01:00
|
|
|
else
|
2021-10-04 07:53:58 +02:00
|
|
|
return unknown;
|
|
|
|
return lhs;
|
2021-08-27 05:46:33 +02:00
|
|
|
} else {
|
2021-10-04 07:53:58 +02:00
|
|
|
pm.values[expr->astOperand1()->exprId()] = rhs;
|
|
|
|
return rhs;
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
2021-10-04 07:53:58 +02:00
|
|
|
} else if (expr->str() == "&&" && expr->astOperand1() && expr->astOperand2()) {
|
|
|
|
ValueFlow::Value lhs = execute(expr->astOperand1(), pm);
|
|
|
|
if (!lhs.isIntValue())
|
|
|
|
return unknown;
|
|
|
|
if (lhs.intvalue == 0)
|
|
|
|
return lhs;
|
|
|
|
return execute(expr->astOperand2(), pm);
|
|
|
|
} else if (expr->str() == "||" && expr->astOperand1() && expr->astOperand2()) {
|
|
|
|
ValueFlow::Value lhs = execute(expr->astOperand1(), pm);
|
|
|
|
if (!lhs.isIntValue())
|
|
|
|
return unknown;
|
|
|
|
if (lhs.intvalue != 0)
|
|
|
|
return lhs;
|
|
|
|
return execute(expr->astOperand2(), pm);
|
2021-08-27 05:46:33 +02:00
|
|
|
} else if (expr->str() == "," && expr->astOperand1() && expr->astOperand2()) {
|
2021-10-04 07:53:58 +02:00
|
|
|
execute(expr->astOperand1(), pm);
|
|
|
|
return execute(expr->astOperand2(), pm);
|
|
|
|
} else if (Token::Match(expr, "++|--") && expr->astOperand1() && expr->astOperand1()->exprId() != 0) {
|
|
|
|
if (!pm.hasValue(expr->astOperand1()->exprId()))
|
|
|
|
return unknown;
|
|
|
|
ValueFlow::Value& lhs = pm.values.at(expr->astOperand1()->exprId());
|
|
|
|
if (!lhs.isIntValue())
|
|
|
|
return unknown;
|
|
|
|
// overflow
|
|
|
|
if (!lhs.isImpossible() && lhs.intvalue == 0 && expr->str() == "--" && astIsUnsigned(expr->astOperand1()))
|
|
|
|
return unknown;
|
|
|
|
|
|
|
|
if (expr->str() == "++")
|
|
|
|
lhs.intvalue++;
|
|
|
|
else
|
|
|
|
lhs.intvalue--;
|
|
|
|
return lhs;
|
|
|
|
} else if (expr->str() == "[" && expr->astOperand1() && expr->astOperand2()) {
|
2019-10-30 17:57:46 +01:00
|
|
|
const Token *tokvalue = nullptr;
|
2021-10-04 07:53:58 +02:00
|
|
|
if (!pm.getTokValue(expr->astOperand1()->exprId(), &tokvalue)) {
|
2019-10-30 17:57:46 +01:00
|
|
|
auto tokvalue_it = std::find_if(expr->astOperand1()->values().begin(),
|
|
|
|
expr->astOperand1()->values().end(),
|
|
|
|
std::mem_fn(&ValueFlow::Value::isTokValue));
|
|
|
|
if (tokvalue_it == expr->astOperand1()->values().end()) {
|
2021-10-04 07:53:58 +02:00
|
|
|
return unknown;
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
tokvalue = tokvalue_it->tokvalue;
|
|
|
|
}
|
|
|
|
if (!tokvalue || !tokvalue->isLiteral()) {
|
2021-10-04 07:53:58 +02:00
|
|
|
return unknown;
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
const std::string strValue = tokvalue->strValue();
|
2021-10-04 07:53:58 +02:00
|
|
|
ValueFlow::Value rhs = execute(expr->astOperand2(), pm);
|
|
|
|
if (!rhs.isIntValue())
|
|
|
|
return unknown;
|
|
|
|
MathLib::bigint index = rhs.intvalue;
|
2019-10-30 17:57:46 +01:00
|
|
|
if (index >= 0 && index < strValue.size())
|
2021-10-04 07:53:58 +02:00
|
|
|
return ValueFlow::Value{strValue[index]};
|
2019-10-30 17:57:46 +01:00
|
|
|
else if (index == strValue.size())
|
2021-10-04 07:53:58 +02:00
|
|
|
return ValueFlow::Value{};
|
|
|
|
} else if (Token::Match(expr, "%cop%") && expr->astOperand1() && expr->astOperand2()) {
|
|
|
|
ValueFlow::Value lhs = execute(expr->astOperand1(), pm);
|
|
|
|
ValueFlow::Value rhs = execute(expr->astOperand2(), pm);
|
|
|
|
if (!lhs.isUninitValue() && !rhs.isUninitValue())
|
|
|
|
return evaluate(expr->str(), lhs, rhs);
|
|
|
|
if (expr->isComparisonOp()) {
|
2021-10-30 22:13:58 +02:00
|
|
|
if (rhs.isIntValue()) {
|
|
|
|
std::vector<ValueFlow::Value> result =
|
|
|
|
infer(makeIntegralInferModel(), expr->str(), expr->astOperand1()->values(), {rhs});
|
2021-12-14 07:22:57 +01:00
|
|
|
if (result.empty() || !result.front().isKnown())
|
2021-10-30 22:13:58 +02:00
|
|
|
return unknown;
|
|
|
|
return result.front();
|
|
|
|
} else if (lhs.isIntValue()) {
|
|
|
|
std::vector<ValueFlow::Value> result =
|
|
|
|
infer(makeIntegralInferModel(), expr->str(), {lhs}, expr->astOperand2()->values());
|
2021-12-14 07:22:57 +01:00
|
|
|
if (result.empty() || !result.front().isKnown())
|
2021-10-30 22:13:58 +02:00
|
|
|
return unknown;
|
|
|
|
return result.front();
|
2021-10-04 07:53:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Unary ops
|
|
|
|
else if (Token::Match(expr, "!|+|-") && expr->astOperand1() && !expr->astOperand2()) {
|
|
|
|
ValueFlow::Value lhs = execute(expr->astOperand1(), pm);
|
|
|
|
if (!lhs.isIntValue())
|
|
|
|
return unknown;
|
|
|
|
if (expr->str() == "!")
|
|
|
|
lhs.intvalue = !lhs.intvalue;
|
|
|
|
if (expr->str() == "-")
|
|
|
|
lhs.intvalue = -lhs.intvalue;
|
|
|
|
return lhs;
|
2021-08-27 05:46:33 +02:00
|
|
|
} else if (expr->str() == "?" && expr->astOperand1() && expr->astOperand2()) {
|
2021-10-04 07:53:58 +02:00
|
|
|
ValueFlow::Value cond = execute(expr->astOperand1(), pm);
|
|
|
|
if (!cond.isIntValue())
|
|
|
|
return unknown;
|
|
|
|
const Token* child = expr->astOperand2();
|
|
|
|
if (cond.intvalue == 0)
|
|
|
|
return execute(child->astOperand2(), pm);
|
2021-08-27 05:46:33 +02:00
|
|
|
else
|
2021-10-04 07:53:58 +02:00
|
|
|
return execute(child->astOperand1(), pm);
|
2021-08-01 14:05:30 +02:00
|
|
|
} else if (expr->str() == "(" && expr->isCast()) {
|
2021-07-26 22:22:23 +02:00
|
|
|
if (Token::simpleMatch(expr->previous(), ">") && expr->previous()->link())
|
2021-10-04 07:53:58 +02:00
|
|
|
return execute(expr->astOperand2(), pm);
|
2021-07-26 22:22:23 +02:00
|
|
|
else
|
2021-10-04 07:53:58 +02:00
|
|
|
return execute(expr->astOperand1(), pm);
|
|
|
|
}
|
|
|
|
if (expr->exprId() > 0 && pm.hasValue(expr->exprId())) {
|
2021-10-30 22:13:58 +02:00
|
|
|
ValueFlow::Value result = pm.values.at(expr->exprId());
|
|
|
|
if (result.isImpossible() && result.isIntValue() && result.intvalue == 0 && isUsedAsBool(expr)) {
|
|
|
|
result.intvalue = !result.intvalue;
|
|
|
|
result.setKnown();
|
|
|
|
}
|
|
|
|
return result;
|
2021-10-04 07:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
void execute(const Token* expr, ProgramMemory* const programMemory, MathLib::bigint* result, bool* error)
|
|
|
|
{
|
|
|
|
ValueFlow::Value v = execute(expr, *programMemory);
|
2021-11-14 18:30:36 +01:00
|
|
|
if (!v.isIntValue() || v.isImpossible()) {
|
|
|
|
if (error)
|
|
|
|
*error = true;
|
|
|
|
} else if (result)
|
2021-10-04 07:53:58 +02:00
|
|
|
*result = v.intvalue;
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|