Skip to content

Commit 9e8f6bf

Browse files
committed
ValueFlow: extracted valueFlowEnumValue() into separate file
1 parent 0ce8419 commit 9e8f6bf

6 files changed

Lines changed: 96 additions & 31 deletions

File tree

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ LIBOBJ = $(libcppdir)/analyzerinfo.o \
247247
$(libcppdir)/tokenlist.o \
248248
$(libcppdir)/utils.o \
249249
$(libcppdir)/valueflow.o \
250+
$(libcppdir)/vf/enumvalue.o \
250251
$(libcppdir)/vfvalue.o
251252

252253
EXTOBJ = externals/simplecpp/simplecpp.o \
@@ -620,9 +621,12 @@ $(libcppdir)/tokenlist.o: lib/tokenlist.cpp externals/simplecpp/simplecpp.h lib/
620621
$(libcppdir)/utils.o: lib/utils.cpp lib/config.h lib/utils.h
621622
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/utils.cpp
622623

623-
$(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/vfvalue.h
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
624625
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/valueflow.cpp
625626

627+
$(libcppdir)/vf/enumvalue.o: lib/vf/enumvalue.cpp lib/vf/enumvalue.h
628+
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf/enumvalue.cpp
629+
626630
$(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
627631
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vfvalue.cpp
628632

lib/cppcheck.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<ClCompile Include="tokenlist.cpp" />
104104
<ClCompile Include="utils.cpp" />
105105
<ClCompile Include="valueflow.cpp" />
106+
<ClCompile Include="vf/enumvalue.cpp" />
106107
<ClCompile Include="vfvalue.cpp" />
107108
</ItemGroup>
108109
<ItemGroup Label="HeaderFiles">
@@ -175,6 +176,7 @@
175176
<ClInclude Include="valueflow.h" />
176177
<ClInclude Include="valueptr.h" />
177178
<ClInclude Include="version.h" />
179+
<ClInclude Include="vf/enumvalue.h" />
178180
<ClInclude Include="vfvalue.h" />
179181
</ItemGroup>
180182
<ItemGroup>

lib/lib.pri

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ HEADERS += $${PWD}/analyzer.h \
7070
$${PWD}/valueflow.h \
7171
$${PWD}/valueptr.h \
7272
$${PWD}/version.h \
73+
$${PWD}/vf/enumvalue.h \
7374
$${PWD}/vfvalue.h
7475

7576
SOURCES += $${PWD}/analyzerinfo.cpp \
@@ -130,4 +131,5 @@ SOURCES += $${PWD}/analyzerinfo.cpp \
130131
$${PWD}/tokenlist.cpp \
131132
$${PWD}/utils.cpp \
132133
$${PWD}/valueflow.cpp \
134+
$${PWD}/vf/enumvalue.cpp \
133135
$${PWD}/vfvalue.cpp

lib/valueflow.cpp

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
#include "valueptr.h"
103103
#include "vfvalue.h"
104104

105+
#include "vf/enumvalue.h"
106+
105107
#include <algorithm>
106108
#include <array>
107109
#include <cassert>
@@ -1862,34 +1864,6 @@ static void valueFlowImpossibleValues(TokenList* tokenList, const Settings* sett
18621864
}
18631865
}
18641866

1865-
static void valueFlowEnumValue(SymbolDatabase * symboldatabase, const Settings * settings)
1866-
{
1867-
1868-
for (Scope & scope : symboldatabase->scopeList) {
1869-
if (scope.type != Scope::eEnum)
1870-
continue;
1871-
MathLib::bigint value = 0;
1872-
bool prev_enum_is_known = true;
1873-
1874-
for (Enumerator & enumerator : scope.enumeratorList) {
1875-
if (enumerator.start) {
1876-
Token *rhs = enumerator.start->previous()->astOperand2();
1877-
ValueFlow::valueFlowConstantFoldAST(rhs, settings);
1878-
if (rhs && rhs->hasKnownIntValue()) {
1879-
enumerator.value = rhs->values().front().intvalue;
1880-
enumerator.value_known = true;
1881-
value = enumerator.value + 1;
1882-
prev_enum_is_known = true;
1883-
} else
1884-
prev_enum_is_known = false;
1885-
} else if (prev_enum_is_known) {
1886-
enumerator.value = value++;
1887-
enumerator.value_known = true;
1888-
}
1889-
}
1890-
}
1891-
}
1892-
18931867
static void valueFlowGlobalConstVar(TokenList* tokenList, const Settings *settings)
18941868
{
18951869
// Get variable values...
@@ -8835,13 +8809,13 @@ void ValueFlow::setValues(TokenList *tokenlist, SymbolDatabase* symboldatabase,
88358809
for (Token *tok = tokenlist->front(); tok; tok = tok->next())
88368810
tok->clearValueFlow();
88378811

8838-
valueFlowEnumValue(symboldatabase, settings);
8812+
ValueFlow::enumValue(symboldatabase, settings);
88398813
valueFlowNumber(tokenlist);
88408814
valueFlowString(tokenlist);
88418815
valueFlowArray(tokenlist);
88428816
valueFlowUnknownFunctionReturn(tokenlist, settings);
88438817
valueFlowGlobalConstVar(tokenlist, settings);
8844-
valueFlowEnumValue(symboldatabase, settings);
8818+
ValueFlow::enumValue(symboldatabase, settings);
88458819
valueFlowNumber(tokenlist);
88468820
valueFlowGlobalStaticVar(tokenlist, settings);
88478821
valueFlowPointerAlias(tokenlist);

lib/vf/enumvalue.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 "enumvalue.h"
20+
21+
#include "mathlib.h"
22+
#include "symboldatabase.h"
23+
#include "token.h"
24+
#include "valueflow.h"
25+
26+
using namespace ValueFlow;
27+
28+
void ValueFlow::enumValue(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 = enumerator.start->previous()->astOperand2();
39+
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+
}

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

0 commit comments

Comments
 (0)