From ba97b804047c54bd7c184509e4306f77ad8930a0 Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 22 Jul 2024 15:54:13 +0200 Subject: [PATCH] findtoken.h: make sure code is being matchcompiled --- Makefile | 4 ++++ lib/cppcheck.vcxproj | 2 ++ lib/findtoken.cpp | 36 ++++++++++++++++++++++++++++++++++++ lib/findtoken.h | 13 ++++++++++--- lib/lib.pri | 2 ++ oss-fuzz/Makefile | 4 ++++ 6 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 lib/findtoken.cpp diff --git a/Makefile b/Makefile index 506b9ca6c21..f08a6307871 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 9373e394219..b027cc232c9 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 00000000000..3f276e7542e --- /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 afb38a682f7..7e42f9e2570 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 4dd6782b633..94eed8b6d7c 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 b7dee5fd426..5238e2e7730 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