Skip to content

Commit c74bc68

Browse files
committed
C#: Fix extraction of qualified delegate calls
1 parent c21bf5d commit c74bc68

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Invocation.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ private Invocation(ExpressionNodeInfo info)
2020

2121
public static Expression Create(ExpressionNodeInfo info) => new Invocation(info).TryPopulate();
2222

23+
private bool IsEventDelegateCall() => Kind == ExprKind.DELEGATE_INVOCATION && Context.GetModel(Syntax.Expression).GetSymbolInfo(Syntax.Expression).Symbol?.Kind == SymbolKind.Event;
24+
25+
private bool IsExplicitDelegateInvokeCall() => Kind == ExprKind.DELEGATE_INVOCATION && Context.GetModel(Syntax.Expression).GetSymbolInfo(Syntax.Expression).Symbol is IMethodSymbol m && m.MethodKind == MethodKind.DelegateInvoke;
26+
2327
protected override void PopulateExpression(TextWriter trapFile)
2428
{
2529
if (IsNameof(Syntax))
@@ -33,7 +37,7 @@ protected override void PopulateExpression(TextWriter trapFile)
3337
var target = TargetSymbol;
3438
switch (Syntax.Expression)
3539
{
36-
case MemberAccessExpressionSyntax memberAccess:
40+
case MemberAccessExpressionSyntax memberAccess when Kind == ExprKind.METHOD_INVOCATION || IsEventDelegateCall() || IsExplicitDelegateInvokeCall():
3741
memberName = memberAccess.Name.Identifier.Text;
3842
if (Syntax.Expression.Kind() == SyntaxKind.SimpleMemberAccessExpression)
3943
// Qualified method call; `x.M()`
@@ -48,7 +52,7 @@ protected override void PopulateExpression(TextWriter trapFile)
4852
Create(Context, FindConditionalQualifier(memberBinding), this, child++);
4953
MakeConditional(trapFile);
5054
break;
51-
case SimpleNameSyntax simpleName when (Kind == ExprKind.METHOD_INVOCATION):
55+
case SimpleNameSyntax simpleName when Kind == ExprKind.METHOD_INVOCATION:
5256
// Unqualified method call; `M()`
5357
memberName = simpleName.Identifier.Text;
5458
if (target is not null && !target.IsStatic)

csharp/ql/test/library-tests/delegates/Delegates4.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
| delegates.cs:74:13:74:23 | delegate call | delegates.cs:74:13:74:15 | access to local variable cd7 |
33
| delegates.cs:79:22:79:26 | delegate call | delegates.cs:79:22:79:23 | access to local variable pi |
44
| delegates.cs:79:30:79:35 | delegate call | delegates.cs:79:30:79:31 | access to local variable ps |
5-
| delegates.cs:93:13:93:25 | delegate call | delegates.cs:93:13:93:16 | this access |
6-
| delegates.cs:94:13:94:28 | delegate call | delegates.cs:94:13:94:16 | this access |
5+
| delegates.cs:93:13:93:25 | delegate call | delegates.cs:93:13:93:22 | access to field Field |
6+
| delegates.cs:94:13:94:28 | delegate call | delegates.cs:94:13:94:25 | access to property Property |
77
| delegates.cs:95:13:95:20 | delegate call | delegates.cs:95:13:95:17 | access to field Field |
88
| delegates.cs:96:13:96:23 | delegate call | delegates.cs:96:13:96:20 | access to property Property |

csharp/ql/test/library-tests/delegates/PrintAst.expected

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,13 @@ delegates.cs:
265265
# 92| 4: [BlockStmt] {...}
266266
# 93| 0: [ExprStmt] ...;
267267
# 93| 0: [DelegateCall] delegate call
268-
# 93| -1: [ThisAccess] this access
268+
# 93| -1: [FieldAccess] access to field Field
269+
# 93| -1: [ThisAccess] this access
269270
# 93| 0: [IntLiteral] 0
270271
# 94| 1: [ExprStmt] ...;
271272
# 94| 0: [DelegateCall] delegate call
272-
# 94| -1: [ThisAccess] this access
273+
# 94| -1: [PropertyCall] access to property Property
274+
# 94| -1: [ThisAccess] this access
273275
# 94| 0: [IntLiteral] 0
274276
# 95| 2: [ExprStmt] ...;
275277
# 95| 0: [DelegateCall] delegate call

0 commit comments

Comments
 (0)