Skip to content

Commit 3194986

Browse files
committed
cleanup
1 parent 2942c0c commit 3194986

File tree

3 files changed

+71
-61
lines changed

3 files changed

+71
-61
lines changed

src/coverlet.core/Symbols/CecilSymbolHelper.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ private static bool IsCompilerGenerated(FieldDefinition fieldDefinition)
4545
return fieldDefinition.DeclaringType.CustomAttributes.Any(ca => ca.AttributeType.FullName == typeof(CompilerGeneratedAttribute).FullName);
4646
}
4747

48+
private static bool IsCompilerGeneratedField(Instruction instruction, out FieldDefinition field)
49+
{
50+
switch (instruction.Operand)
51+
{
52+
case FieldDefinition fieldDefinition when IsCompilerGenerated(fieldDefinition):
53+
field = fieldDefinition;
54+
return true;
55+
case FieldReference fieldReference when IsCompilerGenerated(fieldReference.Resolve()):
56+
field = fieldReference.Resolve();
57+
return true;
58+
default:
59+
field = null;
60+
return false;
61+
}
62+
}
63+
4864
private static bool IsMoveNextInsideAsyncStateMachine(MethodDefinition methodDefinition)
4965
{
5066
if (methodDefinition.FullName.EndsWith("::MoveNext()") && IsCompilerGenerated(methodDefinition))
@@ -707,25 +723,18 @@ static bool CheckForSkipDisposal(List<Instruction> instructions, Instruction ins
707723
}
708724

709725
bool isFollowedByDisposeAsync = false;
710-
FieldDefinition field = default;
711726

712727
if (instructions[currentIndex - 1].OpCode == OpCodes.Ldfld)
713728
{
714-
if (instructions[currentIndex - 1].Operand is FieldDefinition fieldDef && IsCompilerGenerated(fieldDef))
715-
field = fieldDef;
716-
else if (instructions[currentIndex - 1].Operand is FieldReference fieldRef && IsCompilerGenerated(fieldRef.Resolve()))
717-
field = fieldRef.Resolve();
729+
if(! IsCompilerGeneratedField(instructions[currentIndex - 1], out FieldDefinition field)) return false;
718730

719-
if (field == null) return false;
720-
721731
int maxReloadFieldIndex = Math.Min(currentIndex + 2, instructions.Count - 2);
722732

723733
for (int i = currentIndex + 1; i <= maxReloadFieldIndex; ++i)
724734
{
725735
if (instructions[i].OpCode == OpCodes.Ldfld &&
726-
((instructions[i].Operand is FieldDefinition reloadedField && field.Equals(reloadedField)) ||
727-
(instructions[i].Operand is FieldReference reloadedFieldRef && field.Equals(reloadedFieldRef.Resolve())))
728-
&&
736+
IsCompilerGeneratedField(instructions[i], out FieldDefinition reloadedField) &&
737+
field.Equals(reloadedField) &&
729738
instructions[i + 1].OpCode == OpCodes.Callvirt &&
730739
instructions[i + 1].Operand is MethodReference method &&
731740
method.DeclaringType.FullName == "System.IAsyncDisposable" &&
@@ -765,8 +774,7 @@ static bool CheckForSkipDisposal(List<Instruction> instructions, Instruction ins
765774
if ((instructions[i].OpCode == OpCodes.Leave ||
766775
instructions[i].OpCode == OpCodes.Leave_S) &&
767776
instructions[i - 1].OpCode == OpCodes.Stfld &&
768-
((instructions[i - 1].Operand is FieldDefinition storeField && IsCompilerGenerated(storeField)) ||
769-
(instructions[i - 1].Operand is FieldReference storeFieldRef && IsCompilerGenerated(storeFieldRef.Resolve())))
777+
IsCompilerGeneratedField(instructions[i - 1], out FieldDefinition _)
770778
)
771779
{
772780
return true;
@@ -819,10 +827,7 @@ static bool CheckForCleanup(List<Instruction> instructions, Instruction instruct
819827

820828
for (int i = currentIndex - 2; i >= minLoadFieldIndex; --i)
821829
{
822-
if (instructions[i].OpCode == OpCodes.Ldfld &&
823-
((instructions[i].Operand is FieldDefinition loadedField && IsCompilerGenerated(loadedField)) ||
824-
(instructions[i].Operand is FieldReference loadedFieldRef && IsCompilerGenerated(loadedFieldRef.Resolve())))
825-
)
830+
if (instructions[i].OpCode == OpCodes.Ldfld && IsCompilerGeneratedField(instructions[i], out FieldDefinition _))
826831
{
827832
int minRethrowIndex = Math.Max(0, i - 4);
828833

test/coverlet.core.tests/Coverage/CoverageTests.AwaitUsing.cs

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,41 @@ namespace Coverlet.Core.Tests
1111
{
1212
public partial class CoverageTests
1313
{
14-
//[Fact]
15-
//public void AwaitUsing()
16-
//{
17-
// string path = Path.GetTempFileName();
18-
// try
19-
// {
20-
// FunctionExecutor.Run(async (string[] pathSerialize) =>
21-
// {
22-
// CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<AwaitUsing>(async instance =>
23-
// {
24-
// await (ValueTask)instance.HasAwaitUsing();
25-
// await (Task)instance.Issue914_Repro();
26-
27-
// }, persistPrepareResultToFile: pathSerialize[0]);
28-
// return 0;
29-
// }, new string[] { path });
30-
31-
// TestInstrumentationHelper.GetCoverageResult(path)
32-
// .Document("Instrumentation.AwaitUsing.cs")
33-
// .AssertLinesCovered(BuildConfiguration.Debug,
34-
// // HasAwaitUsing()
35-
// (13, 1), (14, 1), (15, 1), (16, 1), (17, 1),
36-
// // Issue914_Repro()
37-
// (21, 1), (22, 1), (23, 1), (24, 1),
38-
// // Issue914_Repro_Example1()
39-
// (28, 1), (29, 1), (30, 1),
40-
// // Issue914_Repro_Example2()
41-
// (34, 1), (35, 1), (36, 1), (37, 1),
42-
// // MyTransaction.DisposeAsync()
43-
// (43, 2), (44, 2), (45, 2)
44-
// )
45-
// .ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 0);
46-
// }
47-
// finally
48-
// {
49-
// File.Delete(path);
50-
// }
51-
//}
52-
5314
[Fact]
54-
public void AwaitUsingGeneric()
15+
public void AwaitUsing()
5516
{
5617
string path = Path.GetTempFileName();
5718
try
5819
{
5920
FunctionExecutor.RunInProcess(async (string[] pathSerialize) =>
6021
{
6122
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<AwaitUsing>(async instance =>
62-
{
63-
await (Task)instance.Issue1490_Repro<string>();
23+
{
24+
await (ValueTask)instance.HasAwaitUsing();
25+
await (Task)instance.Issue914_Repro();
26+
await (Task)instance.Issue1490_Repro<string>();
6427

65-
}, persistPrepareResultToFile: pathSerialize[0]);
28+
}, persistPrepareResultToFile: pathSerialize[0]);
6629
return 0;
6730
}, new string[] { path });
6831

69-
TestInstrumentationHelper.GetCoverageResult(path).GenerateReport(show: true);
32+
TestInstrumentationHelper.GetCoverageResult(path)
33+
.Document("Instrumentation.AwaitUsing.cs")
34+
.AssertLinesCovered(BuildConfiguration.Debug,
35+
// HasAwaitUsing()
36+
(13, 1), (14, 1), (15, 1), (16, 1), (17, 1),
37+
// Issue914_Repro()
38+
(21, 1), (22, 1), (23, 1), (24, 1),
39+
// Issue914_Repro_Example1()
40+
(28, 1), (29, 1), (30, 1),
41+
// Issue914_Repro_Example2()
42+
(34, 1), (35, 1), (36, 1), (37, 1),
43+
// Issue1490_Repro<T>()
44+
(40, 1), (41, 1), (42, 1), (43, 1),
45+
// MyTransaction.DisposeAsync()
46+
(48, 3), (49, 3), (50, 3)
47+
)
48+
.ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 0);
7049
}
7150
finally
7251
{

test/coverlet.core.tests/Samples/Instrumentation.AwaitUsing.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,39 @@ namespace Coverlet.Core.Samples.Tests
99
{
1010
public class AwaitUsing
1111
{
12+
async public ValueTask HasAwaitUsing()
13+
{
14+
await using (var ms = new MemoryStream(Encoding.ASCII.GetBytes("Boo")))
15+
{
16+
}
17+
}
18+
19+
20+
async public Task Issue914_Repro()
21+
{
22+
await Issue914_Repro_Example1();
23+
await Issue914_Repro_Example2();
24+
}
25+
26+
27+
async private Task Issue914_Repro_Example1()
28+
{
29+
await using var transaction = new MyTransaction();
30+
}
31+
32+
33+
async private Task Issue914_Repro_Example2()
34+
{
35+
var transaction = new MyTransaction();
36+
await transaction.DisposeAsync();
37+
}
38+
1239
async public Task<T> Issue1490_Repro<T>()
1340
{
1441
await using var transaction = new MyTransaction();
1542
return default(T);
1643
}
1744

18-
1945
private class MyTransaction : IAsyncDisposable
2046
{
2147
public async ValueTask DisposeAsync()

0 commit comments

Comments
 (0)