Skip to content

Commit 042c118

Browse files
committed
findtoken.h: make sure code is being matchcompiled
1 parent cf02d06 commit 042c118

File tree

6 files changed

+58
-3
lines changed

6 files changed

+58
-3
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
230230
$(libcppdir)/ctu.o \
231231
$(libcppdir)/errorlogger.o \
232232
$(libcppdir)/errortypes.o \
233+
$(libcppdir)/findtoken.o \
233234
$(libcppdir)/forwardanalyzer.o \
234235
$(libcppdir)/fwdanalysis.o \
235236
$(libcppdir)/importproject.o \
@@ -603,6 +604,9 @@ $(libcppdir)/errorlogger.o: lib/errorlogger.cpp externals/tinyxml2/tinyxml2.h li
603604
$(libcppdir)/errortypes.o: lib/errortypes.cpp lib/config.h lib/errortypes.h lib/utils.h
604605
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/errortypes.cpp
605606

607+
$(libcppdir)/findtoken.o: lib/findtoken.cpp lib/config.h lib/errortypes.h lib/findtoken.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/utils.h lib/vfvalue.h
608+
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/findtoken.cpp
609+
606610
$(libcppdir)/forwardanalyzer.o: lib/forwardanalyzer.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/forwardanalyzer.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/tokenlist.h lib/utils.h lib/valueptr.h lib/vfvalue.h
607611
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/forwardanalyzer.cpp
608612

lib/cppcheck.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<ClCompile Include="ctu.cpp" />
6666
<ClCompile Include="errorlogger.cpp" />
6767
<ClCompile Include="errortypes.cpp" />
68+
<ClCompile Include="findtoken.cpp" />
6869
<ClCompile Include="forwardanalyzer.cpp" />
6970
<ClCompile Include="fwdanalysis.cpp" />
7071
<ClCompile Include="importproject.cpp" />
@@ -156,6 +157,7 @@
156157
<ClInclude Include="errortypes.h" />
157158
<ClInclude Include="filesettings.h" />
158159
<ClInclude Include="findtoken.h" />
160+
<ClInclude Include="findtoken.h" />
159161
<ClInclude Include="forwardanalyzer.h" />
160162
<ClInclude Include="fwdanalysis.h" />
161163
<ClInclude Include="importproject.h" />

lib/findtoken.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 "findtoken.h"
20+
21+
#include "token.h"
22+
23+
bool findTokensSkipDeadCodeImplMatch_1(const Token* tok)
24+
{
25+
return Token::Match(tok, "if|for|while (") && Token::simpleMatch(tok->linkAt(1), ") {");
26+
}
27+
28+
bool findTokensSkipDeadCodeImplMatch_2(const Token* tok)
29+
{
30+
return Token::Match(tok->astParent(), "&&|?|%oror%");
31+
}
32+
33+
bool findTokensSkipDeadCodeImplMatch_3(const Token* tok)
34+
{
35+
return Token::simpleMatch(tok, "[") && Token::Match(tok->link(), "] (|{");
36+
}

lib/findtoken.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ T* findToken(T* start, const Token* end, const Predicate& pred)
7474
return result;
7575
}
7676

77+
bool findTokensSkipDeadCodeImplMatch_1(const Token* tok);
78+
bool findTokensSkipDeadCodeImplMatch_2(const Token* tok);
79+
bool findTokensSkipDeadCodeImplMatch_3(const Token* tok);
80+
7781
template<class T,
7882
class Predicate,
7983
class Found,
@@ -92,7 +96,8 @@ bool findTokensSkipDeadCodeImpl(const Library& library,
9296
if (found(tok))
9397
return true;
9498
}
95-
if (Token::Match(tok, "if|for|while (") && Token::simpleMatch(tok->linkAt(1), ") {")) {
99+
//if (Token::Match(tok, "if|for|while (") && Token::simpleMatch(tok->linkAt(1), ") {")) {
100+
if (findTokensSkipDeadCodeImplMatch_1(tok)) {
96101
const Token* condTok = getCondTok(tok);
97102
if (!condTok)
98103
continue;
@@ -124,7 +129,8 @@ bool findTokensSkipDeadCodeImpl(const Library& library,
124129
return true;
125130
tok = thenStart->link();
126131
}
127-
} else if (Token::Match(tok->astParent(), "&&|?|%oror%") && astIsLHS(tok)) {
132+
//} else if (Token::Match(tok->astParent(), "&&|?|%oror%") && astIsLHS(tok)) {
133+
} else if (findTokensSkipDeadCodeImplMatch_2(tok) && astIsLHS(tok)) {
128134
auto result = evaluate(tok);
129135
if (result.empty())
130136
continue;
@@ -158,7 +164,8 @@ bool findTokensSkipDeadCodeImpl(const Library& library,
158164
if (r != 0) {
159165
tok = tok->linkAt(2);
160166
}
161-
} else if (Token::simpleMatch(tok, "[") && Token::Match(tok->link(), "] (|{")) {
167+
//} else if (Token::simpleMatch(tok, "[") && Token::Match(tok->link(), "] (|{")) {
168+
} else if (findTokensSkipDeadCodeImplMatch_3(tok)) {
162169
T* afterCapture = tok->link()->next();
163170
if (Token::simpleMatch(afterCapture, "(") && afterCapture->link())
164171
tok = afterCapture->link()->next();

lib/lib.pri

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ HEADERS += $${PWD}/addoninfo.h \
4545
$${PWD}/errortypes.h \
4646
$${PWD}/filesettings.h \
4747
$${PWD}/findtoken.h \
48+
$${PWD}/findtoken.h \
4849
$${PWD}/forwardanalyzer.h \
4950
$${PWD}/fwdanalysis.h \
5051
$${PWD}/importproject.h \
@@ -143,6 +144,7 @@ SOURCES += $${PWD}/valueflow.cpp \
143144
$${PWD}/ctu.cpp \
144145
$${PWD}/errorlogger.cpp \
145146
$${PWD}/errortypes.cpp \
147+
$${PWD}/findtoken.cpp \
146148
$${PWD}/forwardanalyzer.cpp \
147149
$${PWD}/fwdanalysis.cpp \
148150
$${PWD}/importproject.cpp \

oss-fuzz/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
7373
$(libcppdir)/ctu.o \
7474
$(libcppdir)/errorlogger.o \
7575
$(libcppdir)/errortypes.o \
76+
$(libcppdir)/findtoken.o \
7677
$(libcppdir)/forwardanalyzer.o \
7778
$(libcppdir)/fwdanalysis.o \
7879
$(libcppdir)/importproject.o \
@@ -281,6 +282,9 @@ $(libcppdir)/errorlogger.o: ../lib/errorlogger.cpp ../externals/tinyxml2/tinyxml
281282
$(libcppdir)/errortypes.o: ../lib/errortypes.cpp ../lib/config.h ../lib/errortypes.h ../lib/utils.h
282283
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/errortypes.cpp
283284

285+
$(libcppdir)/findtoken.o: ../lib/findtoken.cpp ../lib/config.h ../lib/errortypes.h ../lib/findtoken.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/utils.h ../lib/vfvalue.h
286+
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/findtoken.cpp
287+
284288
$(libcppdir)/forwardanalyzer.o: ../lib/forwardanalyzer.cpp ../lib/addoninfo.h ../lib/analyzer.h ../lib/astutils.h ../lib/color.h ../lib/config.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/forwardanalyzer.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/tokenlist.h ../lib/utils.h ../lib/valueptr.h ../lib/vfvalue.h
285289
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/forwardanalyzer.cpp
286290

0 commit comments

Comments
 (0)