4040#include < functional>
4141#include < list>
4242#include < memory>
43+ #include < numeric>
4344#include < string>
4445#include < tuple>
4546#include < type_traits>
@@ -155,6 +156,10 @@ namespace {
155156 bool escapeUnknown = false ;
156157 // The path that walked the block is at its end
157158 bool reachesEnd = true ;
159+ // The fork that walked the block, and the condition that holds on it. Without a fork
160+ // this traversal walked the block.
161+ std::shared_ptr<ForwardTraversal> path;
162+ const Token* condTok = nullptr ;
158163 bool isEscape () const {
159164 return escape || escapeUnknown;
160165 }
@@ -424,12 +429,13 @@ namespace {
424429 return (depthLimit < 0 || forkDepth < depthLimit) && (budgetLimit < 0 || *forkBudget < budgetLimit);
425430 }
426431
427- // A value changed in either branch is only possible past the if , on both paths . False if
428- // it cannot be lowered.
429- static bool lowerAfterBranches (Analyzer& a, const Branch& thenBranch, const Branch& elseBranch ) {
430- if ((thenBranch. isModified () || elseBranch. isModified ( )) && !a.lowerToPossible ())
432+ // A value changed in one of the branches is only possible past them , on every path . False
433+ // if it cannot be lowered.
434+ static bool lowerAfterBranches (Analyzer& a, const std::vector< Branch>& branches ) {
435+ if (std::any_of (branches. cbegin (), branches. cend (), std::mem_fn (&Branch::isModified )) && !a.lowerToPossible ())
431436 return false ;
432- if ((thenBranch.isInconclusive () || elseBranch.isInconclusive ()) && !a.lowerToInconclusive ())
437+ if (std::any_of (branches.cbegin (), branches.cend (), std::mem_fn (&Branch::isInconclusive)) &&
438+ !a.lowerToInconclusive ())
433439 return false ;
434440 return true ;
435441 }
@@ -576,30 +582,32 @@ namespace {
576582 }
577583
578584 // Neither outcome of the condition is known: the then block is walked on a fork, the else
579- // block on this traversal. split: the then path goes on to the end of the range on its own.
580- // Otherwise one path goes on past the if for both.
585+ // block on this traversal.
581586 Progress updateBothBranches (const Token* condTok, Token* thenEnd, Token* elseEnd, const Token* end, int depth, bool split)
582587 {
583588 const bool conditional = stopOnCondition (condTok);
584- const int outerBranchCount = branchCount;
585589 Token* ifEnd = elseEnd ? elseEnd : thenEnd;
586590 // The value only flows into the then block when the condition can split it; for an opaque
587591 // or correlated condition (e.g. 'if (f(x))') it does not, so the fork is analyze-only: the
588592 // block's effect is still tracked but nothing is reported in it.
589- ForwardTraversal ft = fork (!analyzer->updateScope (thenEnd, false ));
590- ft.actions = actions;
593+ const std::shared_ptr<ForwardTraversal> ft =
594+ std::make_shared<ForwardTraversal>(fork (!analyzer->updateScope (thenEnd, false )));
595+ ft->actions = actions;
591596 if (split) {
592597 ++(*forkBudget);
593- ++ft. forkDepth ;
598+ ++ft-> forkDepth ;
594599 }
595- ft.analyzer ->assume (condTok, true , Analyzer::Assume::Pending);
596- const Branch thenBranch = ft.updateBranch (thenEnd, depth - 1 );
597- actions |= thenBranch.action ;
600+ ft->analyzer ->assume (condTok, true , Analyzer::Assume::Pending);
601+ Branch thenBranch = ft->updateBranch (thenEnd, depth - 1 );
602+ thenBranch.path = ft;
603+ thenBranch.condTok = condTok;
604+ // The branch limit counts the branches on the way past the if, as it does for this path
605+ ft->branchCount = branchCount;
598606 // A path that stopped in the then block is covered if this path goes on for it
599- Outcome thenStops = std::move (ft. parted );
600- ft. parted = Outcome{};
607+ Outcome thenStops = std::move (ft-> parted );
608+ ft-> parted = Outcome{};
601609 if (!thenBranch.reachesEnd )
602- thenStops.stop (ft. actions , ft. terminate );
610+ thenStops.stop (ft-> actions , ft-> terminate );
603611 parted.add (thenStops, !split);
604612
605613 // The condition is an assumption on this path if the then path drops the value. If the
@@ -611,7 +619,7 @@ namespace {
611619 bool assumed = true ;
612620 if (thenBranch.isDead ())
613621 analyzer->assume (condTok, false , Analyzer::Assume::None | pending);
614- else if (thenBranch.reachesEnd && (split || ft. isConditionFalse (condTok, thenEnd->previous ())))
622+ else if (thenBranch.reachesEnd && (split || ft-> isConditionFalse (condTok, thenEnd->previous ())))
615623 analyzer->assume (condTok, false , Analyzer::Assume::Quiet | Analyzer::Assume::Absolute | pending);
616624 else
617625 assumed = false ;
@@ -627,39 +635,60 @@ namespace {
627635 // at an earlier token, and every condition evaluated later looks for changes from there.
628636 analyzer->updateState (ifEnd->next ());
629637 }
638+ return updatePastBranches ({thenBranch, elseBranch}, conditional, ifEnd->next (), end, depth, split);
639+ }
630640
631- const bool jumps = thenBranch.hasGoto () || elseBranch.hasGoto ();
632- Progress progress = elseBranch.reachesEnd ? Progress::Continue : Progress::Break;
633- if (progress != Progress::Break) {
634- if ((thenBranch.isDead () || elseBranch.isDead ()) && conditional && stopUpdates ())
641+ // The blocks of a branching statement were walked, each on its fork or on this traversal.
642+ // split: every path goes on to the end of the range on its own. Otherwise one path goes on
643+ // past the statement for all of them.
644+ Progress updatePastBranches (const std::vector<Branch>& branches, bool conditional, Token* next, const Token* end, int depth, bool split)
645+ {
646+ const Analyzer::Action action =
647+ std::accumulate (branches.cbegin (), branches.cend (), Analyzer::Action{}, [](Analyzer::Action a, const Branch& branch) {
648+ return a | branch.action ;
649+ });
650+ actions |= action;
651+ const bool jumps = std::any_of (branches.cbegin (), branches.cend (), std::mem_fn (&Branch::hasGoto));
652+ const bool reachesEnd = std::all_of (branches.cbegin (), branches.cend (), [](const Branch& branch) {
653+ return branch.path || branch.reachesEnd ;
654+ });
655+ Progress progress = reachesEnd ? Progress::Continue : Progress::Break;
656+ if (reachesEnd) {
657+ if (std::any_of (branches.cbegin (), branches.cend (), std::mem_fn (&Branch::isDead)) && conditional && stopUpdates ())
635658 progress = Break (Analyzer::Terminate::Conditional);
636- else if (!lowerAfterBranches (*analyzer, thenBranch, elseBranch ) || jumps)
659+ else if (!lowerAfterBranches (*analyzer, branches ) || jumps)
637660 progress = Break (Analyzer::Terminate::Bail);
638661 }
639- if (!thenBranch.reachesEnd )
640- return progress;
641- // Without a split this path goes on for the then path. If it stopped, the then path
642- // goes on in its place.
643- const bool thenGoesOn = split || progress == Progress::Break;
644- ft.actions |= elseBranch.action ;
645- if (jumps) {
646- stopFork (ft, Analyzer::Terminate::Bail);
647- } else if (thenBranch.isEscape ()) {
648- stopFork (ft, Analyzer::Terminate::Escape);
649- } else if (thenGoesOn && !lowerAfterBranches (*ft.analyzer , thenBranch, elseBranch)) {
650- stopFork (ft);
651- } else if (thenGoesOn) {
652- // The branch limit counts the branches on the way past the if, as it does for this path
653- ft.branchCount = outerBranchCount;
654- // Anchor the state (and the block's assignments) at the block end
655- ft.analyzer ->assume (condTok, true , Analyzer::Assume::Quiet | Analyzer::Assume::Absolute);
656- if (split) {
657- parted.join (ft.updatePaths (ifEnd->next (), end, depth - 1 ));
658- } else {
659- replaceWith (std::move (ft));
660- progress = Progress::Continue;
662+ // Without a split this path goes on for the forks. If it stopped, a fork goes on in its place.
663+ const bool forksGoOn = split || progress == Progress::Break;
664+ std::vector<ForwardTraversal*> successors;
665+ for (const Branch& branch : branches) {
666+ if (!branch.path || !branch.reachesEnd )
667+ continue ;
668+ ForwardTraversal& ft = *branch.path ;
669+ ft.actions |= action;
670+ if (jumps) {
671+ stopFork (ft, Analyzer::Terminate::Bail);
672+ } else if (branch.isEscape ()) {
673+ stopFork (ft, Analyzer::Terminate::Escape);
674+ } else if (forksGoOn && !lowerAfterBranches (*ft.analyzer , branches)) {
675+ stopFork (ft);
676+ } else if (forksGoOn) {
677+ // Anchor the state (and the block's assignments) at the block end
678+ ft.analyzer ->assume (branch.condTok , true , Analyzer::Assume::Quiet | Analyzer::Assume::Absolute);
679+ if (split)
680+ parted.join (ft.updatePaths (next, end, depth - 1 ));
681+ else
682+ successors.push_back (&ft);
661683 }
662684 }
685+ // Each fork holds its own condition, so one of several cannot go on for the others
686+ if (successors.size () == 1 ) {
687+ replaceWith (std::move (*successors.front ()));
688+ return Progress::Continue;
689+ }
690+ for (ForwardTraversal* ft : successors)
691+ stopFork (*ft, Analyzer::Terminate::Bail);
663692 return progress;
664693 }
665694
0 commit comments