Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3115,7 +3115,7 @@ namespace {
template<class F>
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);
}
};
}
Expand Down
2 changes: 2 additions & 0 deletions lib/cppcheck.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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 @@ -140,6 +141,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
143 changes: 143 additions & 0 deletions lib/findtoken.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#include "findtoken.h"

#include "astutils.h"
#include "token.h"

template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
static bool findTokensSkipDeadCodeImpl(const Library& library,
T* start,
const Token* end,
const std::function<bool(const Token*)>& pred,
const std::function<bool(T*)>& found,
const std::function<std::vector<MathLib::bigint>(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 (internal::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 (internal::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 (internal::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 (internal::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;
}

namespace internal {
bool findTokensSkipDeadCodeImpl(const Library& library,
Token* start,
const Token* end,
const std::function<bool(const Token*)>& pred,
const std::function<bool(Token*)>& found,
const std::function<std::vector<MathLib::bigint>(const Token*)>& evaluate,
bool skipUnevaluated)
{
return ::findTokensSkipDeadCodeImpl(library, start, end, pred, found, evaluate, skipUnevaluated);
}

bool findTokensSkipDeadCodeImpl(const Library& library,
const Token* start,
const Token* end,
const std::function<bool(const Token*)>& pred,
const std::function<bool(const Token*)>& found,
const std::function<std::vector<MathLib::bigint>(const Token*)>& evaluate,
bool skipUnevaluated)
{
return ::findTokensSkipDeadCodeImpl(library, start, end, pred, found, evaluate, skipUnevaluated);
}
}
121 changes: 19 additions & 102 deletions lib/findtoken.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,105 +74,22 @@ T* findToken(T* start, const Token* end, const Predicate& pred)
return result;
}

template<class T,
class Predicate,
class Found,
class Evaluate,
REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
bool findTokensSkipDeadCodeImpl(const Library& library,
T* start,
const Token* end,
const Predicate& pred,
Found found,
const Evaluate& 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;
namespace internal {
bool findTokensSkipDeadCodeImpl(const Library &library,
Token *start,
const Token *end,
const std::function<bool(const Token *)> &pred,
const std::function<bool(Token *)>& found,
const std::function<std::vector<MathLib::bigint>(const Token *)> &evaluate,
bool skipUnevaluated);

bool findTokensSkipDeadCodeImpl(const Library &library,
const Token *start,
const Token *end,
const std::function<bool(const Token *)> &pred,
const std::function<bool(const Token *)>& found,
const std::function<std::vector<MathLib::bigint>(const Token *)> &evaluate,
bool skipUnevaluated);
}

template<class T, class Predicate, class Evaluate, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
Expand All @@ -183,7 +100,7 @@ std::vector<T*> findTokensSkipDeadCode(const Library& library,
const Evaluate& evaluate)
{
std::vector<T*> result;
(void)findTokensSkipDeadCodeImpl(
(void)internal::findTokensSkipDeadCodeImpl(
library,
start,
end,
Expand Down Expand Up @@ -211,7 +128,7 @@ std::vector<T*> findTokensSkipDeadAndUnevaluatedCode(const Library& library,
const Evaluate& evaluate)
{
std::vector<T*> result;
(void)findTokensSkipDeadCodeImpl(
(void)internal::findTokensSkipDeadCodeImpl(
library,
start,
end,
Expand All @@ -236,7 +153,7 @@ template<class T, class Predicate, class Evaluate, REQUIRES("T must be a Token c
T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred, const Evaluate& evaluate)
{
T* result = nullptr;
(void)findTokensSkipDeadCodeImpl(
(void)internal::findTokensSkipDeadCodeImpl(
library,
start,
end,
Expand Down
4 changes: 4 additions & 0 deletions oss-fuzz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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

Expand Down
Loading