Skip to content

Commit 331fbc1

Browse files
committed
ValueFlow: extracted valueFlowNumber() into separate file
1 parent 9e8f6bf commit 331fbc1

File tree

7 files changed

+105
-34
lines changed

7 files changed

+105
-34
lines changed

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ LIBOBJ = $(libcppdir)/analyzerinfo.o \
248248
$(libcppdir)/utils.o \
249249
$(libcppdir)/valueflow.o \
250250
$(libcppdir)/vf/enumvalue.o \
251+
$(libcppdir)/vf/number.o \
251252
$(libcppdir)/vfvalue.o
252253

253254
EXTOBJ = externals/simplecpp/simplecpp.o \
@@ -540,7 +541,7 @@ $(libcppdir)/clangimport.o: lib/clangimport.cpp lib/clangimport.h lib/config.h l
540541
$(libcppdir)/color.o: lib/color.cpp lib/color.h lib/config.h
541542
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/color.cpp
542543

543-
$(libcppdir)/cppcheck.o: lib/cppcheck.cpp externals/picojson/picojson.h externals/simplecpp/simplecpp.h externals/tinyxml2/tinyxml2.h lib/analyzerinfo.h lib/check.h lib/checkunusedfunctions.h lib/clangimport.h lib/color.h lib/config.h lib/cppcheck.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/version.h lib/vfvalue.h
544+
$(libcppdir)/cppcheck.o: lib/cppcheck.cpp externals/picojson/picojson.h externals/simplecpp/simplecpp.h externals/tinyxml2/tinyxml2.h lib/analyzerinfo.h lib/check.h lib/checkunusedfunctions.h lib/clangimport.h lib/color.h lib/config.h lib/cppcheck.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/version.h lib/vfvalue.h
544545
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/cppcheck.cpp
545546

546547
$(libcppdir)/ctu.o: lib/ctu.cpp externals/tinyxml2/tinyxml2.h lib/astutils.h lib/check.h lib/color.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.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/vfvalue.h
@@ -621,12 +622,15 @@ $(libcppdir)/tokenlist.o: lib/tokenlist.cpp externals/simplecpp/simplecpp.h lib/
621622
$(libcppdir)/utils.o: lib/utils.cpp lib/config.h lib/utils.h
622623
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/utils.cpp
623624

624-
$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/analyzer.h lib/astutils.h lib/calculate.h lib/check.h lib/checkuninitvar.h lib/color.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/forwardanalyzer.h lib/importproject.h lib/infer.h lib/library.h lib/mathlib.h lib/path.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/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf/enumvalue.h lib/vfvalue.h
625+
$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/analyzer.h lib/astutils.h lib/calculate.h lib/check.h lib/checkuninitvar.h lib/color.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/forwardanalyzer.h lib/importproject.h lib/infer.h lib/library.h lib/mathlib.h lib/path.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/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf/enumvalue.h lib/vf/number.h lib/vfvalue.h
625626
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/valueflow.cpp
626627

627628
$(libcppdir)/vf/enumvalue.o: lib/vf/enumvalue.cpp lib/vf/enumvalue.h
628629
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf/enumvalue.cpp
629630

631+
$(libcppdir)/vf/number.o: lib/vf/number.cpp lib/vf/number.h
632+
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf/number.cpp
633+
630634
$(libcppdir)/vfvalue.o: lib/vfvalue.cpp lib/config.h lib/errortypes.h lib/mathlib.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vfvalue.h
631635
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vfvalue.cpp
632636

lib/cppcheck.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
<ClCompile Include="utils.cpp" />
105105
<ClCompile Include="valueflow.cpp" />
106106
<ClCompile Include="vf/enumvalue.cpp" />
107+
<ClCompile Include="vf/number.cpp" />
107108
<ClCompile Include="vfvalue.cpp" />
108109
</ItemGroup>
109110
<ItemGroup Label="HeaderFiles">
@@ -177,6 +178,7 @@
177178
<ClInclude Include="valueptr.h" />
178179
<ClInclude Include="version.h" />
179180
<ClInclude Include="vf/enumvalue.h" />
181+
<ClInclude Include="vf/number.h" />
180182
<ClInclude Include="vfvalue.h" />
181183
</ItemGroup>
182184
<ItemGroup>

lib/lib.pri

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ HEADERS += $${PWD}/analyzer.h \
7171
$${PWD}/valueptr.h \
7272
$${PWD}/version.h \
7373
$${PWD}/vf/enumvalue.h \
74+
$${PWD}/vf/number.h \
7475
$${PWD}/vfvalue.h
7576

7677
SOURCES += $${PWD}/analyzerinfo.cpp \
@@ -132,4 +133,5 @@ SOURCES += $${PWD}/analyzerinfo.cpp \
132133
$${PWD}/utils.cpp \
133134
$${PWD}/valueflow.cpp \
134135
$${PWD}/vf/enumvalue.cpp \
136+
$${PWD}/vf/number.cpp \
135137
$${PWD}/vfvalue.cpp

lib/valueflow.cpp

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
#include "vfvalue.h"
104104

105105
#include "vf/enumvalue.h"
106+
#include "vf/number.h"
106107

107108
#include <algorithm>
108109
#include <array>
@@ -576,10 +577,10 @@ static ValueFlow::Value truncateImplicitConversion(Token* parent, const ValueFlo
576577
}
577578

