Skip to content

Commit 820153a

Browse files
committed
Fix FP with normal level
1 parent e328047 commit 820153a

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

‎lib/forwardanalyzer.cpp‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ namespace {
160160
ForwardTraversal* traversal = nullptr;
161161
// The condition that holds on it, if it is a fork
162162
const Token* condTok = nullptr;
163+
// The paths that stopped in the block, if it was walked on a fork
164+
Outcome stoppedPaths;
163165
bool isEscape() const {
164166
return escape || escapeUnknown;
165167
}
@@ -578,7 +580,7 @@ namespace {
578580
}
579581

580582
// Walk a block on a fork of this traversal, on which the condition holds
581-
Branch updateForkedBranch(ForwardTraversal& ft, const Token* condTok, Token* endBlock, bool split, int depth)
583+
Branch updateForkedBranch(ForwardTraversal& ft, const Token* condTok, Token* endBlock, bool split, int depth) const
582584
{
583585
ft.actions = actions;
584586
if (split) {
@@ -595,12 +597,10 @@ namespace {
595597
// left for the forks that walk to the end of the range, which are the expensive ones.
596598
if (!split && branch.reachesEnd && canFork())
597599
++(*forkBudget);
598-
// A path that stopped in the block is covered if this path goes on for it
599-
Outcome stops = std::move(ft.parted);
600+
branch.stoppedPaths = std::move(ft.parted);
600601
ft.parted = Outcome{};
601602
if (!branch.reachesEnd)
602-
stops.stop(ft.actions, ft.terminate);
603-
parted.add(stops, !split);
603+
branch.stoppedPaths.stop(ft.actions, ft.terminate);
604604
return branch;
605605
}
606606

@@ -689,7 +689,13 @@ namespace {
689689
}
690690
}
691691
// Each fork holds its own condition, so one of several cannot go on for the others
692-
if (successors.size() == 1) {
692+
const bool replaced = successors.size() == 1;
693+
// A path that stopped in a block is covered only if a path goes on for it past the
694+
// statement. Otherwise nothing walks the code that follows on its way.
695+
const bool covered = !split && (replaced || progress == Progress::Continue);
696+
for (const Branch& branch : branches)
697+
parted.add(branch.stoppedPaths, covered);
698+
if (replaced) {
693699
replaceWith(std::move(*successors.front()));
694700
return Progress::Continue;
695701
}

‎test/testvalueflow.cpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4811,6 +4811,29 @@ class TestValueFlow : public TestFixture {
48114811
" if (w) { int a = x; }\n"
48124812
"}\n";
48134813
ASSERT_EQUALS(true, testValueOfX(code, 6U, 1, &s));
4814+
4815+
// the path through 'if (c)' stops at the branch limit and the other path returns, so no
4816+
// path walks 'x = 5'
4817+
const Settings normal = settingsBuilder(settings).checkLevel(Settings::CheckLevel::normal).build();
4818+
code = "void use();\n"
4819+
"void f(bool o, bool c, bool d1, bool d2, bool d3, bool d4) {\n"
4820+
" int x = 0;\n"
4821+
" if (o) {\n"
4822+
" if (c) {\n"
4823+
" if (d1) { use(); }\n"
4824+
" if (d2) { use(); }\n"
4825+
" if (d3) { use(); }\n"
4826+
" if (d4) { use(); }\n"
4827+
" } else {\n"
4828+
" return;\n"
4829+
" }\n"
4830+
" x = 5;\n"
4831+
" }\n"
4832+
" if (x == 0) {}\n"
4833+
"}\n";
4834+
const ValueFlow::Value value = valueOfTok(code, "x ==", &normal);
4835+
ASSERT_EQUALS(0, value.intvalue);
4836+
ASSERT(value.isPossible());
48144837
}
48154838

48164839
void valueFlowForwardAfterCondition()

0 commit comments

Comments
 (0)