diff --git a/Makefile b/Makefile index 506b9ca6c21f..f08a6307871d 100644 --- a/Makefile +++ b/Makefile @@ -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 \ @@ -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 diff --git a/lib/cppcheck.vcxproj b/lib/cppcheck.vcxproj index 9373e3942199..b027cc232c97 100644 --- a/lib/cppcheck.vcxproj +++ b/lib/cppcheck.vcxproj @@ -65,6 +65,7 @@ + @@ -156,6 +157,7 @@ + diff --git a/lib/findtoken.cpp b/lib/findtoken.cpp new file mode 100644 index 000000000000..3f276e7542eb --- /dev/null +++ b/lib/findtoken.cpp @@ -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 . + */ + +#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(), "] (|{"); +} diff --git a/lib/findtoken.h b/lib/findtoken.h index afb38a682f77..7e42f9e25705 100644 --- a/lib/findtoken.h +++ b/lib/findtoken.h @@ -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); + templatelinkAt(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; @@ -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; @@ -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(); diff --git a/lib/lib.pri b/lib/lib.pri index 4dd6782b633a..94eed8b6d7c5 100644 --- a/lib/lib.pri +++ b/lib/lib.pri @@ -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 \ @@ -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 \ diff --git a/oss-fuzz/Makefile b/oss-fuzz/Makefile index b7dee5fd426b..5238e2e77309 100644 --- a/oss-fuzz/Makefile +++ b/oss-fuzz/Makefile @@ -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 \ @@ -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