forked from dotnet/roslyn-analyzers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
IMethodSymbolExtensions.cs
397 lines (350 loc) · 16.1 KB
/
IMethodSymbolExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Operations;
namespace Analyzer.Utilities.Extensions
{
internal static class IMethodSymbolExtensions
{
/// <summary>
/// Checks if the given method overrides <see cref="object.Equals(object)"/>.
/// </summary>
public static bool IsObjectEqualsOverride(this IMethodSymbol method)
{
return method != null &&
method.IsOverride &&
method.Name == WellKnownMemberNames.ObjectEquals &&
method.ReturnType.SpecialType == SpecialType.System_Boolean &&
method.Parameters.Length == 1 &&
method.Parameters[0].Type.SpecialType == SpecialType.System_Object &&
IsObjectMethodOverride(method);
}
/// <summary>
/// Checks if the given method is <see cref="object.Equals(object)"/>.
/// </summary>
public static bool IsObjectEquals(this IMethodSymbol method)
{
return method != null &&
method.ContainingType.SpecialType == SpecialType.System_Object &&
method.IsVirtual &&
method.Name == WellKnownMemberNames.ObjectEquals &&
method.ReturnType.SpecialType == SpecialType.System_Boolean &&
method.Parameters.Length == 1 &&
method.Parameters[0].Type.SpecialType == SpecialType.System_Object;
}
/// <summary>
/// Checks if the given <paramref name="method"/> is <see cref="object.Equals(object, object)"/> or <see cref="object.ReferenceEquals(object, object)"/>.
/// </summary>
public static bool IsStaticObjectEqualsOrReferenceEquals(this IMethodSymbol method)
{
return method != null &&
method.IsStatic &&
method.ContainingType.SpecialType == SpecialType.System_Object &&
method.Parameters.Length == 2 &&
method.ReturnType.SpecialType == SpecialType.System_Boolean &&
method.Parameters[0].Type.SpecialType == SpecialType.System_Object &&
method.Parameters[1].Type.SpecialType == SpecialType.System_Object &&
(method.Name == WellKnownMemberNames.ObjectEquals || method.Name == "ReferenceEquals");
}
/// <summary>
/// Checks if the given method overrides Object.GetHashCode.
/// </summary>
public static bool IsGetHashCodeOverride(this IMethodSymbol method)
{
return method != null &&
method.IsOverride &&
method.Name == WellKnownMemberNames.ObjectGetHashCode &&
method.ReturnType.SpecialType == SpecialType.System_Int32 &&
method.Parameters.Length == 0 &&
IsObjectMethodOverride(method);
}
/// <summary>
/// Checks if the given method overrides Object.ToString.
/// </summary>
public static bool IsToStringOverride(this IMethodSymbol method)
{
return method != null &&
method.IsOverride &&
method.ReturnType.SpecialType == SpecialType.System_String &&
method.Name == WellKnownMemberNames.ObjectToString &&
method.Parameters.Length == 0 &&
IsObjectMethodOverride(method);
}
/// <summary>
/// Checks if the given method overrides a method from System.Object
/// </summary>
private static bool IsObjectMethodOverride(IMethodSymbol method)
{
IMethodSymbol overriddenMethod = method.OverriddenMethod;
while (overriddenMethod != null)
{
if (overriddenMethod.ContainingType.SpecialType == SpecialType.System_Object)
{
return true;
}
overriddenMethod = overriddenMethod.OverriddenMethod;
}
return false;
}
/// <summary>
/// Checks if the given method is a Finalizer implementation.
/// </summary>
public static bool IsFinalizer(this IMethodSymbol method)
{
if (method.MethodKind == MethodKind.Destructor)
{
return true; // for C#
}
if (method.Name != WellKnownMemberNames.DestructorName || method.Parameters.Length != 0 || !method.ReturnsVoid)
{
return false;
}
IMethodSymbol overridden = method.OverriddenMethod;
if (method.ContainingType.SpecialType == SpecialType.System_Object)
{
// This is object.Finalize
return true;
}
if (overridden == null)
{
return false;
}
for (IMethodSymbol o = overridden.OverriddenMethod; o != null; o = o.OverriddenMethod)
{
overridden = o;
}
return overridden.ContainingType.SpecialType == SpecialType.System_Object; // it is object.Finalize
}
/// <summary>
/// Checks if the given method is an implementation of the given interface method
/// Substituted with the given typeargument.
/// </summary>
public static bool IsImplementationOfInterfaceMethod(this IMethodSymbol method, ITypeSymbol typeArgument, INamedTypeSymbol interfaceType, string interfaceMethodName)
{
INamedTypeSymbol constructedInterface = typeArgument != null ? interfaceType?.Construct(typeArgument) : interfaceType;
var interfaceMethod = constructedInterface?.GetMembers(interfaceMethodName).Single() as IMethodSymbol;
return interfaceMethod != null && method.Equals(method.ContainingType.FindImplementationForInterfaceMember(interfaceMethod));
}
/// <summary>
/// Checks if the given method implements IDisposable.Dispose()
/// </summary>
public static bool IsDisposeImplementation(this IMethodSymbol method, Compilation compilation)
{
INamedTypeSymbol iDisposable = WellKnownTypes.IDisposable(compilation);
return method.IsDisposeImplementation(iDisposable);
}
/// <summary>
/// Checks if the given method implements <see cref="IDisposable.Dispose"/> or overrides an implementation of <see cref="IDisposable.Dispose"/>.
/// </summary>
public static bool IsDisposeImplementation(this IMethodSymbol method, INamedTypeSymbol iDisposable)
{
if (method == null)
{
return false;
}
if (method.IsOverride)
{
return method.OverriddenMethod.IsDisposeImplementation(iDisposable);
}
// Identify the implementor of IDisposable.Dispose in the given method's containing type and check
// if it is the given method.
return method.ReturnsVoid &&
method.Parameters.Length == 0 &&
method.IsImplementationOfInterfaceMethod(null, iDisposable, "Dispose");
}
/// <summary>
/// Checks if the given method has the signature "void Dispose()".
/// </summary>
private static bool HasDisposeMethodSignature(this IMethodSymbol method)
{
return method.Name == "Dispose" && method.MethodKind == MethodKind.Ordinary &&
method.ReturnsVoid && method.Parameters.IsEmpty;
}
/// <summary>
/// Checks if the given method has the signature "void Dispose(bool)".
/// </summary>
public static bool HasDisposeBoolMethodSignature(this IMethodSymbol method)
{
if (method.Name == "Dispose" && method.MethodKind == MethodKind.Ordinary &&
method.ReturnsVoid && method.Parameters.Length == 1)
{
IParameterSymbol parameter = method.Parameters[0];
return parameter.Type != null &&
parameter.Type.SpecialType == SpecialType.System_Boolean &&
parameter.RefKind == RefKind.None;
}
return false;
}
/// <summary>
/// Checks if the given method has the signature "void Close()".
/// </summary>
private static bool HasDisposeCloseMethodSignature(this IMethodSymbol method)
{
return method.Name == "Close" && method.MethodKind == MethodKind.Ordinary &&
method.ReturnsVoid && method.Parameters.IsEmpty;
}
/// <summary>
/// Gets the <see cref="DisposeMethodKind"/> for the given method.
/// </summary>
public static DisposeMethodKind GetDisposeMethodKind(this IMethodSymbol method, Compilation compilation)
{
INamedTypeSymbol iDisposable = WellKnownTypes.IDisposable(compilation);
return method.GetDisposeMethodKind(iDisposable);
}
/// <summary>
/// Gets the <see cref="DisposeMethodKind"/> for the given method.
/// </summary>
public static DisposeMethodKind GetDisposeMethodKind(this IMethodSymbol method, INamedTypeSymbol iDisposable)
{
if (method.ContainingType.IsDisposable(iDisposable))
{
if (IsDisposeImplementation(method, iDisposable) ||
(method.ContainingType == iDisposable &&
method.HasDisposeMethodSignature()))
{
return DisposeMethodKind.Dispose;
}
else if (method.HasDisposeBoolMethodSignature())
{
return DisposeMethodKind.DisposeBool;
}
else if (method.HasDisposeCloseMethodSignature())
{
return DisposeMethodKind.Close;
}
}
return DisposeMethodKind.None;
}
/// <summary>
/// Checks if the method is a property getter.
/// </summary>
public static bool IsPropertyGetter(this IMethodSymbol method)
{
return method.MethodKind == MethodKind.PropertyGet &&
method.AssociatedSymbol?.GetParameters().Length == 0;
}
/// <summary>
/// Checks if the method is the getter for an indexer.
/// </summary>
public static bool IsIndexerGetter(this IMethodSymbol method)
{
return method.MethodKind == MethodKind.PropertyGet &&
method.AssociatedSymbol.IsIndexer();
}
/// <summary>
/// Checks if the method is an accessor for a property.
/// </summary>
public static bool IsPropertyAccessor(this IMethodSymbol method)
{
return method.MethodKind == MethodKind.PropertyGet ||
method.MethodKind == MethodKind.PropertySet;
}
/// <summary>
/// Checks if the method is an accessor for an event.
/// </summary>
public static bool IsEventAccessor(this IMethodSymbol method)
{
return method.MethodKind == MethodKind.EventAdd ||
method.MethodKind == MethodKind.EventRaise ||
method.MethodKind == MethodKind.EventRemove;
}
public static bool IsOperator(this IMethodSymbol methodSymbol)
{
return methodSymbol.MethodKind == MethodKind.UserDefinedOperator || methodSymbol.MethodKind == MethodKind.BuiltinOperator;
}
public static bool HasOptionalParameters(this IMethodSymbol methodSymbol)
{
return methodSymbol.Parameters.Any(p => p.IsOptional);
}
public static IEnumerable<IMethodSymbol> GetOverloads(this IMethodSymbol method)
{
foreach (var member in method?.ContainingType?.GetMembers(method.Name).OfType<IMethodSymbol>())
{
if (!member.Equals(method))
{
yield return member;
}
}
}
/// <summary>
/// Determine if the specific method is an Add method that adds to a collection.
/// </summary>
/// <param name="method">The method to test.</param>
/// <returns>'true' if <paramref name="method"/> is believed to be the add method of a collection.</returns>
/// <remarks>
/// The current heuristic is that we consider a method to be an add method if its name begins with "Add" and its
/// enclosing type derives from ICollection or any instantiation of ICollection<T>.
/// </remarks>
public static bool IsCollectionAddMethod(this IMethodSymbol method, ImmutableHashSet<INamedTypeSymbol> iCollectionTypes)
=> !iCollectionTypes.IsEmpty &&
method.Name.StartsWith("Add", StringComparison.Ordinal) &&
method.ContainingType.AllInterfaces.Any(i => iCollectionTypes.Contains(i.OriginalDefinition));
/// <summary>
/// Returns the topmost <see cref="IBlockOperation"/> for given <paramref name="method"/>.
/// </summary>
public static IBlockOperation GetTopmostOperationBlock(this IMethodSymbol method, Compilation compilation, CancellationToken cancellationToken = default(CancellationToken))
{
if (method.ContainingAssembly != compilation.Assembly)
{
return null;
}
foreach (var decl in method.DeclaringSyntaxReferences)
{
var syntax = decl.GetSyntax(cancellationToken);
// VB Workaround: declaration.GetSyntax returns StatementSyntax nodes instead of BlockSyntax nodes
// GetOperation returns null for StatementSyntax, and the method's operation block for BlockSyntax.
if (compilation.Language == LanguageNames.VisualBasic)
{
syntax = syntax.Parent;
}
var semanticModel = compilation.GetSemanticModel(syntax.SyntaxTree);
foreach (var descendant in syntax.DescendantNodesAndSelf())
{
var operation = semanticModel.GetOperation(descendant, cancellationToken);
if (operation is IBlockOperation blockOperation)
{
return blockOperation;
}
}
}
return null;
}
public static bool IsLambdaOrLocalFunctionOrDelegate(this IMethodSymbol method)
{
switch (method.MethodKind)
{
case MethodKind.LambdaMethod:
case MethodKind.LocalFunction:
case MethodKind.DelegateInvoke:
return true;
default:
return false;
}
}
public static int GetParameterIndex(this IMethodSymbol methodSymbol, IParameterSymbol parameterSymbol)
{
for (var i = 0; i < methodSymbol.Parameters.Length; i++)
{
if (parameterSymbol == methodSymbol.Parameters[i])
{
return i;
}
}
throw new ArgumentException("Invalid paramater", nameof(parameterSymbol));
}
/// <summary>
/// Returns true for void returning methods with two parameters, where
/// the first parameter is of <see cref="object"/> type and the second
/// parameter inherits from or equals <see cref="EventArgs"/> type.
/// </summary>
public static bool HasEventHandlerSignature(this IMethodSymbol method, INamedTypeSymbol eventArgsType)
=> eventArgsType != null &&
method.Parameters.Length == 2 &&
method.Parameters[0].Type.SpecialType == SpecialType.System_Object &&
method.Parameters[1].Type.DerivesFrom(eventArgsType, baseTypesOnly: true);
}
}