Skip to content

Commit 3843c54

Browse files
committed
ValueFlow: extracted valueflowForward*() helpers into a separate file
1 parent 07a5d26 commit 3843c54

File tree

7 files changed

+190
-70
lines changed

7 files changed

+190
-70
lines changed

Diff for: Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
262262
$(libcppdir)/vf_conditionexpressions.o \
263263
$(libcppdir)/vf_debug.o \
264264
$(libcppdir)/vf_enumvalue.o \
265+
$(libcppdir)/vf_forward.o \
265266
$(libcppdir)/vf_functionreturn.o \
266267
$(libcppdir)/vf_globalconstvar.o \
267268
$(libcppdir)/vf_globalstaticvar.o \
@@ -481,7 +482,7 @@ validateRules:
481482

482483
###### Build
483484

484-
$(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
485+
$(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
485486
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/valueflow.cpp
486487

487488
$(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
@@ -697,6 +698,9 @@ $(libcppdir)/vf_debug.o: lib/vf_debug.cpp lib/addoninfo.h lib/color.h lib/config
697698
$(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
698699
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_enumvalue.cpp
699700

701+
$(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
702+
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_forward.cpp
703+
700704
$(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
701705
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_functionreturn.cpp
702706

Diff for: lib/cppcheck.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<ClCompile Include="vf_conditionexpressions.cpp" />
9898
<ClCompile Include="vf_debug.cpp" />
9999
<ClCompile Include="vf_enumvalue.cpp" />
100+
<ClCompile Include="vf_forward.cpp" />
100101
<ClCompile Include="vf_functionreturn.cpp" />
101102
<ClCompile Include="vf_globalconstvar.cpp" />
102103
<ClCompile Include="vf_globalstaticvar.cpp" />
@@ -208,6 +209,7 @@
208209
<ClInclude Include="vf_conditionexpressions.h" />
209210
<ClInclude Include="vf_debug.h" />
210211
<ClInclude Include="vf_enumvalue.h" />
212+
<ClInclude Include="vf_forward.h" />
211213
<ClInclude Include="vf_functionreturn.h" />
212214
<ClInclude Include="vf_globalconstvar.h" />
213215
<ClInclude Include="vf_globalstaticvar.h" />

Diff for: lib/lib.pri

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ HEADERS += $${PWD}/addoninfo.h \
9090
$${PWD}/vf_conditionexpressions.h \
9191
$${PWD}/vf_debug.h \
9292
$${PWD}/vf_enumvalue.h \
93+
$${PWD}/vf_forward.h \
9394
$${PWD}/vf_functionreturn.h \
9495
$${PWD}/vf_globalconstvar.h \
9596
$${PWD}/vf_globalstaticvar.h \
@@ -184,6 +185,7 @@ SOURCES += $${PWD}/valueflow.cpp \
184185
$${PWD}/vf_conditionexpressions.cpp \
185186
$${PWD}/vf_debug.cpp \
186187
$${PWD}/vf_enumvalue.cpp \
188+
$${PWD}/vf_forward.cpp \
187189
$${PWD}/vf_functionreturn.cpp \
188190
$${PWD}/vf_globalconstvar.cpp \
189191
$${PWD}/vf_globalstaticvar.cpp \

Diff for: lib/valueflow.cpp

+1-68
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
#include "vf_analyzers.h"
106106
#include "vf_bailout.h"
107107
#include "vf_common.h"
108+
#include "vf_forward.h"
108109
#include "vf_reverse.h"
109110
#include "vf_settokenvalue.h"
110111

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

486-
static Analyzer::Result valueFlowForward(Token* startToken,
487-
const Token* endToken,
488-
const Token* exprTok,
489-
ValueFlow::Value value,
490-
const TokenList& tokenlist,
491-
ErrorLogger& errorLogger,
492-
const Settings& settings,
493-
SourceLocation loc = SourceLocation::current())
494-
{
495-
if (settings.debugnormal)
496-
setSourceLocation(value, loc, startToken);
497-
return valueFlowGenericForward(startToken,
498-
endToken,
499-
makeAnalyzer(exprTok, std::move(value), settings),
500-
tokenlist,
501-
errorLogger,
502-
settings);
503-
}
504-
505-
static Analyzer::Result valueFlowForward(Token* startToken,
506-
const Token* endToken,
507-
const Token* exprTok,
508-
std::list<ValueFlow::Value> values,
509-
const TokenList& tokenlist,
510-
ErrorLogger& errorLogger,
511-
const Settings& settings,
512-
SourceLocation loc = SourceLocation::current())
513-
{
514-
Analyzer::Result result{};
515-
for (ValueFlow::Value& v : values) {
516-
result.update(valueFlowForward(startToken, endToken, exprTok, std::move(v), tokenlist, errorLogger, settings, loc));
517-
}
518-
return result;
519-
}
520487

521-
static Analyzer::Result valueFlowForward(Token* startToken,
522-
const Token* exprTok,
523-
ValueFlow::Value v,
524-
const TokenList& tokenlist,
525-
ErrorLogger& errorLogger,
526-
const Settings& settings,
527-
SourceLocation loc = SourceLocation::current())
528-
{
529-
const Token* endToken = nullptr;
530-
const Function* f = Scope::nestedInFunction(startToken->scope());
531-
if (f && f->functionScope)
532-
endToken = f->functionScope->bodyEnd;
533-
if (!endToken && exprTok && exprTok->variable() && !exprTok->variable()->isLocal())
534-
endToken = startToken->scope()->bodyEnd;
535-
return valueFlowForward(startToken, endToken, exprTok, std::move(v), tokenlist, errorLogger, settings, loc);
536-
}
537-
538-
static Analyzer::Result valueFlowForwardRecursive(Token* top,
539-
const Token* exprTok,
540-
std::list<ValueFlow::Value> values,
541-
const TokenList& tokenlist,
542-
ErrorLogger& errorLogger,
543-
const Settings& settings,
544-
SourceLocation loc = SourceLocation::current())
545-
{
546-
Analyzer::Result result{};
547-
for (ValueFlow::Value& v : values) {
548-
if (settings.debugnormal)
549-
setSourceLocation(v, loc, top);
550-
result.update(
551-
valueFlowGenericForward(top, makeAnalyzer(exprTok, std::move(v), settings), tokenlist, errorLogger, settings));
552-
}
553-
return result;
554-
}
555488

556489
static bool isConditionKnown(const Token* tok, bool then)
557490
{

Diff for: lib/vf_forward.cpp

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2024 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include "vf_forward.h"
20+
21+
#include "forwardanalyzer.h"
22+
#include "settings.h"
23+
#include "symboldatabase.h"
24+
#include "token.h"
25+
#include "vfvalue.h"
26+
27+
#include "vf_common.h"
28+
#include "vf_analyzers.h"
29+
30+
#include <utility>
31+
32+
namespace ValueFlow
33+
{
34+
Analyzer::Result valueFlowForward(Token* startToken,
35+
const Token* endToken,
36+
const Token* exprTok,
37+
ValueFlow::Value value,
38+
const TokenList& tokenlist,
39+
ErrorLogger& errorLogger,
40+
const Settings& settings,
41+
SourceLocation loc)
42+
{
43+
if (settings.debugnormal)
44+
setSourceLocation(value, loc, startToken);
45+
return valueFlowGenericForward(startToken,
46+
endToken,
47+
makeAnalyzer(exprTok, std::move(value), settings),
48+
tokenlist,
49+
errorLogger,
50+
settings);
51+
}
52+
53+
Analyzer::Result valueFlowForward(Token* startToken,
54+
const Token* endToken,
55+
const Token* exprTok,
56+
std::list<ValueFlow::Value> values,
57+
const TokenList& tokenlist,
58+
ErrorLogger& errorLogger,
59+
const Settings& settings,
60+
SourceLocation loc)
61+
{
62+
Analyzer::Result result{};
63+
for (ValueFlow::Value& v : values) {
64+
result.update(valueFlowForward(startToken, endToken, exprTok, std::move(v), tokenlist, errorLogger, settings, loc));
65+
}
66+
return result;
67+
}
68+
69+
Analyzer::Result valueFlowForward(Token* startToken,
70+
const Token* exprTok,
71+
ValueFlow::Value v,
72+
const TokenList& tokenlist,
73+
ErrorLogger& errorLogger,
74+
const Settings& settings,
75+
SourceLocation loc)
76+
{
77+
const Token* endToken = nullptr;
78+
const Function* f = Scope::nestedInFunction(startToken->scope());
79+
if (f && f->functionScope)
80+
endToken = f->functionScope->bodyEnd;
81+
if (!endToken && exprTok && exprTok->variable() && !exprTok->variable()->isLocal())
82+
endToken = startToken->scope()->bodyEnd;
83+
return valueFlowForward(startToken, endToken, exprTok, std::move(v), tokenlist, errorLogger, settings, loc);
84+
}
85+
86+
Analyzer::Result valueFlowForwardRecursive(Token* top,
87+
const Token* exprTok,
88+
std::list<ValueFlow::Value> values,
89+
const TokenList& tokenlist,
90+
ErrorLogger& errorLogger,
91+
const Settings& settings,
92+
SourceLocation loc)
93+
{
94+
Analyzer::Result result{};
95+
for (ValueFlow::Value& v : values) {
96+
if (settings.debugnormal)
97+
setSourceLocation(v, loc, top);
98+
result.update(
99+
valueFlowGenericForward(top, makeAnalyzer(exprTok, std::move(v), settings), tokenlist, errorLogger, settings));
100+
}
101+
return result;
102+
}
103+
}

Diff for: lib/vf_forward.h

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* -*- C++ -*-
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2024 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef vfForwardH
20+
#define vfForwardH
21+
22+
#include "analyzer.h"
23+
#include "sourcelocation.h"
24+
25+
#include <list>
26+
27+
class Token;
28+
class TokenList;
29+
class ErrorLogger;
30+
class Settings;
31+
namespace ValueFlow {
32+
class Value;
33+
}
34+
35+
namespace ValueFlow
36+
{
37+
Analyzer::Result valueFlowForward(Token* startToken,
38+
const Token* endToken,
39+
const Token* exprTok,
40+
ValueFlow::Value value,
41+
const TokenList& tokenlist,
42+
ErrorLogger& errorLogger,
43+
const Settings& settings,
44+
SourceLocation loc = SourceLocation::current());
45+
46+
Analyzer::Result valueFlowForward(Token* startToken,
47+
const Token* endToken,
48+
const Token* exprTok,
49+
std::list<ValueFlow::Value> values,
50+
const TokenList& tokenlist,
51+
ErrorLogger& errorLogger,
52+
const Settings& settings,
53+
SourceLocation loc = SourceLocation::current());
54+
55+
Analyzer::Result valueFlowForward(Token* startToken,
56+
const Token* exprTok,
57+
ValueFlow::Value v,
58+
const TokenList& tokenlist,
59+
ErrorLogger& errorLogger,
60+
const Settings& settings,
61+
SourceLocation loc = SourceLocation::current());
62+
63+
Analyzer::Result valueFlowForwardRecursive(Token* top,
64+
const Token* exprTok,
65+
std::list<ValueFlow::Value> values,
66+
const TokenList& tokenlist,
67+
ErrorLogger& errorLogger,
68+
const Settings& settings,
69+
SourceLocation loc = SourceLocation::current());
70+
}
71+
72+
#endif // vfForwardH

0 commit comments

Comments
 (0)