Skip to content

Commit

Permalink
Fix S1854 FP: Throw should visit finally
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-marichal committed Jul 18, 2024
1 parent a9af306 commit 071ba89
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ private void BuildBranches(BasicBlock block)
if (block.IsEnclosedIn(ControlFlowRegionKind.Catch) && block.Successors.Any(x => x.Semantics == ControlFlowBranchSemantics.Rethrow))
{
BuildBranchesNestedCatchRethrow(block);
BuildBranchesRethrowFinally(block);
}

void AddPredecessorsOutsideRegion(BasicBlock destination)
Expand Down Expand Up @@ -174,6 +175,20 @@ private void BuildBranchesNestedCatchRethrow(BasicBlock block)
}
}

private void BuildBranchesRethrowFinally(BasicBlock block)
{
var tryRegion = block.EnclosingRegion(ControlFlowRegionKind.TryAndCatch).NestedRegion(ControlFlowRegionKind.Try);
var finallyOrCatchBlock = tryRegion.ReachableHandlers()
.Where(x => x.Kind is ControlFlowRegionKind.Catch or ControlFlowRegionKind.Finally && x.FirstBlockOrdinal > block.Ordinal)
.SelectMany(x => x.Blocks(Cfg))
.FirstOrDefault();

if (finallyOrCatchBlock is not null)
{
AddBranch(block, finallyOrCatchBlock);
}
}

private void AddBranch(BasicBlock source, BasicBlock destination)
{
blockSuccessors[source.Ordinal].Add(destination);
Expand Down
Loading

0 comments on commit 071ba89

Please sign in to comment.