Skip to content

Commit e18534e

Browse files
committed
C#: Force population of compiler generated delegates used for lambdas.
1 parent 9a64e2a commit e18534e

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/OrdinaryMethod.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ protected OrdinaryMethod(Context cx, IMethodSymbol init)
1818

1919
public IMethodSymbol SourceDeclaration => Symbol.OriginalDefinition;
2020

21-
public override Microsoft.CodeAnalysis.Location ReportingLocation => Symbol.GetSymbolLocation();
21+
public override Microsoft.CodeAnalysis.Location ReportingLocation =>
22+
IsCompilerGeneratedDelegate()
23+
? Symbol.ContainingType.GetSymbolLocation()
24+
: Symbol.GetSymbolLocation();
25+
26+
public override bool NeedsPopulation => base.NeedsPopulation || IsCompilerGeneratedDelegate();
2227

2328
public override void Populate(TextWriter trapFile)
2429
{
@@ -47,6 +52,18 @@ public override void Populate(TextWriter trapFile)
4752
ExtractCompilerGenerated(trapFile);
4853
}
4954

55+
private bool IsCompilerGeneratedDelegate()
56+
{
57+
// Lambdas with parameter defaults or a `params` parameter are implemented
58+
// using compiler generated delegate types.
59+
if (Symbol.MethodKind == MethodKind.DelegateInvoke &&
60+
Symbol.ContainingType is INamedTypeSymbol nt)
61+
{
62+
return nt.TypeKind == TypeKind.Delegate && nt.IsImplicitlyDeclared;
63+
}
64+
return false;
65+
}
66+
5067
public static new OrdinaryMethod Create(Context cx, IMethodSymbol method)
5168
{
5269
if (method.MethodKind == MethodKind.ReducedExtension)

0 commit comments

Comments
 (0)