578579
/** set ValueFlow value and perform calculations if possible */
579-
static void setTokenValue(Token* tok,
580-
ValueFlow::Value value,
581-
const Settings* settings,
582-
SourceLocation loc = SourceLocation::current())
580+
void ValueFlow::setTokenValue(Token* tok,
581+
ValueFlow::Value value,
582+
const Settings* settings,
583+
SourceLocation loc)
583584
{
584585
// Skip setting values that are too big since its ambiguous
585586
if (!value.isImpossible() && value.isIntValue() && value.intvalue < 0 && astIsUnsigned(tok) &&
@@ -1090,7 +1091,7 @@ size_t ValueFlow::getSizeOf(const ValueType &vt, const Settings *settings)
10901091
static bool getMinMaxValues(const ValueType* vt, const cppcheck::Platform& platform, MathLib::bigint* minValue, MathLib::bigint* maxValue);
10911092

10921093
// Handle various constants..
1093-
static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, bool cpp)
1094+
Token * ValueFlow::valueFlowSetConstantValue(Token *tok, const Settings *settings, bool cpp)
10941095
{
10951096
if ((tok->isNumber() && MathLib::isInt(tok->str())) || (tok->tokType() == Token::eChar)) {
10961097
try {
@@ -1290,31 +1291,6 @@ static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, b
12901291
return tok->next();
12911292
}
12921293

1293-
static void valueFlowNumber(TokenList *tokenlist)
1294-
{
1295-
for (Token *tok = tokenlist->front(); tok;) {
1296-
tok = valueFlowSetConstantValue(tok, tokenlist->getSettings(), tokenlist->isCPP());
1297-
}
1298-
1299-
if (tokenlist->isCPP()) {
1300-
for (Token *tok = tokenlist->front(); tok; tok = tok->next()) {
1301-
if (tok->isName() && !tok->varId() && Token::Match(tok, "false|true")) {
1302-
ValueFlow::Value value(tok->str() == "true");
1303-
if (!tok->isTemplateArg())
1304-
value.setKnown();
1305-
setTokenValue(tok, value, tokenlist->getSettings());
1306-
} else if (Token::Match(tok, "[(,] NULL [,)]")) {
1307-
// NULL function parameters are not simplified in the
1308-
// normal tokenlist
1309-
ValueFlow::Value value(0);
1310-
if (!tok->isTemplateArg())
1311-
value.setKnown();
1312-
setTokenValue(tok->next(), value, tokenlist->getSettings());
1313-
}
1314-
}
1315-
}
1316-
}
1317-
13181294
static void valueFlowString(TokenList *tokenlist)
13191295
{
13201296
for (Token *tok = tokenlist->front(); tok; tok = tok->next()) {
@@ -8810,13 +8786,13 @@ void ValueFlow::setValues(TokenList *tokenlist, SymbolDatabase* symboldatabase,
88108786
tok->clearValueFlow();
88118787

88128788
ValueFlow::enumValue(symboldatabase, settings);
8813-
valueFlowNumber(tokenlist);
8789+
ValueFlow::number(tokenlist);
88148790
valueFlowString(tokenlist);
88158791
valueFlowArray(tokenlist);
88168792
valueFlowUnknownFunctionReturn(tokenlist, settings);
88178793
valueFlowGlobalConstVar(tokenlist, settings);
88188794
ValueFlow::enumValue(symboldatabase, settings);
8819-
valueFlowNumber(tokenlist);
8795+
ValueFlow::number(tokenlist);
88208796
valueFlowGlobalStaticVar(tokenlist, settings);
88218797
valueFlowPointerAlias(tokenlist);
88228798
valueFlowLifetime(tokenlist, symboldatabase, errorLogger, settings);

lib/valueflow.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "config.h"
2525
#include "mathlib.h"
26+
#include "sourcelocation.h"
2627
#include "vfvalue.h"
2728

2829
#include <cstdlib>
@@ -127,6 +128,13 @@ namespace ValueFlow {
127128
const Token* getEndOfExprScope(const Token* tok, const Scope* defaultScope = nullptr, bool smallest = true);
128129

129130
void combineValueProperties(const Value& value1, const Value& value2, Value* result);
131+
132+
Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, bool cpp);
133+
134+
void setTokenValue(Token* tok,
135+
ValueFlow::Value value,
136+
const Settings* settings,
137+
SourceLocation loc = SourceLocation::current());
130138
}
131139

132140
#endif // valueflowH

lib/vf/number.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2022 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 "number.h"
20+
21+
#include "token.h"
22+
#include "tokenlist.h"
23+
#include "valueflow.h"
24+
#include "vfvalue.h"
25+
26+
using namespace ValueFlow;
27+
28+
void ValueFlow::number(TokenList *tokenlist) {
29+
for (Token *tok = tokenlist->front(); tok;) {
30+
tok = valueFlowSetConstantValue(tok, tokenlist->getSettings(), tokenlist->isCPP());
31+
}
32+
33+
if (tokenlist->isCPP()) {
34+
for (Token *tok = tokenlist->front(); tok; tok = tok->next()) {
35+
if (tok->isName() && !tok->varId() && Token::Match(tok, "false|true")) {
36+
Value value(tok->str() == "true");
37+
if (!tok->isTemplateArg())
38+
value.setKnown();
39+
setTokenValue(tok, value, tokenlist->getSettings());
40+
} else if (Token::Match(tok, "[(,] NULL [,)]")) {
41+
// NULL function parameters are not simplified in the
42+
// normal tokenlist
43+
Value value(0);
44+
if (!tok->isTemplateArg())
45+
value.setKnown();
46+
setTokenValue(tok->next(), value, tokenlist->getSettings());
47+
}
48+
}
49+
}
50+
}

lib/vf/number.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2022 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 vfNumberH
20+
#define vfNumberH
21+
22+
class TokenList;
23+
24+
namespace ValueFlow
25+
{
26+
void number(TokenList *tokenlist);
27+
}
28+
29+
#endif // vfNumberH

0 commit comments

Comments
 (0)