Skip to content

Commit

Permalink
findtoken.h: make sure code is being matchcompiled
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Jul 26, 2024
1 parent 8368943 commit ba97b80
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
$(libcppdir)/ctu.o \
$(libcppdir)/errorlogger.o \
$(libcppdir)/errortypes.o \
$(libcppdir)/findtoken.o \
$(libcppdir)/forwardanalyzer.o \
$(libcppdir)/fwdanalysis.o \
$(libcppdir)/importproject.o \
Expand Down Expand Up @@ -602,6 +603,9 @@ $(libcppdir)/errorlogger.o: lib/errorlogger.cpp externals/tinyxml2/tinyxml2.h li
$(libcppdir)/errortypes.o: lib/errortypes.cpp lib/config.h lib/errortypes.h lib/utils.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/errortypes.cpp

$(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
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/findtoken.cpp

$(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
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/forwardanalyzer.cpp

Expand Down
2 changes: 2 additions & 0 deletions lib/cppcheck.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<ClCompile Include="ctu.cpp" />
<ClCompile Include="errorlogger.cpp" />
<ClCompile Include="errortypes.cpp" />
<ClCompile Include="findtoken.cpp" />
<ClCompile Include="forwardanalyzer.cpp" />
<ClCompile Include="fwdanalysis.cpp" />
<ClCompile Include="importproject.cpp" />
Expand Down Expand Up @@ -156,6 +157,7 @@
<ClInclude Include="errortypes.h" />
<ClInclude Include="filesettings.h" />
<ClInclude Include="findtoken.h" />
<ClInclude Include="findtoken.h" />
<ClInclude Include="forwardanalyzer.h" />
<ClInclude Include="fwdanalysis.h" />
<ClInclude Include="importproject.h" />
Expand Down
36 changes: 36 additions & 0 deletions lib/findtoken.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2024 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "findtoken.h"

#include "token.h"

bool findTokensSkipDeadCodeImplMatch_1(const Token* tok)
{
return Token::Match(tok, "if|for|while (") && Token::simpleMatch(tok->linkAt(1), ") {");
}

bool findTokensSkipDeadCodeImplMatch_2(const Token* tok)
{
return Token::Match(tok->astParent(), "&&|?|%oror%");
}

bool findTokensSkipDeadCodeImplMatch_3(const Token* tok)
{
return Token::simpleMatch(tok, "[") && Token::Match(tok->link(), "] (|{");
}
13 changes: 10 additions & 3 deletions lib/findtoken.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ T* findToken(T* start, const Token* end, const Predicate& pred)
return result;
}

bool findTokensSkipDeadCodeImplMatch_1(const Token* tok);
bool findTokensSkipDeadCodeImplMatch_2(const Token* tok);
bool findTokensSkipDeadCodeImplMatch_3(const Token* tok);

template<class T,
class Predicate,
class Found,
Expand All @@ -92,7 +96,8 @@ bool findTokensSkipDeadCodeImpl(const Library& library,
if (found(tok))
return true;
}
if (Token::Match(tok, "if|for|while (") && Token::simpleMatch(tok->linkAt(1), ") {")) {
//if (Token::Match(tok, "if|for|while (") && Token::simpleMatch(tok->linkAt(1), ") {")) {
if (findTokensSkipDeadCodeImplMatch_1(tok)) {
const Token* condTok = getCondTok(tok);
if (!condTok)
continue;
Expand Down Expand Up @@ -124,7 +129,8 @@ bool findTokensSkipDeadCodeImpl(const Library& library,
return true;
tok = thenStart->link();
}
} else if (Token::Match(tok->astParent(), "&&|?|%oror%") && astIsLHS(tok)) {
//} else if (Token::Match(tok->astParent(), "&&|?|%oror%") && astIsLHS(tok)) {
} else if (findTokensSkipDeadCodeImplMatch_2(tok) && astIsLHS(tok)) {
auto result = evaluate(tok);
if (result.empty())
continue;
Expand Down Expand Up @@ -158,7 +164,8 @@ bool findTokensSkipDeadCodeImpl(const Library& library,
if (r != 0) {
tok = tok->linkAt(2);
}
} else if (Token::simpleMatch(tok, "[") && Token::Match(tok->link(), "] (|{")) {
//} else if (Token::simpleMatch(tok, "[") && Token::Match(tok->link(), "] (|{")) {
} else if (findTokensSkipDeadCodeImplMatch_3(tok)) {
T* afterCapture = tok->link()->next();
if (Token::simpleMatch(afterCapture, "(") && afterCapture->link())
tok = afterCapture->link()->next();
Expand Down
2 changes: 2 additions & 0 deletions lib/lib.pri
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ HEADERS += $${PWD}/addoninfo.h \
$${PWD}/errortypes.h \
$${PWD}/filesettings.h \
$${PWD}/findtoken.h \
$${PWD}/findtoken.h \
$${PWD}/forwardanalyzer.h \
$${PWD}/fwdanalysis.h \
$${PWD}/importproject.h \
Expand Down Expand Up @@ -143,6 +144,7 @@ SOURCES += $${PWD}/valueflow.cpp \
$${PWD}/ctu.cpp \
$${PWD}/errorlogger.cpp \
$${PWD}/errortypes.cpp \
$${PWD}/findtoken.cpp \
$${PWD}/forwardanalyzer.cpp \
$${PWD}/fwdanalysis.cpp \
$${PWD}/importproject.cpp \
Expand Down
4 changes: 4 additions & 0 deletions oss-fuzz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
$(libcppdir)/ctu.o \
$(libcppdir)/errorlogger.o \
$(libcppdir)/errortypes.o \
$(libcppdir)/findtoken.o \
$(libcppdir)/forwardanalyzer.o \
$(libcppdir)/fwdanalysis.o \
$(libcppdir)/importproject.o \
Expand Down Expand Up @@ -281,6 +282,9 @@ $(libcppdir)/errorlogger.o: ../lib/errorlogger.cpp ../externals/tinyxml2/tinyxml
$(libcppdir)/errortypes.o: ../lib/errortypes.cpp ../lib/config.h ../lib/errortypes.h ../lib/utils.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/errortypes.cpp

$(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
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/findtoken.cpp

$(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
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/forwardanalyzer.cpp

Expand Down

0 comments on commit ba97b80

Please sign in to comment.