-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathConfigurationBindingGenerator.cs
176 lines (153 loc) · 7.99 KB
/
ConfigurationBindingGenerator.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//#define LAUNCH_DEBUGGER
using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using SourceGenerators;
namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
/// <summary>
/// Generates source code to optimize binding with ConfigurationBinder.
/// </summary>
[Generator]
public sealed partial class ConfigurationBindingGenerator : IIncrementalGenerator
{
private static readonly string ProjectName = Emitter.s_assemblyName.Name!;
public const string GenSpecTrackingName = nameof(SourceGenerationSpec);
public void Initialize(IncrementalGeneratorInitializationContext context)
{
#if LAUNCH_DEBUGGER
if (!System.Diagnostics.Debugger.IsAttached)
{
System.Diagnostics.Debugger.Launch();
}
#endif
IncrementalValueProvider<CompilationData?> compilationData =
context.CompilationProvider
.Select((compilation, _) => compilation.Options is CSharpCompilationOptions options
? new CompilationData((CSharpCompilation)compilation)
: null);
IncrementalValueProvider<(SourceGenerationSpec?, ImmutableEquatableArray<DiagnosticInfo>?)> genSpec = context.SyntaxProvider
.CreateSyntaxProvider(
(node, _) => BinderInvocation.IsCandidateSyntaxNode(node),
BinderInvocation.Create)
.Where(invocation => invocation is not null)
.Collect()
.Combine(compilationData)
.Select((tuple, cancellationToken) =>
{
if (tuple.Right is not CompilationData compilationData)
{
return (null, null);
}
try
{
Parser parser = new(compilationData);
SourceGenerationSpec? spec = parser.GetSourceGenerationSpec(tuple.Left, cancellationToken);
ImmutableEquatableArray<DiagnosticInfo>? diagnostics = parser.Diagnostics?.ToImmutableEquatableArray();
return (spec, diagnostics);
}
catch (Exception ex)
{
throw ex;
}
})
.WithTrackingName(GenSpecTrackingName);
context.RegisterSourceOutput(genSpec, ReportDiagnosticsAndEmitSource);
if (!s_hasInitializedInterceptorVersion)
{
InterceptorVersion = DetermineInterceptableVersion();
s_hasInitializedInterceptorVersion = true;
}
}
internal static int InterceptorVersion { get; private set; }
// Used with v1 interceptor lightup approach:
private static bool s_hasInitializedInterceptorVersion;
internal static Func<SemanticModel, InvocationExpressionSyntax, CancellationToken, object>? GetInterceptableLocationFunc { get; private set; }
internal static MethodInfo? InterceptableLocationVersionGetDisplayLocation { get; private set; }
internal static MethodInfo? InterceptableLocationDataGetter { get; private set; }
internal static MethodInfo? InterceptableLocationVersionGetter { get; private set; }
internal static int DetermineInterceptableVersion()
{
MethodInfo? getInterceptableLocationMethod = null;
int? interceptableVersion = null;
#if UPDATE_BASELINES
#pragma warning disable RS1035 // Do not use APIs banned for analyzers
string? interceptableVersionString = Environment.GetEnvironmentVariable("InterceptableAttributeVersion");
#pragma warning restore RS1035
if (interceptableVersionString is not null)
{
if (int.TryParse(interceptableVersionString, out int version) && (version == 0 || version == 1))
{
interceptableVersion = version;
}
else
{
throw new InvalidOperationException($"Invalid InterceptableAttributeVersion value: {interceptableVersionString}");
}
}
if (interceptableVersion is null || interceptableVersion == 1)
#endif
{
getInterceptableLocationMethod = typeof(Microsoft.CodeAnalysis.CSharp.CSharpExtensions).GetMethod(
"GetInterceptableLocation",
BindingFlags.Static | BindingFlags.Public,
binder: null,
new Type[] { typeof(SemanticModel), typeof(InvocationExpressionSyntax), typeof(CancellationToken) },
modifiers: Array.Empty<ParameterModifier>());
interceptableVersion = getInterceptableLocationMethod is null ? 0 : 1;
}
if (interceptableVersion == 1)
{
GetInterceptableLocationFunc = (Func<SemanticModel, InvocationExpressionSyntax, CancellationToken, object>)
getInterceptableLocationMethod.CreateDelegate(typeof(Func<SemanticModel, InvocationExpressionSyntax, CancellationToken, object>), target: null);
Type? interceptableLocationType = typeof(Microsoft.CodeAnalysis.CSharp.CSharpExtensions).Assembly.GetType("Microsoft.CodeAnalysis.CSharp.InterceptableLocation");
InterceptableLocationVersionGetDisplayLocation = interceptableLocationType.GetMethod("GetDisplayLocation", BindingFlags.Instance | BindingFlags.Public);
InterceptableLocationVersionGetter = interceptableLocationType.GetProperty("Version", BindingFlags.Instance | BindingFlags.Public).GetGetMethod();
InterceptableLocationDataGetter = interceptableLocationType.GetProperty("Data", BindingFlags.Instance | BindingFlags.Public).GetGetMethod();
}
return interceptableVersion.Value;
}
/// <summary>
/// Instrumentation helper for unit tests.
/// </summary>
public Action<SourceGenerationSpec>? OnSourceEmitting { get; init; }
private void ReportDiagnosticsAndEmitSource(SourceProductionContext sourceProductionContext, (SourceGenerationSpec? SourceGenerationSpec, ImmutableEquatableArray<DiagnosticInfo>? Diagnostics) input)
{
if (input.Diagnostics is ImmutableEquatableArray<DiagnosticInfo> diagnostics)
{
foreach (DiagnosticInfo diagnostic in diagnostics)
{
sourceProductionContext.ReportDiagnostic(diagnostic.CreateDiagnostic());
}
}
if (input.SourceGenerationSpec is SourceGenerationSpec spec)
{
OnSourceEmitting?.Invoke(spec);
Emitter emitter = new(spec);
emitter.Emit(sourceProductionContext);
}
}
internal sealed class CompilationData
{
public bool LanguageVersionIsSupported { get; }
public KnownTypeSymbols? TypeSymbols { get; }
public CompilationData(CSharpCompilation compilation)
{
// We don't have a CSharp21 value available yet. Polyfill the value here for forward compat, rather than use the LanguageVersion.Preview enum value.
// /~https://github.com/dotnet/roslyn/blob/168689931cb4e3150641ec2fb188a64ce4b3b790/src/Compilers/CSharp/Portable/LanguageVersion.cs#L218-L232
const int LangVersion_CSharp12 = 1200;
LanguageVersionIsSupported = (int)compilation.LanguageVersion >= LangVersion_CSharp12;
if (LanguageVersionIsSupported)
{
TypeSymbols = new KnownTypeSymbols(compilation);
}
}
}
}
}