Skip to content

Commit

Permalink
ValueFlow: extracted valueflowForward*() helpers into a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Oct 23, 2024
1 parent 07a5d26 commit 3843c54
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 70 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
$(libcppdir)/vf_conditionexpressions.o \
$(libcppdir)/vf_debug.o \
$(libcppdir)/vf_enumvalue.o \
$(libcppdir)/vf_forward.o \
$(libcppdir)/vf_functionreturn.o \
$(libcppdir)/vf_globalconstvar.o \
$(libcppdir)/vf_globalstaticvar.o \
Expand Down Expand Up @@ -481,7 +482,7 @@ validateRules:

###### Build

$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.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/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_subfunction.h lib/vf_switchvariable.h lib/vf_symbolicinfer.h lib/vf_symbolicoperators.h lib/vf_unknownfunctionreturn.h lib/vfvalue.h
$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.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/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_forward.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_subfunction.h lib/vf_switchvariable.h lib/vf_symbolicinfer.h lib/vf_symbolicoperators.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 @@ -697,6 +698,9 @@ $(libcppdir)/vf_debug.o: lib/vf_debug.cpp lib/addoninfo.h lib/color.h lib/config
$(libcppdir)/vf_enumvalue.o: lib/vf_enumvalue.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/utils.h lib/valueflow.h lib/vf_enumvalue.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_enumvalue.cpp

$(libcppdir)/vf_forward.o: lib/vf_forward.cpp lib/addoninfo.h lib/analyzer.h lib/config.h lib/errortypes.h lib/forwardanalyzer.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/utils.h lib/valueptr.h lib/vf_analyzers.h lib/vf_common.h lib/vf_forward.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_forward.cpp

$(libcppdir)/vf_functionreturn.o: lib/vf_functionreturn.cpp lib/addoninfo.h lib/astutils.h lib/config.h lib/errortypes.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/token.h lib/tokenlist.h lib/utils.h lib/vf_bailout.h lib/vf_functionreturn.h lib/vf_settokenvalue.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_functionreturn.cpp

Expand Down
2 changes: 2 additions & 0 deletions lib/cppcheck.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<ClCompile Include="vf_conditionexpressions.cpp" />
<ClCompile Include="vf_debug.cpp" />
<ClCompile Include="vf_enumvalue.cpp" />
<ClCompile Include="vf_forward.cpp" />
<ClCompile Include="vf_functionreturn.cpp" />
<ClCompile Include="vf_globalconstvar.cpp" />
<ClCompile Include="vf_globalstaticvar.cpp" />
Expand Down Expand Up @@ -208,6 +209,7 @@
<ClInclude Include="vf_conditionexpressions.h" />
<ClInclude Include="vf_debug.h" />
<ClInclude Include="vf_enumvalue.h" />
<ClInclude Include="vf_forward.h" />
<ClInclude Include="vf_functionreturn.h" />
<ClInclude Include="vf_globalconstvar.h" />
<ClInclude Include="vf_globalstaticvar.h" />
Expand Down
2 changes: 2 additions & 0 deletions lib/lib.pri
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ HEADERS += $${PWD}/addoninfo.h \
$${PWD}/vf_conditionexpressions.h \
$${PWD}/vf_debug.h \
$${PWD}/vf_enumvalue.h \
$${PWD}/vf_forward.h \
$${PWD}/vf_functionreturn.h \
$${PWD}/vf_globalconstvar.h \
$${PWD}/vf_globalstaticvar.h \
Expand Down Expand Up @@ -184,6 +185,7 @@ SOURCES += $${PWD}/valueflow.cpp \
$${PWD}/vf_conditionexpressions.cpp \
$${PWD}/vf_debug.cpp \
$${PWD}/vf_enumvalue.cpp \
$${PWD}/vf_forward.cpp \
$${PWD}/vf_functionreturn.cpp \
$${PWD}/vf_globalconstvar.cpp \
$${PWD}/vf_globalstaticvar.cpp \
Expand Down
69 changes: 1 addition & 68 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
#include "vf_analyzers.h"
#include "vf_bailout.h"
#include "vf_common.h"
#include "vf_forward.h"
#include "vf_reverse.h"
#include "vf_settokenvalue.h"

Expand Down Expand Up @@ -483,75 +484,7 @@ size_t ValueFlow::getSizeOf(const ValueType &vt, const Settings &settings, int m
return 0;
}

static Analyzer::Result valueFlowForward(Token* startToken,
const Token* endToken,
const Token* exprTok,
ValueFlow::Value value,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current())
{
if (settings.debugnormal)
setSourceLocation(value, loc, startToken);
return valueFlowGenericForward(startToken,
endToken,
makeAnalyzer(exprTok, std::move(value), settings),
tokenlist,
errorLogger,
settings);
}

