2022-01-26 19:02:20 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 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>
|
2022-07-09 07:40:32 +02:00
|
|
|
#include <cmath>
|
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;
|
2023-03-02 22:05:41 +01:00
|
|
|
if (getIntValue(tok->exprId(), result))
|
2022-05-04 05:59:01 +02:00
|
|
|
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);
|
2022-12-30 15:13:47 +01:00
|
|
|
const bool found = it != mValues.cend() && (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;
|
2023-06-20 18:43:21 +02:00
|
|
|
return nullptr;
|
2020-09-11 23:03:57 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 22:02:25 +01:00
|
|
|
// cppcheck-suppress unusedFunction
|
2023-03-02 22:05:41 +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()) {
|
2023-03-02 22:05:41 +01:00
|
|
|
result = value->intvalue;
|
2020-09-11 23:03:57 +02:00
|
|
|
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
|
2023-03-02 22:05:41 +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()) {
|
2023-03-02 22:05:41 +01:00
|
|
|
result = value->intvalue;
|
2020-09-11 23:03:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-08-11 03:08:49 +02:00
|
|
|
}
|
2023-03-02 22:05:41 +01:00
|
|
|
bool ProgramMemory::getContainerEmptyValue(nonneg int exprid, MathLib::bigint& result) const
|
2021-01-28 12:37:56 +01:00
|
|
|
{
|
|
|
|
const ValueFlow::Value* value = getValue(exprid, true);
|
|
|
|
if (value && value->isContainerSizeValue()) {
|
|
|
|
if (value->isImpossible() && value->intvalue == 0) {
|
2023-03-02 22:05:41 +01:00
|
|
|
result = false;
|
2021-01-28 12:37:56 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!value->isImpossible()) {
|
2023-03-02 22:05:41 +01:00
|
|
|
result = (value->intvalue == 0);
|
2021-01-28 12:37:56 +01:00
|
|
|
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) {
|
2023-10-18 23:49:22 +02:00
|
|
|
if (evaluateCondition(op, r, condition->astOperand1(), pm, settings) ||
|
|
|
|
evaluateCondition(op, r, condition->astOperand2(), pm, settings)) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
MathLib::bigint result = 0;
|
|
|
|
bool error = false;
|
2023-03-02 22:05:41 +01:00
|
|
|
execute(condition, pm, &result, &error, settings);
|
2021-12-20 07:28:40 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-03-24 13:31:26 +01:00
|
|
|
static bool isTrue(const ValueFlow::Value& v)
|
|
|
|
{
|
|
|
|
if (v.isImpossible())
|
|
|
|
return v.intvalue == 0;
|
|
|
|
return v.intvalue != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isFalse(const ValueFlow::Value& v)
|
|
|
|
{
|
|
|
|
if (v.isImpossible())
|
|
|
|
return false;
|
|
|
|
return v.intvalue == 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2023-03-02 22:05:41 +01:00
|
|
|
execute(t, pm, &result, &error);
|
2021-08-25 20:33:41 +02:00
|
|
|
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;
|
2023-10-19 18:42:52 +02:00
|
|
|
if (endTok && findExpressionChanged(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);
|
2023-10-18 23:49:22 +02:00
|
|
|
else if (frontIs(rhs, !then))
|
2021-08-25 20:33:41 +02:00
|
|
|
programMemoryParseCondition(pm, tok->astOperand1(), endTok, settings, then);
|
2023-10-18 23:49:22 +02:00
|
|
|
else
|
|
|
|
pm.setIntValue(tok, 0, then);
|
2021-08-25 20:33:41 +02:00
|
|
|
}
|
2021-01-21 19:49:37 +01:00
|
|
|
} else if (tok->exprId() > 0) {
|
2023-10-19 18:42:52 +02:00
|
|
|
if (endTok && findExpressionChanged(tok, tok->next(), endTok, settings, true))
|
2021-01-21 19:49:37 +01:00
|
|
|
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;
|
2023-03-02 22:05:41 +01:00
|
|
|
execute(condTok, pm, &result, &error);
|
2021-06-09 09:20:43 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-03-09 20:05:44 +01:00
|
|
|
static void fillProgramMemoryFromAssignments(ProgramMemory& pm, const Token* tok, const Settings* settings, 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();
|
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())) {
|
2023-05-03 10:02:16 +02:00
|
|
|
const Token* valuetok = tok2->astOperand2();
|
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()) &&
|
2023-03-09 20:05:44 +01:00
|
|
|
isVariableChanged(tok2, 0, settings, 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
|
|
|
}
|
2023-10-07 14:01:06 +02:00
|
|
|
if (tok2->str() == "}" && !Token::Match(tok2->link()->previous(), "%var% {")) {
|
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,
|
2023-03-09 20:05:44 +01:00
|
|
|
const Settings* settings,
|
2021-08-07 20:51:18 +02:00
|
|
|
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;
|
2023-03-09 20:05:44 +01:00
|
|
|
fillProgramMemoryFromAssignments(pm, tok, settings, state, vars);
|
2020-01-05 16:25:33 +01:00
|
|
|
removeModifiedVars(pm, tok, origin);
|
|
|
|
}
|
|
|
|
return pm;
|
|
|
|
}
|
|
|
|
|
2023-08-08 11:05:02 +02:00
|
|
|
ProgramMemoryState::ProgramMemoryState(const Settings* s) : settings(s) {}
|
2021-08-23 09:03:48 +02:00
|
|
|
|
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;
|
2023-03-09 20:05:44 +01:00
|
|
|
fillProgramMemoryFromAssignments(pm, tok, settings, 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();
|
2022-12-09 07:15:47 +01:00
|
|
|
if (top && Token::Match(top->previous(), "for|while|if (") && !Token::simpleMatch(tok->astParent(), "?")) {
|
|
|
|
origin = top->link()->next();
|
|
|
|
if (!b && origin->link()) {
|
|
|
|
origin = origin->link();
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 09:20:43 +02:00
|
|
|
replace(pm, origin);
|
2020-02-19 07:55:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramMemoryState::removeModifiedVars(const Token* tok)
|
|
|
|
{
|
2023-09-13 23:27:08 +02:00
|
|
|
ProgramMemory pm = state;
|
|
|
|
auto eval = [&](const Token* cond) -> std::vector<MathLib::bigint> {
|
|
|
|
if (conditionIsTrue(cond, pm))
|
|
|
|
return {1};
|
|
|
|
if (conditionIsFalse(cond, pm))
|
|
|
|
return {0};
|
|
|
|
return {};
|
|
|
|
};
|
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;
|
2023-10-19 18:42:52 +02:00
|
|
|
if (!expr || findExpressionChangedSkipDeadCode(expr, start, tok, settings, true, eval)) {
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
2023-03-09 20:05:44 +01:00
|
|
|
programMemory.replace(getInitialProgramState(tok, value.tokvalue, settings));
|
|
|
|
programMemory.replace(getInitialProgramState(tok, value.condition, settings));
|
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;
|
2023-03-09 20:05:44 +01:00
|
|
|
fillProgramMemoryFromAssignments(programMemory, tok, settings, 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) {
|
2022-12-30 15:13:47 +01:00
|
|
|
return std::string{assign.cbegin(), assign.cend() - 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
|
|
|
};
|
|
|
|
|
2022-06-11 11:02:04 +02:00
|
|
|
static bool isIntegralValue(const ValueFlow::Value& value)
|
|
|
|
{
|
|
|
|
return value.isIntValue() || value.isIteratorValue() || value.isSymbolicValue();
|
|
|
|
}
|
|
|
|
|
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;
|
2023-03-24 13:31:26 +01:00
|
|
|
combineValueProperties(lhs, rhs, result);
|
2021-10-04 07:53:58 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2022-06-11 11:02:04 +02:00
|
|
|
// Must be integral types
|
|
|
|
if (!isIntegralValue(lhs) && !isIntegralValue(rhs))
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
// If not the same type then one must be int
|
|
|
|
if (lhs.valueType != rhs.valueType && !lhs.isIntValue() && !rhs.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const bool compareOp = contains({"==", "!=", "<", ">", ">=", "<="}, op);
|
2022-06-11 11:02:04 +02:00
|
|
|
// Comparison must be the same type
|
|
|
|
if (compareOp && lhs.valueType != rhs.valueType)
|
2021-10-04 07:53:58 +02:00
|
|
|
return ValueFlow::Value::unknown();
|
2022-06-11 11:02:04 +02:00
|
|
|
// Only add, subtract, and compare for non-integers
|
|
|
|
if (!compareOp && !contains({"+", "-"}, op) && !lhs.isIntValue() && !rhs.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-07-10 22:40:05 +02:00
|
|
|
// Both can't be iterators for non-compare
|
2022-06-11 11:02:04 +02:00
|
|
|
if (!compareOp && lhs.isIteratorValue() && rhs.isIteratorValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
// Symbolic values must be in the same ring
|
|
|
|
if (lhs.isSymbolicValue() && rhs.isSymbolicValue() && lhs.tokvalue != rhs.tokvalue)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
if (!lhs.isIntValue() && !compareOp) {
|
|
|
|
result.valueType = lhs.valueType;
|
|
|
|
result.tokvalue = lhs.tokvalue;
|
|
|
|
} else if (!rhs.isIntValue() && !compareOp) {
|
|
|
|
result.valueType = rhs.valueType;
|
|
|
|
result.tokvalue = rhs.tokvalue;
|
|
|
|
} else {
|
|
|
|
result.valueType = ValueFlow::Value::ValueType::INT;
|
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();
|
2023-03-24 13:31:26 +01:00
|
|
|
if (result.isImpossible() && op == "!=") {
|
|
|
|
if (isTrue(result)) {
|
|
|
|
result.intvalue = 1;
|
|
|
|
} else if (isFalse(result)) {
|
|
|
|
result.intvalue = 0;
|
|
|
|
} else {
|
|
|
|
return ValueFlow::Value::unknown();
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
2023-03-24 13:31:26 +01:00
|
|
|
result.setPossible();
|
|
|
|
result.bound = ValueFlow::Value::Bound::Point;
|
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-07-09 07:40:32 +02:00
|
|
|
using BuiltinLibraryFunction = std::function<ValueFlow::Value(const std::vector<ValueFlow::Value>&)>;
|
|
|
|
static std::unordered_map<std::string, BuiltinLibraryFunction> createBuiltinLibraryFunctions()
|
|
|
|
{
|
|
|
|
std::unordered_map<std::string, BuiltinLibraryFunction> functions;
|
|
|
|
functions["strlen"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!(v.isTokValue() && v.tokvalue->tokType() == Token::eString))
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::INT;
|
|
|
|
v.intvalue = Token::getStrLength(v.tokvalue);
|
|
|
|
v.tokvalue = nullptr;
|
|
|
|
return v;
|
|
|
|
};
|
2023-06-27 10:44:11 +02:00
|
|
|
functions["strcmp"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 2)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
const ValueFlow::Value& lhs = args[0];
|
|
|
|
if (!(lhs.isTokValue() && lhs.tokvalue->tokType() == Token::eString))
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
const ValueFlow::Value& rhs = args[1];
|
|
|
|
if (!(rhs.isTokValue() && rhs.tokvalue->tokType() == Token::eString))
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v(getStringLiteral(lhs.tokvalue->str()).compare(getStringLiteral(rhs.tokvalue->str())));
|
|
|
|
ValueFlow::combineValueProperties(lhs, rhs, v);
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["strncmp"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 3)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
const ValueFlow::Value& lhs = args[0];
|
|
|
|
if (!(lhs.isTokValue() && lhs.tokvalue->tokType() == Token::eString))
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
const ValueFlow::Value& rhs = args[1];
|
|
|
|
if (!(rhs.isTokValue() && rhs.tokvalue->tokType() == Token::eString))
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
const ValueFlow::Value& len = args[2];
|
|
|
|
if (!len.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v(getStringLiteral(lhs.tokvalue->str())
|
|
|
|
.compare(0, len.intvalue, getStringLiteral(rhs.tokvalue->str()), 0, len.intvalue));
|
|
|
|
ValueFlow::combineValueProperties(lhs, rhs, v);
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 07:40:32 +02:00
|
|
|
functions["sin"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::sin(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 19:59:16 +02:00
|
|
|
functions["lgamma"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 19:59:16 +02:00
|
|
|
v.floatValue = std::lgamma(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 07:40:32 +02:00
|
|
|
functions["cos"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::cos(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["tan"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::tan(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["asin"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::asin(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["acos"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::acos(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["atan"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::atan(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["atan2"] = [](const std::vector<ValueFlow::Value>& args) {
|
2022-12-30 15:13:47 +01:00
|
|
|
if (args.size() != 2 || !std::all_of(args.cbegin(), args.cend(), [](const ValueFlow::Value& v) {
|
2022-07-28 22:11:23 +02:00
|
|
|
return v.isFloatValue() || v.isIntValue();
|
|
|
|
}))
|
2022-07-09 07:40:32 +02:00
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-28 22:11:23 +02:00
|
|
|
ValueFlow::Value v;
|
2023-03-02 22:05:41 +01:00
|
|
|
combineValueProperties(args[0], args[1], v);
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::atan2(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 22:36:17 +02:00
|
|
|
functions["remainder"] = [](const std::vector<ValueFlow::Value>& args) {
|
2022-12-30 15:13:47 +01:00
|
|
|
if (args.size() != 2 || !std::all_of(args.cbegin(), args.cend(), [](const ValueFlow::Value& v) {
|
2022-07-28 22:11:23 +02:00
|
|
|
return v.isFloatValue() || v.isIntValue();
|
|
|
|
}))
|
2022-07-09 22:36:17 +02:00
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-28 22:11:23 +02:00
|
|
|
ValueFlow::Value v;
|
2023-03-02 22:05:41 +01:00
|
|
|
combineValueProperties(args[0], args[1], v);
|
2022-07-09 22:36:17 +02:00
|
|
|
v.floatValue = std::remainder(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["nextafter"] = [](const std::vector<ValueFlow::Value>& args) {
|
2022-12-30 15:13:47 +01:00
|
|
|
if (args.size() != 2 || !std::all_of(args.cbegin(), args.cend(), [](const ValueFlow::Value& v) {
|
2022-07-28 22:11:23 +02:00
|
|
|
return v.isFloatValue() || v.isIntValue();
|
|
|
|
}))
|
2022-07-09 22:36:17 +02:00
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-28 22:11:23 +02:00
|
|
|
ValueFlow::Value v;
|
2023-03-02 22:05:41 +01:00
|
|
|
combineValueProperties(args[0], args[1], v);
|
2022-07-09 22:36:17 +02:00
|
|
|
v.floatValue = std::nextafter(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["nexttoward"] = [](const std::vector<ValueFlow::Value>& args) {
|
2022-12-30 15:13:47 +01:00
|
|
|
if (args.size() != 2 || !std::all_of(args.cbegin(), args.cend(), [](const ValueFlow::Value& v) {
|
2022-07-28 22:11:23 +02:00
|
|
|
return v.isFloatValue() || v.isIntValue();
|
|
|
|
}))
|
2022-07-09 22:36:17 +02:00
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-28 22:11:23 +02:00
|
|
|
ValueFlow::Value v;
|
2023-03-02 22:05:41 +01:00
|
|
|
combineValueProperties(args[0], args[1], v);
|
2022-07-09 22:36:17 +02:00
|
|
|
v.floatValue = std::nexttoward(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 19:59:16 +02:00
|
|
|
functions["hypot"] = [](const std::vector<ValueFlow::Value>& args) {
|
2022-12-30 15:13:47 +01:00
|
|
|
if (args.size() != 2 || !std::all_of(args.cbegin(), args.cend(), [](const ValueFlow::Value& v) {
|
2022-07-28 22:11:23 +02:00
|
|
|
return v.isFloatValue() || v.isIntValue();
|
|
|
|
}))
|
2022-07-09 19:59:16 +02:00
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-28 22:11:23 +02:00
|
|
|
ValueFlow::Value v;
|
2023-03-02 22:05:41 +01:00
|
|
|
combineValueProperties(args[0], args[1], v);
|
2022-07-09 19:59:16 +02:00
|
|
|
v.floatValue = std::hypot(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 19:21:58 +02:00
|
|
|
functions["fdim"] = [](const std::vector<ValueFlow::Value>& args) {
|
2022-12-30 15:13:47 +01:00
|
|
|
if (args.size() != 2 || !std::all_of(args.cbegin(), args.cend(), [](const ValueFlow::Value& v) {
|
2022-07-28 22:11:23 +02:00
|
|
|
return v.isFloatValue() || v.isIntValue();
|
|
|
|
}))
|
2022-07-09 19:21:58 +02:00
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-28 22:11:23 +02:00
|
|
|
ValueFlow::Value v;
|
2023-03-02 22:05:41 +01:00
|
|
|
combineValueProperties(args[0], args[1], v);
|
2022-07-09 19:21:58 +02:00
|
|
|
v.floatValue = std::fdim(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["fmax"] = [](const std::vector<ValueFlow::Value>& args) {
|
2022-12-30 15:13:47 +01:00
|
|
|
if (args.size() != 2 || !std::all_of(args.cbegin(), args.cend(), [](const ValueFlow::Value& v) {
|
2022-07-28 22:11:23 +02:00
|
|
|
return v.isFloatValue() || v.isIntValue();
|
|
|
|
}))
|
2022-07-09 19:21:58 +02:00
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-28 22:11:23 +02:00
|
|
|
ValueFlow::Value v;
|
2023-03-02 22:05:41 +01:00
|
|
|
combineValueProperties(args[0], args[1], v);
|
2022-07-09 19:21:58 +02:00
|
|
|
v.floatValue = std::fmax(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["fmin"] = [](const std::vector<ValueFlow::Value>& args) {
|
2022-12-30 15:13:47 +01:00
|
|
|
if (args.size() != 2 || !std::all_of(args.cbegin(), args.cend(), [](const ValueFlow::Value& v) {
|
2022-07-28 22:11:23 +02:00
|
|
|
return v.isFloatValue() || v.isIntValue();
|
|
|
|
}))
|
2022-07-09 19:21:58 +02:00
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-28 22:11:23 +02:00
|
|
|
ValueFlow::Value v;
|
2023-03-02 22:05:41 +01:00
|
|
|
combineValueProperties(args[0], args[1], v);
|
2022-07-09 19:21:58 +02:00
|
|
|
v.floatValue = std::fmin(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["fmod"] = [](const std::vector<ValueFlow::Value>& args) {
|
2022-12-30 15:13:47 +01:00
|
|
|
if (args.size() != 2 || !std::all_of(args.cbegin(), args.cend(), [](const ValueFlow::Value& v) {
|
2022-07-28 22:11:23 +02:00
|
|
|
return v.isFloatValue() || v.isIntValue();
|
|
|
|
}))
|
2022-07-09 19:21:58 +02:00
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-28 22:11:23 +02:00
|
|
|
ValueFlow::Value v;
|
2023-03-02 22:05:41 +01:00
|
|
|
combineValueProperties(args[0], args[1], v);
|
2022-07-09 19:21:58 +02:00
|
|
|
v.floatValue = std::fmod(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 07:40:32 +02:00
|
|
|
functions["pow"] = [](const std::vector<ValueFlow::Value>& args) {
|
2022-12-30 15:13:47 +01:00
|
|
|
if (args.size() != 2 || !std::all_of(args.cbegin(), args.cend(), [](const ValueFlow::Value& v) {
|
2022-07-28 22:11:23 +02:00
|
|
|
return v.isFloatValue() || v.isIntValue();
|
|
|
|
}))
|
2022-07-09 07:40:32 +02:00
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-28 22:11:23 +02:00
|
|
|
ValueFlow::Value v;
|
2023-03-02 22:05:41 +01:00
|
|
|
combineValueProperties(args[0], args[1], v);
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::pow(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 22:36:17 +02:00
|
|
|
functions["scalbln"] = [](const std::vector<ValueFlow::Value>& args) {
|
2022-12-30 15:13:47 +01:00
|
|
|
if (args.size() != 2 || !std::all_of(args.cbegin(), args.cend(), [](const ValueFlow::Value& v) {
|
2022-07-28 22:11:23 +02:00
|
|
|
return v.isFloatValue() || v.isIntValue();
|
|
|
|
}))
|
2022-07-09 22:36:17 +02:00
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-28 22:11:23 +02:00
|
|
|
ValueFlow::Value v;
|
2023-03-02 22:05:41 +01:00
|
|
|
combineValueProperties(args[0], args[1], v);
|
2022-07-09 22:36:17 +02:00
|
|
|
v.floatValue = std::scalbln(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 19:59:16 +02:00
|
|
|
functions["ldexp"] = [](const std::vector<ValueFlow::Value>& args) {
|
2022-12-30 15:13:47 +01:00
|
|
|
if (args.size() != 2 || !std::all_of(args.cbegin(), args.cend(), [](const ValueFlow::Value& v) {
|
2022-07-28 22:11:23 +02:00
|
|
|
return v.isFloatValue() || v.isIntValue();
|
|
|
|
}))
|
2022-07-09 19:59:16 +02:00
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-28 22:11:23 +02:00
|
|
|
ValueFlow::Value v;
|
2023-03-02 22:05:41 +01:00
|
|
|
combineValueProperties(args[0], args[1], v);
|
2022-07-09 19:59:16 +02:00
|
|
|
v.floatValue = std::ldexp(value, args[1].isFloatValue() ? args[1].floatValue : args[1].intvalue);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["ilogb"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 19:59:16 +02:00
|
|
|
v.intvalue = std::ilogb(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::INT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 17:38:28 +02:00
|
|
|
functions["erf"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 17:38:28 +02:00
|
|
|
v.floatValue = std::erf(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 17:43:17 +02:00
|
|
|
functions["erfc"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 17:43:17 +02:00
|
|
|
v.floatValue = std::erfc(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 19:21:58 +02:00
|
|
|
functions["floor"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 19:21:58 +02:00
|
|
|
v.floatValue = std::floor(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 07:40:32 +02:00
|
|
|
functions["sqrt"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::sqrt(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 17:28:15 +02:00
|
|
|
functions["cbrt"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 17:28:15 +02:00
|
|
|
v.floatValue = std::cbrt(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 17:32:32 +02:00
|
|
|
functions["ceil"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 17:32:32 +02:00
|
|
|
v.floatValue = std::ceil(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 07:40:32 +02:00
|
|
|
functions["exp"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::exp(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
2022-07-09 17:47:21 +02:00
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["exp2"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 17:47:21 +02:00
|
|
|
v.floatValue = std::exp2(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
2022-07-09 17:52:58 +02:00
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["expm1"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 19:21:58 +02:00
|
|
|
v.floatValue = std::expm1(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["fabs"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 19:21:58 +02:00
|
|
|
v.floatValue = std::fabs(value);
|
2022-07-09 17:52:58 +02:00
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
2022-07-09 07:40:32 +02:00
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["log"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::log(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["log10"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::log10(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 22:36:17 +02:00
|
|
|
functions["log1p"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 22:36:17 +02:00
|
|
|
v.floatValue = std::log1p(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["log2"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 22:36:17 +02:00
|
|
|
v.floatValue = std::log2(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["logb"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 22:36:17 +02:00
|
|
|
v.floatValue = std::logb(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["nearbyint"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 22:36:17 +02:00
|
|
|
v.floatValue = std::nearbyint(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 07:40:32 +02:00
|
|
|
functions["sinh"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::sinh(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["cosh"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::cosh(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["tanh"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::tanh(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["asinh"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::asinh(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["acosh"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::acosh(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["atanh"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 07:40:32 +02:00
|
|
|
v.floatValue = std::atanh(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 22:36:17 +02:00
|
|
|
functions["round"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 22:36:17 +02:00
|
|
|
v.floatValue = std::round(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["tgamma"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 22:36:17 +02:00
|
|
|
v.floatValue = std::tgamma(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
functions["trunc"] = [](const std::vector<ValueFlow::Value>& args) {
|
|
|
|
if (args.size() != 1)
|
|
|
|
return ValueFlow::Value::unknown();
|
|
|
|
ValueFlow::Value v = args[0];
|
|
|
|
if (!v.isFloatValue() && !v.isIntValue())
|
|
|
|
return ValueFlow::Value::unknown();
|
2022-10-02 07:12:40 +02:00
|
|
|
const double value = args[0].isFloatValue() ? args[0].floatValue : args[0].intvalue;
|
2022-07-09 22:36:17 +02:00
|
|
|
v.floatValue = std::trunc(value);
|
|
|
|
v.valueType = ValueFlow::Value::ValueType::FLOAT;
|
|
|
|
return v;
|
|
|
|
};
|
2022-07-09 07:40:32 +02:00
|
|
|
return functions;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BuiltinLibraryFunction getBuiltinLibraryFunction(const std::string& name)
|
|
|
|
{
|
|
|
|
static const std::unordered_map<std::string, BuiltinLibraryFunction> functions = createBuiltinLibraryFunctions();
|
|
|
|
auto it = functions.find(name);
|
|
|
|
if (it == functions.end())
|
|
|
|
return nullptr;
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2023-07-01 09:43:57 +02:00
|
|
|
struct Executor {
|
|
|
|
ProgramMemory* pm = nullptr;
|
|
|
|
const Settings* settings = nullptr;
|
|
|
|
int fdepth = 4;
|
|
|
|
|
|
|
|
explicit Executor(ProgramMemory* pm = nullptr, const Settings* settings = nullptr) : pm(pm), settings(settings) {}
|
|
|
|
|
|
|
|
ValueFlow::Value executeImpl(const Token* expr)
|
|
|
|
{
|
|
|
|
const ValueFlow::Value* value = nullptr;
|
|
|
|
if (!expr)
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
if (expr->hasKnownIntValue() && !expr->isAssignmentOp() && expr->str() != ",")
|
|
|
|
return expr->values().front();
|
|
|
|
if ((value = expr->getKnownValue(ValueFlow::Value::ValueType::FLOAT)) ||
|
|
|
|
(value = expr->getKnownValue(ValueFlow::Value::ValueType::TOK)) ||
|
|
|
|
(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;
|
2023-06-20 18:43:21 +02:00
|
|
|
}
|
2023-07-01 09:43:57 +02:00
|
|
|
if (expr->isNumber()) {
|
|
|
|
if (MathLib::isFloat(expr->str()))
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-10-05 19:21:42 +02:00
|
|
|
MathLib::bigint i = MathLib::toBigNumber(expr->str());
|
2023-07-01 09:43:57 +02:00
|
|
|
if (i < 0 && astIsUnsigned(expr))
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
return ValueFlow::Value{i};
|
2021-08-27 05:46:33 +02:00
|
|
|
}
|
2023-07-01 09:43:57 +02:00
|
|
|
if (expr->isBoolean())
|
|
|
|
return ValueFlow::Value{expr->str() == "true"};
|
|
|
|
if (Token::Match(expr->tokAt(-2), ". %name% (") && astIsContainer(expr->tokAt(-2)->astOperand1())) {
|
|
|
|
const Token* containerTok = expr->tokAt(-2)->astOperand1();
|
|
|
|
const Library::Container::Yield yield = containerTok->valueType()->container->getYield(expr->strAt(-1));
|
|
|
|
if (yield == Library::Container::Yield::SIZE) {
|
|
|
|
ValueFlow::Value v = execute(containerTok);
|
|
|
|
if (!v.isContainerSizeValue())
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
v.valueType = ValueFlow::Value::ValueType::INT;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
if (yield == Library::Container::Yield::EMPTY) {
|
|
|
|
ValueFlow::Value v = execute(containerTok);
|
|
|
|
if (!v.isContainerSizeValue())
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
if (v.isImpossible() && v.intvalue == 0)
|
|
|
|
return ValueFlow::Value{0};
|
|
|
|
if (!v.isImpossible())
|
|
|
|
return ValueFlow::Value{v.intvalue == 0};
|
|
|
|
}
|
|
|
|
} else if (expr->isAssignmentOp() && expr->astOperand1() && expr->astOperand2() &&
|
|
|
|
expr->astOperand1()->exprId() > 0) {
|
|
|
|
ValueFlow::Value rhs = execute(expr->astOperand2());
|
|
|
|
if (rhs.isUninitValue())
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
if (expr->str() != "=") {
|
|
|
|
if (!pm->hasValue(expr->astOperand1()->exprId()))
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
ValueFlow::Value& lhs = pm->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));
|
|
|
|
else
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
pm->setValue(expr->astOperand1(), rhs);
|
|
|
|
return rhs;
|
|
|
|
} else if (expr->str() == "&&" && expr->astOperand1() && expr->astOperand2()) {
|
|
|
|
ValueFlow::Value lhs = execute(expr->astOperand1());
|
|
|
|
if (!lhs.isIntValue())
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
if (isFalse(lhs))
|
|
|
|
return lhs;
|
|
|
|
if (isTrue(lhs))
|
|
|
|
return execute(expr->astOperand2());
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
} else if (expr->str() == "||" && expr->astOperand1() && expr->astOperand2()) {
|
|
|
|
ValueFlow::Value lhs = execute(expr->astOperand1());
|
|
|
|
if (!lhs.isIntValue() || lhs.isImpossible())
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
if (isTrue(lhs))
|
|
|
|
return lhs;
|
|
|
|
if (isFalse(lhs))
|
|
|
|
return execute(expr->astOperand2());
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
} else if (expr->str() == "," && expr->astOperand1() && expr->astOperand2()) {
|
|
|
|
execute(expr->astOperand1());
|
|
|
|
return execute(expr->astOperand2());
|
|
|
|
} else if (expr->tokType() == Token::eIncDecOp && expr->astOperand1() && expr->astOperand1()->exprId() != 0) {
|
|
|
|
if (!pm->hasValue(expr->astOperand1()->exprId()))
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
ValueFlow::Value& lhs = pm->at(expr->astOperand1()->exprId());
|
|
|
|
if (!lhs.isIntValue())
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
// overflow
|
|
|
|
if (!lhs.isImpossible() && lhs.intvalue == 0 && expr->str() == "--" && astIsUnsigned(expr->astOperand1()))
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2021-10-04 07:53:58 +02:00
|
|
|
|
2023-07-01 09:43:57 +02:00
|
|
|
if (expr->str() == "++")
|
|
|
|
lhs.intvalue++;
|
|
|
|
else
|
|
|
|
lhs.intvalue--;
|
|
|
|
return lhs;
|
|
|
|
} else if (expr->str() == "[" && expr->astOperand1() && expr->astOperand2()) {
|
|
|
|
const Token* tokvalue = nullptr;
|
|
|
|
if (!pm->getTokValue(expr->astOperand1()->exprId(), &tokvalue)) {
|
|
|
|
auto tokvalue_it = std::find_if(expr->astOperand1()->values().cbegin(),
|
|
|
|
expr->astOperand1()->values().cend(),
|
|
|
|
std::mem_fn(&ValueFlow::Value::isTokValue));
|
|
|
|
if (tokvalue_it == expr->astOperand1()->values().cend() || !tokvalue_it->isKnown()) {
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
}
|
|
|
|
tokvalue = tokvalue_it->tokvalue;
|
|
|
|
}
|
|
|
|
if (!tokvalue || !tokvalue->isLiteral()) {
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
2023-07-01 09:43:57 +02:00
|
|
|
const std::string strValue = tokvalue->strValue();
|
|
|
|
ValueFlow::Value rhs = execute(expr->astOperand2());
|
|
|
|
if (!rhs.isIntValue())
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
const MathLib::bigint index = rhs.intvalue;
|
|
|
|
if (index >= 0 && index < strValue.size())
|
|
|
|
return ValueFlow::Value{strValue[index]};
|
|
|
|
if (index == strValue.size())
|
|
|
|
return ValueFlow::Value{};
|
|
|
|
} else if (Token::Match(expr, "%cop%") && expr->astOperand1() && expr->astOperand2()) {
|
|
|
|
ValueFlow::Value lhs = execute(expr->astOperand1());
|
|
|
|
ValueFlow::Value rhs = execute(expr->astOperand2());
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
ValueFlow::Value r = ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
if (!lhs.isUninitValue() && !rhs.isUninitValue())
|
|
|
|
r = evaluate(expr->str(), lhs, rhs);
|
|
|
|
if (expr->isComparisonOp() && (r.isUninitValue() || r.isImpossible())) {
|
|
|
|
if (rhs.isIntValue()) {
|
|
|
|
std::vector<ValueFlow::Value> result =
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
infer(ValueFlow::makeIntegralInferModel(), expr->str(), expr->astOperand1()->values(), {std::move(rhs)});
|
2023-07-01 09:43:57 +02:00
|
|
|
if (!result.empty() && result.front().isKnown())
|
|
|
|
return result.front();
|
|
|
|
}
|
|
|
|
if (lhs.isIntValue()) {
|
|
|
|
std::vector<ValueFlow::Value> result =
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
infer(ValueFlow::makeIntegralInferModel(), expr->str(), {std::move(lhs)}, expr->astOperand2()->values());
|
2023-07-01 09:43:57 +02:00
|
|
|
if (!result.empty() && result.front().isKnown())
|
|
|
|
return result.front();
|
|
|
|
}
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
}
|
|
|
|
return r;
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
2023-07-01 09:43:57 +02:00
|
|
|
// Unary ops
|
|
|
|
else if (Token::Match(expr, "!|+|-") && expr->astOperand1() && !expr->astOperand2()) {
|
|
|
|
ValueFlow::Value lhs = execute(expr->astOperand1());
|
|
|
|
if (!lhs.isIntValue())
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
if (expr->str() == "!") {
|
|
|
|
if (isTrue(lhs)) {
|
|
|
|
lhs.intvalue = 0;
|
|
|
|
} else if (isFalse(lhs)) {
|
|
|
|
lhs.intvalue = 1;
|
|
|
|
} else {
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
}
|
|
|
|
lhs.setPossible();
|
|
|
|
lhs.bound = ValueFlow::Value::Bound::Point;
|
|
|
|
}
|
|
|
|
if (expr->str() == "-")
|
|
|
|
lhs.intvalue = -lhs.intvalue;
|
|
|
|
return lhs;
|
|
|
|
} else if (expr->str() == "?" && expr->astOperand1() && expr->astOperand2()) {
|
|
|
|
ValueFlow::Value cond = execute(expr->astOperand1());
|
|
|
|
if (!cond.isIntValue())
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
const Token* child = expr->astOperand2();
|
|
|
|
if (isFalse(cond))
|
|
|
|
return execute(child->astOperand2());
|
|
|
|
if (isTrue(cond))
|
|
|
|
return execute(child->astOperand1());
|
|
|
|
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
} else if (expr->str() == "(" && expr->isCast()) {
|
|
|
|
if (Token::simpleMatch(expr->previous(), ">") && expr->previous()->link())
|
|
|
|
return execute(expr->astOperand2());
|
|
|
|
return execute(expr->astOperand1());
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
2023-07-01 09:43:57 +02:00
|
|
|
if (expr->exprId() > 0 && pm->hasValue(expr->exprId())) {
|
|
|
|
ValueFlow::Value result = pm->at(expr->exprId());
|
|
|
|
if (result.isImpossible() && result.isIntValue() && result.intvalue == 0 && isUsedAsBool(expr)) {
|
|
|
|
result.intvalue = !result.intvalue;
|
|
|
|
result.setKnown();
|
2021-10-04 07:53:58 +02:00
|
|
|
}
|
2023-07-01 09:43:57 +02:00
|
|
|
return result;
|
2021-10-04 07:53:58 +02:00
|
|
|
}
|
2023-07-01 09:43:57 +02:00
|
|
|
|
|
|
|
if (Token::Match(expr->previous(), ">|%name% {|(")) {
|
|
|
|
const Token* ftok = expr->previous();
|
|
|
|
const Function* f = ftok->function();
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
ValueFlow::Value result = ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
if (settings && expr->str() == "(") {
|
|
|
|
std::vector<const Token*> tokArgs = getArguments(expr);
|
|
|
|
std::vector<ValueFlow::Value> args(tokArgs.size());
|
|
|
|
std::transform(tokArgs.cbegin(), tokArgs.cend(), args.begin(), [&](const Token* tok) {
|
|
|
|
return execute(tok);
|
|
|
|
});
|
|
|
|
if (f) {
|
2023-07-13 20:38:47 +02:00
|
|
|
if (fdepth >= 0 && !f->isImplicitlyVirtual()) {
|
2023-07-01 09:43:57 +02:00
|
|
|
ProgramMemory functionState;
|
|
|
|
for (std::size_t i = 0; i < args.size(); ++i) {
|
|
|
|
const Variable* const arg = f->getArgumentVar(i);
|
|
|
|
if (!arg)
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
functionState.setValue(arg->nameToken(), args[i]);
|
|
|
|
}
|
|
|
|
Executor ex = *this;
|
|
|
|
ex.pm = &functionState;
|
|
|
|
ex.fdepth--;
|
|
|
|
auto r = ex.execute(f->functionScope);
|
|
|
|
if (!r.empty())
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
result = std::move(r.front());
|
2023-07-01 09:43:57 +02:00
|
|
|
// TODO: Track values changed by reference
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BuiltinLibraryFunction lf = getBuiltinLibraryFunction(ftok->str());
|
|
|
|
if (lf)
|
|
|
|
return lf(args);
|
|
|
|
const std::string& returnValue = settings->library.returnValue(ftok);
|
|
|
|
if (!returnValue.empty()) {
|
|
|
|
std::unordered_map<nonneg int, ValueFlow::Value> arg_map;
|
|
|
|
int argn = 0;
|
|
|
|
for (const ValueFlow::Value& v : args) {
|
|
|
|
if (!v.isUninitValue())
|
|
|
|
arg_map[argn] = v;
|
|
|
|
argn++;
|
|
|
|
}
|
|
|
|
return evaluateLibraryFunction(arg_map, returnValue, settings);
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 13:31:26 +01:00
|
|
|
}
|
2023-07-01 09:43:57 +02:00
|
|
|
// Check if function modifies argument
|
|
|
|
visitAstNodes(expr->astOperand2(), [&](const Token* child) {
|
|
|
|
if (child->exprId() > 0 && pm->hasValue(child->exprId())) {
|
|
|
|
ValueFlow::Value& v = pm->at(child->exprId());
|
|
|
|
if (v.valueType == ValueFlow::Value::ValueType::CONTAINER_SIZE) {
|
|
|
|
if (ValueFlow::isContainerSizeChanged(child, v.indirect, settings))
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
v = ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
} else if (v.valueType != ValueFlow::Value::ValueType::UNINIT) {
|
|
|
|
if (isVariableChanged(child, v.indirect, settings, true))
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
v = ValueFlow::Value::unknown();
|
2023-07-01 09:43:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ChildrenToVisit::op1_and_op2;
|
|
|
|
});
|
|
|
|
return result;
|
2023-03-24 13:31:26 +01:00
|
|
|
}
|
2023-06-20 18:43:21 +02:00
|
|
|
|
optimized `ProgramMemory` a bit (#5668)
Scanning `common/file.c` of the `xrdp` project with `--force --std=c11
--std=c++11 --inline-suppr --enable=warning`:
Clang 16 `4,208,373,435` -> `4,156,024,955`
GCC 13 `4,336,042,153` -> `4,277,293,971`
2023-11-16 14:35:25 +01:00
|
|
|
return ValueFlow::Value::unknown();
|
2021-10-04 07:53:58 +02:00
|
|
|
}
|
2023-07-01 09:43:57 +02:00
|
|
|
static const ValueFlow::Value* getImpossibleValue(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return nullptr;
|
|
|
|
std::vector<const ValueFlow::Value*> values;
|
|
|
|
for (const ValueFlow::Value& v : tok->values()) {
|
|
|
|
if (!v.isImpossible())
|
|
|
|
continue;
|
|
|
|
if (v.isContainerSizeValue() || v.isIntValue()) {
|
|
|
|
values.push_back(std::addressof(v));
|
2022-04-06 06:35:38 +02:00
|
|
|
}
|
|
|
|
}
|
2023-07-01 09:43:57 +02:00
|
|
|
auto it =
|
|
|
|
std::max_element(values.begin(), values.end(), [](const ValueFlow::Value* x, const ValueFlow::Value* y) {
|
|
|
|
return x->intvalue < y->intvalue;
|
2021-12-20 07:28:40 +01:00
|
|
|
});
|
2023-07-01 09:43:57 +02:00
|
|
|
if (it == values.end())
|
|
|
|
return nullptr;
|
|
|
|
return *it;
|
2021-12-20 07:28:40 +01:00
|
|
|
}
|
|
|
|
|
2023-07-01 09:43:57 +02:00
|
|
|
ValueFlow::Value execute(const Token* expr)
|
|
|
|
{
|
|
|
|
ValueFlow::Value v = executeImpl(expr);
|
|
|
|
if (!v.isUninitValue())
|
|
|
|
return v;
|
|
|
|
if (!expr)
|
|
|
|
return v;
|
|
|
|
if (expr->exprId() > 0 && pm->hasValue(expr->exprId()))
|
|
|
|
return pm->at(expr->exprId());
|
|
|
|
if (const ValueFlow::Value* value = getImpossibleValue(expr))
|
|
|
|
return *value;
|
|
|
|
return v;
|
|
|
|
}
|
2021-10-04 07:53:58 +02:00
|
|
|
|
2023-07-01 09:43:57 +02:00
|
|
|
std::vector<ValueFlow::Value> execute(const Scope* scope)
|
|
|
|
{
|
|
|
|
static const std::vector<ValueFlow::Value> unknown = {ValueFlow::Value::unknown()};
|
|
|
|
if (!scope)
|
|
|
|
return unknown;
|
|
|
|
if (!scope->bodyStart)
|
|
|
|
return unknown;
|
|
|
|
for (const Token* tok = scope->bodyStart->next(); precedes(tok, scope->bodyEnd); tok = tok->next()) {
|
|
|
|
const Token* top = tok->astTop();
|
|
|
|
if (!top)
|
|
|
|
return unknown;
|
|
|
|
|
|
|
|
if (Token::simpleMatch(top, "return") && top->astOperand1())
|
|
|
|
return {execute(top->astOperand1())};
|
|
|
|
|
|
|
|
if (Token::Match(top, "%op%")) {
|
|
|
|
if (execute(top).isUninitValue())
|
|
|
|
return unknown;
|
|
|
|
const Token* next = nextAfterAstRightmostLeaf(top);
|
|
|
|
if (!next)
|
|
|
|
return unknown;
|
|
|
|
tok = next;
|
|
|
|
} else if (Token::simpleMatch(top->previous(), "if (")) {
|
|
|
|
const Token* condTok = top->astOperand2();
|
|
|
|
ValueFlow::Value v = execute(condTok);
|
|
|
|
if (!v.isIntValue())
|
|
|
|
return unknown;
|
|
|
|
const Token* thenStart = top->link()->next();
|
|
|
|
const Token* next = thenStart->link();
|
|
|
|
const Token* elseStart = nullptr;
|
|
|
|
if (Token::simpleMatch(thenStart->link(), "} else {")) {
|
|
|
|
elseStart = thenStart->link()->tokAt(2);
|
|
|
|
next = elseStart->link();
|
|
|
|
}
|
|
|
|
std::vector<ValueFlow::Value> result;
|
|
|
|
if (isTrue(v)) {
|
|
|
|
result = execute(thenStart->scope());
|
|
|
|
} else if (isFalse(v)) {
|
|
|
|
if (elseStart)
|
|
|
|
result = execute(elseStart->scope());
|
|
|
|
} else {
|
|
|
|
return unknown;
|
|
|
|
}
|
|
|
|
if (!result.empty())
|
|
|
|
return result;
|
|
|
|
tok = next;
|
|
|
|
} else {
|
|
|
|
return unknown;
|
|
|
|
}
|
2023-03-24 13:31:26 +01:00
|
|
|
}
|
2023-07-01 09:43:57 +02:00
|
|
|
return {};
|
2023-03-24 13:31:26 +01:00
|
|
|
}
|
2023-07-01 09:43:57 +02:00
|
|
|
};
|
2023-03-24 13:31:26 +01:00
|
|
|
|
2022-02-10 20:22:14 +01:00
|
|
|
static ValueFlow::Value execute(const Token* expr, ProgramMemory& pm, const Settings* settings)
|
|
|
|
{
|
2023-07-01 09:43:57 +02:00
|
|
|
Executor ex{&pm, settings};
|
|
|
|
return ex.execute(expr);
|
2022-02-10 20:22:14 +01:00
|
|
|
}
|
|
|
|
|
2023-03-24 13:31:26 +01:00
|
|
|
std::vector<ValueFlow::Value> execute(const Scope* scope, ProgramMemory& pm, const Settings* settings)
|
|
|
|
{
|
2023-07-01 09:43:57 +02:00
|
|
|
Executor ex{&pm, settings};
|
|
|
|
return ex.execute(scope);
|
2023-03-24 13:31:26 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2022-07-13 13:46:03 +02:00
|
|
|
thread_local static std::unordered_map<std::string,
|
|
|
|
std::function<ValueFlow::Value(const std::unordered_map<nonneg int, ValueFlow::Value>& arg)>>
|
2022-04-06 06:35:38 +02:00
|
|
|
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,
|
2023-03-02 22:05:41 +01:00
|
|
|
ProgramMemory& programMemory,
|
2021-12-20 07:28:40 +01:00
|
|
|
MathLib::bigint* result,
|
|
|
|
bool* error,
|
|
|
|
const Settings* settings)
|
2021-10-04 07:53:58 +02:00
|
|
|
{
|
2023-03-02 22:05:41 +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
|
|
|
}
|