Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueFlow: extracted valueFlowInferCondition() into separate file #6782

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
$(libcppdir)/vf_globalconstvar.o \
$(libcppdir)/vf_globalstaticvar.o \
$(libcppdir)/vf_impossiblevalues.o \
$(libcppdir)/vf_infercondition.o \
$(libcppdir)/vf_iteratorinfer.o \
$(libcppdir)/vf_iterators.o \
$(libcppdir)/vf_number.o \
Expand Down Expand Up @@ -484,7 +485,7 @@ validateRules:

###### Build

$(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/reverseanalyzer.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_debug.h lib/vf_enumvalue.h lib/vf_functionreturn.h lib/vf_globalconstvar.h lib/vf_globalstaticvar.h lib/vf_impossiblevalues.h lib/vf_iteratorinfer.h lib/vf_iterators.h lib/vf_number.h lib/vf_pointeralias.h lib/vf_rightshift.h lib/vf_sameexpressions.h lib/vf_settokenvalue.h lib/vf_string.h lib/vf_symbolicinfer.h lib/vf_unknownfunctionreturn.h lib/vfvalue.h
$(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/reverseanalyzer.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_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_rightshift.h lib/vf_sameexpressions.h lib/vf_settokenvalue.h lib/vf_string.h lib/vf_symbolicinfer.h lib/vf_unknownfunctionreturn.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/valueflow.cpp

$(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
Expand Down Expand Up @@ -709,6 +710,9 @@ $(libcppdir)/vf_globalstaticvar.o: lib/vf_globalstaticvar.cpp lib/astutils.h lib
$(libcppdir)/vf_impossiblevalues.o: lib/vf_impossiblevalues.cpp lib/astutils.h lib/calculate.h lib/config.h lib/errortypes.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/tokenlist.h lib/utils.h lib/vf_impossiblevalues.h lib/vf_settokenvalue.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_impossiblevalues.cpp

$(libcppdir)/vf_infercondition.o: lib/vf_infercondition.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/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf_infercondition.h lib/vf_settokenvalue.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_infercondition.cpp

$(libcppdir)/vf_iteratorinfer.o: lib/vf_iteratorinfer.cpp lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vf_common.h lib/vf_iteratorinfer.h lib/vf_settokenvalue.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_iteratorinfer.cpp

Expand Down
2 changes: 2 additions & 0 deletions lib/cppcheck.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<ClCompile Include="vf_globalconstvar.cpp" />
<ClCompile Include="vf_globalstaticvar.cpp" />
<ClCompile Include="vf_impossiblevalues.cpp" />
<ClCompile Include="vf_infercondition.cpp" />
<ClCompile Include="vf_iteratorinfer.cpp" />
<ClCompile Include="vf_iterators.cpp" />
<ClCompile Include="vf_number.cpp" />
Expand Down Expand Up @@ -205,6 +206,7 @@
<ClInclude Include="vf_globalconstvar.h" />
<ClInclude Include="vf_globalstaticvar.h" />
<ClInclude Include="vf_impossiblevalues.h" />
<ClInclude Include="vf_infercondition.h" />
<ClInclude Include="vf_iteratorinfer.h" />
<ClInclude Include="vf_iterators.h" />
<ClInclude Include="vf_number.h" />
Expand Down
2 changes: 2 additions & 0 deletions lib/lib.pri
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ HEADERS += $${PWD}/addoninfo.h \
$${PWD}/vf_globalconstvar.h \
$${PWD}/vf_globalstaticvar.h \
$${PWD}/vf_impossiblevalues.h \
$${PWD}/vf_infercondition.h \
$${PWD}/vf_iteratorinfer.h \
$${PWD}/vf_iterators.h \
$${PWD}/vf_number.h \
Expand Down Expand Up @@ -181,6 +182,7 @@ SOURCES += $${PWD}/valueflow.cpp \
$${PWD}/vf_globalconstvar.cpp \
$${PWD}/vf_globalstaticvar.cpp \
$${PWD}/vf_impossiblevalues.cpp \
$${PWD}/vf_infercondition.cpp \
$${PWD}/vf_iteratorinfer.cpp \
$${PWD}/vf_iterators.cpp \
$${PWD}/vf_number.cpp \
Expand Down
96 changes: 2 additions & 94 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
#include "vf_settokenvalue.h"

#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cstdint>
Expand Down Expand Up @@ -4114,103 +4113,12 @@ static ValueFlow::Value inferCondition(const std::string& op, const Token* varTo
return ValueFlow::Value{};
if (varTok->hasKnownIntValue())
return ValueFlow::Value{};
std::vector<ValueFlow::Value> r = infer(IntegralInferModel{}, op, varTok->values(), val);
std::vector<ValueFlow::Value> r = infer(ValueFlow::makeIntegralInferModel(), op, varTok->values(), val);
if (r.size() == 1 && r.front().isKnown())
return r.front();
return ValueFlow::Value{};
}

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%");
}

static bool isIntegralOrPointer(const Token* tok)
{
if (!tok)
return false;
if (astIsIntegral(tok, false))
return true;
if (astIsPointer(tok))
return true;
if (Token::Match(tok, "NULL|nullptr"))
return true;
if (tok->valueType())
return false;
// These operators only work on integers
if (isIntegralOnlyOperator(tok))
return true;
if (isIntegralOnlyOperator(tok->astParent()))
return true;
if (Token::Match(tok, "+|-|*|/") && tok->isBinaryOp())
return isIntegralOrPointer(tok->astOperand1()) && isIntegralOrPointer(tok->astOperand2());
return false;
}

static void valueFlowInferCondition(TokenList& tokenlist,
const Settings& settings)
{
for (Token* tok = tokenlist.front(); tok; tok = tok->next()) {
if (!tok->astParent())
continue;
if (tok->hasKnownIntValue())
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{}};
for (const ValuePtr<InferModel>& model : iteratorModels) {
std::vector<ValueFlow::Value> result =
infer(model, tok->str(), tok->astOperand1()->values(), tok->astOperand2()->values());
for (ValueFlow::Value value : result) {
value.valueType = ValueFlow::Value::ValueType::INT;
setTokenValue(tok, std::move(value), settings);
}
}
} else if (isIntegralOrPointer(tok->astOperand1()) && isIntegralOrPointer(tok->astOperand2())) {
std::vector<ValueFlow::Value> result =
infer(IntegralInferModel{}, tok->str(), tok->astOperand1()->values(), tok->astOperand2()->values());
for (ValueFlow::Value& value : result) {
setTokenValue(tok, std::move(value), settings);
}
}
} else if (Token::Match(tok->astParent(), "?|&&|!|%oror%") ||
Token::Match(tok->astParent()->previous(), "if|while (") ||
(astIsPointer(tok) && isUsedAsBool(tok, settings))) {
std::vector<ValueFlow::Value> result = infer(IntegralInferModel{}, "!=", tok->values(), 0);
if (result.size() != 1)
continue;
ValueFlow::Value value = result.front();
setTokenValue(tok, std::move(value), settings);
}
}
}

