-
Notifications
You must be signed in to change notification settings - Fork 222
/
SymbolHelper.cs
360 lines (307 loc) · 15.3 KB
/
SymbolHelper.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
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2023 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using System.Reflection;
using static System.Net.WebRequestMethods;
namespace SonarAnalyzer.Helpers
{
internal static class SymbolHelper
{
private static readonly PropertyInfo ITypeSymbolIsRecord = typeof(ITypeSymbol).GetProperty("IsRecord");
public static IEnumerable<INamedTypeSymbol> GetAllNamedTypes(this INamespaceSymbol @namespace)
{
if (@namespace == null)
{
yield break;
}
foreach (var typeMember in @namespace.GetTypeMembers().SelectMany(GetAllNamedTypes))
{
yield return typeMember;
}
foreach (var typeMember in @namespace.GetNamespaceMembers().SelectMany(GetAllNamedTypes))
{
yield return typeMember;
}
}
public static IEnumerable<INamedTypeSymbol> GetAllNamedTypes(this INamedTypeSymbol type)
{
if (type == null)
{
yield break;
}
yield return type;
foreach (var nestedType in type.GetTypeMembers().SelectMany(GetAllNamedTypes))
{
yield return nestedType;
}
}
public static T GetInterfaceMember<T>(this T symbol)
where T : class, ISymbol
{
if (symbol == null
|| symbol.IsOverride
|| !CanBeInterfaceMember(symbol))
{
return null;
}
return symbol.ContainingType
.AllInterfaces
.SelectMany(@interface => @interface.GetMembers())
.OfType<T>()
.FirstOrDefault(member => symbol.Equals(symbol.ContainingType.FindImplementationForInterfaceMember(member)));
}
public static T GetOverriddenMember<T>(this T symbol)
where T : class, ISymbol
{
if (!(symbol is { IsOverride: true }))
{
return null;
}
return symbol.Kind switch
{
SymbolKind.Method => (T)((IMethodSymbol)symbol).OverriddenMethod,
SymbolKind.Property => (T)((IPropertySymbol)symbol).OverriddenProperty,
SymbolKind.Event => (T)((IEventSymbol)symbol).OverriddenEvent,
_ => throw new ArgumentException($"Only methods, properties and events can be overridden. {typeof(T).Name} was provided", nameof(symbol))
};
}
public static IEnumerable<INamedTypeSymbol> GetSelfAndBaseTypes(this ITypeSymbol type)
{
if (type == null)
{
yield break;
}
var currentType = type;
while (currentType?.Kind == SymbolKind.NamedType)
{
yield return (INamedTypeSymbol)currentType;
currentType = currentType.BaseType;
}
}
public static bool IsChangeable(this ISymbol symbol) =>
!symbol.IsAbstract
&& !symbol.IsVirtual
&& symbol.GetInterfaceMember() == null
&& symbol.GetOverriddenMember() == null;
public static bool IsExtensionOn(this IMethodSymbol methodSymbol, KnownType type)
{
if (!(methodSymbol is { IsExtensionMethod: true }))
{
return false;
}
var receiverType = methodSymbol.ReceiverType as INamedTypeSymbol;
if (methodSymbol.MethodKind == MethodKind.Ordinary)
{
receiverType = methodSymbol.Parameters.First().Type as INamedTypeSymbol;
}
var constructedFrom = receiverType?.ConstructedFrom;
return constructedFrom.Is(type);
}
public static IEnumerable<IParameterSymbol> GetParameters(this ISymbol symbol) =>
symbol.Kind switch
{
SymbolKind.Method => ((IMethodSymbol)symbol).Parameters,
SymbolKind.Property => ((IPropertySymbol)symbol).Parameters,
_ => Enumerable.Empty<IParameterSymbol>()
};
public static bool IsAnyAttributeInOverridingChain(IPropertySymbol propertySymbol) =>
IsAnyAttributeInOverridingChain(propertySymbol, property => property.OverriddenProperty);
public static bool IsAnyAttributeInOverridingChain(IMethodSymbol methodSymbol) =>
IsAnyAttributeInOverridingChain(methodSymbol, method => method.OverriddenMethod);
public static Accessibility GetEffectiveAccessibility(this ISymbol symbol)
{
if (symbol == null)
{
return Accessibility.NotApplicable;
}
var result = symbol.DeclaredAccessibility;
if (result == Accessibility.Private)
{
return Accessibility.Private;
}
for (var container = symbol.ContainingType; container != null; container = container.ContainingType)
{
if (container.DeclaredAccessibility == Accessibility.Private)
{
return Accessibility.Private;
}
if (container.DeclaredAccessibility == Accessibility.Internal)
{
result = Accessibility.Internal;
}
}
return result;
}
public static bool IsPubliclyAccessible(this ISymbol symbol)
{
var effectiveAccessibility = GetEffectiveAccessibility(symbol);
return effectiveAccessibility == Accessibility.Public
|| effectiveAccessibility == Accessibility.Protected
|| effectiveAccessibility == Accessibility.ProtectedOrInternal;
}
public static bool IsConstructor(this ISymbol symbol) =>
symbol.Kind == SymbolKind.Method && symbol.Name == ".ctor";
public static bool IsDestructor(this IMethodSymbol method) =>
method.MethodKind == MethodKind.Destructor;
public static bool IsSameNamespace(this INamespaceSymbol namespace1, INamespaceSymbol namespace2) =>
(namespace1.IsGlobalNamespace && namespace2.IsGlobalNamespace)
|| (namespace1.Name.Equals(namespace2.Name)
&& namespace1.ContainingNamespace != null
&& namespace2.ContainingNamespace != null
&& IsSameNamespace(namespace1.ContainingNamespace, namespace2.ContainingNamespace));
public static bool IsSameOrAncestorOf(this INamespaceSymbol thisNamespace, INamespaceSymbol namespaceToCheck) =>
IsSameNamespace(thisNamespace, namespaceToCheck)
|| (namespaceToCheck.ContainingNamespace != null && IsSameOrAncestorOf(thisNamespace, namespaceToCheck.ContainingNamespace));
internal static IEnumerable<AttributeData> GetAttributes(this ISymbol symbol, KnownType attributeType) =>
symbol?.GetAttributes().Where(a => a.AttributeClass.Is(attributeType))
?? Enumerable.Empty<AttributeData>();
internal static IEnumerable<AttributeData> GetAttributes(this ISymbol symbol, ImmutableArray<KnownType> attributeTypes) =>
symbol?.GetAttributes().Where(a => a.AttributeClass.IsAny(attributeTypes))
?? Enumerable.Empty<AttributeData>();
/// <summary>
/// Returns attributes for the symbol by also respecting <see cref="AttributeUsageAttribute.Inherited"/>.
/// The returned <see cref="AttributeData"/> is consistent with the results from <see cref="MemberInfo.GetCustomAttributes(bool)"/>.
/// </summary>
public static IEnumerable<AttributeData> GetAttributesWithInherited(this ISymbol symbol)
{
foreach (var attribute in symbol.GetAttributes())
{
yield return attribute;
}
var baseSymbol = BaseSymbol(symbol);
while (baseSymbol is not null)
{
foreach (var attribute in baseSymbol.GetAttributes().Where(x => x.HasAttributeUsageInherited()))
{
yield return attribute;
}
baseSymbol = BaseSymbol(baseSymbol);
}
static ISymbol BaseSymbol(ISymbol symbol) =>
symbol switch
{
INamedTypeSymbol namedType => namedType.BaseType,
IMethodSymbol { OriginalDefinition: { } originalDefinition } method when !method.Equals(originalDefinition) => BaseSymbol(originalDefinition),
IMethodSymbol { OverriddenMethod: { } overridenMethod } => overridenMethod,
// Support for other kinds of symbols needs to be implemented/tested as needed. A full list can be found here:
// https://learn.microsoft.com/dotnet/api/system.attributetargets
_ => null,
};
}
internal static bool AnyAttributeDerivesFrom(this ISymbol symbol, KnownType attributeType) =>
symbol?.GetAttributes().Any(a => a.AttributeClass.DerivesFrom(attributeType)) ?? false;
internal static bool AnyAttributeDerivesFromAny(this ISymbol symbol, ImmutableArray<KnownType> attributeTypes) =>
symbol?.GetAttributes().Any(a => a.AttributeClass.DerivesFromAny(attributeTypes)) ?? false;
internal static bool AnyAttributeDerivesFromOrImplementsAny(this ISymbol symbol, ImmutableArray<KnownType> attributeTypesOrInterfaces) =>
symbol?.GetAttributes().Any(a => a.AttributeClass.DerivesOrImplementsAny(attributeTypesOrInterfaces)) ?? false;
internal static bool IsKnownType(this SyntaxNode syntaxNode, KnownType knownType, SemanticModel semanticModel)
{
var symbolType = semanticModel.GetSymbolInfo(syntaxNode).Symbol.GetSymbolType();
return symbolType.Is(knownType) || symbolType?.OriginalDefinition?.Is(knownType) == true;
}
internal static bool IsDeclarationKnownType(this SyntaxNode syntaxNode, KnownType knownType, SemanticModel semanticModel)
{
var symbolType = semanticModel.GetDeclaredSymbol(syntaxNode)?.GetSymbolType();
return symbolType.Is(knownType);
}
private static bool IsAnyAttributeInOverridingChain<TSymbol>(TSymbol symbol, Func<TSymbol, TSymbol> getOverriddenMember)
where TSymbol : class, ISymbol
{
var currentSymbol = symbol;
while (currentSymbol != null)
{
if (currentSymbol.GetAttributes().Any())
{
return true;
}
if (!currentSymbol.IsOverride)
{
return false;
}
currentSymbol = getOverriddenMember(currentSymbol);
}
return false;
}
private static bool CanBeInterfaceMember(ISymbol symbol) =>
symbol.Kind == SymbolKind.Method
|| symbol.Kind == SymbolKind.Property
|| symbol.Kind == SymbolKind.Event;
public static string GetClassification(this ISymbol symbol) =>
symbol switch
{
{ Kind: SymbolKind.Alias } => "alias",
{ Kind: SymbolKind.ArrayType } => "array",
{ Kind: SymbolKind.Assembly } => "assembly",
{ Kind: SymbolKindEx.Discard } => "discard",
{ Kind: SymbolKind.DynamicType } => "dynamic",
{ Kind: SymbolKind.ErrorType } => "error",
{ Kind: SymbolKind.Event } => "event",
{ Kind: SymbolKindEx.FunctionPointerType } => "function pointer",
{ Kind: SymbolKind.Field } => "field",
{ Kind: SymbolKind.Label } => "label",
{ Kind: SymbolKind.Local } => "local",
{ Kind: SymbolKind.Namespace } => "namespace",
{ Kind: SymbolKind.NetModule } => "netmodule",
{ Kind: SymbolKind.PointerType } => "pointer",
{ Kind: SymbolKind.Preprocessing } => "preprocessing",
{ Kind: SymbolKind.Parameter } => "parameter",
{ Kind: SymbolKind.RangeVariable } => "range variable",
{ Kind: SymbolKind.Property } => "property",
{ Kind: SymbolKind.TypeParameter } => "type parameter",
IMethodSymbol methodSymbol => methodSymbol switch
{
{ MethodKind: MethodKind.BuiltinOperator or MethodKind.UserDefinedOperator or MethodKind.Conversion } => "operator",
{ MethodKind: MethodKind.Constructor or MethodKind.StaticConstructor or MethodKind.SharedConstructor } => "constructor",
{ MethodKind: MethodKind.Destructor } => "destructor",
{ MethodKind: MethodKind.PropertyGet } => "getter",
{ MethodKind: MethodKind.PropertySet } => "setter",
_ => "method",
},
INamedTypeSymbol namedTypeSymbol => namedTypeSymbol switch
{
{ TypeKind: TypeKind.Array } => "array",
{ TypeKind: TypeKind.Class } namedType => namedType.IsRecord() ? "record" : "class",
{ TypeKind: TypeKind.Dynamic } => "dynamic",
{ TypeKind: TypeKind.Delegate } => "delegate",
{ TypeKind: TypeKind.Enum } => "enum",
{ TypeKind: TypeKind.Error } => "error",
{ TypeKind: TypeKindEx.FunctionPointer } => "function pointer",
{ TypeKind: TypeKind.Interface } => "interface",
{ TypeKind: TypeKind.Module } => "module",
{ TypeKind: TypeKind.Pointer } => "pointer",
{ TypeKind: TypeKind.Struct or TypeKind.Structure } namedType => namedType.IsRecord() ? "record struct" : "struct",
{ TypeKind: TypeKind.Submission } => "submission",
{ TypeKind: TypeKind.TypeParameter } => "type parameter",
{ TypeKind: TypeKind.Unknown } => "unknown",
#if DEBUG
_ => throw new NotSupportedException($"symbol is of a not yet supported kind."),
#else
_ => "type",
#endif
},
#if DEBUG
_ => throw new NotSupportedException($"symbol is of a not yet supported kind."),
#else
_ => "symbol",
#endif
};
public static bool IsRecord(this ITypeSymbol typeSymbol)
=> ITypeSymbolIsRecord?.GetValue(typeSymbol) is true;
}
}