@@ -65,6 +65,38 @@ namespace {
6565 // Total forks of the traversal (shared via the fork() copy); backstop past the depth bound.
6666 std::shared_ptr<int > forkBudget = std::make_shared<int >(0 );
6767
68+ // The paths that left this traversal without escaping: forks carried past their if and
69+ // then-paths that are not followed further.
70+ struct OtherPaths {
71+ Analyzer::Result result;
72+ bool exist = false ;
73+ void add (const Analyzer::Result& r) {
74+ if (r.terminate == Analyzer::Terminate::Escape)
75+ return ;
76+ result.update (r);
77+ exist = true ;
78+ }
79+ void add (const OtherPaths& paths) {
80+ if (paths.exist )
81+ add (paths.result );
82+ }
83+ };
84+ OtherPaths otherPaths;
85+
86+ // The result over every path to end, not only the path this traversal follows
87+ Analyzer::Result result (const Token* end) const {
88+ Analyzer::Result r{actions, terminate};
89+ if (!otherPaths.exist )
90+ return r;
91+ if (terminate == Analyzer::Terminate::Escape) {
92+ if (isReturnScope (end, settings ().library ))
93+ return r;
94+ r.terminate = otherPaths.result .terminate ;
95+ }
96+ r.action |= otherPaths.result .action ;
97+ return r;
98+ }
99+
68100 Progress Break (Analyzer::Terminate t = Analyzer::Terminate::None) {
69101 if ((!analyzeOnly || analyzeTerminate) && t != Analyzer::Terminate::None)
70102 terminate = t;
@@ -334,6 +366,7 @@ namespace {
334366 ft.analyzeTerminate = true ;
335367 }
336368 ft.actions = Analyzer::Action::None;
369+ ft.otherPaths = OtherPaths{};
337370 return ft;
338371 }
339372
@@ -407,6 +440,8 @@ namespace {
407440 // Save and reset actions
408441 Analyzer::Action prevActions = actions;
409442 actions = Analyzer::Action::None;
443+ OtherPaths prevPaths = otherPaths;
444+ otherPaths = OtherPaths{};
410445 Progress p = updateRange (branch.endBlock ->link (), branch.endBlock , depth);
411446 branch.action |= actions;
412447 // Restore actions
@@ -434,6 +469,14 @@ namespace {
434469 }
435470 }
436471
472+ // The other paths escape as well when the scope always escapes
473+ if (!branch.isConclusiveEscape ()) {
474+ if (otherPaths.exist )
475+ branch.action |= otherPaths.result .action ;
476+ prevPaths.add (otherPaths);
477+ }
478+ otherPaths = prevPaths;
479+
437480 return p;
438481 }
439482
@@ -838,6 +881,9 @@ namespace {
838881 ++ft.forkDepth ;
839882 ft.analyzer ->assume (condTok, true , Analyzer::Assume::Quiet | Analyzer::Assume::Absolute);
840883 ft.updateRange (tok->next (), end, depth - 1 );
884+ otherPaths.add (ft.result (end));
885+ } else if (!thenBranch.isConclusiveEscape ()) {
886+ otherPaths.add (ft.result (thenBranch.endBlock ));
841887 }
842888 if (elseBranch.progress == Progress::Break) {
843889 // Only the else branch escaped; the then branch falls through, so
@@ -995,7 +1041,7 @@ Analyzer::Result valueFlowGenericForward(Token* start, const Token* end, const V
9951041 if (start)
9961042 ft.analyzer ->updateState (start);
9971043 ft.updateRange (start, end);
998- return Analyzer::Result{ ft.actions , ft. terminate } ;
1044+ return ft.result (end) ;
9991045}
10001046
10011047Analyzer::Result valueFlowGenericForward (Token* start, const ValuePtr<Analyzer>& a, const TokenList& tokenList, ErrorLogger& errorLogger, const Settings& settings)
0 commit comments