Skip to content

Commit

Permalink
moved SymbolicInferModel to infer.{cpp|h}
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Feb 6, 2025
1 parent fd3e458 commit 0ca5c58
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
26 changes: 26 additions & 0 deletions lib/infer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,32 @@ ValuePtr<InferModel> makeIntegralInferModel()
return IntegralInferModel{};
}

namespace {
struct SymbolicInferModel : InferModel {
const Token* expr;
explicit SymbolicInferModel(const Token* tok) : expr(tok) {
assert(expr->exprId() != 0);
}
bool match(const ValueFlow::Value& value) const override
{
return value.isSymbolicValue() && value.tokvalue && value.tokvalue->exprId() == expr->exprId();
}
ValueFlow::Value yield(MathLib::bigint value) const override
{
ValueFlow::Value result(value);
result.valueType = ValueFlow::Value::ValueType::SYMBOLIC;
result.tokvalue = expr;
result.setKnown();
return result;
}
};
}

ValuePtr<InferModel> makeSymbolicInferModel(const Token* token)
{
return SymbolicInferModel{token};
}

ValueFlow::Value inferCondition(const std::string& op, const Token* varTok, MathLib::bigint val)
{
if (!varTok)
Expand Down
2 changes: 2 additions & 0 deletions lib/infer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ std::vector<MathLib::bigint> getMaxValue(const ValuePtr<InferModel>& model, cons

ValuePtr<InferModel> makeIntegralInferModel();

ValuePtr<InferModel> makeSymbolicInferModel(const Token* token);

ValueFlow::Value inferCondition(const std::string& op, const Token* varTok, MathLib::bigint val);

#endif
23 changes: 2 additions & 21 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3664,25 +3664,6 @@ static void valueFlowSymbolicOperators(const SymbolDatabase& symboldatabase, con
}
}

struct SymbolicInferModel : InferModel {
const Token* expr;
explicit SymbolicInferModel(const Token* tok) : expr(tok) {
assert(expr->exprId() != 0);
}
bool match(const ValueFlow::Value& value) const override
{
return value.isSymbolicValue() && value.tokvalue && value.tokvalue->exprId() == expr->exprId();
}
ValueFlow::Value yield(MathLib::bigint value) const override
{
ValueFlow::Value result(value);
result.valueType = ValueFlow::Value::ValueType::SYMBOLIC;
result.tokvalue = expr;
result.setKnown();
return result;
}
};

static void valueFlowSymbolicInfer(const SymbolDatabase& symboldatabase, const Settings& settings)
{
for (const Scope* scope : symboldatabase.functionScopes) {
Expand Down Expand Up @@ -3710,11 +3691,11 @@ static void valueFlowSymbolicInfer(const SymbolDatabase& symboldatabase, const S

std::vector<ValueFlow::Value> values;
{
SymbolicInferModel leftModel{tok->astOperand1()};
auto leftModel = makeSymbolicInferModel(tok->astOperand1());
values = infer(leftModel, tok->str(), 0, tok->astOperand2()->values());
}
if (values.empty()) {
SymbolicInferModel rightModel{tok->astOperand2()};
auto rightModel = makeSymbolicInferModel(tok->astOperand2());
values = infer(rightModel, tok->str(), tok->astOperand1()->values(), 0);
}
for (ValueFlow::Value& value : values) {
Expand Down

0 comments on commit 0ca5c58

Please sign in to comment.