Skip to content

Commit 97e6f3e

Browse files
authored
Merge pull request #108 from tonerdo/exclude-compiler-generated
Exclude compiler generated code
2 parents 883d1f7 + e7e407c commit 97e6f3e

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/coverlet.core/Attributes/ExcludeFromCoverage.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace Coverlet.Core.Attributes
44
{
5-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Constructor)]
5+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class)]
66
public sealed class ExcludeFromCoverageAttribute : Attribute { }
77
}

src/coverlet.core/Helpers/InstrumentationHelper.cs

+3
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ public static bool IsTypeExcluded(string module, string type, string[] filters)
165165
return false;
166166
}
167167

168+
public static bool IsLocalMethod(string method)
169+
=> new Regex(WildcardToRegex("<*>*__*|*")).IsMatch(method);
170+
168171
public static string[] GetExcludedFiles(string[] rules)
169172
{
170173
const string RELATIVE_KEY = nameof(RELATIVE_KEY);

src/coverlet.core/Instrumentation/Instrumenter.cs

+19-10
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,19 @@ private void InstrumentModule()
6060
{
6161
resolver.AddSearchDirectory(Path.GetDirectoryName(_module));
6262
var parameters = new ReaderParameters { ReadSymbols = true, AssemblyResolver = resolver };
63+
6364
using (var module = ModuleDefinition.ReadModule(stream, parameters))
6465
{
65-
foreach (var type in module.GetTypes())
66+
var types = module.GetTypes();
67+
foreach (var type in types)
6668
{
67-
InstrumentType(type);
69+
TypeDefinition actualType = type;
70+
if (type.FullName.Contains("/"))
71+
actualType = types.FirstOrDefault(t => t.FullName == type.FullName.Split('/')[0]);
72+
73+
if (!actualType.CustomAttributes.Any(IsExcludeAttribute)
74+
&& !InstrumentationHelper.IsTypeExcluded(_module, actualType.FullName, _filters))
75+
InstrumentType(type);
6876
}
6977

7078
module.Write(stream);
@@ -74,13 +82,14 @@ private void InstrumentModule()
7482

7583
private void InstrumentType(TypeDefinition type)
7684
{
77-
if (type.CustomAttributes.Any(IsExcludeAttribute)
78-
|| InstrumentationHelper.IsTypeExcluded(_module, type.FullName, _filters))
79-
return;
80-
81-
foreach (var method in type.Methods)
85+
var methods = type.GetMethods();
86+
foreach (var method in methods)
8287
{
83-
if (!method.CustomAttributes.Any(IsExcludeAttribute))
88+
MethodDefinition actualMethod = method;
89+
if (InstrumentationHelper.IsLocalMethod(method.Name))
90+
actualMethod = methods.FirstOrDefault(m => m.Name == method.Name.Split('>')[0].Substring(1)) ?? method;
91+
92+
if (!actualMethod.CustomAttributes.Any(IsExcludeAttribute))
8493
InstrumentMethod(method);
8594
}
8695
}
@@ -116,7 +125,7 @@ private void InstrumentIL(MethodDefinition method)
116125
var instruction = processor.Body.Instructions[index];
117126
var sequencePoint = method.DebugInformation.GetSequencePoint(instruction);
118127
var targetedBranchPoints = branchPoints.Where(p => p.EndOffset == instruction.Offset);
119-
128+
120129
if (sequencePoint != null && !sequencePoint.IsHidden)
121130
{
122131
var target = AddInstrumentationCode(method, processor, instruction, sequencePoint);
@@ -139,7 +148,7 @@ private void InstrumentIL(MethodDefinition method)
139148
*/
140149
if (_branchTarget.StartLine == -1 || _branchTarget.Document == null)
141150
continue;
142-
151+
143152
var target = AddInstrumentationCode(method, processor, instruction, _branchTarget);
144153
foreach (var _instruction in processor.Body.Instructions)
145154
ReplaceInstructionTarget(_instruction, instruction, target);

0 commit comments

Comments
 (0)