static Analyzer::Result valueFlowForward(Token* startToken,
const Token* endToken,
const Token* exprTok,
std::list<ValueFlow::Value> values,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current())
{
Analyzer::Result result{};
for (ValueFlow::Value& v : values) {
result.update(valueFlowForward(startToken, endToken, exprTok, std::move(v), tokenlist, errorLogger, settings, loc));
}
return result;
}

static Analyzer::Result valueFlowForward(Token* startToken,
const Token* exprTok,
ValueFlow::Value v,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current())
{
const Token* endToken = nullptr;
const Function* f = Scope::nestedInFunction(startToken->scope());
if (f && f->functionScope)
endToken = f->functionScope->bodyEnd;
if (!endToken && exprTok && exprTok->variable() && !exprTok->variable()->isLocal())
endToken = startToken->scope()->bodyEnd;
return valueFlowForward(startToken, endToken, exprTok, std::move(v), tokenlist, errorLogger, settings, loc);
}

static Analyzer::Result valueFlowForwardRecursive(Token* top,
const Token* exprTok,
std::list<ValueFlow::Value> values,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current())
{
Analyzer::Result result{};
for (ValueFlow::Value& v : values) {
if (settings.debugnormal)
setSourceLocation(v, loc, top);
result.update(
valueFlowGenericForward(top, makeAnalyzer(exprTok, std::move(v), settings), tokenlist, errorLogger, settings));
}
return result;
}

static bool isConditionKnown(const Token* tok, bool then)
{
Expand Down
103 changes: 103 additions & 0 deletions lib/vf_forward.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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_forward.h"

#include "forwardanalyzer.h"
#include "settings.h"
#include "symboldatabase.h"
#include "token.h"
#include "vfvalue.h"

#include "vf_common.h"
#include "vf_analyzers.h"

#include <utility>

namespace ValueFlow
{
Analyzer::Result valueFlowForward(Token* startToken,
const Token* endToken,
const Token* exprTok,
ValueFlow::Value value,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc)
{
if (settings.debugnormal)
setSourceLocation(value, loc, startToken);
return valueFlowGenericForward(startToken,
endToken,
makeAnalyzer(exprTok, std::move(value), settings),
tokenlist,
errorLogger,
settings);
}

Analyzer::Result valueFlowForward(Token* startToken,
const Token* endToken,
const Token* exprTok,
std::list<ValueFlow::Value> values,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc)
{
Analyzer::Result result{};
for (ValueFlow::Value& v : values) {
result.update(valueFlowForward(startToken, endToken, exprTok, std::move(v), tokenlist, errorLogger, settings, loc));
}
return result;
}

Analyzer::Result valueFlowForward(Token* startToken,
const Token* exprTok,
ValueFlow::Value v,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc)
{
const Token* endToken = nullptr;
const Function* f = Scope::nestedInFunction(startToken->scope());
if (f && f->functionScope)
endToken = f->functionScope->bodyEnd;
if (!endToken && exprTok && exprTok->variable() && !exprTok->variable()->isLocal())
endToken = startToken->scope()->bodyEnd;
return valueFlowForward(startToken, endToken, exprTok, std::move(v), tokenlist, errorLogger, settings, loc);
}

Analyzer::Result valueFlowForwardRecursive(Token* top,
const Token* exprTok,
std::list<ValueFlow::Value> values,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc)
{
Analyzer::Result result{};
for (ValueFlow::Value& v : values) {
if (settings.debugnormal)
setSourceLocation(v, loc, top);
result.update(
valueFlowGenericForward(top, makeAnalyzer(exprTok, std::move(v), settings), tokenlist, errorLogger, settings));
}
return result;
}
}
72 changes: 72 additions & 0 deletions lib/vf_forward.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* -*- 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 vfForwardH
#define vfForwardH

#include "analyzer.h"
#include "sourcelocation.h"

#include <list>

class Token;
class TokenList;
class ErrorLogger;
class Settings;
namespace ValueFlow {
class Value;
}

namespace ValueFlow
{
Analyzer::Result valueFlowForward(Token* startToken,
const Token* endToken,
const Token* exprTok,
ValueFlow::Value value,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current());

Analyzer::Result valueFlowForward(Token* startToken,
const Token* endToken,
const Token* exprTok,
std::list<ValueFlow::Value> values,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current());

Analyzer::Result valueFlowForward(Token* startToken,
const Token* exprTok,
ValueFlow::Value v,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current());

Analyzer::Result valueFlowForwardRecursive(Token* top,
const Token* exprTok,
std::list<ValueFlow::Value> values,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current());
}

#endif // vfForwardH
Loading

0 comments on commit 3843c54

Please sign in to comment.