struct SymbolicConditionHandler : SimpleConditionHandler {

static bool isNegatedBool(const Token* tok)
Expand Down Expand Up @@ -6213,7 +6121,7 @@ void ValueFlow::setValues(TokenList& tokenlist,
VFA(valueFlowAfterAssign(tokenlist, symboldatabase, errorLogger, settings, skippedFunctions)),
VFA_CPP(valueFlowAfterSwap(tokenlist, symboldatabase, errorLogger, settings)),
VFA(valueFlowCondition(SimpleConditionHandler{}, tokenlist, symboldatabase, errorLogger, settings, skippedFunctions)),
VFA(valueFlowInferCondition(tokenlist, settings)),
VFA(analyzeInferCondition(tokenlist, settings)),
VFA(valueFlowSwitchVariable(tokenlist, symboldatabase, errorLogger, settings)),
VFA(valueFlowForLoop(tokenlist, symboldatabase, errorLogger, settings)),
VFA(valueFlowSubFunction(tokenlist, symboldatabase, errorLogger, settings)),
Expand Down
1 change: 1 addition & 0 deletions lib/valueflow.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//---------------------------------------------------------------------------

#include "config.h"
#include "errortypes.h"
#include "mathlib.h"
#include "vfvalue.h"

Expand Down
1 change: 1 addition & 0 deletions lib/vf_analyze.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "vf_globalconstvar.h" // IWYU pragma: export
#include "vf_globalstaticvar.h" // IWYU pragma: export
#include "vf_impossiblevalues.h" // IWYU pragma: export
#include "vf_infercondition.h" // IWYU pragma: export
#include "vf_iteratorinfer.h" // IWYU pragma: export
#include "vf_iterators.h" // IWYU pragma: export
#include "vf_number.h" // IWYU pragma: export
Expand Down
128 changes: 128 additions & 0 deletions lib/vf_infercondition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2024 Cppcheck team.
*
* 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/>.
*/

#include "vf_infercondition.h"

#include "astutils.h"
#include "infer.h"
#include "mathlib.h"
#include "token.h"
#include "tokenlist.h"
#include "valueflow.h"
#include "valueptr.h"
#include "vfvalue.h"

#include "vf_settokenvalue.h"

#include <array>
#include <list>
#include <utility>
#include <vector>

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

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

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

static bool isIntegralOnlyOperator(const Token* tok) {
return Token::Match(tok, "%|<<|>>|&|^|~|%or%");
}

static bool isIntegralOrPointer(const Token* tok)
{
if (!tok)
return false;
if (astIsIntegral(tok, false))
return true;
if (astIsPointer(tok))
return true;
if (Token::Match(tok, "NULL|nullptr"))
return true;
if (tok->valueType())
return false;
// These operators only work on integers
if (isIntegralOnlyOperator(tok))
return true;
if (isIntegralOnlyOperator(tok->astParent()))
return true;
if (Token::Match(tok, "+|-|*|/") && tok->isBinaryOp())
return isIntegralOrPointer(tok->astOperand1()) && isIntegralOrPointer(tok->astOperand2());
return false;
}

void analyzeInferCondition(TokenList& tokenlist, const Settings& settings)
{
for (Token* tok = tokenlist.front(); tok; tok = tok->next()) {
if (!tok->astParent())
continue;
if (tok->hasKnownIntValue())
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{}};
for (const ValuePtr<InferModel>& model : iteratorModels) {
std::vector<Value> result =
infer(model, tok->str(), tok->astOperand1()->values(), tok->astOperand2()->values());
for (Value value : result) {
value.valueType = Value::ValueType::INT;
setTokenValue(tok, std::move(value), settings);
}
}
} else if (isIntegralOrPointer(tok->astOperand1()) && isIntegralOrPointer(tok->astOperand2())) {
std::vector<Value> result =
infer(makeIntegralInferModel(), tok->str(), tok->astOperand1()->values(), tok->astOperand2()->values());
for (Value& value : result) {
setTokenValue(tok, std::move(value), settings);
}
}
} else if (Token::Match(tok->astParent(), "?|&&|!|%oror%") ||
Token::Match(tok->astParent()->previous(), "if|while (") ||
(astIsPointer(tok) && isUsedAsBool(tok, settings))) {
std::vector<Value> result = infer(makeIntegralInferModel(), "!=", tok->values(), 0);
if (result.size() != 1)
continue;
Value value = result.front();
setTokenValue(tok, std::move(value), settings);
}
}
}
}
30 changes: 30 additions & 0 deletions lib/vf_infercondition.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* -*- C++ -*-
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2024 Cppcheck team.
*
* 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/>.
*/

#ifndef vfInferConditionH
#define vfInferConditionH

class TokenList;
class Settings;

namespace ValueFlow
{
void analyzeInferCondition(TokenList& tokenlist, const Settings& settings);
}

#endif // vfInferConditionH
Loading
Loading