Skip to content

Commit 66d53a2

Browse files
authored
ValueFlow: extracted valueFlowSymbolicOperators() into separate file (#6845)
1 parent 8ae11ea commit 66d53a2

15 files changed

+275
-188
lines changed

Makefile

+6-2
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
278278
$(libcppdir)/vf_string.o \
279279
$(libcppdir)/vf_switchvariable.o \
280280
$(libcppdir)/vf_symbolicinfer.o \
281+
$(libcppdir)/vf_symbolicoperators.o \
281282
$(libcppdir)/vf_unknownfunctionreturn.o \
282283
$(libcppdir)/vfvalue.o
283284

@@ -479,7 +480,7 @@ validateRules:
479480

480481
###### Build
481482

482-
$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/check.h lib/checkuninitvar.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/findtoken.h lib/forwardanalyzer.h lib/infer.h lib/library.h lib/mathlib.h lib/platform.h lib/programmemory.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf_analyze.h lib/vf_analyzers.h lib/vf_array.h lib/vf_arraybool.h lib/vf_arrayelement.h lib/vf_bailout.h lib/vf_bitand.h lib/vf_common.h lib/vf_conditionexpressions.h lib/vf_debug.h lib/vf_enumvalue.h lib/vf_functionreturn.h lib/vf_globalconstvar.h lib/vf_globalstaticvar.h lib/vf_impossiblevalues.h lib/vf_infercondition.h lib/vf_iteratorinfer.h lib/vf_iterators.h lib/vf_number.h lib/vf_pointeralias.h lib/vf_reverse.h lib/vf_rightshift.h lib/vf_sameexpressions.h lib/vf_settokenvalue.h lib/vf_string.h lib/vf_switchvariable.h lib/vf_symbolicinfer.h lib/vf_unknownfunctionreturn.h lib/vfvalue.h
483+
$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/check.h lib/checkuninitvar.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/findtoken.h lib/forwardanalyzer.h lib/infer.h lib/library.h lib/mathlib.h lib/platform.h lib/programmemory.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf_analyze.h lib/vf_analyzers.h lib/vf_array.h lib/vf_arraybool.h lib/vf_arrayelement.h lib/vf_bailout.h lib/vf_bitand.h lib/vf_common.h lib/vf_conditionexpressions.h lib/vf_debug.h lib/vf_enumvalue.h lib/vf_functionreturn.h lib/vf_globalconstvar.h lib/vf_globalstaticvar.h lib/vf_impossiblevalues.h lib/vf_infercondition.h lib/vf_iteratorinfer.h lib/vf_iterators.h lib/vf_number.h lib/vf_pointeralias.h lib/vf_reverse.h lib/vf_rightshift.h lib/vf_sameexpressions.h lib/vf_settokenvalue.h lib/vf_string.h lib/vf_switchvariable.h lib/vf_symbolicinfer.h lib/vf_symbolicoperators.h lib/vf_unknownfunctionreturn.h lib/vfvalue.h
483484
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/valueflow.cpp
484485

485486
$(libcppdir)/tokenize.o: lib/tokenize.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/astutils.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/summaries.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/vfvalue.h
@@ -608,7 +609,7 @@ $(libcppdir)/fwdanalysis.o: lib/fwdanalysis.cpp lib/addoninfo.h lib/astutils.h l
608609
$(libcppdir)/importproject.o: lib/importproject.cpp externals/picojson/picojson.h externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/config.h lib/errortypes.h lib/filesettings.h lib/importproject.h lib/json.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h
609610
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/importproject.cpp
610611

611-
$(libcppdir)/infer.o: lib/infer.cpp lib/calculate.h lib/config.h lib/errortypes.h lib/infer.h lib/mathlib.h lib/valueptr.h lib/vfvalue.h
612+
$(libcppdir)/infer.o: lib/infer.cpp lib/calculate.h lib/config.h lib/errortypes.h lib/infer.h lib/mathlib.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueptr.h lib/vfvalue.h
612613
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/infer.cpp
613614

614615
$(libcppdir)/keywords.o: lib/keywords.cpp lib/config.h lib/keywords.h lib/standards.h lib/utils.h
@@ -743,6 +744,9 @@ $(libcppdir)/vf_switchvariable.o: lib/vf_switchvariable.cpp lib/addoninfo.h lib/
743744
$(libcppdir)/vf_symbolicinfer.o: lib/vf_symbolicinfer.cpp lib/astutils.h lib/config.h lib/errortypes.h lib/infer.h lib/library.h lib/mathlib.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueptr.h lib/vf_settokenvalue.h lib/vf_symbolicinfer.h lib/vfvalue.h
744745
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_symbolicinfer.cpp
745746

747+
$(libcppdir)/vf_symbolicoperators.o: lib/vf_symbolicoperators.cpp lib/astutils.h lib/config.h lib/errortypes.h lib/infer.h lib/library.h lib/mathlib.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vf_common.h lib/vf_settokenvalue.h lib/vf_symbolicoperators.h lib/vfvalue.h
748+
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_symbolicoperators.cpp
749+
746750
$(libcppdir)/vf_unknownfunctionreturn.o: lib/vf_unknownfunctionreturn.cpp lib/addoninfo.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vf_common.h lib/vf_settokenvalue.h lib/vf_unknownfunctionreturn.h lib/vfvalue.h
747751
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_unknownfunctionreturn.cpp
748752

lib/astutils.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ bool extractForLoopValues(const Token *forToken,
959959
const Token *incExpr = forToken->next()->astOperand2()->astOperand2()->astOperand2();
960960
if (!initExpr || !initExpr->isBinaryOp() || initExpr->str() != "=" || !Token::Match(initExpr->astOperand1(), "%var%"))
961961
return false;
962-
std::vector<MathLib::bigint> minInitValue = getMinValue(ValueFlow::makeIntegralInferModel(), initExpr->astOperand2()->values());
962+
std::vector<MathLib::bigint> minInitValue = getMinValue(makeIntegralInferModel(), initExpr->astOperand2()->values());
963963
if (minInitValue.empty()) {
964964
const ValueFlow::Value* v = initExpr->astOperand2()->getMinValue(true);
965965
if (v)

lib/cppcheck.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
<ClCompile Include="vf_string.cpp" />
114114
<ClCompile Include="vf_switchvariable.cpp" />
115115
<ClCompile Include="vf_symbolicinfer.cpp" />
116+
<ClCompile Include="vf_symbolicoperators.cpp" />
116117
<ClCompile Include="vf_unknownfunctionreturn.cpp" />
117118
<ClCompile Include="vfvalue.cpp" />
118119
</ItemGroup>
@@ -222,6 +223,7 @@
222223
<ClInclude Include="vf_string.h" />
223224
<ClInclude Include="vf_switchvariable.h" />
224225
<ClInclude Include="vf_symbolicinfer.h" />
226+
<ClInclude Include="vf_symbolicoperators.h" />
225227
<ClInclude Include="vf_unknownfunctionreturn.h" />
226228
<ClInclude Include="vfvalue.h" />
227229
<ClInclude Include="xml.h" />

lib/infer.cpp

+33-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "calculate.h"
2222
#include "errortypes.h"
23+
#include "token.h"
2324
#include "valueptr.h"
2425

2526
#include <cassert>
@@ -29,8 +30,6 @@
2930
#include <unordered_set>
3031
#include <utility>
3132

32-
class Token;
33-
3433
template<class Predicate, class Compare>
3534
static const ValueFlow::Value* getCompareValue(const std::list<ValueFlow::Value>& values, Predicate pred, Compare compare)
3635
{
@@ -388,3 +387,35 @@ std::vector<MathLib::bigint> getMaxValue(const ValuePtr<InferModel>& model, cons
388387
return model->match(v);
389388
}).maxvalue;
390389
}
390+
391+
namespace {
392+
struct IntegralInferModel : InferModel {
393+
bool match(const ValueFlow::Value& value) const override {
394+
return value.isIntValue();
395+
}
396+
ValueFlow::Value yield(MathLib::bigint value) const override
397+
{
398+
ValueFlow::Value result(value);
399+
result.valueType = ValueFlow::Value::ValueType::INT;
400+
result.setKnown();
401+
return result;
402+
}
403+
};
404+
}
405+
406+
ValuePtr<InferModel> makeIntegralInferModel()
407+
{
408+
return IntegralInferModel{};
409+
}
410+
411+
ValueFlow::Value inferCondition(const std::string& op, const Token* varTok, MathLib::bigint val)
412+
{
413+
if (!varTok)
414+
return ValueFlow::Value{};
415+
if (varTok->hasKnownIntValue())
416+
return ValueFlow::Value{};
417+
std::vector<ValueFlow::Value> r = infer(makeIntegralInferModel(), op, varTok->values(), val);
418+
if (r.size() == 1 && r.front().isKnown())
419+
return r.front();
420+
return ValueFlow::Value{};
421+
}

lib/infer.h

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <string>
2828
#include <vector>
2929

30+
class Token;
3031
template<class T> class ValuePtr;
3132

3233
struct InferModel {
@@ -56,4 +57,8 @@ std::vector<ValueFlow::Value> infer(const ValuePtr<InferModel>& model,
5657
CPPCHECKLIB std::vector<MathLib::bigint> getMinValue(const ValuePtr<InferModel>& model, const std::list<ValueFlow::Value>& values);
5758
std::vector<MathLib::bigint> getMaxValue(const ValuePtr<InferModel>& model, const std::list<ValueFlow::Value>& values);
5859

60+
ValuePtr<InferModel> makeIntegralInferModel();
61+
62+
ValueFlow::Value inferCondition(const std::string& op, const Token* varTok, MathLib::bigint val);
63+
5964
#endif

lib/lib.pri

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ HEADERS += $${PWD}/addoninfo.h \
106106
$${PWD}/vf_string.h \
107107
$${PWD}/vf_switchvariable.h \
108108
$${PWD}/vf_symbolicinfer.h \
109+
$${PWD}/vf_symbolicoperators.h \
109110
$${PWD}/vf_unknownfunctionreturn.h \
110111
$${PWD}/vfvalue.h \
111112
$${PWD}/xml.h
@@ -198,5 +199,6 @@ SOURCES += $${PWD}/valueflow.cpp \
198199
$${PWD}/vf_string.cpp \
199200
$${PWD}/vf_switchvariable.cpp \
200201
$${PWD}/vf_symbolicinfer.cpp \
202+
$${PWD}/vf_symbolicoperators.cpp \
201203
$${PWD}/vf_unknownfunctionreturn.cpp \
202204
$${PWD}/vfvalue.cpp

lib/programmemory.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1526,13 +1526,13 @@ namespace {
15261526
if (expr->isComparisonOp() && (r.isUninitValue() || r.isImpossible())) {
15271527
if (rhs.isIntValue() && !expr->astOperand1()->values().empty()) {
15281528
std::vector<ValueFlow::Value> result =
1529-
infer(ValueFlow::makeIntegralInferModel(), expr->str(), expr->astOperand1()->values(), {std::move(rhs)});
1529+
infer(makeIntegralInferModel(), expr->str(), expr->astOperand1()->values(), {std::move(rhs)});
15301530
if (!result.empty() && result.front().isKnown())
15311531
return std::move(result.front());
15321532
}
15331533
if (lhs.isIntValue() && !expr->astOperand2()->values().empty()) {
15341534
std::vector<ValueFlow::Value> result =
1535-
infer(ValueFlow::makeIntegralInferModel(), expr->str(), {std::move(lhs)}, expr->astOperand2()->values());
1535+
infer(makeIntegralInferModel(), expr->str(), {std::move(lhs)}, expr->astOperand2()->values());
15361536
if (!result.empty() && result.front().isKnown())
15371537
return std::move(result.front());
15381538
}

0 commit comments

Comments
 (0)