Skip to content

ValueFlow: various small optimizations #6756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
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
26 changes: 20 additions & 6 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ const Token* findExpression(const nonneg int exprid,
static int findArgumentPosRecursive(const Token* tok, const Token* tokToFind, bool &found, nonneg int depth=0)
{
++depth;
if (!tok || depth >= 100)
if (depth >= 100)
return -1; // TODO: add bailout message
if (!tok)
return -1;
if (tok->str() == ",") {
int res = findArgumentPosRecursive(tok->astOperand1(), tokToFind, found, depth);
Expand Down Expand Up @@ -112,8 +114,10 @@ template<class T, class OuputIterator, REQUIRES("T must be a Token class", std::
static void astFlattenCopy(T* tok, const char* op, OuputIterator out, nonneg int depth = 100)
{
--depth;
if (!tok || depth < 0)
if (!tok || depth < 0) {
// TODO: add bailout message
return;
}
if (strcmp(tok->str().c_str(), op) == 0) {
astFlattenCopy(tok->astOperand1(), op, out, depth);
astFlattenCopy(tok->astOperand2(), op, out, depth);
Expand All @@ -140,8 +144,10 @@ std::vector<Token*> astFlatten(Token* tok, const char* op)
nonneg int astCount(const Token* tok, const char* op, int depth)
{
--depth;
if (!tok || depth < 0)
if (!tok || depth < 0) {
// TODO: add bailout message
return 0;
}
if (strcmp(tok->str().c_str(), op) == 0)
return astCount(tok->astOperand1(), op, depth) + astCount(tok->astOperand2(), op, depth);
return 1;
Expand Down Expand Up @@ -1115,6 +1121,7 @@ bool exprDependsOnThis(const Token* expr, bool onVar, nonneg int depth)
return true;
if (depth >= 1000)
// Abort recursion to avoid stack overflow
// TODO: add bailout message
return true;
++depth;

Expand Down Expand Up @@ -1256,6 +1263,7 @@ SmallVector<ReferenceToken> followAllReferences(const Token* tok,
if (!tok)
return {};
if (depth < 0) {
// TODO: add bailout message
SmallVector<ReferenceToken> refs_result;
refs_result.emplace_back(tok, std::move(errors));
return refs_result;
Expand Down Expand Up @@ -2901,8 +2909,10 @@ static bool isExpressionChangedAt(const F& getExprTok,
const Settings& settings,
int depth)
{
if (depth < 0)
if (depth < 0) {
// TODO: add bailout message
return true;
}
if (!isMutableExpression(tok))
return false;
if (tok->exprId() != exprid || (!tok->varId() && !tok->isName())) {
Expand Down Expand Up @@ -2952,8 +2962,10 @@ Token* findVariableChanged(Token *start, const Token *end, int indirect, const n
{
if (!precedes(start, end))
return nullptr;
if (depth < 0)
if (depth < 0) {
// TODO: add bailout message
return start;
}
auto getExprTok = memoize([&] {
return findExpression(start, exprid);
});
Expand Down Expand Up @@ -3051,8 +3063,10 @@ static const Token* findExpressionChangedImpl(const Token* expr,
int depth,
Find find)
{
if (depth < 0)
if (depth < 0) {
// TODO: add bailout message
return start;
}
if (!precedes(start, end))
return nullptr;
const Token* result = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ static const ValueFlow::Value* getInnerLifetime(const Token* tok,
int depth = 4)
{
if (depth < 0)
return nullptr;
return nullptr;// TODO: add bailout message
if (!tok)
return nullptr;
for (const ValueFlow::Value& val : tok->values()) {
Expand Down
3 changes: 2 additions & 1 deletion lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
namespace {
struct ForwardTraversal {
enum class Progress : std::uint8_t { Continue, Break, Skip };
// TODO: analyzer is copied
ForwardTraversal(const ValuePtr<Analyzer>& analyzer, const TokenList& tokenList, ErrorLogger& errorLogger, const Settings& settings)
: analyzer(analyzer), tokenList(tokenList), errorLogger(errorLogger), settings(settings)
{}
Expand Down Expand Up @@ -545,7 +546,7 @@ namespace {

Progress updateRange(Token* start, const Token* end, int depth = 20) {
if (depth < 0)
return Break(Analyzer::Terminate::Bail);
return Break(Analyzer::Terminate::Bail); // TODO: add bailout message
std::size_t i = 0;
for (Token* tok = start; precedes(tok, end); tok = tok->next()) {
Token* next = nullptr;
Expand Down
9 changes: 5 additions & 4 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ ProgramMemory ProgramMemoryState::get(const Token* tok, const Token* ctx, const
} else {
local.removeModifiedVars(ctx);
}
return local.state;
return std::move(local.state);
}

ProgramMemory getProgramMemory(const Token* tok, const Token* expr, const ValueFlow::Value& value, const Settings& settings)
Expand Down Expand Up @@ -1305,7 +1305,7 @@ namespace {

nonneg int n = astCount(expr, expr->str().c_str());
if (n > 50)
return unknown();
return unknown(); // TODO: add bailout message
std::vector<const Token*> conditions1 = flattenConditions(expr);
if (conditions1.empty())
return unknown();
Expand All @@ -1322,7 +1322,7 @@ namespace {
}
if (condValues.size() == conditions1.size() && allNegated)
return negatedValue;
if (n > 4)
if (n > 4) // TODO: add bailout message
return unknown();
if (!sortConditions(conditions1))
return unknown();
Expand Down Expand Up @@ -1561,6 +1561,7 @@ namespace {
return execute(tok);
});
if (f) {
// TODO: add bailout message
if (fdepth >= 0 && !f->isImplicitlyVirtual()) {
ProgramMemory functionState;
for (std::size_t i = 0; i < args.size(); ++i) {
Expand Down Expand Up @@ -1649,7 +1650,7 @@ namespace {
depth++;
}};
if (depth < 0)
return unknown();
return unknown(); // TODO: add bailout message
ValueFlow::Value v = unknown();
if (updateValue(v, executeImpl(expr)))
return v;
Expand Down
Loading