Skip to content

Commit a0e7206

Browse files
committed
ValueFlow: extracted valueFlowBitAnd() into separate file
1 parent 6f0b76a commit a0e7206

File tree

8 files changed

+108
-34
lines changed

8 files changed

+108
-34
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
253253
$(libcppdir)/tokenlist.o \
254254
$(libcppdir)/utils.o \
255255
$(libcppdir)/vf_array.o \
256+
$(libcppdir)/vf_bitand.o \
256257
$(libcppdir)/vf_common.o \
257258
$(libcppdir)/vf_enumvalue.o \
258259
$(libcppdir)/vf_globalconstvar.o \
@@ -470,7 +471,7 @@ validateRules:
470471

471472
###### Build
472473

473-
$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/calculate.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/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/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf_analyze.h lib/vf_array.h lib/vf_common.h lib/vf_enumvalue.h lib/vf_globalconstvar.h lib/vf_globalstaticvar.h lib/vf_number.h lib/vf_pointeralias.h lib/vf_settokenvalue.h lib/vf_string.h lib/vf_unknownfunctionreturn.h lib/vfvalue.h
474+
$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/calculate.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/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/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf_analyze.h lib/vf_array.h lib/vf_bitand.h lib/vf_common.h lib/vf_enumvalue.h lib/vf_globalconstvar.h lib/vf_globalstaticvar.h lib/vf_number.h lib/vf_pointeralias.h lib/vf_settokenvalue.h lib/vf_string.h lib/vf_unknownfunctionreturn.h lib/vfvalue.h
474475
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/valueflow.cpp
475476

476477
$(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
@@ -659,6 +660,9 @@ $(libcppdir)/utils.o: lib/utils.cpp lib/config.h lib/utils.h
659660
$(libcppdir)/vf_array.o: lib/vf_array.cpp lib/astutils.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_array.h lib/vf_settokenvalue.h lib/vfvalue.h
660661
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_array.cpp
661662

663+
$(libcppdir)/vf_bitand.o: lib/vf_bitand.cpp lib/config.h lib/errortypes.h lib/mathlib.h lib/sourcelocation.h lib/standards.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vf_bitand.h lib/vf_settokenvalue.h lib/vfvalue.h
664+
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_bitand.cpp
665+
662666
$(libcppdir)/vf_common.o: lib/vf_common.cpp lib/addoninfo.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.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/valueflow.h lib/vf_common.h lib/vf_settokenvalue.h lib/vfvalue.h
663667
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_common.cpp
664668

lib/cppcheck.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<ClCompile Include="tokenlist.cpp" />
8989
<ClCompile Include="utils.cpp" />
9090
<ClCompile Include="vf_array.cpp" />
91+
<ClCompile Include="vf_bitand.cpp" />
9192
<ClCompile Include="vf_common.cpp" />
9293
<ClCompile Include="vf_enumvalue.cpp" />
9394
<ClCompile Include="vf_globalconstvar.cpp" />
@@ -178,6 +179,7 @@
178179
<ClInclude Include="version.h" />
179180
<ClInclude Include="vf_analyze.h" />
180181
<ClInclude Include="vf_array.h" />
182+
<ClInclude Include="vf_bitand.h" />
181183
<ClInclude Include="vf_common.h" />
182184
<ClInclude Include="vf_enumvalue.h" />
183185
<ClInclude Include="vf_globalconstvar.h" />

lib/lib.pri

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ HEADERS += $${PWD}/addoninfo.h \
7979
$${PWD}/version.h \
8080
$${PWD}/vf_analyze.h \
8181
$${PWD}/vf_array.h \
82+
$${PWD}/vf_bitand.h \
8283
$${PWD}/vf_common.h \
8384
$${PWD}/vf_enumvalue.h \
8485
$${PWD}/vf_globalconstvar.h \
@@ -154,6 +155,7 @@ SOURCES += $${PWD}/valueflow.cpp \
154155
$${PWD}/tokenlist.cpp \
155156
$${PWD}/utils.cpp \
156157
$${PWD}/vf_array.cpp \
158+
$${PWD}/vf_bitand.cpp \
157159
$${PWD}/vf_common.cpp \
158160
$${PWD}/vf_enumvalue.cpp \
159161
$${PWD}/vf_globalconstvar.cpp \

lib/valueflow.cpp

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -678,37 +678,6 @@ static void valueFlowArrayElement(TokenList& tokenlist, const Settings& settings
678678
}
679679
}
680680

