forked from neo-project/neo-devpack-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionTypesUsageAnalyzer.cs
More file actions
198 lines (167 loc) · 8.92 KB
/
Copy pathCollectionTypesUsageAnalyzer.cs
File metadata and controls
198 lines (167 loc) · 8.92 KB
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
// Copyright (C) 2015-2026 The Neo Project.
//
// CollectionTypesUsageAnalyzer.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
using System;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
using System.Collections.Immutable;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Neo.SmartContract.Analyzer
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class CollectionTypesUsageAnalyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "NC4013";
private readonly string[] _unsupportedCollectionTypes = {
"System.Collections.Generic.Dictionary<TKey, TValue>",
"System.Collections.Generic.List<T>",
"System.Collections.Generic.Stack<T>",
"System.Collections.Generic.Queue<T>",
"System.Collections.Generic.HashSet<T>",
"System.Collections.Generic.SortedSet<T>",
"System.Collections.Generic.LinkedList<T>",
"System.Collections.ObjectModel.ObservableCollection<T>",
"System.Collections.Concurrent.ConcurrentQueue<T>",
"System.Collections.Concurrent.ConcurrentStack<T>",
"System.Collections.Concurrent.ConcurrentBag<T>",
"System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>",
"System.Collections.Immutable.ImmutableList<T>",
"System.Collections.Immutable.ImmutableStack<T>",
"System.Collections.Immutable.ImmutableQueue<T>",
"System.Collections.Immutable.ImmutableHashSet<T>",
"System.Collections.Immutable.ImmutableSortedSet<T>",
"System.Collections.Immutable.ImmutableDictionary<TKey, TValue>",
"System.Collections.Generic.SortedDictionary<TKey, TValue>",
"System.Collections.ObjectModel.KeyedCollection<TKey, TItem>",
"System.Collections.Immutable.ImmutableArray<T>",
"System.Collections.Immutable.ImmutableSortedDictionary<TKey, TValue>",
"System.Collections.Concurrent.BlockingCollection<T>",
"System.Collections.Specialized.NameValueCollection",
"System.Collections.Specialized.StringCollection",
"System.Collections.Specialized.HybridDictionary",
"System.Collections.Specialized.OrderedDictionary",
"System.Collections.ArrayList",
"System.Collections.Hashtable",
"System.Collections.SortedList",
"System.Collections.BitArray",
"System.Collections.ObjectModel.Collection<T>",
"System.Collections.ObjectModel.ReadOnlyCollection<T>",
"System.Collections.ObjectModel.ReadOnlyObservableCollection<T>"
};
private static readonly DiagnosticDescriptor Rule = new(
DiagnosticId,
"Unsupported collection type is used",
"Do not use collection type: {0}. Use {1} instead.",
"Type",
DiagnosticSeverity.Error,
isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterOperationAction(AnalyzeOperation, OperationKind.VariableDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeMethodDeclaration, SyntaxKind.MethodDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeParameter, SyntaxKind.Parameter);
context.RegisterSyntaxNodeAction(AnalyzePropertyDeclaration, SyntaxKind.PropertyDeclaration);
}
private void AnalyzeOperation(OperationAnalysisContext context)
{
if (context.Operation is not IVariableDeclarationOperation variableDeclaration) return;
var variableType = variableDeclaration.GetDeclaredVariables().FirstOrDefault()?.Type;
ReportIfUnsupportedCollectionType(context, variableDeclaration.Syntax.GetLocation(), variableType);
}
private void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
{
if (context.Node is not MethodDeclarationSyntax methodDeclaration) return;
var type = context.SemanticModel.GetTypeInfo(methodDeclaration.ReturnType, context.CancellationToken).Type;
ReportIfUnsupportedCollectionType(context, methodDeclaration.ReturnType.GetLocation(), type);
}
private void AnalyzeParameter(SyntaxNodeAnalysisContext context)
{
if (context.Node is not ParameterSyntax parameter || parameter.Type is null) return;
var type = (context.SemanticModel.GetDeclaredSymbol(parameter, context.CancellationToken) as IParameterSymbol)?.Type;
ReportIfUnsupportedCollectionType(context, parameter.Type.GetLocation(), type);
}
private void AnalyzePropertyDeclaration(SyntaxNodeAnalysisContext context)
{
if (context.Node is not PropertyDeclarationSyntax propertyDeclaration) return;
var type = (context.SemanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken) as IPropertySymbol)?.Type;
ReportIfUnsupportedCollectionType(context, propertyDeclaration.Type.GetLocation(), type);
}
private void ReportIfUnsupportedCollectionType(OperationAnalysisContext context, Location location, ITypeSymbol? type)
{
if (GetUnsupportedCollectionType(type) is not { } originalType) return;
var diagnostic = Diagnostic.Create(Rule, location, originalType, GetSuggestedType(originalType));
context.ReportDiagnostic(diagnostic);
}
private void ReportIfUnsupportedCollectionType(SyntaxNodeAnalysisContext context, Location location, ITypeSymbol? type)
{
if (GetUnsupportedCollectionType(type) is not { } originalType) return;
var diagnostic = Diagnostic.Create(Rule, location, originalType, GetSuggestedType(originalType));
context.ReportDiagnostic(diagnostic);
}
private string? GetUnsupportedCollectionType(ITypeSymbol? type)
{
return GetUnsupportedCollectionType(
type,
new HashSet<ITypeSymbol>(SymbolEqualityComparer.Default));
}
private string? GetUnsupportedCollectionType(
ITypeSymbol? type,
HashSet<ITypeSymbol> visitedTypes)
{
if (type is null || !visitedTypes.Add(type)) return null;
if (type is IArrayTypeSymbol arrayType)
return GetUnsupportedCollectionType(arrayType.ElementType, visitedTypes);
if (type is IPointerTypeSymbol pointerType)
return GetUnsupportedCollectionType(pointerType.PointedAtType, visitedTypes);
if (type is IFunctionPointerTypeSymbol functionPointerType)
{
var returnType = GetUnsupportedCollectionType(
functionPointerType.Signature.ReturnType,
visitedTypes);
if (returnType is not null) return returnType;
foreach (var parameter in functionPointerType.Signature.Parameters)
{
var parameterType = GetUnsupportedCollectionType(parameter.Type, visitedTypes);
if (parameterType is not null) return parameterType;
}
return null;
}
if (type is not INamedTypeSymbol namedType) return null;
var originalType = namedType.OriginalDefinition.ToString() ??
throw new ArgumentNullException(nameof(type));
if (_unsupportedCollectionTypes.Contains(originalType))
return originalType;
if (namedType.ContainingType is not null)
{
var containingType = GetUnsupportedCollectionType(
namedType.ContainingType,
visitedTypes);
if (containingType is not null) return containingType;
}
foreach (var typeArgument in namedType.TypeArguments)
{
var unsupportedType = GetUnsupportedCollectionType(typeArgument, visitedTypes);
if (unsupportedType is not null) return unsupportedType;
}
return null;
}
private static string GetSuggestedType(string originalType)
{
return originalType.Contains("Dictionary") ? "Map<TKey, TValue>" : "List<T>";
}
}
}