Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/coverlet.core/Symbols/CecilSymbolHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public static List<BranchPoint> GetBranchPoints(MethodDefinition methodDefinitio
continue;
}

if (BranchIsInGeneratedExceptionFilter(instruction, methodDefinition))
continue;

if (BranchIsInGeneratedFinallyBlock(instruction, methodDefinition))
continue;

Expand Down Expand Up @@ -197,6 +200,35 @@ private static uint BuildPointsForSwitchCases(List<BranchPoint> list, BranchPoin
return ordinal;
}

private static bool BranchIsInGeneratedExceptionFilter(Instruction branchInstruction, MethodDefinition methodDefinition)
{
if (!methodDefinition.Body.HasExceptionHandlers)
return false;

// a generated filter block will have no sequence points in its range
var handlers = methodDefinition.Body.ExceptionHandlers
.Where(e => e.HandlerType == ExceptionHandlerType.Filter)
.ToList();

foreach (var exceptionHandler in handlers)
{
Instruction startFilter = exceptionHandler.FilterStart;
Instruction endFilter = startFilter;

while(endFilter.OpCode != OpCodes.Endfilter && endFilter != null)
{
endFilter = endFilter.Next;
}

if(branchInstruction.Offset >= startFilter.Offset && branchInstruction.Offset <= endFilter.Offset)
{
return true;
}
}

return false;
}

private static bool BranchIsInGeneratedFinallyBlock(Instruction branchInstruction, MethodDefinition methodDefinition)
{
if (!methodDefinition.Body.HasExceptionHandlers)
Expand Down
27 changes: 27 additions & 0 deletions test/coverlet.core.tests/Samples/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,31 @@ public string Method(string input)
return input;
}
}

public class ExceptionFilter
{
public void Test()
{
try
{
int a = 0;
int b = 1;
int c = b / a;
}
catch (Exception ex) when (True() && False())
{
Console.WriteLine(ex.Message);
}
}

public bool True()
{
return true;
}

public bool False()
{
return false;
}
}
}
12 changes: 12 additions & 0 deletions test/coverlet.core.tests/Symbols/CecilSymbolHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,17 @@ public void GetBranchPoints_IgnoresSwitchIn_GeneratedMoveNext()
Assert.Empty(points);

}

[Fact]
public void GetBranchPoints_ExceptionFilter()
{
// arrange
var type = _module.Types.Single(x => x.FullName == typeof(ExceptionFilter).FullName);
var method = type.Methods.Single(x => x.FullName.Contains($"::{nameof(ExceptionFilter.Test)}"));
// act
var points = CecilSymbolHelper.GetBranchPoints(method);

Assert.Empty(points);
}
}
}