2022-01-26 19:02:20 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2022-02-05 11:45:17 +01:00
|
|
|
* Copyright (C) 2007-2022 Cppcheck team.
|
2022-01-26 19:02:20 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2019-10-30 17:57:46 +01:00
|
|
|
|
|
|
|
#include "programmemory.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
|
2019-10-30 17:57:46 +01:00
|
|
|
#include "astutils.h"
|
2021-10-04 07:53:58 +02:00
|
|
|
#include "calculate.h"
|
2021-10-30 22:13:58 +02:00
|
|
|
#include "infer.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "library.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"
|
2022-02-11 19:44:08 +01:00
|
|
|
#include "utils.h"
|
2021-03-30 14:02:28 +02:00
|
|
|
#include "valueflow.h"
|
2021-10-30 22:13:58 +02:00
|
|
|
#include "valueptr.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
|
2020-05-19 08:53:38 +02:00
|
|
|
#include <algorithm>
|
2019-10-30 17:57:46 +01:00
|
|
|
#include <cassert>
|
2021-07-24 22:44:18 +02:00
|
|
|
#include <functional>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <list>
|
2020-09-11 23:03:57 +02:00
|
|
|
#include <memory>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2019-10-30 17:57:46 +01:00
|
|
|
|
2022-02-06 20:15:45 +01:00
|
|
|
nonneg int ExprIdToken::getExpressionId() const {
|
|
|
|
return tok ? tok->exprId() : exprid;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t ExprIdToken::Hash::operator()(ExprIdToken etok) const
|
2021-01-22 21:47:24 +01:00
|
|
|
{
|
2022-02-06 20:15:45 +01:00
|
|
|
return std::hash<nonneg int>()(etok.getExpressionId());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramMemory::setValue(const Token* expr, const ValueFlow::Value& value) {
|
|
|
|
mValues[expr] = value;
|
2022-05-04 05:59:01 +02:00
|
|
|
ValueFlow::Value subvalue = value;
|
|
|
|
const Token* subexpr = solveExprValue(
|
|
|
|
expr,
|
|
|
|
[&](const Token* tok) -> std::vector<MathLib::bigint> {
|
|
|
|
if (tok->hasKnownIntValue())
|
|
|
|
return {tok->values().front().intvalue};
|
|
|
|
MathLib::bigint result = 0;
|
|
|
|
if (getIntValue(tok->exprId(), &result))
|
|
|
|
return {result};
|
|
|
|
return {};
|
|
|
|
},
|
|
|
|
subvalue);
|
|
|
|
if (subexpr)
|
|
|
|
mValues[subexpr] = subvalue;
|
2021-01-22 21:47:24 +01:00
|
|
|
}
|
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
|
|
|
{
|
2022-02-06 20:15:45 +01:00
|
|
|
const ProgramMemory::Map::const_iterator it = mValues.find(exprid);
|
|
|
|
const bool found = it != mValues.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;
|
|
|
|
}
|
|
|
|
|
2022-01-18 22:02:25 +01:00
|
|
|
// cppcheck-suppress unusedFunction
|
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
|
|
|
}
|
|
|
|
|
2022-02-06 20:15:45 +01:00
|
|
|
void ProgramMemory::setIntValue(const Token* expr, 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();
|
2022-05-04 05:59:01 +02:00
|
|
|
setValue(expr, 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
|
|
|
}
|
|
|
|
|
2022-01-18 22:02:25 +01:00
|
|
|
// cppcheck-suppress unusedFunction
|
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
|
|
|
|
2022-02-06 20:15:45 +01:00
|
|
|
void ProgramMemory::setContainerSizeValue(const Token* expr, MathLib::bigint value, bool isEqual)
|
2021-07-16 18:49:07 +02:00
|
|
|
{
|
|
|
|
ValueFlow::Value v(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
|
|
|
|
if (!isEqual)
|
|
|
|
v.valueKind = ValueFlow::Value::ValueKind::Impossible;
|
2022-05-04 05:59:01 +02:00
|
|
|
setValue(expr, v);
|
2021-07-16 18:49:07 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 20:15:45 +01:00
|
|
|
void ProgramMemory::setUnknown(const Token* expr) {
|
|
|
|
mValues[expr].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
|
|
|
{
|
2022-02-06 20:15:45 +01:00
|
|
|
return mValues.find(exprid) != mValues.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ValueFlow::Value& ProgramMemory::at(nonneg int exprid) const {
|
|
|
|
return mValues.at(exprid);
|
|
|
|
}
|
|
|
|
ValueFlow::Value& ProgramMemory::at(nonneg int exprid) {
|
|
|
|
return mValues.at(exprid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramMemory::erase_if(const std::function<bool(const ExprIdToken&)>& pred)
|
|
|
|
{
|
|
|
|
for (auto it = mValues.begin(); it != mValues.end();) {
|
|
|
|
if (pred(it->first))
|
|
|
|
it = mValues.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
2021-01-22 21:47:24 +01:00
|
|
|
}
|
2019-10-30 17:57:46 +01:00
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
void ProgramMemory::swap(ProgramMemory &pm)
|
|
|
|
{
|
2022-02-06 20:15:45 +01:00
|
|
|
mValues.swap(pm.mValues);
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
void ProgramMemory::clear()
|
|
|
|
{
|
2022-02-06 20:15:45 +01:00
|
|
|
mValues.clear();
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
bool ProgramMemory::empty() const
|
|
|
|
{
|
2022-02-06 20:15:45 +01:00
|
|
|
return mValues.empty();
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
void ProgramMemory::replace(const ProgramMemory &pm)
|
|
|
|
{
|
2022-02-06 20:15:45 +01:00
|
|
|
for (auto&& p : pm.mValues) {
|
|
|
|
mValues[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)
|
|
|
|
{
|
2022-02-06 20:15:45 +01:00
|
|
|
for (auto&& p : pm)
|
|
|
|
mValues.insert(p);
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
2022-05-05 06:54:44 +02:00
|
|
|
static ValueFlow::Value execute(const Token* expr, ProgramMemory& pm, const Settings* settings = nullptr);
|
|
|
|
|
2022-03-13 20:07:58 +01:00
|
|
|
static bool evaluateCondition(const std::string& op,
|
|
|
|
MathLib::bigint r,
|
|
|
|
const Token* condition,
|
|
|
|
ProgramMemory& pm,
|
|
|
|
const Settings* settings)
|
2019-10-30 17:57:46 +01:00
|
|
|
{
|
|
|
|
if (!condition)
|
|
|
|
return false;
|
2021-12-20 07:28:40 +01:00
|
|
|
if (condition->str() == op) {
|
|
|
|
return evaluateCondition(op, r, condition->astOperand1(), pm, settings) ||
|
|
|
|
evaluateCondition(op, r, condition->astOperand2(), pm, settings);
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
MathLib::bigint result = 0;
|
|
|
|
bool error = false;
|
2021-12-20 07:28:40 +01:00
|
|
|
execute(condition, &pm, &result, &error, settings);
|
|
|
|
return !error && result == r;
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
2021-12-20 07:28:40 +01:00
|
|
|
bool conditionIsFalse(const Token* condition, ProgramMemory pm, const Settings* settings)
|
2019-10-30 17:57:46 +01:00
|
|
|
{
|
2021-12-20 07:28:40 +01:00
|
|
|
return evaluateCondition("&&", 0, condition, pm, settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool conditionIsTrue(const Token* condition, ProgramMemory pm, const Settings* settings)
|
|
|
|
{
|
|
|
|
return evaluateCondition("||", 1, condition, pm, settings);
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-12 06:37:48 +02:00
|
|
|
// If the scope is a non-range for loop
|
|
|
|
static bool isBasicForLoop(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
if (Token::simpleMatch(tok, "}"))
|
|
|
|
return isBasicForLoop(tok->link());
|
|
|
|
if (!Token::simpleMatch(tok->previous(), ") {"))
|
|
|
|
return false;
|
|
|
|
const Token* start = tok->linkAt(-1);
|
|
|
|
if (!start)
|
|
|
|
return false;
|
|
|
|
if (!Token::simpleMatch(start->previous(), "for ("))
|
|
|
|
return false;
|
|
|
|
if (!Token::simpleMatch(start->astOperand2(), ";"))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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;
|
2022-03-12 06:15:35 +01:00
|
|
|
const bool impossible = (tok->str() == "==" && !then) || (tok->str() == "!=" && then);
|
|
|
|
const ValueFlow::Value& v = then ? truevalue : falsevalue;
|
|
|
|
pm.setValue(vartok, impossible ? asImpossible(v) : v);
|
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)
|
2022-03-12 06:15:35 +01:00
|
|
|
pm.setContainerSizeValue(containerTok, v.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;
|
2022-02-06 20:15:45 +01:00
|
|
|
pm.setIntValue(tok, 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)
|
2022-02-06 20:15:45 +01:00
|
|
|
pm.setContainerSizeValue(containerTok, 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;
|
2022-02-06 20:15:45 +01:00
|
|
|
pm.setValue(vartok, 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())) {
|
2022-05-05 06:54:44 +02:00
|
|
|
pm.setValue(vartok, execute(valuetok, pm));
|
2021-01-21 19:49:37 +01:00
|
|
|
}
|
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)) {
|
2022-02-06 20:15:45 +01:00
|
|
|
pm.setUnknown(tok2);
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tok2->str() == "{") {
|
2021-08-27 05:46:57 +02:00
|
|
|
if (indentlevel <= 0) {
|
2022-05-12 06:37:48 +02:00
|
|
|
const Token* cond = getCondTokFromEnd(tok2->link());
|
|
|
|
// Keep progressing with anonymous/do scopes and always true branches
|
|
|
|
if (!Token::Match(tok2->previous(), "do|; {") && !conditionIsTrue(cond, state) &&
|
|
|
|
(cond || !isBasicForLoop(tok2)))
|
2021-08-27 05:46:57 +02:00
|
|
|
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;
|
|
|
|
}
|
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)
|
|
|
|
{
|
2022-02-06 20:15:45 +01:00
|
|
|
pm.erase_if([&](const ExprIdToken& e) {
|
|
|
|
return isVariableChanged(origin, tok, e.getExpressionId(), false, nullptr, true);
|
|
|
|
});
|
2020-01-05 16:25:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-02-06 20:15:45 +01:00
|
|
|
for (auto&& p : pm)
|
|
|
|
origins.insert(std::make_pair(p.first.getExpressionId(), origin));
|
2020-02-19 07:55:04 +01:00
|
|
|
state.insert(pm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramMemoryState::replace(const ProgramMemory &pm, const Token* origin)
|
|
|
|
{
|
2020-02-21 16:18:41 +01:00
|
|
|
if (origin)
|
2022-02-06 20:15:45 +01:00
|
|
|
for (auto&& p : pm)
|
|
|
|
origins[p.first.getExpressionId()] = origin;
|
2020-02-19 07:55:04 +01:00
|
|
|
state.replace(pm);
|
|
|
|
}
|
|
|
|
|
2022-01-04 21:19:45 +01:00
|
|
|
static void addVars(ProgramMemory& pm, const ProgramMemory::Map& vars)
|
2020-02-19 07:55:04 +01:00
|
|
|
{
|
|
|
|
for (const auto& p:vars) {
|
|
|
|
const ValueFlow::Value &value = p.second;
|
2022-02-06 20:15:45 +01:00
|
|
|
pm.setValue(p.first.tok, value);
|
2020-02-19 07:55:04 +01:00
|
|
|
}
|
2022-01-04 21:19:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramMemoryState::addState(const Token* tok, const ProgramMemory::Map& vars)
|
|
|
|
{
|
|
|
|
ProgramMemory pm = state;
|
|
|
|
addVars(pm, vars);
|
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);
|
2022-01-04 21:19:45 +01:00
|
|
|
addVars(pm, vars);
|
2020-02-19 07:55:04 +01:00
|
|
|
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)
|
2022-02-06 20:15:45 +01:00
|
|
|
pm.setContainerSizeValue(tok, 0, b);
|
2021-07-16 18:49:07 +02:00
|
|
|
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)
|
|
|
|
{
|
2022-02-06 20:15:45 +01:00
|
|
|
state.erase_if([&](const ExprIdToken& e) {
|
|
|
|
const Token* start = origins[e.getExpressionId()];
|
|
|
|
const Token* expr = e.tok;
|
2021-08-23 09:03:48 +02:00
|
|
|
if (!expr || isExpressionChanged(expr, start, tok, settings, true)) {
|
2022-02-06 20:15:45 +01:00
|
|
|
origins.erase(e.getExpressionId());
|
|
|
|
return true;
|
2020-02-19 07:55:04 +01:00
|
|
|
}
|
2022-02-06 20:15:45 +01:00
|
|
|
return false;
|
|
|
|
});
|
2020-02-19 07:55:04 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-02-16 16:02:22 +01:00
|
|
|
const ValueFlow::Value &value = p.second;
|
2022-02-06 20:15:45 +01:00
|
|
|
programMemory.setValue(p.first.tok, value);
|
2020-02-16 16:02:22 +01:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2022-02-06 20:15:45 +01:00
|
|
|
ProgramMemory getProgramMemory(const Token* tok, const Token* expr, 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);
|
2022-02-06 20:15:45 +01:00
|
|
|
programMemory.setValue(expr, value);
|
2019-10-30 17:57:46 +01:00
|
|
|
const ProgramMemory state = programMemory;
|
2022-02-06 20:15:45 +01:00
|
|
|
fillProgramMemoryFromAssignments(programMemory, tok, state, {{expr, 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
|
|
|
|
2022-02-10 20:22:14 +01:00
|
|
|
static ValueFlow::Value executeImpl(const Token* expr, ProgramMemory& pm, const Settings* settings)
|
2021-10-04 07:53:58 +02:00
|
|
|
{
|
|
|
|
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;
|
2022-02-06 20:15:45 +01:00
|
|
|
ValueFlow::Value& lhs = pm.at(expr->astOperand1()->exprId());
|
2021-10-04 07:53:58 +02:00
|
|
|
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 {
|
2022-02-06 20:15:45 +01:00
|
|
|
pm.setValue(expr->astOperand1(), rhs);
|
2021-10-04 07:53:58 +02:00
|
|
|
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;
|
2022-02-06 20:15:45 +01:00
|
|
|
ValueFlow::Value& lhs = pm.at(expr->astOperand1()->exprId());
|
2021-10-04 07:53:58 +02:00
|
|
|
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())) {
|
2022-02-06 20:15:45 +01:00
|
|
|
ValueFlow::Value result = pm.at(expr->exprId());
|
2021-10-30 22:13:58 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-20 07:28:40 +01:00
|
|
|
if (Token::Match(expr->previous(), ">|%name% {|(")) {
|
2022-04-06 06:35:38 +02:00
|
|
|
const Token* ftok = expr->previous();
|
|
|
|
const Function* f = ftok->function();
|
|
|
|
// TODO: Evaluate inline functions as well
|
|
|
|
if (!f && settings && expr->str() == "(") {
|
|
|
|
std::unordered_map<nonneg int, ValueFlow::Value> args;
|
|
|
|
int argn = 0;
|
|
|
|
for (const Token* tok : getArguments(expr)) {
|
|
|
|
ValueFlow::Value result = execute(tok, pm, settings);
|
|
|
|
if (!result.isUninitValue())
|
|
|
|
args[argn] = result;
|
|
|
|
argn++;
|
|
|
|
}
|
|
|
|
// strlen is a special builtin
|
|
|
|
if (Token::simpleMatch(ftok, "strlen")) {
|
|
|
|
if (args.count(0) > 0) {
|
|
|
|
ValueFlow::Value v = args.at(0);
|
|
|
|
if (v.isTokValue() && v.tokvalue->tokType() == Token::eString) {
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::INT;
|
|
|
|
v.intvalue = Token::getStrLength(v.tokvalue);
|
|
|
|
v.tokvalue = nullptr;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const std::string& returnValue = settings->library.returnValue(ftok);
|
|
|
|
if (!returnValue.empty())
|
|
|
|
return evaluateLibraryFunction(args, returnValue, settings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if functon modifies argument
|
2021-12-20 07:28:40 +01:00
|
|
|
visitAstNodes(expr->astOperand2(), [&](const Token* child) {
|
|
|
|
if (child->exprId() > 0 && pm.hasValue(child->exprId())) {
|
2022-02-06 20:15:45 +01:00
|
|
|
ValueFlow::Value& v = pm.at(child->exprId());
|
2021-12-20 07:28:40 +01:00
|
|
|
if (v.valueType == ValueFlow::Value::ValueType::CONTAINER_SIZE) {
|
|
|
|
if (isContainerSizeChanged(child, settings))
|
|
|
|
v = unknown;
|
|
|
|
} else if (v.valueType != ValueFlow::Value::ValueType::UNINIT) {
|
|
|
|
if (isVariableChanged(child, v.indirect, settings, true))
|
|
|
|
v = unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ChildrenToVisit::op1_and_op2;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-04 07:53:58 +02:00
|
|
|
return unknown;
|
|
|
|
}
|
|
|
|
|
2022-02-10 20:22:14 +01:00
|
|
|
static ValueFlow::Value execute(const Token* expr, ProgramMemory& pm, const Settings* settings)
|
|
|
|
{
|
|
|
|
ValueFlow::Value v = executeImpl(expr, pm, settings);
|
|
|
|
if (!v.isUninitValue())
|
|
|
|
return v;
|
|
|
|
if (!expr)
|
|
|
|
return v;
|
|
|
|
if (pm.hasValue(expr->exprId()))
|
|
|
|
return pm.at(expr->exprId());
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2022-04-06 06:35:38 +02:00
|
|
|
ValueFlow::Value evaluateLibraryFunction(const std::unordered_map<nonneg int, ValueFlow::Value>& args,
|
|
|
|
const std::string& returnValue,
|
|
|
|
const Settings* settings)
|
|
|
|
{
|
|
|
|
static std::unordered_map<std::string,
|
|
|
|
std::function<ValueFlow::Value(const std::unordered_map<nonneg int, ValueFlow::Value>& arg)>>
|
|
|
|
functions = {};
|
|
|
|
if (functions.count(returnValue) == 0) {
|
|
|
|
|
|
|
|
std::unordered_map<nonneg int, const Token*> lookupVarId;
|
|
|
|
std::shared_ptr<Token> expr = createTokenFromExpression(returnValue, settings, &lookupVarId);
|
|
|
|
|
|
|
|
functions[returnValue] =
|
|
|
|
[lookupVarId, expr, settings](const std::unordered_map<nonneg int, ValueFlow::Value>& xargs) {
|
|
|
|
if (!expr)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ProgramMemory pm{};
|
|
|
|
for (const auto& p : xargs) {
|
|
|
|
auto it = lookupVarId.find(p.first);
|
|
|
|
if (it != lookupVarId.end())
|
|
|
|
pm.setValue(it->second, p.second);
|
|
|
|
}
|
|
|
|
return execute(expr.get(), pm, settings);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return functions.at(returnValue)(args);
|
|
|
|
}
|
|
|
|
|
2021-12-20 07:28:40 +01:00
|
|
|
void execute(const Token* expr,
|
|
|
|
ProgramMemory* const programMemory,
|
|
|
|
MathLib::bigint* result,
|
|
|
|
bool* error,
|
|
|
|
const Settings* settings)
|
2021-10-04 07:53:58 +02:00
|
|
|
{
|
2021-12-20 07:28:40 +01:00
|
|
|
ValueFlow::Value v = execute(expr, *programMemory, settings);
|
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
|
|
|
}
|