-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCompilationProcessor.cs
More file actions
440 lines (357 loc) · 16.8 KB
/
Copy pathCompilationProcessor.cs
File metadata and controls
440 lines (357 loc) · 16.8 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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
namespace Cometary
{
using Extensions;
/// <summary>
/// Class in charge of processing a <see cref="CSharpCompilation"/> by
/// finding, and initializing its <see cref="CometaryAttribute"/>s, thus
/// building a collection of <see cref="CompilationEditor"/>s that will be able
/// to edit the assembly to which this processor is bound.
/// </summary>
internal sealed class CompilationProcessor : IDisposable
{
#region Static
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> describing an unexpected exception
/// thrown by a <see cref="CompilationEditor"/>.
/// </summary>
public static DiagnosticDescriptor EditorError { get; }
= new DiagnosticDescriptor("EditorError", "Unexpected error", "Exception thrown by the '{0}' editor: '{1}'", Common.DiagnosticsCategory, DiagnosticSeverity.Error, true);
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> describing an unexpected exception
/// encountered when modifying a <see cref="CSharpCompilation"/>.
/// </summary>
public static DiagnosticDescriptor ProcessingError { get; }
= new DiagnosticDescriptor("ProcessingError", "Unexpected error", "Exception thrown during the {0} step: '{1}'. Stack trace: {2}.", Common.DiagnosticsCategory, DiagnosticSeverity.Error, true);
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> describing an unexpected exception
/// encountered when initializing a <see cref="CometaryAttribute"/>.
/// </summary>
public static DiagnosticDescriptor InitializationError { get; }
= new DiagnosticDescriptor("InitializationError", "Unexpected error", "Exception thrown during the initialization by the '{0}' attribute: '{1}'. Stack trace: {2}.", Common.DiagnosticsCategory, DiagnosticSeverity.Error, true);
#endregion
public List<CompilationEditor> Editors { get; }
public FlatteningList<Edit<CSharpCompilation>> CompilationPipeline { get; } = new FlatteningList<Edit<CSharpCompilation>>();
public FlatteningList<Edit<ISourceAssemblySymbol>> AssemblyPipeline { get; } = new FlatteningList<Edit<ISourceAssemblySymbol>>();
public bool IsInitialized { get; private set; }
public bool IsInitializationSuccessful { get; private set; }
public Action<Diagnostic> AddDiagnostic { get; }
public Func<IEnumerable<Diagnostic>> GetDiagnostics { get; }
public Store SharedStorage { get; }
/// <summary>
/// Delegate given by the <see cref="Hooks.CheckOptionsAndCreateModuleBuilder"/> method,
/// allowing the processor to compute the result of the original call before continuing.
/// </summary>
internal readonly Func<CSharpCompilation, object> getModuleBuilder;
/// <summary>
/// List of <see cref="Exception"/>s encountered during initialization,
/// before diagnostics could be added.
/// </summary>
internal readonly ImmutableArray<(Exception Exception, AttributeData Data)>.Builder initializationExceptions = ImmutableArray.CreateBuilder<(Exception, AttributeData)>();
private CompilationProcessor(
Func<CSharpCompilation, object> moduleBuilderGetter,
Action<Diagnostic> addDiagnostic,
Func<IEnumerable<Diagnostic>> getDiagnostics,
IEnumerable<CompilationEditor> editors)
{
Editors = new List<CompilationEditor>(editors);
SharedStorage = new Store();
AddDiagnostic = addDiagnostic;
GetDiagnostics = getDiagnostics;
getModuleBuilder = moduleBuilderGetter;
}
/// <summary>
/// Creates a new <see cref="CompilationProcessor"/>.
/// </summary>
public static CompilationProcessor Create(
Func<CSharpCompilation, object> moduleBuilderGetter,
Action<Diagnostic> addDiagnostic,
Func<IEnumerable<Diagnostic>> getDiagnostics,
params CompilationEditor[] editors)
{
Debug.Assert(editors != null);
Debug.Assert(editors.All(x => x != null));
return new CompilationProcessor(moduleBuilderGetter, addDiagnostic, getDiagnostics, editors);
}
#region Initialization
/// <summary>
/// Registers all <see cref="CometaryAttribute"/>s set on the given <paramref name="assembly"/>,
/// and every <see cref="CompilationEditor"/> returned by each of those attributes.
/// </summary>
public void RegisterAttributes(IAssemblySymbol assembly)
{
// Sort the attributes based on order and declaration
// Note: Since we're getting symbols here, the order is based
// on the order in which the files were read, and the order in code.
var attributes = assembly.GetAttributes();
// Find all used editors, and register 'em
var allEditors = new Dictionary<int, IList<CompilationEditor>>(attributes.Length);
int editorsCount = 0;
for (int i = 0; i < attributes.Length; i++)
{
AttributeData attr = attributes[i];
INamedTypeSymbol attrType = attr.AttributeClass;
// Make sure the attribute inherits CometaryAttribute
for (;;)
{
attrType = attrType.BaseType;
if (attrType == null)
goto NextAttribute;
if (attrType.Name == nameof(CometaryAttribute))
break;
}
// We got here: we have a cometary attribute
IEnumerable<CompilationEditor> editors;
int order;
try
{
editors = InitializeAttribute(attr, out order);
}
catch (TargetInvocationException e)
{
initializationExceptions.Add((e.InnerException, attr));
continue;
}
catch (TypeInitializationException e)
{
initializationExceptions.Add((e.InnerException, attr));
continue;
}
catch (Exception e)
{
initializationExceptions.Add((e, attr));
continue;
}
if (!allEditors.TryGetValue(order, out var editorsOfSameOrder))
{
editorsOfSameOrder = new LightList<CompilationEditor>();
allEditors[order] = editorsOfSameOrder;
}
foreach (CompilationEditor editor in editors)
{
if (editor == null)
continue;
editorsOfSameOrder.Add(editor);
editorsCount++;
}
NextAttribute:;
}
Editors.Capacity = Editors.Count + editorsCount;
Editors.AddRange(allEditors.OrderBy(x => x.Key).SelectMany(x => x.Value));
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static IEnumerable<CompilationEditor> InitializeAttribute(AttributeData data, out int order)
{
CometaryAttribute attribute = data.Construct<CometaryAttribute>();
order = attribute.Order;
return attribute.Initialize();
}
/// <summary>
/// Initializes the <see cref="CompilationProcessor"/>, and all its registered members.
/// </summary>
public bool TryInitialize(CSharpCompilation compilation, CancellationToken cancellationToken)
{
if (IsInitialized && IsInitializationSuccessful)
return true;
List<CompilationEditor> editors = Editors;
var addDiagnostic = AddDiagnostic;
CSharpCompilation clone = compilation.Clone();
IsInitialized = true;
// Log all previously encountered exceptions
initializationExceptions.Capacity = initializationExceptions.Count;
var exceptions = initializationExceptions.MoveToImmutable();
for (int i = 0; i < exceptions.Length; i++)
{
var (exception, data) = exceptions[i];
var location = data.ApplicationSyntaxReference.ToLocation();
addDiagnostic(Diagnostic.Create(InitializationError, location, data.AttributeClass, exception.Message.Filter(), exception.StackTrace.Filter()));
}
if (exceptions.Length > 0)
return false;
// Initialize all editors
int editorsCount = editors.Count;
for (int i = 0; i < editorsCount; i++)
{
CompilationEditor editor = editors[i];
try
{
// Register
if (!editor.TryRegister(this, addDiagnostic, clone, cancellationToken, out var children, out var exception))
{
addDiagnostic(Diagnostic.Create(EditorError, Location.None, editor.ToString(), exception.ToString()));
return false;
}
// Make sure no error was diagnosed by the editor
if (GetDiagnostics().Any(x => x.Severity == DiagnosticSeverity.Error))
{
return false;
}
// Optionally register some children
if (children == null || children.Length == 0)
continue;
editors.Capacity += children.Length;
for (int j = 0; j < children.Length; j++)
{
CompilationEditor child = children[j];
if (child == null)
{
addDiagnostic(Diagnostic.Create(
id: "MissingChild", category: Common.DiagnosticsCategory,
message: $"A child returned by the '{editor}' editor is null.",
severity: DiagnosticSeverity.Warning, defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true, warningLevel: 1, isSuppressed: false));
continue;
}
editors.Insert(i + j + 1, child);
editorsCount++;
}
// Since we insert them right after this one, the for loop will take care of initializing them easily
// => No recursion, baby
}
catch (Exception e)
{
while (e is TargetInvocationException tie)
e = tie.InnerException;
addDiagnostic(Diagnostic.Create(EditorError, Location.None, editor.ToString(), e.Message.Filter()));
return false;
}
}
// We got this far: the initialization is a success.
IsInitializationSuccessful = true;
return true;
}
/// <summary>
/// Attempts to uninitialize the processor.
/// </summary>
public bool TryUninitialize()
{
if (!IsInitialized || !IsInitializationSuccessful)
return false;
var editors = Editors;
// Uninitialize editors
for (int i = 0; i < editors.Count; i++)
{
editors[i].UnregisterAll(this);
}
return true;
}
#endregion
#region Editing
/// <summary>
/// Edits the given <paramref name="compilation"/>, and returns a value describing whether or
/// not an error was encountered during the edition.
/// </summary>
public bool TryEditCompilation(CSharpCompilation compilation, CancellationToken cancellationToken, out CSharpCompilation modified, out object outputBuilder)
{
modified = compilation;
if (!IsInitialized && !TryInitialize(compilation, cancellationToken) ||
IsInitialized && !IsInitializationSuccessful)
{
outputBuilder = null;
return false;
}
// Recompute compilation if needed
if (SharedStorage.TryGet(Helpers.RecomputeKey, out Pipeline<Func<CSharpParseOptions, CSharpParseOptions>> pipeline))
{
Func<CSharpParseOptions, CSharpParseOptions> del = pipeline.MakeDelegate(opts => opts);
modified = modified.RecomputeCompilationWithOptions(del, cancellationToken);
}
List<CompilationEditor> editors = Editors;
string step = "NotifyCompilationStart";
// Run the compilation
try
{
// Run the compilation
for (int i = 0; i < editors.Count; i++)
editors[i].TriggerCompilationStart(compilation);
step = "Preprocessing";
foreach (var edit in CompilationPipeline)
modified = edit(modified, cancellationToken) ?? modified;
step = "NotifyCompilationEnd";
// Notify of end of compilation, and start of emission
for (int i = 0; i < editors.Count; i++)
editors[i].TriggerCompilationEnd(compilation);
step = "NotifyEmissionStart";
for (int i = 0; i < editors.Count; i++)
editors[i].TriggerEmissionStart();
step = "Processing";
// Emit the assembly, and notify of start of emission
object moduleBuilder = getModuleBuilder(modified);
Type assemblySymbolInterf = typeof(IAssemblySymbol);
FieldInfo assemblyField = moduleBuilder.GetType().GetAllFields()
.First(x => x.FieldType.GetInterfaces().Contains(assemblySymbolInterf));
ISourceAssemblySymbol assemblySymbol = assemblyField.GetValue(moduleBuilder) as ISourceAssemblySymbol;
ISourceAssemblySymbol originalAssembly = assemblySymbol;
foreach (var edit in AssemblyPipeline)
assemblySymbol = edit(assemblySymbol, cancellationToken) ?? assemblySymbol;
step = "NotifyEmissionEnd";
// Notify of overall end
for (int i = 0; i < editors.Count; i++)
editors[i].TriggerEmissionEnd();
step = "Continuing";
// Copy modified assembly to builder
if (!ReferenceEquals(originalAssembly, assemblySymbol))
assemblyField.SetValue(moduleBuilder, assemblySymbol);
outputBuilder = moduleBuilder;
return true;
}
catch (Exception e)
{
do
{
if (e is DiagnosticException de)
{
AddDiagnostic(de.Diagnostic);
}
else if (e is AggregateException ae)
{
foreach (Exception ex in ae.InnerExceptions)
{
ReportDiagnostic(step, ex.Message, ex.Source);
}
}
else
{
ReportDiagnostic(step, e.Message, e.Source);
}
}
while ((e = e.InnerException) != null);
}
outputBuilder = null;
return false;
}
#endregion
/// <summary>
/// Reports a <see cref="Diagnostic"/>, using the <see cref="ProcessingError"/> descriptor.
/// </summary>
public void ReportDiagnostic(string step, string message, string stackTrace)
{
AddDiagnostic(Diagnostic.Create(ProcessingError, Location.None, step, message.Filter(), stackTrace.Filter()));
}
/// <inheritdoc />
public void Dispose()
{
foreach (var editor in Editors)
{
editor.UnregisterAll(this);
try
{
editor.Dispose();
}
catch
{
// Right now we don't care
}
}
}
}
}