681-
static void valueFlowBitAnd(TokenList &tokenlist, const Settings& settings)
682-
{
683-
for (Token *tok = tokenlist.front(); tok; tok = tok->next()) {
684-
if (tok->str() != "&")
685-
continue;
686-
687-
if (tok->hasKnownValue())
688-
continue;
689-
690-
if (!tok->astOperand1() || !tok->astOperand2())
691-
continue;
692-
693-
MathLib::bigint number;
694-
if (MathLib::isInt(tok->astOperand1()->str()))
695-
number = MathLib::toBigNumber(tok->astOperand1()->str());
696-
else if (MathLib::isInt(tok->astOperand2()->str()))
697-
number = MathLib::toBigNumber(tok->astOperand2()->str());
698-
else
699-
continue;
700-
701-
int bit = 0;
702-
while (bit <= (MathLib::bigint_bits - 2) && ((((MathLib::bigint)1) << bit) < number))
703-
++bit;
704-
705-
if ((((MathLib::bigint)1) << bit) == number) {
706-
setTokenValue(tok, ValueFlow::Value(0), settings);
707-
setTokenValue(tok, ValueFlow::Value(number), settings);
708-
}
709-
}
710-
}
711-
712681
static void valueFlowSameExpressions(TokenList &tokenlist, const Settings& settings)
713682
{
714683
for (Token *tok = tokenlist.front(); tok; tok = tok->next()) {
@@ -8416,7 +8385,7 @@ void ValueFlow::setValues(TokenList& tokenlist,
84168385
VFA(analyzePointerAlias(tokenlist, settings)),
84178386
VFA(valueFlowLifetime(tokenlist, errorLogger, settings)),
84188387
VFA(valueFlowSymbolic(tokenlist, symboldatabase, errorLogger, settings)),
8419-
VFA(valueFlowBitAnd(tokenlist, settings)),
8388+
VFA(analyzeBitAnd(tokenlist, settings)),
84208389
VFA(valueFlowSameExpressions(tokenlist, settings)),
84218390
VFA(valueFlowConditionExpressions(tokenlist, symboldatabase, errorLogger, settings)),
84228391
});

lib/vf_analyze.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define vfAnalyzeH
2121

2222
#include "vf_array.h" // IWYU pragma: export
23+
#include "vf_bitand.h" // IWYU pragma: export
2324
#include "vf_enumvalue.h" // IWYU pragma: export
2425
#include "vf_globalconstvar.h" // IWYU pragma: export
2526
#include "vf_globalstaticvar.h" // IWYU pragma: export

lib/vf_bitand.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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_bitand.h"
20+
21+
#include "mathlib.h"
22+
#include "token.h"
23+
#include "tokenlist.h"
24+
#include "vfvalue.h"
25+
26+
#include "vf_settokenvalue.h"
27+
28+
#include <string>
29+
30+
namespace ValueFlow
31+
{
32+
void analyzeBitAnd(TokenList &tokenlist, const Settings& settings)
33+
{
34+
for (Token *tok = tokenlist.front(); tok; tok = tok->next()) {
35+
if (tok->str() != "&")
36+
continue;
37+
38+
if (tok->hasKnownValue())
39+
continue;
40+
41+
if (!tok->astOperand1() || !tok->astOperand2())
42+
continue;
43+
44+
MathLib::bigint number;
45+
if (MathLib::isInt(tok->astOperand1()->str()))
46+
number = MathLib::toBigNumber(tok->astOperand1()->str());
47+
else if (MathLib::isInt(tok->astOperand2()->str()))
48+
number = MathLib::toBigNumber(tok->astOperand2()->str());
49+
else
50+
continue;
51+
52+
int bit = 0;
53+
while (bit <= (MathLib::bigint_bits - 2) && ((((MathLib::bigint)1) << bit) < number))
54+
++bit;
55+
56+
if ((((MathLib::bigint)1) << bit) == number) {
57+
setTokenValue(tok, Value(0), settings);
58+
setTokenValue(tok, Value(number), settings);
59+
}
60+
}
61+
}
62+
}

lib/vf_bitand.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
#ifndef vfBitAndH
20+
#define vfBitAndH
21+
22+
class TokenList;
23+
class Settings;
24+
25+
namespace ValueFlow
26+
{
27+
void analyzeBitAnd(TokenList &tokenlist, const Settings& settings);
28+
}
29+
30+
#endif // vfBitAndH

oss-fuzz/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
9696
$(libcppdir)/tokenlist.o \
9797
$(libcppdir)/utils.o \
9898
$(libcppdir)/vf_array.o \
99+
$(libcppdir)/vf_bitand.o \
99100
$(libcppdir)/vf_common.o \
100101
$(libcppdir)/vf_enumvalue.o \
101102
$(libcppdir)/vf_globalconstvar.o \
@@ -149,7 +150,7 @@ simplecpp.o: ../externals/simplecpp/simplecpp.cpp ../externals/simplecpp/simplec
149150
tinyxml2.o: ../externals/tinyxml2/tinyxml2.cpp ../externals/tinyxml2/tinyxml2.h
150151
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -w -c -o $@ ../externals/tinyxml2/tinyxml2.cpp
151152

152-
$(libcppdir)/valueflow.o: ../lib/valueflow.cpp ../lib/addoninfo.h ../lib/analyzer.h ../lib/astutils.h ../lib/calculate.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/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/tokenize.h ../lib/tokenlist.h ../lib/utils.h ../lib/valueflow.h ../lib/valueptr.h ../lib/vf_analyze.h ../lib/vf_array.h ../lib/vf_common.h ../lib/vf_enumvalue.h ../lib/vf_globalconstvar.h ../lib/vf_globalstaticvar.h ../lib/vf_number.h ../lib/vf_pointeralias.h ../lib/vf_settokenvalue.h ../lib/vf_string.h ../lib/vf_unknownfunctionreturn.h ../lib/vfvalue.h
153+
$(libcppdir)/valueflow.o: ../lib/valueflow.cpp ../lib/addoninfo.h ../lib/analyzer.h ../lib/astutils.h ../lib/calculate.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/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/tokenize.h ../lib/tokenlist.h ../lib/utils.h ../lib/valueflow.h ../lib/valueptr.h ../lib/vf_analyze.h ../lib/vf_array.h ../lib/vf_bitand.h ../lib/vf_common.h ../lib/vf_enumvalue.h ../lib/vf_globalconstvar.h ../lib/vf_globalstaticvar.h ../lib/vf_number.h ../lib/vf_pointeralias.h ../lib/vf_settokenvalue.h ../lib/vf_string.h ../lib/vf_unknownfunctionreturn.h ../lib/vfvalue.h
153154
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/valueflow.cpp
154155

155156
$(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
@@ -338,6 +339,9 @@ $(libcppdir)/utils.o: ../lib/utils.cpp ../lib/config.h ../lib/utils.h
338339
$(libcppdir)/vf_array.o: ../lib/vf_array.cpp ../lib/astutils.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_array.h ../lib/vf_settokenvalue.h ../lib/vfvalue.h
339340
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_array.cpp
340341

342+
$(libcppdir)/vf_bitand.o: ../lib/vf_bitand.cpp ../lib/config.h ../lib/errortypes.h ../lib/mathlib.h ../lib/sourcelocation.h ../lib/standards.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/vf_bitand.h ../lib/vf_settokenvalue.h ../lib/vfvalue.h
343+
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_bitand.cpp
344+
341345
$(libcppdir)/vf_common.o: ../lib/vf_common.cpp ../lib/addoninfo.h ../lib/config.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/path.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/valueflow.h ../lib/vf_common.h ../lib/vf_settokenvalue.h ../lib/vfvalue.h
342346
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_common.cpp
343347

0 commit comments

Comments
 (0)