Skip to content

Commit

Permalink
moved IteratorInferModel and related classes 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 0ca5c58 commit 10cf643
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
38 changes: 38 additions & 0 deletions lib/infer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,44 @@ ValuePtr<InferModel> makeSymbolicInferModel(const Token* token)
return SymbolicInferModel{token};
}

namespace {
struct IteratorInferModel : InferModel {
virtual ValueFlow::Value::ValueType getType() const = 0;
bool match(const ValueFlow::Value& value) const override {
return value.valueType == getType();
}
ValueFlow::Value yield(MathLib::bigint value) const override
{
ValueFlow::Value result(value);
result.valueType = getType();
result.setKnown();
return result;
}
};

struct EndIteratorInferModel : IteratorInferModel {
ValueFlow::Value::ValueType getType() const override {
return ValueFlow::Value::ValueType::ITERATOR_END;
}
};

struct StartIteratorInferModel : IteratorInferModel {
ValueFlow::Value::ValueType getType() const override {
return ValueFlow::Value::ValueType::ITERATOR_END;
}
};
}

ValuePtr<InferModel> makeEndIteratorInferModel()
{
return EndIteratorInferModel{};
}

ValuePtr<InferModel> makeStartIteratorInferModel()
{
return StartIteratorInferModel{};
}

ValueFlow::Value inferCondition(const std::string& op, const Token* varTok, MathLib::bigint val)
{
if (!varTok)
Expand Down
3 changes: 3 additions & 0 deletions lib/infer.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ ValuePtr<InferModel> makeIntegralInferModel();

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

ValuePtr<InferModel> makeEndIteratorInferModel();
ValuePtr<InferModel> makeStartIteratorInferModel();

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

#endif
30 changes: 2 additions & 28 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4900,32 +4900,6 @@ struct SimpleConditionHandler : ConditionHandler {
}
};

struct IteratorInferModel : InferModel {
virtual ValueFlow::Value::ValueType getType() const = 0;
bool match(const ValueFlow::Value& value) const override {
return value.valueType == getType();
}
ValueFlow::Value yield(MathLib::bigint value) const override
{
ValueFlow::Value result(value);
result.valueType = getType();
result.setKnown();
return result;
}
};

struct EndIteratorInferModel : IteratorInferModel {
ValueFlow::Value::ValueType getType() const override {
return ValueFlow::Value::ValueType::ITERATOR_END;
}
};

struct StartIteratorInferModel : IteratorInferModel {
ValueFlow::Value::ValueType getType() const override {
return ValueFlow::Value::ValueType::ITERATOR_END;
}
};

static bool isIntegralOnlyOperator(const Token* tok) {
return Token::Match(tok, "%|<<|>>|&|^|~|%or%");
}
Expand Down Expand Up @@ -4961,8 +4935,8 @@ static void valueFlowInferCondition(TokenList& tokenlist, const Settings& settin
continue;
if (Token::Match(tok, "%comp%|-") && tok->astOperand1() && tok->astOperand2()) {
if (astIsIterator(tok->astOperand1()) || astIsIterator(tok->astOperand2())) {
static const std::array<ValuePtr<InferModel>, 2> iteratorModels = {EndIteratorInferModel{},
StartIteratorInferModel{}};
static const std::array<ValuePtr<InferModel>, 2> iteratorModels = {makeEndIteratorInferModel(),
makeStartIteratorInferModel()};
for (const ValuePtr<InferModel>& model : iteratorModels) {
std::vector<ValueFlow::Value> result =
infer(model, tok->str(), tok->astOperand1()->values(), tok->astOperand2()->values());
Expand Down

0 comments on commit 10cf643

Please sign in to comment.