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 17, 2024
1 parent a4a64cf commit 0efe47a
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 43 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.FirstOrDefault(x => x.Semantics == ControlFlowBranchSemantics.Rethrow) is { })
{
BuildBranchesNestedCatchRethrow(block);
BuildBranchesRethrowFinally(block);
}

void AddPredecessorsOutsideRegion(BasicBlock destination)
Expand Down Expand Up @@ -169,13 +170,27 @@ private void BuildBranchesNestedCatchRethrow(BasicBlock block)
{
var tryRegion = block.EnclosingRegion(ControlFlowRegionKind.TryAndCatch).NestedRegion(ControlFlowRegionKind.Try);
foreach (var catchBlock in tryRegion.ReachableHandlers()
.Where(x => x.Kind == ControlFlowRegionKind.Catch && x != block.EnclosingRegion && x.FirstBlockOrdinal > block.Ordinal)
.SelectMany(x => x.Blocks(Cfg)))
.Where(x => x.Kind == ControlFlowRegionKind.Catch && x != block.EnclosingRegion && x.FirstBlockOrdinal > block.Ordinal)
.SelectMany(x => x.Blocks(Cfg)))
{
AddBranch(block, catchBlock);
}
}

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 0efe47a

Please sign in to comment.