Skip to content

Commit 5bba4f5

Browse files
committed
ValueFlow: extracted valueFlowEnumValue() into separate file / introduced vf_analyze.h to group all analyze headers
1 parent 4f922b0 commit 5bba4f5

File tree

8 files changed

+126
-31
lines changed

8 files changed

+126
-31
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
252252
$(libcppdir)/token.o \
253253
$(libcppdir)/tokenlist.o \
254254
$(libcppdir)/utils.o \
255+
$(libcppdir)/vf_enumvalue.o \
255256
$(libcppdir)/vfvalue.o
256257

257258
EXTOBJ = externals/simplecpp/simplecpp.o \
@@ -459,7 +460,7 @@ validateRules:
459460

460461
###### Build
461462

462-
$(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/vfvalue.h
463+
$(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_enumvalue.h lib/vfvalue.h
463464
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/valueflow.cpp
464465

465466
$(libcppdir)/tokenize.o: lib/tokenize.cpp externals/simplecpp/simplecpp.h lib/addoninfo.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/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
@@ -645,6 +646,9 @@ $(libcppdir)/tokenlist.o: lib/tokenlist.cpp externals/simplecpp/simplecpp.h lib/
645646
$(libcppdir)/utils.o: lib/utils.cpp lib/config.h lib/utils.h
646647
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/utils.cpp
647648

649+
$(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
650+
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_enumvalue.cpp
651+
648652
$(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
649653
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vfvalue.cpp
650654

lib/lib.pri

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ HEADERS += $${PWD}/addoninfo.h \
7777
$${PWD}/valueflow.h \
7878
$${PWD}/valueptr.h \
7979
$${PWD}/version.h \
80+
$${PWD}/vf_analyze.h \
81+
$${PWD}/vf_enumvalue.h \
8082
$${PWD}/vfvalue.h \
8183
$${PWD}/xml.h
8284

@@ -142,4 +144,5 @@ SOURCES += $${PWD}/valueflow.cpp \
142144
$${PWD}/token.cpp \
143145
$${PWD}/tokenlist.cpp \
144146
$${PWD}/utils.cpp \
147+
$${PWD}/vf_enumvalue.cpp \
145148
$${PWD}/vfvalue.cpp

lib/valueflow.cpp

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@
105105
#include "valueptr.h"
106106
#include "vfvalue.h"
107107

108+
#include "vf_analyze.h"
109+
108110
#include <algorithm>
109111
#include <array>
110112
#include <cassert>
@@ -2030,33 +2032,6 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings& sett
20302032
}
20312033
}
20322034

2033-
static void valueFlowEnumValue(SymbolDatabase & symboldatabase, const Settings & settings)
2034-
{
2035-
for (Scope & scope : symboldatabase.scopeList) {
2036-
if (scope.type != Scope::eEnum)
2037-
continue;
2038-
MathLib::bigint value = 0;
2039-
bool prev_enum_is_known = true;
2040-
2041-
for (Enumerator & enumerator : scope.enumeratorList) {
2042-
if (enumerator.start) {
2043-
Token* rhs = const_cast<Token*>(enumerator.start->previous()->astOperand2());
2044-
ValueFlow::valueFlowConstantFoldAST(rhs, settings);
2045-
if (rhs && rhs->hasKnownIntValue()) {
2046-
enumerator.value = rhs->values().front().intvalue;
2047-
enumerator.value_known = true;
2048-
value = enumerator.value + 1;
2049-
prev_enum_is_known = true;
2050-
} else
2051-
prev_enum_is_known = false;
2052-
} else if (prev_enum_is_known) {
2053-
enumerator.value = value++;
2054-
enumerator.value_known = true;
2055-
}
2056-
}
2057-
}
2058-
}
2059-
20602035
static void valueFlowGlobalConstVar(TokenList& tokenList, const Settings &settings)
20612036
{
20622037
// Get variable values...
@@ -9607,13 +9582,13 @@ void ValueFlow::setValues(TokenList& tokenlist,
96079582

96089583
ValueFlowPassRunner runner{ValueFlowState{tokenlist, symboldatabase, errorLogger, settings}, timerResults};
96099584
runner.run_once({
9610-
VFA(valueFlowEnumValue(symboldatabase, settings)),
9585+
VFA(analyzeEnumValue(symboldatabase, settings)),
96119586
VFA(valueFlowNumber(tokenlist, settings)),
96129587
VFA(valueFlowString(tokenlist, settings)),
96139588
VFA(valueFlowArray(tokenlist, settings)),
96149589
VFA(valueFlowUnknownFunctionReturn(tokenlist, settings)),
96159590
VFA(valueFlowGlobalConstVar(tokenlist, settings)),
9616-
VFA(valueFlowEnumValue(symboldatabase, settings)),
9591+
VFA(analyzeEnumValue(symboldatabase, settings)),
96179592
VFA(valueFlowGlobalStaticVar(tokenlist, settings)),
96189593
VFA(valueFlowPointerAlias(tokenlist, settings)),
96199594
VFA(valueFlowLifetime(tokenlist, errorLogger, settings)),

lib/vf_analyze.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 vfAnalyzeH
20+
#define vfAnalyzeH
21+
22+
#include "vf_enumvalue.h" // IWYU pragma: export
23+
24+
#endif // vfAnalyzeH

lib/vf_enumvalue.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_enumvalue.h"
20+
21+
#include "mathlib.h"
22+
#include "symboldatabase.h"
23+
#include "token.h"
24+
#include "valueflow.h"
25+
26+
namespace ValueFlow
27+
{
28+
void analyzeEnumValue(SymbolDatabase & symboldatabase, const Settings & settings)
29+
{
30+
for (Scope & scope : symboldatabase.scopeList) {
31+
if (scope.type != Scope::eEnum)
32+
continue;
33+
MathLib::bigint value = 0;
34+
bool prev_enum_is_known = true;
35+
36+
for (Enumerator & enumerator : scope.enumeratorList) {
37+
if (enumerator.start) {
38+
Token* rhs = const_cast<Token*>(enumerator.start->previous()->astOperand2());
39+
ValueFlow::valueFlowConstantFoldAST(rhs, settings);
40+
if (rhs && rhs->hasKnownIntValue()) {
41+
enumerator.value = rhs->values().front().intvalue;
42+
enumerator.value_known = true;
43+
value = enumerator.value + 1;
44+
prev_enum_is_known = true;
45+
} else
46+
prev_enum_is_known = false;
47+
} else if (prev_enum_is_known) {
48+
enumerator.value = value++;
49+
enumerator.value_known = true;
50+
}
51+
}
52+
}
53+
}
54+
}

lib/vf_enumvalue.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 vfEnumValueH
20+
#define vfEnumValueH
21+
22+
class SymbolDatabase;
23+
class Settings;
24+
25+
namespace ValueFlow
26+
{
27+
void analyzeEnumValue(SymbolDatabase & symboldatabase, const Settings & settings);
28+
}
29+
30+
#endif // vfEnumValueH

oss-fuzz/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
9595
$(libcppdir)/token.o \
9696
$(libcppdir)/tokenlist.o \
9797
$(libcppdir)/utils.o \
98+
$(libcppdir)/vf_enumvalue.o \
9899
$(libcppdir)/vfvalue.o
99100

100101
EXTOBJ = simplecpp.o \
@@ -139,7 +140,7 @@ simplecpp.o: ../externals/simplecpp/simplecpp.cpp ../externals/simplecpp/simplec
139140
tinyxml2.o: ../externals/tinyxml2/tinyxml2.cpp ../externals/tinyxml2/tinyxml2.h
140141
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -w -c -o $@ ../externals/tinyxml2/tinyxml2.cpp
141142

142-
$(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/vfvalue.h
143+
$(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_enumvalue.h ../lib/vfvalue.h
143144
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/valueflow.cpp
144145

145146
$(libcppdir)/tokenize.o: ../lib/tokenize.cpp ../externals/simplecpp/simplecpp.h ../lib/addoninfo.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/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
@@ -325,6 +326,9 @@ $(libcppdir)/tokenlist.o: ../lib/tokenlist.cpp ../externals/simplecpp/simplecpp.
325326
$(libcppdir)/utils.o: ../lib/utils.cpp ../lib/config.h ../lib/utils.h
326327
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/utils.cpp
327328

329+
$(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
330+
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_enumvalue.cpp
331+
328332
$(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
329333
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vfvalue.cpp
330334

tools/dmake/dmake.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ int main(int argc, char **argv)
471471
libfiles_h.emplace_back("tokenrange.h");
472472
libfiles_h.emplace_back("valueptr.h");
473473
libfiles_h.emplace_back("version.h");
474+
libfiles_h.emplace_back("vf_analyze.h");
474475
libfiles_h.emplace_back("xml.h");
475476
std::sort(libfiles_h.begin(), libfiles_h.end());
476477

0 commit comments

Comments
 (0)