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
65 changes: 49 additions & 16 deletions lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ namespace {

template<class T, class F, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
Progress traverseConditional(T* tok, F f, bool traverseUnknown) {
if (Token::Match(tok, "?|&&|%oror%") && tok->astOperand1() && tok->astOperand2()) {
Analyzer::Action action = analyzer->analyze(tok, Analyzer::Direction::Forward);
if (action.isNone() && Token::Match(tok, "?|&&|%oror%") && tok->astOperand1() && tok->astOperand2()) {
const T* condTok = tok->astOperand1();
T* childTok = tok->astOperand2();
bool checkThen, checkElse;
Expand All @@ -227,12 +228,19 @@ namespace {
if (traverseRecursive(childTok, f, traverseUnknown) == Progress::Break)
return Break();
}
} else {
return f(tok, action);
}
return Progress::Continue;
}

Progress update(Token* tok) {
Analyzer::Action action = analyzer->analyze(tok, Analyzer::Direction::Forward);
return update(tok, action);
}

Progress update(Token* tok, Analyzer::Action action)
{
actions |= action;
if (!action.isNone() && !analyzeOnly)
analyzer->update(tok, action, Analyzer::Direction::Forward);
Expand All @@ -246,29 +254,54 @@ namespace {
return Progress::Continue;
}

struct AsUpdate {
ForwardTraversal* self = nullptr;

explicit AsUpdate(ForwardTraversal* self) : self(self) {}

template<class... Ts>
Progress operator()(Ts... xs) const
{
assert(self);
return self->update(xs ...);
}
};

Progress updateTok(Token* tok, Token** out = nullptr) {
auto f = [this](Token* tok2) {
return update(tok2);
};
return traverseTok(tok, f, false, out);
return traverseTok(tok, AsUpdate{this}, false, out);
}

Progress updateRecursive(Token* tok) {
auto f = [this](Token* tok2) {
return update(tok2);
};
return traverseRecursive(tok, f, false);
return traverseRecursive(tok, AsUpdate{this}, false);
}

struct AsAnalyze {
ForwardTraversal* self = nullptr;
Analyzer::Action* result = nullptr;

AsAnalyze(ForwardTraversal* self, Analyzer::Action* result) : self(self), result(result) {}

Progress operator()(const Token* tok) const
{
assert(self);
assert(result);
return (*this)(tok, self->analyzer->analyze(tok, Analyzer::Direction::Forward));
}

Progress operator()(const Token* /*unused*/, Analyzer::Action action) const
{
assert(self);
assert(result);
*result = action;
if (result->isModified() || result->isInconclusive())
return self->Break();
return Progress::Continue;
}
};

Analyzer::Action analyzeRecursive(const Token* start) {
Analyzer::Action result = Analyzer::Action::None;
auto f = [&](const Token* tok) {
result = analyzer->analyze(tok, Analyzer::Direction::Forward);
if (result.isModified() || result.isInconclusive())
return Break();
return Progress::Continue;
};
traverseRecursive(start, f, true);
traverseRecursive(start, AsAnalyze{this, &result}, true);
return result;
}

Expand Down
8 changes: 8 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6364,6 +6364,14 @@ class TestValueFlow : public TestFixture {
" return false;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 6U, 0));

code = "bool f(bool b1, bool b2) {\n"
" if (b1 && b2)\n"
" return;\n"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we should return a value here, e.g. return true;

" int x = b1 && b2;\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 5U, 0));
}

static std::string isPossibleContainerSizeValue(std::list<ValueFlow::Value> values,
Expand Down
Loading