-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestGenerator.cs
553 lines (460 loc) · 23.2 KB
/
TestGenerator.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using CodeExerciseLibrary.SourceGenerator.Extensions;
using CodeExerciseLibrary.SourceGenerator.Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.FlowAnalysis;
using Microsoft.CodeAnalysis.Text;
namespace CodeExerciseLibrary.SourceGenerator
{
[Generator]
public partial class TestGenerator : ISourceGenerator
{
private HashSet<string> GeneratedMethods;
public TestGenerator()
{
this.GeneratedMethods = new HashSet<string>();
}
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
}
public void Execute(GeneratorExecutionContext context)
{
if (context.SyntaxReceiver is not SyntaxReceiver receiver)
{
return;
}
Compilation compilation = context.Compilation;
foreach (ClassDeclarationSyntax @class in receiver.Classes)
{
NamespaceDeclarationSyntax? @namespace = @class.FindParent<NamespaceDeclarationSyntax>();
if (@namespace is null)
{
continue;
}
foreach (MemberDeclarationSyntax member in @class.Members)
{
if (member is not MethodDeclarationSyntax method || method.Body is null)
{
continue;
}
this.ProcessSyntax(context, ref compilation, @namespace, @class, method.Body);
}
}
foreach (LambdaExpressionSyntax lambda in receiver.Lambda)
{
NamespaceDeclarationSyntax? @namespace = lambda.FindParent<NamespaceDeclarationSyntax>();
ClassDeclarationSyntax? @class = lambda.FindParent<ClassDeclarationSyntax>();
if (@namespace is null || @class is null)
{
continue;
}
this.ProcessSyntax(context, ref compilation, @namespace, @class, lambda.Body);
}
}
private void ProcessSyntax(GeneratorExecutionContext context, ref Compilation compilation, NamespaceDeclarationSyntax @namespace, ClassDeclarationSyntax @class, CSharpSyntaxNode syntax)
{
switch (syntax)
{
case BlockSyntax block:
{
foreach (StatementSyntax statementSyntax in block.Statements)
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, statementSyntax);
}
break;
}
case InvocationExpressionSyntax invocation:
this.ProcessInvocation(context, ref compilation, @namespace, @class, invocation, null);
break;
case LocalDeclarationStatementSyntax declaration:
{
this.ProcessLocalDeclaration(context, ref compilation, @namespace, declaration);
foreach (VariableDeclaratorSyntax variableDeclaration in declaration.Declaration.Variables)
{
switch (variableDeclaration.Initializer?.Value)
{
case InvocationExpressionSyntax invocation:
this.ProcessInvocation(context, ref compilation, @namespace, @class, invocation, declaration.Declaration.Type);
break;
case ObjectCreationExpressionSyntax objectCreation:
this.ProcessObjectCreation(context, ref compilation, @namespace, @class, objectCreation);
break;
case SwitchExpressionSyntax @switch:
foreach (SwitchExpressionArmSyntax switchArm in @switch.Arms)
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, switchArm.Expression);
}
break;
}
}
break;
}
case ExpressionStatementSyntax expression:
{
if (expression.Expression is not InvocationExpressionSyntax invocation)
{
return;
}
this.ProcessInvocation(context, ref compilation, @namespace, @class, invocation, null);
break;
}
case UsingStatementSyntax @using:
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, @using.Statement);
break;
}
case TryStatementSyntax @try:
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, @try.Block);
foreach (CatchClauseSyntax @catch in @try.Catches)
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, @catch.Block);
}
if (@try.Finally is not null)
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, @try.Finally);
}
break;
}
case FinallyClauseSyntax @finally:
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, @finally.Block);
break;
}
case ForEachStatementSyntax @foreach:
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, @foreach.Statement);
break;
}
case ForStatementSyntax @for:
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, @for.Statement);
break;
}
case WhileStatementSyntax @while:
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, @while.Statement);
break;
}
case DoStatementSyntax @do:
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, @do.Statement);
this.ProcessSyntax(context, ref compilation, @namespace, @class, @do.Condition);
break;
}
case CheckedStatementSyntax @checked:
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, @checked.Block);
break;
}
case FixedStatementSyntax @fixed:
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, @fixed.Statement);
break;
}
case UnsafeStatementSyntax @unsafe:
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, @unsafe.Block);
break;
}
case LockStatementSyntax @lock:
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, @lock.Statement);
break;
}
case SwitchStatementSyntax @switch:
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, @switch.Expression);
foreach (SwitchSectionSyntax section in @switch.Sections)
{
foreach (StatementSyntax statement in section.Statements)
{
this.ProcessSyntax(context, ref compilation, @namespace, @class, statement);
}
}
break;
}
}
}
private void ProcessObjectCreation(GeneratorExecutionContext context, ref Compilation compilation, NamespaceDeclarationSyntax @namespace, ClassDeclarationSyntax @class, ObjectCreationExpressionSyntax objectCreation)
{
if (!(objectCreation.Initializer is null))
{
foreach (ExpressionSyntax initializerExpression in objectCreation.Initializer.Expressions)
{
if (initializerExpression is ObjectCreationExpressionSyntax initializerObjectCreation)
{
SemanticModel classMethodModel = compilation.GetSemanticModel(initializerObjectCreation.SyntaxTree);
SymbolInfo classTargetSymbol = classMethodModel.GetSymbolInfo(initializerObjectCreation.Type);
//If the type doesn't exist, create it
if (classTargetSymbol.Symbol is null)
{
this.GenerateEmptyClass(context, ref compilation, @namespace, initializerObjectCreation.Type.ToString());
}
this.ProcessObjectCreation(context, ref compilation, @namespace, @class, initializerObjectCreation);
}
}
}
//Ignore constructors without arguments
if (objectCreation.ArgumentList is null || objectCreation.ArgumentList.Arguments.Count <= 0)
{
return;
}
SemanticModel methodModel = compilation.GetSemanticModel(objectCreation.SyntaxTree);
SymbolInfo targetSymbol = methodModel.GetSymbolInfo(objectCreation.Type);
this.ProcessArguments(context, ref compilation, @namespace, @class, objectCreation.ArgumentList.Arguments);
//This is used for generating constructors only (for now, at least), type should exist already
if (targetSymbol.Symbol is null)
{
return;
}
SemanticModel methodModelOriginal = context.Compilation.GetSemanticModel(objectCreation.SyntaxTree);
SymbolInfo targetSymbolOriginal = methodModelOriginal.GetSymbolInfo(objectCreation.Type);
//Make sure we only add constructors for missing classes
if (!(targetSymbolOriginal.Symbol is null))
{
return;
}
string className = objectCreation.Type.ToString();
string arguments = CSharpMemberGenerator.GetArgumentList(objectCreation.ArgumentList.Arguments);
string identifier = $"{className}_{arguments.Length}.cs";
if (!this.GeneratedMethods.Add(identifier))
{
return;
}
string generateMissingMethod = CSharpMemberGenerator.GetEmptyClass(@namespace, className, arguments);
SourceText generatedMethod = SourceText.From(generateMissingMethod, Encoding.UTF8);
context.AddSource(identifier, generatedMethod);
}
private void ProcessLocalDeclaration(GeneratorExecutionContext context, ref Compilation compilation, NamespaceDeclarationSyntax @namespace, LocalDeclarationStatementSyntax declaration)
{
SemanticModel methodModel = compilation.GetSemanticModel(declaration.SyntaxTree);
SymbolInfo targetSymbol = methodModel.GetSymbolInfo(declaration.Declaration.Type);
//If the declaration type doesn't exist, create it
if (!(targetSymbol.Symbol is null))
{
return;
}
string className = declaration.Declaration.Type.ToString();
this.GenerateEmptyClass(context, ref compilation, @namespace, className);
}
private ITypeSymbol GenerateEmptyClass(GeneratorExecutionContext context, ref Compilation compilation, NamespaceDeclarationSyntax @namespace, string className)
{
if (!this.GeneratedMethods.Add($"{className}.cs"))
{
return compilation.GetTypeByMetadataName($"{@namespace.Name}.{className}")!;
}
SyntaxToken classIdentifier = SyntaxFactory.Identifier(className);
ClassDeclarationSyntax declarationClass = SyntaxFactory.ClassDeclaration(
identifier: classIdentifier,
modifiers: SyntaxTokenList.Create(SyntaxFactory.Token(SyntaxKind.PublicKeyword)),
attributeLists: default,
typeParameterList: default,
baseList: default,
constraintClauses: default,
members: default
);
NamespaceDeclarationSyntax newNamespace = SyntaxFactory.NamespaceDeclaration(
name: @namespace.Name,
members: new SyntaxList<MemberDeclarationSyntax>(declarationClass),
externs: default,
usings: default
);
CompilationUnitSyntax compilationUnit = SyntaxFactory.CompilationUnit(
members: new SyntaxList<MemberDeclarationSyntax>(newNamespace),
externs: default,
usings: default,
attributeLists: default
);
CSharpCompilation csharpCompilation = CSharpCompilation.Create(
assemblyName: "GeneratedClass",
syntaxTrees: ImmutableList.Create(compilationUnit.SyntaxTree),
references: new[]
{
MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
},
options: new CSharpCompilationOptions(outputKind: OutputKind.DynamicallyLinkedLibrary)
);
static byte[] Emit(CSharpCompilation compilation)
{
using MemoryStream memoryStream = new();
compilation.Emit(memoryStream);
return memoryStream.ToArray();
}
MetadataReference metadataReference = MetadataReference.CreateFromImage(Emit(csharpCompilation));
//Add the new generated class reference to the global compilation context
compilation = compilation.AddReferences(metadataReference);
string generatedMissingClass = CSharpMemberGenerator.GetEmptyClass(newNamespace, className);
SourceText generatedClass = SourceText.From(generatedMissingClass, Encoding.UTF8);
context.AddSource($"{className}.cs", generatedClass);
return compilation.GetTypeByMetadataName($"{@namespace.Name}.{declarationClass.Identifier}")!;
}
private void ProcessArguments(GeneratorExecutionContext context, ref Compilation compilation, NamespaceDeclarationSyntax @namespace, ClassDeclarationSyntax @class, SeparatedSyntaxList<ArgumentSyntax> arguments)
{
foreach (ArgumentSyntax argument in arguments)
{
switch (argument.Expression)
{
case InvocationExpressionSyntax argumentInvocation:
{
TypeSyntax argumentReturnType = SyntaxFactory.ParseTypeName("dynamic");
this.ProcessInvocation(context, ref compilation, @namespace, @class, argumentInvocation, argumentReturnType);
break;
}
case MemberAccessExpressionSyntax memberAccess:
{
IdentifierNameSyntax? targetIdentifier = memberAccess.DescendantNodes().OfType<IdentifierNameSyntax>().LastOrDefault();
if (targetIdentifier is null)
{
continue;
}
(bool isStatic, ITypeSymbol? extendingClass) = this.ProcessMemberAccess(context, ref compilation, @namespace, memberAccess);
if (!isStatic || extendingClass is null)
{
continue;
}
this.GenerateStaticField(context, @namespace, @class, extendingClass, targetIdentifier);
break;
}
}
}
}
private void ProcessInvocation(GeneratorExecutionContext context, ref Compilation compilation, NamespaceDeclarationSyntax @namespace, ClassDeclarationSyntax @class, InvocationExpressionSyntax invocation, TypeSyntax? returnType)
{
this.ProcessArguments(context, ref compilation, @namespace, @class, invocation.ArgumentList.Arguments);
if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess)
{
return;
}
(bool isStatic, ITypeSymbol? extendingClass) = this.ProcessMemberAccess(context, ref compilation, @namespace, memberAccess);
if (extendingClass is null)
{
return;
}
string arguments = CSharpMemberGenerator.GetArgumentList(invocation.ArgumentList.Arguments);
this.GenerateMethod(context, isStatic, @namespace, @class, extendingClass, memberAccess, arguments, "dynamic"); //Use dynamic as quick hack for return values
}
private (bool isStatic, ITypeSymbol? symbol) ProcessMemberAccess(GeneratorExecutionContext context, ref Compilation compilation, NamespaceDeclarationSyntax @namespace, MemberAccessExpressionSyntax memberAccess)
{
//Skip any generic method
if (memberAccess.DescendantNodes().OfType<GenericNameSyntax>().Any())
{
return default;
}
IdentifierNameSyntax targetIdentifier = memberAccess.DescendantNodes().OfType<IdentifierNameSyntax>().LastOrDefault();
if (targetIdentifier is null)
{
return default;
}
SemanticModel methodModel = compilation.GetSemanticModel(memberAccess.SyntaxTree);
SymbolInfo targetSymbol = methodModel.GetSymbolInfo(targetIdentifier);
//If we are not sure about the symbol we should just ditch without breaking everything by accident!
if (targetSymbol.CandidateReason != CandidateReason.None)
{
return default;
}
SymbolInfo methodSymbolInfo = methodModel.GetSymbolInfo(memberAccess);
//If there's no symbol for target then it doesn't exists
if (targetSymbol.Symbol is null)
{
IdentifierNameSyntax? last = null;
foreach (IdentifierNameSyntax node in memberAccess.DescendantNodes().OfType<IdentifierNameSyntax>())
{
if (node == targetIdentifier)
{
break;
}
last = node;
}
if (!(last is null))
{
targetSymbol = methodModel.GetSymbolInfo(last);
if (targetSymbol.Symbol is null)
{
targetIdentifier = last;
}
}
}
//If there's no symbol for method then it doesn't exists
if (!(methodSymbolInfo.Symbol is null))
{
return default;
}
bool isStatic = false;
ITypeSymbol extendingClass;
switch (targetSymbol.Symbol)
{
case ITypeSymbol typeSymbol:
extendingClass = typeSymbol;
isStatic = true;
break;
case ILocalSymbol localSymbol:
extendingClass = localSymbol.Type;
break;
case IMethodSymbol methodSymbol:
extendingClass = methodSymbol.ReturnType;
break;
default:
{
//Ehh... Lets assume its a missing static class? Ehheheh...
ITypeSymbol symbol = this.GenerateEmptyClass(context, ref compilation, @namespace, targetIdentifier.ToString());
return (true, symbol);
}
}
return (isStatic, extendingClass);
}
private void GenerateMethod(GeneratorExecutionContext context, bool isStatic, NamespaceDeclarationSyntax @namespace, ClassDeclarationSyntax @class, ITypeSymbol extendingClass, MemberAccessExpressionSyntax invokeMember, string arguments, string returnType)
{
string identifier = $"{extendingClass.Name}_{invokeMember.Name}_{arguments.Length}_{returnType}.cs";
if (!this.GeneratedMethods.Add(identifier))
{
return;
}
string generateMissingMethod = isStatic
? CSharpMemberGenerator.GetStaticMethod(@namespace, @class, extendingClass, invokeMember, arguments, returnType)
: CSharpMemberGenerator.GetInstanceMethod(@namespace, extendingClass, invokeMember, arguments, returnType);
SourceText generatedMethod = SourceText.From(generateMissingMethod, Encoding.UTF8);
context.AddSource(identifier, generatedMethod);
}
private void GenerateStaticField(GeneratorExecutionContext context, NamespaceDeclarationSyntax @namespace, ClassDeclarationSyntax @class, ITypeSymbol extendingClass, IdentifierNameSyntax targetIdentifer)
{
string identifier = $"{extendingClass.Name}_{targetIdentifer}.cs";
if (!this.GeneratedMethods.Add(identifier))
{
return;
}
string generateMissingField = CSharpMemberGenerator.GetStaticField(@namespace, @class, extendingClass, targetIdentifer);
SourceText generatedField = SourceText.From(generateMissingField, Encoding.UTF8);
context.AddSource(identifier, generatedField);
}
private class SyntaxReceiver : ISyntaxReceiver
{
internal List<ClassDeclarationSyntax> Classes { get; } = new();
internal List<LambdaExpressionSyntax> Lambda { get; } = new();
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
switch (syntaxNode)
{
case ClassDeclarationSyntax @class:
this.Classes.Add(@class);
break;
case LambdaExpressionSyntax lambda:
this.Lambda.Add(lambda);
break;
}
}
}
}
}