Skip to content

Commit b46eaa8

Browse files
committed
C#: Re-factor TargetSymbol into an extension method.
1 parent cc0daca commit b46eaa8

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed

csharp/extractor/Semmle.Extraction.CSharp/CodeAnalysisExtensions/SymbolExtensions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Linq;
66
using Microsoft.CodeAnalysis;
7+
using Microsoft.CodeAnalysis.CSharp.Syntax;
78
using Semmle.Util;
89
using Semmle.Extraction.CSharp.Entities;
910

@@ -856,5 +857,34 @@ public static Parameter.Kind GetParameterKind(this IParameterSymbol parameter)
856857
return Parameter.Kind.None;
857858
}
858859
}
860+
861+
public static IMethodSymbol? GetTargetSymbol(this ExpressionNodeInfo info, Context cx)
862+
{
863+
var si = info.SymbolInfo;
864+
if (si.Symbol is ISymbol symbol)
865+
{
866+
var method = symbol as IMethodSymbol;
867+
// Case for compiler-generated extension methods.
868+
return method?.TryGetExtensionMethod() ?? method;
869+
}
870+
871+
if (si.CandidateReason == CandidateReason.OverloadResolutionFailure && info.Node is InvocationExpressionSyntax syntax)
872+
{
873+
// This seems to be a bug in Roslyn
874+
// For some reason, typeof(X).InvokeMember(...) fails to resolve the correct
875+
// InvokeMember() method, even though the number of parameters clearly identifies the correct method
876+
877+
var candidates = si.CandidateSymbols
878+
.OfType<IMethodSymbol>()
879+
.Where(method => method.Parameters.Length >= syntax.ArgumentList.Arguments.Count)
880+
.Where(method => method.Parameters.Count(p => !p.HasExplicitDefaultValue) <= syntax.ArgumentList.Arguments.Count);
881+
882+
return cx.ExtractionContext.IsStandalone ?
883+
candidates.FirstOrDefault() :
884+
candidates.SingleOrDefault();
885+
}
886+
887+
return si.Symbol as IMethodSymbol;
888+
}
859889
}
860890
}

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

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected override void PopulateExpression(TextWriter trapFile)
4444

4545
var child = -1;
4646
string? memberName = null;
47-
var target = TargetSymbol;
47+
var target = info.GetTargetSymbol(Context);
4848
switch (Syntax.Expression)
4949
{
5050
case MemberAccessExpressionSyntax memberAccess when IsValidMemberAccessKind():
@@ -129,39 +129,6 @@ private static bool IsOperatorLikeCall(ExpressionNodeInfo info)
129129
method.TryGetExtensionMethod()?.MethodKind == MethodKind.UserDefinedOperator;
130130
}
131131

132-
public IMethodSymbol? TargetSymbol
133-
{
134-
get
135-
{
136-
var si = SymbolInfo;
137-
138-
if (si.Symbol is ISymbol symbol)
139-
{
140-
var method = symbol as IMethodSymbol;
141-
// Case for compiler-generated extension methods.
142-
return method?.TryGetExtensionMethod() ?? method;
143-
}
144-
145-
if (si.CandidateReason == CandidateReason.OverloadResolutionFailure)
146-
{
147-
// This seems to be a bug in Roslyn
148-
// For some reason, typeof(X).InvokeMember(...) fails to resolve the correct
149-
// InvokeMember() method, even though the number of parameters clearly identifies the correct method
150-
151-
var candidates = si.CandidateSymbols
152-
.OfType<IMethodSymbol>()
153-
.Where(method => method.Parameters.Length >= Syntax.ArgumentList.Arguments.Count)
154-
.Where(method => method.Parameters.Count(p => !p.HasExplicitDefaultValue) <= Syntax.ArgumentList.Arguments.Count);
155-
156-
return Context.ExtractionContext.IsStandalone ?
157-
candidates.FirstOrDefault() :
158-
candidates.SingleOrDefault();
159-
}
160-
161-
return si.Symbol as IMethodSymbol;
162-
}
163-
}
164-
165132
private static bool IsDelegateLikeCall(ExpressionNodeInfo info)
166133
{
167134
return IsDelegateLikeCall(info, symbol => IsFunctionPointer(symbol) || IsDelegateInvoke(symbol));

0 commit comments

Comments
 (0)