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
15 changes: 13 additions & 2 deletions src/coverlet.core/Instrumentation/Instrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,8 @@ private void InstrumentType(TypeDefinition type)
{
MethodDefinition actualMethod = method;
IEnumerable<CustomAttribute> customAttributes = method.CustomAttributes;
if (_instrumentationHelper.IsLocalMethod(method.Name))
bool isLocalFunction = _instrumentationHelper.IsLocalMethod(method.Name);
if (isLocalFunction)
actualMethod = methods.FirstOrDefault(m => m.Name == method.Name.Split('>')[0].Substring(1)) ?? method;

if (actualMethod.IsGetter || actualMethod.IsSetter)
Expand All @@ -725,7 +726,17 @@ private void InstrumentType(TypeDefinition type)
continue;
}

if (!customAttributes.Any(IsExcludeAttribute))
// Issue #2045: Local functions are compiled as regular methods decorated with
// [CompilerGeneratedAttribute] by Roslyn. Since this attribute is in the default
// ExcludeByAttribute list, local functions would otherwise be entirely excluded
// from instrumentation. Mirror the Issue #1843 state-machine carve-out: ignore
// CompilerGenerated/GeneratedCode attributes for local functions, but still honor
// other user-configured exclusion attributes (e.g. ExcludeFromCodeCoverage).
bool isExcluded = isLocalFunction
? customAttributes.Any(attr => IsExcludeAttribute(attr) && !IsCompilerGeneratedOrGeneratedCodeAttribute(attr))
: customAttributes.Any(IsExcludeAttribute);

if (!isExcluded)
{
InstrumentMethod(method);
}
Expand Down
21 changes: 21 additions & 0 deletions test/coverlet.core.tests/Instrumentation/InstrumenterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Coverlet.Core.Abstractions;
using Coverlet.Core.Helpers;
Expand Down Expand Up @@ -164,6 +165,26 @@ public void TestInstrument_ClassesWithExcludeAttributeAreExcluded(Type excludedT
instrumenterTest.Directory.Delete(true);
}

[Fact]
public void TestInstrument_LocalFunction_IsNotExcludedByDefaultCompilerGeneratedAttribute()
{
// Regression test for https://github.com/coverlet-coverage/coverlet/issues/2045:
// local functions are compiled by Roslyn as regular methods decorated with
// [CompilerGeneratedAttribute]. Since that attribute is part of the default
// ExcludeByAttribute list, the local function body must still be instrumented
// rather than being entirely dropped from the coverage report.
InstrumenterTest instrumenterTest = CreateInstrumentor(attributesToIgnore: new string[] { nameof(CompilerGeneratedAttribute) });
InstrumenterResult result = instrumenterTest.Instrumenter.Instrument();

Coverlet.Core.Instrumentation.Document doc = result.Documents.Values.FirstOrDefault(d => Path.GetFileName(d.Path) == "Samples.cs");
Assert.NotNull(doc);

bool found = doc.Lines.Values.Any(l => l.Method.Contains("Throw"));
Assert.True(found, "Local function should still be instrumented even though CompilerGeneratedAttribute is in the exclude list");

instrumenterTest.Directory.Delete(true);
}

[Theory]
[InlineData(typeof(ClassExcludedByAttrWithoutAttributeNameSuffix), nameof(TestSDKAutoGeneratedCode))]
[InlineData(typeof(ClassExcludedByAttrWithoutAttributeNameSuffix), "Microsoft.VisualStudio.TestPlatform.TestSDKAutoGeneratedCode")]
Expand Down
20 changes: 20 additions & 0 deletions test/coverlet.core.tests/Samples/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,24 @@ public bool IsLowerInSimpleIf(char c)
return false;
}
}

// Sample for https://github.com/coverlet-coverage/coverlet/issues/2045
// Local functions are compiled by Roslyn as regular methods on the declaring type,
// decorated with [CompilerGeneratedAttribute]. Since that attribute is part of the
// default ExcludeByAttribute list, the local function body must still be instrumented.
public class ClassWithLocalFunction
{
public static void TestCapacity(uint capacity)
{
if (capacity < 16)
{
Throw(capacity);
}

static void Throw(uint capacity)
{
throw new ArgumentException($"Capacity must be >= 16. Actual value was {capacity}.");
}
}
}
}
Loading