From 7abea40a223d0b5b1583ce1b40603444d9cdfe86 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 21 Dec 2024 19:45:38 +0100 Subject: [PATCH 01/10] pred --- lib/findtoken.h | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/findtoken.h b/lib/findtoken.h index b82c4416293..b9a52b9001f 100644 --- a/lib/findtoken.h +++ b/lib/findtoken.h @@ -41,8 +41,8 @@ inline std::vector evaluateKnownValues(const Token* tok) return {tok->getKnownIntValue()}; } -template )> -void findTokensImpl(T* start, const Token* end, const Predicate& pred, Found found) +template )> +void findTokensImpl(T* start, const Token* end, const std::function& pred, Found found) { for (T* tok = start; precedes(tok, end); tok = tok->next()) { if (pred(tok)) { @@ -52,8 +52,8 @@ void findTokensImpl(T* start, const Token* end, const Predicate& pred, Found fou } } -template )> -std::vector findTokens(T* start, const Token* end, const Predicate& pred) +template )> +std::vector findTokens(T* start, const Token* end, const std::function& pred) { std::vector result; findTokensImpl(start, end, pred, [&](T* tok) { @@ -63,8 +63,8 @@ std::vector findTokens(T* start, const Token* end, const Predicate& pred) return result; } -template )> -T* findToken(T* start, const Token* end, const Predicate& pred) +template )> +T* findToken(T* start, const Token* end, const std::function& pred) { T* result = nullptr; findTokensImpl(start, end, pred, [&](T* tok) { @@ -75,14 +75,13 @@ T* findToken(T* start, const Token* end, const Predicate& pred) } template )> bool findTokensSkipDeadCodeImpl(const Library& library, T* start, const Token* end, - const Predicate& pred, + const std::function& pred, Found found, const Evaluate& evaluate, bool skipUnevaluated) @@ -175,11 +174,11 @@ bool findTokensSkipDeadCodeImpl(const Library& library, return false; } -template )> +template )> std::vector findTokensSkipDeadCode(const Library& library, T* start, const Token* end, - const Predicate& pred, + const std::function& pred, const Evaluate& evaluate) { std::vector result; @@ -197,17 +196,17 @@ std::vector findTokensSkipDeadCode(const Library& library, return result; } -template )> -std::vector findTokensSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred) +template )> +std::vector findTokensSkipDeadCode(const Library& library, T* start, const Token* end, const std::function& pred) { return findTokensSkipDeadCode(library, start, end, pred, &evaluateKnownValues); } -template )> +template )> std::vector findTokensSkipDeadAndUnevaluatedCode(const Library& library, T* start, const Token* end, - const Predicate& pred, + const std::function& pred, const Evaluate& evaluate) { std::vector result; @@ -225,15 +224,15 @@ std::vector findTokensSkipDeadAndUnevaluatedCode(const Library& library, return result; } -template )> -std::vector findTokensSkipDeadAndUnevaluatedCode(const Library& library, T* start, const Token* end, const Predicate& pred) +template )> +std::vector findTokensSkipDeadAndUnevaluatedCode(const Library& library, T* start, const Token* end, const std::function& pred) { return findTokensSkipDeadAndUnevaluatedCode(library, start, end, pred, &evaluateKnownValues); } -template )> -T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred, const Evaluate& evaluate) +template )> +T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const std::function& pred, const Evaluate& evaluate) { T* result = nullptr; (void)findTokensSkipDeadCodeImpl( @@ -250,8 +249,8 @@ T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, con return result; } -template )> -T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred) +template )> +T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const std::function& pred) { return findTokenSkipDeadCode(library, start, end, pred, &evaluateKnownValues); } From 08723e69666fd50002ac5d1ca99600061fb8384f Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 21 Dec 2024 19:47:29 +0100 Subject: [PATCH 02/10] move --- lib/astutils.cpp | 2 +- lib/findtoken.h | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 8175a977e79..ef3d83c89aa 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -3115,7 +3115,7 @@ namespace { template const Token* operator()(const Token* start, const Token* end, F f) const { - return findTokenSkipDeadCode(library, start, end, f, *evaluate); + return findTokenSkipDeadCode(library, start, end, std::move(f), *evaluate); } }; } diff --git a/lib/findtoken.h b/lib/findtoken.h index b9a52b9001f..fa15bc3b067 100644 --- a/lib/findtoken.h +++ b/lib/findtoken.h @@ -98,7 +98,7 @@ bool findTokensSkipDeadCodeImpl(const Library& library, auto result = evaluate(condTok); if (result.empty()) continue; - if (findTokensSkipDeadCodeImpl(library, tok->next(), tok->linkAt(1), pred, found, evaluate, skipUnevaluated)) + if (findTokensSkipDeadCodeImpl(library, tok->next(), tok->linkAt(1), pred, std::move(found), evaluate, skipUnevaluated)) return true; T* thenStart = tok->linkAt(1)->next(); T* elseStart = nullptr; @@ -108,7 +108,7 @@ bool findTokensSkipDeadCodeImpl(const Library& library, auto r = result.front(); if (r == 0) { if (elseStart) { - if (findTokensSkipDeadCodeImpl(library, elseStart, elseStart->link(), pred, found, evaluate, skipUnevaluated)) + if (findTokensSkipDeadCodeImpl(library, elseStart, elseStart->link(), pred, std::move(found), evaluate, skipUnevaluated)) return true; if (isReturnScope(elseStart->link(), library)) return true; @@ -117,7 +117,7 @@ bool findTokensSkipDeadCodeImpl(const Library& library, tok = thenStart->link(); } } else { - if (findTokensSkipDeadCodeImpl(library, thenStart, thenStart->link(), pred, found, evaluate, skipUnevaluated)) + if (findTokensSkipDeadCodeImpl(library, thenStart, thenStart->link(), pred, std::move(found), evaluate, skipUnevaluated)) return true; if (isReturnScope(thenStart->link(), library)) return true; @@ -137,7 +137,7 @@ bool findTokensSkipDeadCodeImpl(const Library& library, if (!cond) { next = colon; } else { - if (findTokensSkipDeadCodeImpl(library, tok->astParent()->next(), colon, pred, found, evaluate, skipUnevaluated)) + if (findTokensSkipDeadCodeImpl(library, tok->astParent()->next(), colon, pred, std::move(found), evaluate, skipUnevaluated)) return true; next = nextAfterAstRightmostLeaf(colon); } From c107d5f8dcff08e8bf91e164d1bc9515b9f395fe Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 21 Dec 2024 19:54:23 +0100 Subject: [PATCH 03/10] eval --- lib/findtoken.h | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/findtoken.h b/lib/findtoken.h index fa15bc3b067..72e1b984497 100644 --- a/lib/findtoken.h +++ b/lib/findtoken.h @@ -76,14 +76,13 @@ T* findToken(T* start, const Token* end, const std::function template )> bool findTokensSkipDeadCodeImpl(const Library& library, T* start, const Token* end, const std::function& pred, Found found, - const Evaluate& evaluate, + const std::function(const Token*)>& evaluate, bool skipUnevaluated) { for (T* tok = start; precedes(tok, end); tok = tok->next()) { @@ -174,12 +173,12 @@ bool findTokensSkipDeadCodeImpl(const Library& library, return false; } -template )> +template )> std::vector findTokensSkipDeadCode(const Library& library, T* start, const Token* end, const std::function& pred, - const Evaluate& evaluate) + const std::function(const Token*)>& evaluate) { std::vector result; (void)findTokensSkipDeadCodeImpl( @@ -202,12 +201,12 @@ std::vector findTokensSkipDeadCode(const Library& library, T* start, const T return findTokensSkipDeadCode(library, start, end, pred, &evaluateKnownValues); } -template )> +template )> std::vector findTokensSkipDeadAndUnevaluatedCode(const Library& library, T* start, const Token* end, const std::function& pred, - const Evaluate& evaluate) + const std::function(const Token*)>& evaluate) { std::vector result; (void)findTokensSkipDeadCodeImpl( @@ -231,8 +230,8 @@ std::vector findTokensSkipDeadAndUnevaluatedCode(const Library& library, T* } -template )> -T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const std::function& pred, const Evaluate& evaluate) +template )> +T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const std::function& pred, const std::function(const Token*)>& evaluate) { T* result = nullptr; (void)findTokensSkipDeadCodeImpl( From 10f1de0eda571a0d795d6baf752e1b4e475020e7 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 21 Dec 2024 20:53:16 +0100 Subject: [PATCH 04/10] found --- lib/findtoken.h | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/findtoken.h b/lib/findtoken.h index 72e1b984497..afaa797a00d 100644 --- a/lib/findtoken.h +++ b/lib/findtoken.h @@ -74,14 +74,12 @@ T* findToken(T* start, const Token* end, const std::function return result; } -template )> +template )> bool findTokensSkipDeadCodeImpl(const Library& library, T* start, const Token* end, const std::function& pred, - Found found, + std::function found, const std::function(const Token*)>& evaluate, bool skipUnevaluated) { @@ -181,15 +179,18 @@ std::vector findTokensSkipDeadCode(const Library& library, const std::function(const Token*)>& evaluate) { std::vector result; + + std::function f = [&](T* tok) { + result.push_back(tok); + return false; + }; + (void)findTokensSkipDeadCodeImpl( library, start, end, pred, - [&](T* tok) { - result.push_back(tok); - return false; - }, + std::move(f), evaluate, false); return result; @@ -209,15 +210,18 @@ std::vector findTokensSkipDeadAndUnevaluatedCode(const Library& library, const std::function(const Token*)>& evaluate) { std::vector result; + + std::function f = [&](T* tok) { + result.push_back(tok); + return false; + }; + (void)findTokensSkipDeadCodeImpl( library, start, end, pred, - [&](T* tok) { - result.push_back(tok); - return false; - }, + std::move(f), evaluate, true); return result; @@ -234,15 +238,19 @@ template& pred, const std::function(const Token*)>& evaluate) { T* result = nullptr; + + std::function f = [&](T* tok) + { + result = tok; + return true; + }; + (void)findTokensSkipDeadCodeImpl( library, start, end, pred, - [&](T* tok) { - result = tok; - return true; - }, + std::move(f), evaluate, false); return result; From a6e0edb288199dff312fdcd28d0f2fd045d986ee Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 21 Dec 2024 21:06:25 +0100 Subject: [PATCH 05/10] cpp --- Makefile | 4 ++ lib/cppcheck.vcxproj | 2 + lib/findtoken.cpp | 143 +++++++++++++++++++++++++++++++++++++++++++ lib/findtoken.h | 108 ++++++-------------------------- oss-fuzz/Makefile | 4 ++ 5 files changed, 172 insertions(+), 89 deletions(-) create mode 100644 lib/findtoken.cpp diff --git a/Makefile b/Makefile index c01dc8df126..261f73feae0 100644 --- a/Makefile +++ b/Makefile @@ -221,6 +221,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 \ @@ -571,6 +572,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/astutils.h 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 6074eb664cb..b77deac42a4 100644 --- a/lib/cppcheck.vcxproj +++ b/lib/cppcheck.vcxproj @@ -66,6 +66,7 @@ + @@ -140,6 +141,7 @@ + diff --git a/lib/findtoken.cpp b/lib/findtoken.cpp new file mode 100644 index 00000000000..b58654b2da9 --- /dev/null +++ b/lib/findtoken.cpp @@ -0,0 +1,143 @@ +/* + * 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 "astutils.h" +#include "token.h" + +template )> +static bool findTokensSkipDeadCodeImpl2(const Library& library, + T* start, + const Token* end, + const std::function& pred, + const std::function& found, + const std::function(const Token*)>& evaluate, + bool skipUnevaluated) +{ + for (T* tok = start; precedes(tok, end); tok = tok->next()) { + if (pred(tok)) { + if (found(tok)) + return true; + } + if (Token::Match(tok, "if|for|while (") && Token::simpleMatch(tok->linkAt(1), ") {")) { + const Token* condTok = getCondTok(tok); + if (!condTok) + continue; + auto result = evaluate(condTok); + if (result.empty()) + continue; + if (findTokensSkipDeadCodeImpl(library, tok->next(), tok->linkAt(1), pred, found, evaluate, skipUnevaluated)) + return true; + T* thenStart = tok->linkAt(1)->next(); + T* elseStart = nullptr; + if (Token::simpleMatch(thenStart->link(), "} else {")) + elseStart = thenStart->link()->tokAt(2); + + auto r = result.front(); + if (r == 0) { + if (elseStart) { + if (findTokensSkipDeadCodeImpl(library, elseStart, elseStart->link(), pred, found, evaluate, skipUnevaluated)) + return true; + if (isReturnScope(elseStart->link(), library)) + return true; + tok = elseStart->link(); + } else { + tok = thenStart->link(); + } + } else { + if (findTokensSkipDeadCodeImpl(library, thenStart, thenStart->link(), pred, found, evaluate, skipUnevaluated)) + return true; + if (isReturnScope(thenStart->link(), library)) + return true; + tok = thenStart->link(); + } + } else if (Token::Match(tok->astParent(), "&&|?|%oror%") && astIsLHS(tok)) { + auto result = evaluate(tok); + if (result.empty()) + continue; + const bool cond = result.front() != 0; + T* next = nullptr; + if ((cond && Token::simpleMatch(tok->astParent(), "||")) || + (!cond && Token::simpleMatch(tok->astParent(), "&&"))) { + next = nextAfterAstRightmostLeaf(tok->astParent()); + } else if (Token::simpleMatch(tok->astParent(), "?")) { + T* colon = tok->astParent()->astOperand2(); + if (!cond) { + next = colon; + } else { + if (findTokensSkipDeadCodeImpl(library, tok->astParent()->next(), colon, pred, found, evaluate, skipUnevaluated)) + return true; + next = nextAfterAstRightmostLeaf(colon); + } + } + if (next) + tok = next; + } else if (Token::simpleMatch(tok, "} else {")) { + const Token* condTok = getCondTokFromEnd(tok); + if (!condTok) + continue; + auto result = evaluate(condTok); + if (result.empty()) + continue; + if (isReturnScope(tok->link(), library)) + return true; + auto r = result.front(); + if (r != 0) { + tok = tok->linkAt(2); + } + } else if (Token::simpleMatch(tok, "[") && Token::Match(tok->link(), "] (|{")) { + T* afterCapture = tok->link()->next(); + if (Token::simpleMatch(afterCapture, "(") && afterCapture->link()) + tok = afterCapture->link()->next(); + else + tok = afterCapture; + } + if (skipUnevaluated && isUnevaluated(tok)) { + T *next = tok->linkAt(1); + if (!next) + continue; + tok = next; + } + } + return false; +} + +template<> +bool findTokensSkipDeadCodeImpl(const Library& library, + Token* start, + const Token* end, + const std::function& pred, + std::function found, + const std::function(const Token*)>& evaluate, + bool skipUnevaluated) +{ + return findTokensSkipDeadCodeImpl2(library, start, end, pred, found, evaluate, skipUnevaluated); +} + +template<> +bool findTokensSkipDeadCodeImpl(const Library& library, + const Token* start, + const Token* end, + const std::function& pred, + std::function found, + const std::function(const Token*)>& evaluate, + bool skipUnevaluated) +{ + return findTokensSkipDeadCodeImpl2(library, start, end, pred, found, evaluate, skipUnevaluated); +} diff --git a/lib/findtoken.h b/lib/findtoken.h index afaa797a00d..d78e9d45d61 100644 --- a/lib/findtoken.h +++ b/lib/findtoken.h @@ -74,102 +74,32 @@ T* findToken(T* start, const Token* end, const std::function return result; } -template )> +template bool findTokensSkipDeadCodeImpl(const Library& library, T* start, const Token* end, const std::function& pred, std::function found, const std::function(const Token*)>& evaluate, - bool skipUnevaluated) -{ - for (T* tok = start; precedes(tok, end); tok = tok->next()) { - if (pred(tok)) { - if (found(tok)) - return true; - } - if (Token::Match(tok, "if|for|while (") && Token::simpleMatch(tok->linkAt(1), ") {")) { - const Token* condTok = getCondTok(tok); - if (!condTok) - continue; - auto result = evaluate(condTok); - if (result.empty()) - continue; - if (findTokensSkipDeadCodeImpl(library, tok->next(), tok->linkAt(1), pred, std::move(found), evaluate, skipUnevaluated)) - return true; - T* thenStart = tok->linkAt(1)->next(); - T* elseStart = nullptr; - if (Token::simpleMatch(thenStart->link(), "} else {")) - elseStart = thenStart->link()->tokAt(2); + bool skipUnevaluated) = delete; - auto r = result.front(); - if (r == 0) { - if (elseStart) { - if (findTokensSkipDeadCodeImpl(library, elseStart, elseStart->link(), pred, std::move(found), evaluate, skipUnevaluated)) - return true; - if (isReturnScope(elseStart->link(), library)) - return true; - tok = elseStart->link(); - } else { - tok = thenStart->link(); - } - } else { - if (findTokensSkipDeadCodeImpl(library, thenStart, thenStart->link(), pred, std::move(found), evaluate, skipUnevaluated)) - return true; - if (isReturnScope(thenStart->link(), library)) - return true; - tok = thenStart->link(); - } - } else if (Token::Match(tok->astParent(), "&&|?|%oror%") && astIsLHS(tok)) { - auto result = evaluate(tok); - if (result.empty()) - continue; - const bool cond = result.front() != 0; - T* next = nullptr; - if ((cond && Token::simpleMatch(tok->astParent(), "||")) || - (!cond && Token::simpleMatch(tok->astParent(), "&&"))) { - next = nextAfterAstRightmostLeaf(tok->astParent()); - } else if (Token::simpleMatch(tok->astParent(), "?")) { - T* colon = tok->astParent()->astOperand2(); - if (!cond) { - next = colon; - } else { - if (findTokensSkipDeadCodeImpl(library, tok->astParent()->next(), colon, pred, std::move(found), evaluate, skipUnevaluated)) - return true; - next = nextAfterAstRightmostLeaf(colon); - } - } - if (next) - tok = next; - } else if (Token::simpleMatch(tok, "} else {")) { - const Token* condTok = getCondTokFromEnd(tok); - if (!condTok) - continue; - auto result = evaluate(condTok); - if (result.empty()) - continue; - if (isReturnScope(tok->link(), library)) - return true; - auto r = result.front(); - if (r != 0) { - tok = tok->linkAt(2); - } - } else if (Token::simpleMatch(tok, "[") && Token::Match(tok->link(), "] (|{")) { - T* afterCapture = tok->link()->next(); - if (Token::simpleMatch(afterCapture, "(") && afterCapture->link()) - tok = afterCapture->link()->next(); - else - tok = afterCapture; - } - if (skipUnevaluated && isUnevaluated(tok)) { - T *next = tok->linkAt(1); - if (!next) - continue; - tok = next; - } - } - return false; -} +template<> +bool findTokensSkipDeadCodeImpl(const Library& library, + Token* start, + const Token* end, + const std::function& pred, + std::function found, + const std::function(const Token*)>& evaluate, + bool skipUnevaluated); + +template<> +bool findTokensSkipDeadCodeImpl(const Library& library, + const Token* start, + const Token* end, + const std::function& pred, + std::function found, + const std::function(const Token*)>& evaluate, + bool skipUnevaluated); template )> std::vector findTokensSkipDeadCode(const Library& library, diff --git a/oss-fuzz/Makefile b/oss-fuzz/Makefile index 4203c9deb46..6ef1d053c54 100644 --- a/oss-fuzz/Makefile +++ b/oss-fuzz/Makefile @@ -74,6 +74,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 \ @@ -267,6 +268,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/astutils.h ../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 From 2d8b7e3fc8ffc55ccb617b461d31c953e4d4e9e3 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 21 Dec 2024 21:08:14 +0100 Subject: [PATCH 06/10] restore --- lib/findtoken.h | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/findtoken.h b/lib/findtoken.h index d78e9d45d61..f5af531d512 100644 --- a/lib/findtoken.h +++ b/lib/findtoken.h @@ -41,8 +41,8 @@ inline std::vector evaluateKnownValues(const Token* tok) return {tok->getKnownIntValue()}; } -template )> -void findTokensImpl(T* start, const Token* end, const std::function& pred, Found found) +template )> +void findTokensImpl(T* start, const Token* end, const Predicate& pred, Found found) { for (T* tok = start; precedes(tok, end); tok = tok->next()) { if (pred(tok)) { @@ -52,8 +52,8 @@ void findTokensImpl(T* start, const Token* end, const std::function )> -std::vector findTokens(T* start, const Token* end, const std::function& pred) +template )> +std::vector findTokens(T* start, const Token* end, const Predicate& pred) { std::vector result; findTokensImpl(start, end, pred, [&](T* tok) { @@ -63,8 +63,8 @@ std::vector findTokens(T* start, const Token* end, const std::function )> -T* findToken(T* start, const Token* end, const std::function& pred) +template )> +T* findToken(T* start, const Token* end, const Predicate& pred) { T* result = nullptr; findTokensImpl(start, end, pred, [&](T* tok) { @@ -101,12 +101,12 @@ bool findTokensSkipDeadCodeImpl(const Library& library, const std::function(const Token*)>& evaluate, bool skipUnevaluated); -template )> +template )> std::vector findTokensSkipDeadCode(const Library& library, T* start, const Token* end, - const std::function& pred, - const std::function(const Token*)>& evaluate) + const Predicate& pred, + const Evaluate& evaluate) { std::vector result; @@ -126,18 +126,18 @@ std::vector findTokensSkipDeadCode(const Library& library, return result; } -template )> -std::vector findTokensSkipDeadCode(const Library& library, T* start, const Token* end, const std::function& pred) +template )> +std::vector findTokensSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred) { return findTokensSkipDeadCode(library, start, end, pred, &evaluateKnownValues); } -template )> +template )> std::vector findTokensSkipDeadAndUnevaluatedCode(const Library& library, T* start, const Token* end, - const std::function& pred, - const std::function(const Token*)>& evaluate) + const Predicate& pred, + const Evaluate& evaluate) { std::vector result; @@ -157,15 +157,15 @@ std::vector findTokensSkipDeadAndUnevaluatedCode(const Library& library, return result; } -template )> -std::vector findTokensSkipDeadAndUnevaluatedCode(const Library& library, T* start, const Token* end, const std::function& pred) +template )> +std::vector findTokensSkipDeadAndUnevaluatedCode(const Library& library, T* start, const Token* end, const Predicate& pred) { return findTokensSkipDeadAndUnevaluatedCode(library, start, end, pred, &evaluateKnownValues); } -template )> -T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const std::function& pred, const std::function(const Token*)>& evaluate) +template )> +T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred, const Evaluate& evaluate) { T* result = nullptr; @@ -186,8 +186,8 @@ T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, con return result; } -template )> -T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const std::function& pred) +template )> +T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred) { return findTokenSkipDeadCode(library, start, end, pred, &evaluateKnownValues); } From f17db22a3ace20a121b8e90987a792b6399234d2 Mon Sep 17 00:00:00 2001 From: firewave Date: Sun, 22 Dec 2024 15:33:30 +0100 Subject: [PATCH 07/10] internal --- lib/findtoken.cpp | 66 ++++++++++++++++++++++++----------------------- lib/findtoken.h | 60 +++++++++++++++++++++--------------------- 2 files changed, 65 insertions(+), 61 deletions(-) diff --git a/lib/findtoken.cpp b/lib/findtoken.cpp index b58654b2da9..eb9c13d41e6 100644 --- a/lib/findtoken.cpp +++ b/lib/findtoken.cpp @@ -22,13 +22,13 @@ #include "token.h" template )> -static bool findTokensSkipDeadCodeImpl2(const Library& library, - T* start, - const Token* end, - const std::function& pred, - const std::function& found, - const std::function(const Token*)>& evaluate, - bool skipUnevaluated) +static bool findTokensSkipDeadCodeImpl(const Library& library, + T* start, + const Token* end, + const std::function& pred, + const std::function& found, + const std::function(const Token*)>& evaluate, + bool skipUnevaluated) { for (T* tok = start; precedes(tok, end); tok = tok->next()) { if (pred(tok)) { @@ -42,7 +42,7 @@ static bool findTokensSkipDeadCodeImpl2(const Library& library, auto result = evaluate(condTok); if (result.empty()) continue; - if (findTokensSkipDeadCodeImpl(library, tok->next(), tok->linkAt(1), pred, found, evaluate, skipUnevaluated)) + if (internal::findTokensSkipDeadCodeImpl(library, tok->next(), tok->linkAt(1), pred, found, evaluate, skipUnevaluated)) return true; T* thenStart = tok->linkAt(1)->next(); T* elseStart = nullptr; @@ -52,7 +52,7 @@ static bool findTokensSkipDeadCodeImpl2(const Library& library, auto r = result.front(); if (r == 0) { if (elseStart) { - if (findTokensSkipDeadCodeImpl(library, elseStart, elseStart->link(), pred, found, evaluate, skipUnevaluated)) + if (internal::findTokensSkipDeadCodeImpl(library, elseStart, elseStart->link(), pred, found, evaluate, skipUnevaluated)) return true; if (isReturnScope(elseStart->link(), library)) return true; @@ -61,7 +61,7 @@ static bool findTokensSkipDeadCodeImpl2(const Library& library, tok = thenStart->link(); } } else { - if (findTokensSkipDeadCodeImpl(library, thenStart, thenStart->link(), pred, found, evaluate, skipUnevaluated)) + if (internal::findTokensSkipDeadCodeImpl(library, thenStart, thenStart->link(), pred, found, evaluate, skipUnevaluated)) return true; if (isReturnScope(thenStart->link(), library)) return true; @@ -81,7 +81,7 @@ static bool findTokensSkipDeadCodeImpl2(const Library& library, if (!cond) { next = colon; } else { - if (findTokensSkipDeadCodeImpl(library, tok->astParent()->next(), colon, pred, found, evaluate, skipUnevaluated)) + if (internal::findTokensSkipDeadCodeImpl(library, tok->astParent()->next(), colon, pred, found, evaluate, skipUnevaluated)) return true; next = nextAfterAstRightmostLeaf(colon); } @@ -118,26 +118,28 @@ static bool findTokensSkipDeadCodeImpl2(const Library& library, return false; } -template<> -bool findTokensSkipDeadCodeImpl(const Library& library, - Token* start, - const Token* end, - const std::function& pred, - std::function found, - const std::function(const Token*)>& evaluate, - bool skipUnevaluated) -{ - return findTokensSkipDeadCodeImpl2(library, start, end, pred, found, evaluate, skipUnevaluated); -} +namespace internal { + template<> + bool findTokensSkipDeadCodeImpl(const Library& library, + Token* start, + const Token* end, + const std::function& pred, + std::function found, + const std::function(const Token*)>& evaluate, + bool skipUnevaluated) + { + return ::findTokensSkipDeadCodeImpl(library, start, end, pred, found, evaluate, skipUnevaluated); + } -template<> -bool findTokensSkipDeadCodeImpl(const Library& library, - const Token* start, - const Token* end, - const std::function& pred, - std::function found, - const std::function(const Token*)>& evaluate, - bool skipUnevaluated) -{ - return findTokensSkipDeadCodeImpl2(library, start, end, pred, found, evaluate, skipUnevaluated); + template<> + bool findTokensSkipDeadCodeImpl(const Library& library, + const Token* start, + const Token* end, + const std::function& pred, + std::function found, + const std::function(const Token*)>& evaluate, + bool skipUnevaluated) + { + return ::findTokensSkipDeadCodeImpl(library, start, end, pred, found, evaluate, skipUnevaluated); + } } diff --git a/lib/findtoken.h b/lib/findtoken.h index f5af531d512..79cf72cb0c3 100644 --- a/lib/findtoken.h +++ b/lib/findtoken.h @@ -74,32 +74,34 @@ T* findToken(T* start, const Token* end, const Predicate& pred) return result; } -template -bool findTokensSkipDeadCodeImpl(const Library& library, - T* start, - const Token* end, - const std::function& pred, - std::function found, - const std::function(const Token*)>& evaluate, - bool skipUnevaluated) = delete; - -template<> -bool findTokensSkipDeadCodeImpl(const Library& library, - Token* start, - const Token* end, - const std::function& pred, - std::function found, - const std::function(const Token*)>& evaluate, - bool skipUnevaluated); - -template<> -bool findTokensSkipDeadCodeImpl(const Library& library, - const Token* start, - const Token* end, - const std::function& pred, - std::function found, - const std::function(const Token*)>& evaluate, - bool skipUnevaluated); +namespace internal { + template + bool findTokensSkipDeadCodeImpl(const Library &library, + T *start, + const Token *end, + const std::function &pred, + std::function found, + const std::function(const Token *)> &evaluate, + bool skipUnevaluated) = delete; + + template<> + bool findTokensSkipDeadCodeImpl(const Library &library, + Token *start, + const Token *end, + const std::function &pred, + std::function found, + const std::function(const Token *)> &evaluate, + bool skipUnevaluated); + + template<> + bool findTokensSkipDeadCodeImpl(const Library &library, + const Token *start, + const Token *end, + const std::function &pred, + std::function found, + const std::function(const Token *)> &evaluate, + bool skipUnevaluated); +} template )> std::vector findTokensSkipDeadCode(const Library& library, @@ -115,7 +117,7 @@ std::vector findTokensSkipDeadCode(const Library& library, return false; }; - (void)findTokensSkipDeadCodeImpl( + (void)internal::findTokensSkipDeadCodeImpl( library, start, end, @@ -146,7 +148,7 @@ std::vector findTokensSkipDeadAndUnevaluatedCode(const Library& library, return false; }; - (void)findTokensSkipDeadCodeImpl( + (void)internal::findTokensSkipDeadCodeImpl( library, start, end, @@ -175,7 +177,7 @@ T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, con return true; }; - (void)findTokensSkipDeadCodeImpl( + (void)internal::findTokensSkipDeadCodeImpl( library, start, end, From 02e72008042c9d06dc18cc88e16a327351a5cf7e Mon Sep 17 00:00:00 2001 From: firewave Date: Sun, 22 Dec 2024 15:35:58 +0100 Subject: [PATCH 08/10] const ref --- lib/findtoken.cpp | 4 ++-- lib/findtoken.h | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/findtoken.cpp b/lib/findtoken.cpp index eb9c13d41e6..af17608844f 100644 --- a/lib/findtoken.cpp +++ b/lib/findtoken.cpp @@ -124,7 +124,7 @@ namespace internal { Token* start, const Token* end, const std::function& pred, - std::function found, + const std::function& found, const std::function(const Token*)>& evaluate, bool skipUnevaluated) { @@ -136,7 +136,7 @@ namespace internal { const Token* start, const Token* end, const std::function& pred, - std::function found, + const std::function& found, const std::function(const Token*)>& evaluate, bool skipUnevaluated) { diff --git a/lib/findtoken.h b/lib/findtoken.h index 79cf72cb0c3..c9a6c5c89b6 100644 --- a/lib/findtoken.h +++ b/lib/findtoken.h @@ -80,7 +80,7 @@ namespace internal { T *start, const Token *end, const std::function &pred, - std::function found, + const std::function& found, const std::function(const Token *)> &evaluate, bool skipUnevaluated) = delete; @@ -89,7 +89,7 @@ namespace internal { Token *start, const Token *end, const std::function &pred, - std::function found, + const std::function& found, const std::function(const Token *)> &evaluate, bool skipUnevaluated); @@ -98,7 +98,7 @@ namespace internal { const Token *start, const Token *end, const std::function &pred, - std::function found, + const std::function& found, const std::function(const Token *)> &evaluate, bool skipUnevaluated); } @@ -122,7 +122,7 @@ std::vector findTokensSkipDeadCode(const Library& library, start, end, pred, - std::move(f), + f, evaluate, false); return result; @@ -153,7 +153,7 @@ std::vector findTokensSkipDeadAndUnevaluatedCode(const Library& library, start, end, pred, - std::move(f), + f, evaluate, true); return result; @@ -182,7 +182,7 @@ T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, con start, end, pred, - std::move(f), + f, evaluate, false); return result; From 20e8ab380ec450a7bb7df2b6a05b80cad3e76dfe Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 4 Jan 2025 20:36:25 +0100 Subject: [PATCH 09/10] no template --- lib/findtoken.cpp | 2 -- lib/findtoken.h | 11 ----------- 2 files changed, 13 deletions(-) diff --git a/lib/findtoken.cpp b/lib/findtoken.cpp index af17608844f..5e1be685190 100644 --- a/lib/findtoken.cpp +++ b/lib/findtoken.cpp @@ -119,7 +119,6 @@ static bool findTokensSkipDeadCodeImpl(const Library& library, } namespace internal { - template<> bool findTokensSkipDeadCodeImpl(const Library& library, Token* start, const Token* end, @@ -131,7 +130,6 @@ namespace internal { return ::findTokensSkipDeadCodeImpl(library, start, end, pred, found, evaluate, skipUnevaluated); } - template<> bool findTokensSkipDeadCodeImpl(const Library& library, const Token* start, const Token* end, diff --git a/lib/findtoken.h b/lib/findtoken.h index c9a6c5c89b6..d6ad786440d 100644 --- a/lib/findtoken.h +++ b/lib/findtoken.h @@ -75,16 +75,6 @@ T* findToken(T* start, const Token* end, const Predicate& pred) } namespace internal { - template - bool findTokensSkipDeadCodeImpl(const Library &library, - T *start, - const Token *end, - const std::function &pred, - const std::function& found, - const std::function(const Token *)> &evaluate, - bool skipUnevaluated) = delete; - - template<> bool findTokensSkipDeadCodeImpl(const Library &library, Token *start, const Token *end, @@ -93,7 +83,6 @@ namespace internal { const std::function(const Token *)> &evaluate, bool skipUnevaluated); - template<> bool findTokensSkipDeadCodeImpl(const Library &library, const Token *start, const Token *end, From 7878241654c409133e27a00c9ffbc545edca51aa Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 4 Jan 2025 20:43:13 +0100 Subject: [PATCH 10/10] found --- lib/findtoken.h | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/lib/findtoken.h b/lib/findtoken.h index d6ad786440d..1927fb484b4 100644 --- a/lib/findtoken.h +++ b/lib/findtoken.h @@ -100,18 +100,15 @@ std::vector findTokensSkipDeadCode(const Library& library, const Evaluate& evaluate) { std::vector result; - - std::function f = [&](T* tok) { - result.push_back(tok); - return false; - }; - (void)internal::findTokensSkipDeadCodeImpl( library, start, end, pred, - f, + [&](T* tok) { + result.push_back(tok); + return false; + }, evaluate, false); return result; @@ -131,18 +128,15 @@ std::vector findTokensSkipDeadAndUnevaluatedCode(const Library& library, const Evaluate& evaluate) { std::vector result; - - std::function f = [&](T* tok) { - result.push_back(tok); - return false; - }; - (void)internal::findTokensSkipDeadCodeImpl( library, start, end, pred, - f, + [&](T* tok) { + result.push_back(tok); + return false; + }, evaluate, true); return result; @@ -159,19 +153,15 @@ template f = [&](T* tok) - { - result = tok; - return true; - }; - (void)internal::findTokensSkipDeadCodeImpl( library, start, end, pred, - f, + [&](T* tok) { + result = tok; + return true; + }, evaluate, false); return result;