-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathBindingTypeSemanticAnalyzer.cs
More file actions
273 lines (248 loc) · 10.6 KB
/
BindingTypeSemanticAnalyzer.cs
File metadata and controls
273 lines (248 loc) · 10.6 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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.Macios.Bindings.Analyzer.Extensions;
using Microsoft.Macios.Generator;
namespace Microsoft.Macios.Bindings.Analyzer;
/// <summary>
/// Analyzer that ensures that the types that have been declared as binding types are partial and follow the correct
/// pattern.
/// </summary>
[DiagnosticAnalyzer (LanguageNames.CSharp)]
public class BindingTypeSemanticAnalyzer : DiagnosticAnalyzer, IBindingTypeAnalyzer<BaseTypeDeclarationSyntax> {
/// <summary>
/// All binding types should be partial.
/// </summary>
internal static readonly DiagnosticDescriptor RBI0001 = new (
"RBI0001",
new LocalizableResourceString (nameof (Resources.RBI0001Title), Resources.ResourceManager, typeof (Resources)),
new LocalizableResourceString (nameof (Resources.RBI0001MessageFormat), Resources.ResourceManager,
typeof (Resources)),
"Usage",
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: new LocalizableResourceString (nameof (Resources.RBI0001Description), Resources.ResourceManager,
typeof (Resources))
);
/// <summary>
/// BindingType<Class> can only decorate partial classes.
/// </summary>
internal static readonly DiagnosticDescriptor RBI0002 = new (
"RBI0002",
new LocalizableResourceString (nameof (Resources.RBI0002Title), Resources.ResourceManager, typeof (Resources)),
new LocalizableResourceString (nameof (Resources.RBI0002MessageFormat), Resources.ResourceManager,
typeof (Resources)),
"Usage",
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: new LocalizableResourceString (nameof (Resources.RBI0002Description), Resources.ResourceManager,
typeof (Resources))
);
/// <summary>
/// BindingType<Category> can only decorate partial classes.
/// </summary>
internal static readonly DiagnosticDescriptor RBI0003 = new (
"RBI0003",
new LocalizableResourceString (nameof (Resources.RBI0003Title), Resources.ResourceManager, typeof (Resources)),
new LocalizableResourceString (nameof (Resources.RBI0003MessageFormat), Resources.ResourceManager,
typeof (Resources)),
"Usage",
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: new LocalizableResourceString (nameof (Resources.RBI0003Description), Resources.ResourceManager,
typeof (Resources))
);
/// <summary>
/// BindingType<Category> can only decorate static classes.
/// </summary>
internal static readonly DiagnosticDescriptor RBI0004 = new (
"RBI0004",
new LocalizableResourceString (nameof (Resources.RBI0004Title), Resources.ResourceManager, typeof (Resources)),
new LocalizableResourceString (nameof (Resources.RBI0004MessageFormat), Resources.ResourceManager,
typeof (Resources)),
"Usage",
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: new LocalizableResourceString (nameof (Resources.RBI0004Description), Resources.ResourceManager,
typeof (Resources))
);
/// <summary>
/// BindingType<Protocol> can only decorate interfaces.
/// </summary>
internal static readonly DiagnosticDescriptor RBI0005 = new (
"RBI0005",
new LocalizableResourceString (nameof (Resources.RBI0005Title), Resources.ResourceManager, typeof (Resources)),
new LocalizableResourceString (nameof (Resources.RBI0005MessageFormat), Resources.ResourceManager,
typeof (Resources)),
"Usage",
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: new LocalizableResourceString (nameof (Resources.RBI0005Description), Resources.ResourceManager,
typeof (Resources))
);
/// <summary>
/// BindingType can only decorate enumerators.
/// </summary>
internal static readonly DiagnosticDescriptor RBI0006 = new (
"RBI0006",
new LocalizableResourceString (nameof (Resources.RBI0006Title), Resources.ResourceManager, typeof (Resources)),
new LocalizableResourceString (nameof (Resources.RBI0006MessageFormat), Resources.ResourceManager,
typeof (Resources)),
"Usage",
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: new LocalizableResourceString (nameof (Resources.RBI0006Description), Resources.ResourceManager,
typeof (Resources))
);
internal static readonly DiagnosticDescriptor RBI0007 = new (
"RBI0007",
new LocalizableResourceString (nameof (Resources.RBI0007Title), Resources.ResourceManager, typeof (Resources)),
new LocalizableResourceString (nameof (Resources.RBI0007MessageFormat), Resources.ResourceManager,
typeof (Resources)),
"Usage",
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: new LocalizableResourceString (nameof (Resources.RBI0007Description), Resources.ResourceManager,
typeof (Resources))
);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = [
RBI0001,
RBI0002,
RBI0003,
RBI0004,
RBI0005,
RBI0006,
RBI0007,
];
public override void Initialize (AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis (GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution ();
context.RegisterSyntaxNodeAction (AnalysisContext,
SyntaxKind.ClassDeclaration,
SyntaxKind.InterfaceDeclaration,
SyntaxKind.EnumDeclaration);
}
void AnalysisContext (SyntaxNodeAnalysisContext context)
=> this.AnalyzeBindingType (context);
static readonly HashSet<string> attributes = new HashSet<string> (AttributesNames.BindingTypes);
public IReadOnlySet<string> AttributeNames => attributes;
ImmutableArray<Diagnostic> ValidateClass (BaseTypeDeclarationSyntax declarationNode, INamedTypeSymbol symbol)
{
var bucket = ImmutableArray.CreateBuilder<Diagnostic> ();
if (declarationNode is ClassDeclarationSyntax classDeclarationSyntax) {
if (!classDeclarationSyntax.IsPartial ()) {
var partialDiagnostic = Diagnostic.Create (
descriptor: RBI0001, // Binding types should be declared as partial classes.
location: declarationNode.Identifier.GetLocation (),
messageArgs: symbol.ToDisplayString ());
bucket.Add (partialDiagnostic);
}
} else {
var notAClassDiagnostic = Diagnostic.Create (
descriptor: RBI0002, // BindingType<Class> must be on a class
location: declarationNode.Identifier.GetLocation (),
messageArgs: symbol.ToDisplayString ());
bucket.Add (notAClassDiagnostic);
}
return bucket.ToImmutable ();
}
ImmutableArray<Diagnostic> ValidateCategory (BaseTypeDeclarationSyntax declarationNode, INamedTypeSymbol symbol)
{
var bucket = ImmutableArray.CreateBuilder<Diagnostic> ();
if (declarationNode is ClassDeclarationSyntax classDeclarationSyntax) {
if (!classDeclarationSyntax.IsPartial ()) {
var partialDiagnostic = Diagnostic.Create (
descriptor: RBI0001, // Binding types should be declared as partial classes.
location: declarationNode.Identifier.GetLocation (),
messageArgs: symbol.ToDisplayString ());
bucket.Add (partialDiagnostic);
}
if (!classDeclarationSyntax.IsStatic ()) {
var partialDiagnostic = Diagnostic.Create (
descriptor: RBI0004, // BindingType<Category> must be on a static class
location: declarationNode.Identifier.GetLocation (),
messageArgs: symbol.ToDisplayString ());
bucket.Add (partialDiagnostic);
}
} else {
var notAClassDiagnostic = Diagnostic.Create (
descriptor: RBI0003, // BindingType<Category> must be on a class
location: declarationNode.Identifier.GetLocation (),
messageArgs: symbol.ToDisplayString ());
bucket.Add (notAClassDiagnostic);
}
return bucket.ToImmutable ();
}
ImmutableArray<Diagnostic> ValidateProtocol (BaseTypeDeclarationSyntax declarationNode, INamedTypeSymbol symbol)
{
var bucket = ImmutableArray.CreateBuilder<Diagnostic> ();
if (declarationNode is InterfaceDeclarationSyntax interfaceDeclarationSyntax) {
if (!interfaceDeclarationSyntax.IsPartial ()) {
var partialDiagnostic = Diagnostic.Create (
descriptor: RBI0001, // Binding types should be declared as partial classes.
location: declarationNode.Identifier.GetLocation (),
messageArgs: symbol.ToDisplayString ());
bucket.Add (partialDiagnostic);
}
} else {
var notAInterfaceDiagnostic = Diagnostic.Create (
descriptor: RBI0005, // BindingType<Protocol> must be on an interface
location: declarationNode.Identifier.GetLocation (),
messageArgs: symbol.ToDisplayString ());
bucket.Add (notAInterfaceDiagnostic);
}
return bucket.ToImmutable ();
}
ImmutableArray<Diagnostic> ValidateStrongDictionary (BaseTypeDeclarationSyntax declarationNode,
INamedTypeSymbol symbol)
{
var bucket = ImmutableArray.CreateBuilder<Diagnostic> ();
if (declarationNode is ClassDeclarationSyntax classDeclarationSyntax) {
if (!classDeclarationSyntax.IsPartial ()) {
var partialDiagnostic = Diagnostic.Create (
descriptor: RBI0001, // Binding types should be declared as partial classes.
location: declarationNode.Identifier.GetLocation (),
messageArgs: symbol.ToDisplayString ());
bucket.Add (partialDiagnostic);
}
} else {
var notAInterfaceDiagnostic = Diagnostic.Create (
descriptor: RBI0007, // BindingType<StrongDictionary> must be on a class
location: declarationNode.Identifier.GetLocation (),
messageArgs: symbol.ToDisplayString ());
bucket.Add (notAInterfaceDiagnostic);
}
return bucket.ToImmutable ();
}
public ImmutableArray<Diagnostic> ValidateSmartEnum (BaseTypeDeclarationSyntax declarationNode,
INamedTypeSymbol symbol)
{
var bucket = ImmutableArray.CreateBuilder<Diagnostic> ();
if (declarationNode is not EnumDeclarationSyntax) {
var notAInterfaceDiagnostic = Diagnostic.Create (
descriptor: RBI0006, // BindingType must be on an enumerator
location: declarationNode.Identifier.GetLocation (),
messageArgs: symbol.ToDisplayString ());
bucket.Add (notAInterfaceDiagnostic);
}
return bucket.ToImmutable ();
}
public ImmutableArray<Diagnostic> Analyze (string matchedAttribute, PlatformName _,
BaseTypeDeclarationSyntax declarationNode, INamedTypeSymbol symbol)
=> matchedAttribute switch {
AttributesNames.ClassAttribute => ValidateClass (declarationNode, symbol),
AttributesNames.CategoryAttribute => ValidateCategory (declarationNode, symbol),
AttributesNames.ProtocolAttribute => ValidateProtocol (declarationNode, symbol),
AttributesNames.SmartEnumAttribute => ValidateSmartEnum (declarationNode, symbol),
AttributesNames.StrongDictionaryAttribute => ValidateStrongDictionary (declarationNode, symbol),
_ => throw new InvalidOperationException ($"Not recognized attribute {matchedAttribute}.")
};
}