From 10cf643a167b858fd97eec0572aabae95276b514 Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 30 Sep 2024 18:57:00 +0200 Subject: [PATCH] moved `IteratorInferModel` and related classes to `infer.{cpp|h}` --- lib/infer.cpp | 38 ++++++++++++++++++++++++++++++++++++++ lib/infer.h | 3 +++ lib/valueflow.cpp | 30 ++---------------------------- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/lib/infer.cpp b/lib/infer.cpp index 8762e16cbcc..278237a2c66 100644 --- a/lib/infer.cpp +++ b/lib/infer.cpp @@ -434,6 +434,44 @@ ValuePtr 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 makeEndIteratorInferModel() +{ + return EndIteratorInferModel{}; +} + +ValuePtr makeStartIteratorInferModel() +{ + return StartIteratorInferModel{}; +} + ValueFlow::Value inferCondition(const std::string& op, const Token* varTok, MathLib::bigint val) { if (!varTok) diff --git a/lib/infer.h b/lib/infer.h index 0b5761b8ed1..436ee95eb14 100644 --- a/lib/infer.h +++ b/lib/infer.h @@ -61,6 +61,9 @@ ValuePtr makeIntegralInferModel(); ValuePtr makeSymbolicInferModel(const Token* token); +ValuePtr makeEndIteratorInferModel(); +ValuePtr makeStartIteratorInferModel(); + ValueFlow::Value inferCondition(const std::string& op, const Token* varTok, MathLib::bigint val); #endif diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index e07b8e5e19d..e81d8030354 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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%"); } @@ -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, 2> iteratorModels = {EndIteratorInferModel{}, - StartIteratorInferModel{}}; + static const std::array, 2> iteratorModels = {makeEndIteratorInferModel(), + makeStartIteratorInferModel()}; for (const ValuePtr& model : iteratorModels) { std::vector result = infer(model, tok->str(), tok->astOperand1()->values(), tok->astOperand2()->values());