-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHooks.cs
245 lines (205 loc) · 9 KB
/
Hooks.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Ryder;
namespace Cometary
{
/*
* This class contains the static Hooks placed on Diagnostic.IsSuppressed,
* Compilation.CheckOptionsAndCreateModuleBuilder, etc.
*
* Here, everything is static. Right now the code is a mess, but it'd be nice:
* - Hooks are static, and completely independant. They don't care about CometaryManager,
* or anything of the sort.
* - CometaryManager is never static, and always used for a single compilation. It should be
* completely dependant on a compilation, and never share state.
*/
/// <summary>
/// Provides access to global methods used as replacement
/// by <see cref="Redirection"/>s.
/// </summary>
internal static class Hooks
{
#region Diagnostics
/// <summary>
/// List of <see cref="Diagnostic"/> predicates that may allow
/// suppression of certain errors.
/// </summary>
internal static readonly List<Predicate<Diagnostic>> DiagnosticPredicates;
/// <summary>
/// <see cref="MethodRedirection"/> that handles
/// redirection of the <see cref="Diagnostic.IsSuppressed"/> property.
/// </summary>
internal static readonly MethodRedirection DiagnosticSuppressionRedirection;
/// <summary>
/// Returns whether or not the specified <see cref="Diagnostic"/> is suppressed,
/// either by design (via <see cref="Diagnostic.IsSuppressed"/>), or by redirection
/// (via <see cref="CompilationEditor.SuppressDiagnostic"/>).
/// </summary>
internal static bool IsSuppressed(Diagnostic diagnostic)
{
if (diagnostic == null)
return false;
DiagnosticSuppressionRedirection.Stop();
if (diagnostic.IsSuppressed)
{
DiagnosticSuppressionRedirection.Start();
return true;
}
var predicates = DiagnosticPredicates;
try
{
for (int i = 0; i < predicates.Count; i++)
{
if (predicates[i](diagnostic))
return true;
}
return false;
}
catch
{
return false;
}
finally
{
DiagnosticSuppressionRedirection.Start();
}
}
#endregion
#region Compilation
/// <summary>
/// <see cref="MethodRedirection"/> that handles
/// redirection of the <see cref="CheckOptionsAndCreateModuleBuilder"/> method.
/// </summary>
internal static readonly ObservableRedirection CompilationRedirection;
/// <summary>
/// List of <see cref="CSharpCompilation"/>s that have already been modified.
/// This list is kept to ensure no compilation is modified more than once.
/// </summary>
internal static readonly List<CSharpCompilation> ModifiedCompilations;
/// <summary>
/// Injects the custom <see cref="CSharpCompilation"/> to the emitting process,
/// and resumes it.
/// </summary>
/// <seealso href="http://source.roslyn.io/#Microsoft.CodeAnalysis/Compilation/Compilation.cs,42341c66e909e676"/>
private static void CheckOptionsAndCreateModuleBuilder(RedirectionContext context)
{
// Sender is a CSharpCompilation
CSharpCompilation compilation = (CSharpCompilation)context.Sender;
CSharpCompilation clone = compilation.Clone();
// First argument is a DiagnosticBag
object diagnosticBag = context.Arguments[0];
Action<Diagnostic> addDiagnostic = Helpers.MakeAddDiagnostic(diagnosticBag);
Func<IEnumerable<Diagnostic>> getDiagnostics = Helpers.MakeGetDiagnostics(diagnosticBag);
object GetOriginal(CSharpCompilation newCompilation)
{
object[] args = new object[context.Arguments.Count];
context.Arguments.CopyTo(args, 0);
newCompilation.CopyTo(compilation);
return context.Invoke(args);
}
// CancellationToken should be last argument, but whatever.
CancellationToken cancellationToken = context.Arguments.OfType<CancellationToken>().FirstOrDefault();
// Edit the compilation (if a matching CometaryManager is found)
CompilationRedirection.Stop();
using (CompilationProcessor manager = CompilationProcessor.Create(GetOriginal, addDiagnostic, getDiagnostics))
{
manager.RegisterAttributes(compilation.Assembly);
// Edit the compilation, and emit it.
if (manager.TryEditCompilation(compilation, cancellationToken, out CSharpCompilation _, out object moduleBuilder))
{
// No error, we can keep going
context.ReturnValue = moduleBuilder;
addDiagnostic(Diagnostic.Create(
id: "ProcessSuccess",
category: Common.DiagnosticsCategory,
message: "Successfully edited the emitted compilation.",
severity: DiagnosticSeverity.Info,
defaultSeverity: DiagnosticSeverity.Info,
isEnabledByDefault: true,
warningLevel: -1,
isSuppressed: false));
}
else
{
// Keep going as if we were never here (the errors will be reported anyways)
clone.CopyTo(compilation);
context.ReturnValue = context.Invoke(context.Arguments.ToArray());
}
}
CompilationRedirection.Start();
}
#endregion
/// <summary>
/// Ensures all hooks have been set up.
/// </summary>
internal static void EnsureInitialized()
{
// Calling this method for the first time calls .cctor(), so there's nothing to do here.
}
/// <summary>
/// Ensures all hooks are active.
/// </summary>
internal static void EnsureActive()
{
CompilationRedirection.Start();
}
static Hooks()
{
#region Diagnostics
DiagnosticPredicates = new List<Predicate<Diagnostic>>();
// Initialize 'Diagnostic.IsSuppressed' redirection.
MethodInfo getIsSuppressed = typeof(Diagnostic)
.GetTypeInfo().Assembly
.GetType("Microsoft.CodeAnalysis.DiagnosticWithInfo")
.GetProperty(nameof(Diagnostic.IsSuppressed), BindingFlags.Instance | BindingFlags.Public)
.GetGetMethod();
MethodInfo getCustomIsSuppressed = typeof(Hooks)
.GetMethod(nameof(IsSuppressed), BindingFlags.Static | BindingFlags.NonPublic);
IsSuppressed(null);
// Make sure the method has been jitted
try
{
Diagnostic diagnostic = getIsSuppressed.DeclaringType
.GetTypeInfo().DeclaredConstructors.First()
.Invoke(new object[] { null, null, true }) as Diagnostic;
getIsSuppressed.Invoke(diagnostic, null);
}
catch (TargetInvocationException)
{
// Happened on purpose!
}
DiagnosticSuppressionRedirection = Redirection.Redirect(getIsSuppressed, getCustomIsSuppressed);
#endregion
#region Compilation
ModifiedCompilations = new List<CSharpCompilation>();
MethodInfo originalMethod = typeof(Compilation)
.GetMethod(nameof(CheckOptionsAndCreateModuleBuilder), BindingFlags.Instance | BindingFlags.NonPublic);
try
{
CSharpCompilation csc = CSharpCompilation.Create("Init");
ParameterInfo[] parameters = originalMethod.GetParameters();
object[] arguments = new object[parameters.Length];
for (int i = 0; i < parameters.Length; i++)
{
Type paramType = parameters[i].ParameterType;
arguments[i] = paramType.GetTypeInfo().IsValueType
? Activator.CreateInstance(paramType, true) // struct
: null; // class
}
originalMethod.Invoke(csc, arguments);
}
catch (TargetInvocationException)
{
// Happened on purpose!
}
CompilationRedirection = Redirection.Observe(originalMethod);
CompilationRedirection.Subscribe(CheckOptionsAndCreateModuleBuilder);
#endregion
}
}
}