Skip to content

Commit cf77855

Browse files
committed
ValueFlow: extracted valueFlowSameExpressions() into separate file
1 parent a0e7206 commit cf77855

8 files changed

+113
-39
lines changed

Diff for: Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
260260
$(libcppdir)/vf_globalstaticvar.o \
261261
$(libcppdir)/vf_number.o \
262262
$(libcppdir)/vf_pointeralias.o \
263+
$(libcppdir)/vf_sameexpressions.o \
263264
$(libcppdir)/vf_settokenvalue.o \
264265
$(libcppdir)/vf_string.o \
265266
$(libcppdir)/vf_unknownfunctionreturn.o \
@@ -471,7 +472,7 @@ validateRules:
471472

472473
###### Build
473474

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
475+
$(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_sameexpressions.h lib/vf_settokenvalue.h lib/vf_string.h lib/vf_unknownfunctionreturn.h lib/vfvalue.h
475476
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/valueflow.cpp
476477

477478
$(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
@@ -681,6 +682,9 @@ $(libcppdir)/vf_number.o: lib/vf_number.cpp lib/config.h lib/errortypes.h lib/li
681682
$(libcppdir)/vf_pointeralias.o: lib/vf_pointeralias.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_pointeralias.h lib/vf_settokenvalue.h lib/vfvalue.h
682683
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_pointeralias.cpp
683684

685+
$(libcppdir)/vf_sameexpressions.o: lib/vf_sameexpressions.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_sameexpressions.h lib/vf_settokenvalue.h lib/vfvalue.h
686+
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_sameexpressions.cpp
687+
684688
$(libcppdir)/vf_settokenvalue.o: lib/vf_settokenvalue.cpp lib/addoninfo.h lib/astutils.h lib/calculate.h lib/config.h lib/errortypes.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/token.h lib/utils.h lib/valueflow.h lib/vf_common.h lib/vf_settokenvalue.h lib/vfvalue.h
685689
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_settokenvalue.cpp
686690

Diff for: lib/cppcheck.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<ClCompile Include="vf_globalstaticvar.cpp" />
9696
<ClCompile Include="vf_number.cpp" />
9797
<ClCompile Include="vf_pointeralias.cpp" />
98+
<ClCompile Include="vf_sameexpressions.cpp" />
9899
<ClCompile Include="vf_settokenvalue.cpp" />
99100
<ClCompile Include="vf_string.cpp" />
100101
<ClCompile Include="vf_unknownfunctionreturn.cpp" />
@@ -186,6 +187,7 @@
186187
<ClInclude Include="vf_globalstaticvar.h" />
187188
<ClInclude Include="vf_number.h" />
188189
<ClInclude Include="vf_pointeralias.h" />
190+
<ClInclude Include="vf_sameexpressions.h" />
189191
<ClInclude Include="vf_settokenvalue.h" />
190192
<ClInclude Include="vf_string.h" />
191193
<ClInclude Include="vf_unknownfunctionreturn.h" />

Diff for: lib/lib.pri

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ HEADERS += $${PWD}/addoninfo.h \
8686
$${PWD}/vf_globalstaticvar.h \
8787
$${PWD}/vf_number.h \
8888
$${PWD}/vf_pointeralias.h \
89+
$${PWD}/vf_sameexpressions.h \
8990
$${PWD}/vf_settokenvalue.h \
9091
$${PWD}/vf_string.h \
9192
$${PWD}/vf_unknownfunctionreturn.h \
@@ -162,6 +163,7 @@ SOURCES += $${PWD}/valueflow.cpp \
162163
$${PWD}/vf_globalstaticvar.cpp \
163164
$${PWD}/vf_number.cpp \
164165
$${PWD}/vf_pointeralias.cpp \
166+
$${PWD}/vf_sameexpressions.cpp \
165167
$${PWD}/vf_settokenvalue.cpp \
166168
$${PWD}/vf_string.cpp \
167169
$${PWD}/vf_unknownfunctionreturn.cpp \

Diff for: lib/valueflow.cpp

+1-37
Original file line numberDiff line numberDiff line change
@@ -678,42 +678,6 @@ static void valueFlowArrayElement(TokenList& tokenlist, const Settings& settings
678678
}
679679
}
680680

681-
static void valueFlowSameExpressions(TokenList &tokenlist, const Settings& settings)
682-
{
683-
for (Token *tok = tokenlist.front(); tok; tok = tok->next()) {
684-
if (tok->hasKnownIntValue())
685-
continue;
686-
687-
if (!tok->astOperand1() || !tok->astOperand2())
688-
continue;
689-
690-
if (tok->astOperand1()->isLiteral() || tok->astOperand2()->isLiteral())
691-
continue;
692-
693-
if (!astIsIntegral(tok->astOperand1(), false) && !astIsIntegral(tok->astOperand2(), false))
694-
continue;
695-
696-
ValueFlow::Value val;
697-
698-
if (Token::Match(tok, "==|>=|<=|/")) {
699-
val = ValueFlow::Value(1);
700-
val.setKnown();
701-
}
702-
703-
if (Token::Match(tok, "!=|>|<|%|-")) {
704-
val = ValueFlow::Value(0);
705-
val.setKnown();
706-
}
707-
708-
if (!val.isKnown())
709-
continue;
710-
711-
if (isSameExpression(false, tok->astOperand1(), tok->astOperand2(), settings, true, true, &val.errorPath)) {
712-
setTokenValue(tok, std::move(val), settings);
713-
}
714-
}
715-
}
716-
717681
static bool getExpressionRange(const Token *expr, MathLib::bigint *minvalue, MathLib::bigint *maxvalue)
718682
{
719683
if (expr->hasKnownIntValue()) {
@@ -8386,7 +8350,7 @@ void ValueFlow::setValues(TokenList& tokenlist,
83868350
VFA(valueFlowLifetime(tokenlist, errorLogger, settings)),
83878351
VFA(valueFlowSymbolic(tokenlist, symboldatabase, errorLogger, settings)),
83888352
VFA(analyzeBitAnd(tokenlist, settings)),
8389-
VFA(valueFlowSameExpressions(tokenlist, settings)),
8353+
VFA(analyzeSameExpressions(tokenlist, settings)),
83908354
VFA(valueFlowConditionExpressions(tokenlist, symboldatabase, errorLogger, settings)),
83918355
});
83928356

Diff for: lib/vf_analyze.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "vf_globalstaticvar.h" // IWYU pragma: export
2727
#include "vf_number.h" // IWYU pragma: export
2828
#include "vf_pointeralias.h" // IWYU pragma: export
29+
#include "vf_sameexpressions.h" // IWYU pragma: export
2930
#include "vf_string.h" // IWYU pragma: export
3031
#include "vf_unknownfunctionreturn.h" // IWYU pragma: export
3132

Diff for: lib/vf_sameexpressions.cpp

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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_sameexpressions.h"
20+
21+
#include "astutils.h"
22+
#include "token.h"
23+
#include "tokenlist.h"
24+
#include "vfvalue.h"
25+
26+
#include "vf_settokenvalue.h"
27+
28+
#include <utility>
29+
30+
namespace ValueFlow
31+
{
32+
void analyzeSameExpressions(TokenList &tokenlist, const Settings& settings)
33+
{
34+
for (Token *tok = tokenlist.front(); tok; tok = tok->next()) {
35+
if (tok->hasKnownIntValue())
36+
continue;
37+
38+
if (!tok->astOperand1() || !tok->astOperand2())
39+
continue;
40+
41+
if (tok->astOperand1()->isLiteral() || tok->astOperand2()->isLiteral())
42+
continue;
43+
44+
if (!astIsIntegral(tok->astOperand1(), false) && !astIsIntegral(tok->astOperand2(), false))
45+
continue;
46+
47+
Value val;
48+
49+
if (Token::Match(tok, "==|>=|<=|/")) {
50+
val = ValueFlow::Value(1);
51+
val.setKnown();
52+
}
53+
54+
if (Token::Match(tok, "!=|>|<|%|-")) {
55+
val = ValueFlow::Value(0);
56+
val.setKnown();
57+
}
58+
59+
if (!val.isKnown())
60+
continue;
61+
62+
if (isSameExpression(false, tok->astOperand1(), tok->astOperand2(), settings, true, true, &val.errorPath)) {
63+
setTokenValue(tok, std::move(val), settings);
64+
}
65+
}
66+
}
67+
}

Diff for: lib/vf_sameexpressions.h

+30
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 vfSameExpressionsH
20+
#define vfSameExpressionsH
21+
22+
class TokenList;
23+
class Settings;
24+
25+
namespace ValueFlow
26+
{
27+
void analyzeSameExpressions(TokenList &tokenlist, const Settings& settings);
28+
}
29+
30+
#endif // vfSameExpressionsH

Diff for: oss-fuzz/Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
103103
$(libcppdir)/vf_globalstaticvar.o \
104104
$(libcppdir)/vf_number.o \
105105
$(libcppdir)/vf_pointeralias.o \
106+
$(libcppdir)/vf_sameexpressions.o \
106107
$(libcppdir)/vf_settokenvalue.o \
107108
$(libcppdir)/vf_string.o \
108109
$(libcppdir)/vf_unknownfunctionreturn.o \
@@ -150,7 +151,7 @@ simplecpp.o: ../externals/simplecpp/simplecpp.cpp ../externals/simplecpp/simplec
150151
tinyxml2.o: ../externals/tinyxml2/tinyxml2.cpp ../externals/tinyxml2/tinyxml2.h
151152
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -w -c -o $@ ../externals/tinyxml2/tinyxml2.cpp
152153

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
154+
$(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_sameexpressions.h ../lib/vf_settokenvalue.h ../lib/vf_string.h ../lib/vf_unknownfunctionreturn.h ../lib/vfvalue.h
154155
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/valueflow.cpp
155156

156157
$(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
@@ -360,6 +361,9 @@ $(libcppdir)/vf_number.o: ../lib/vf_number.cpp ../lib/config.h ../lib/errortypes
360361
$(libcppdir)/vf_pointeralias.o: ../lib/vf_pointeralias.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_pointeralias.h ../lib/vf_settokenvalue.h ../lib/vfvalue.h
361362
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_pointeralias.cpp
362363

364+
$(libcppdir)/vf_sameexpressions.o: ../lib/vf_sameexpressions.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_sameexpressions.h ../lib/vf_settokenvalue.h ../lib/vfvalue.h
365+
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_sameexpressions.cpp
366+
363367
$(libcppdir)/vf_settokenvalue.o: ../lib/vf_settokenvalue.cpp ../lib/addoninfo.h ../lib/astutils.h ../lib/calculate.h ../lib/config.h ../lib/errortypes.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/token.h ../lib/utils.h ../lib/valueflow.h ../lib/vf_common.h ../lib/vf_settokenvalue.h ../lib/vfvalue.h
364368
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_settokenvalue.cpp
365369

0 commit comments

Comments
 (0)