-
Notifications
You must be signed in to change notification settings - Fork 10
/
BinaryLogReader.cs
507 lines (442 loc) · 19 KB
/
BinaryLogReader.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
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Basic.CompilerLog.Util.Impl;
using MessagePack.Formatters;
using Microsoft.Build.Logging.StructuredLogger;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.VisualBasic;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
namespace Basic.CompilerLog.Util;
public sealed class BinaryLogReader : ICompilerCallReader, IBasicAnalyzerHostDataProvider
{
private Stream _stream;
private readonly bool _leaveOpen;
private readonly Dictionary<string, PortableExecutableReference> _metadataReferenceMap = new(PathUtil.Comparer);
private readonly Dictionary<string, AssemblyIdentityData> _assemblyIdentityDataMap = new(PathUtil.Comparer);
private readonly Dictionary<CompilerCall, CommandLineArguments> _argumentsMap = new();
private readonly Lazy<List<CompilerCall>> _lazyCompilerCalls;
private readonly Lazy<Dictionary<Guid, int>> _lazyMvidToCompilerCallIndexMap;
public bool OwnsLogReaderState { get; }
public LogReaderState LogReaderState { get; }
public BasicAnalyzerKind BasicAnalyzerKind { get; }
public bool IsDisposed => _stream is null;
private BinaryLogReader(Stream stream, bool leaveOpen, BasicAnalyzerKind? basicAnalyzerKind, LogReaderState? state)
{
_stream = stream;
BasicAnalyzerKind = basicAnalyzerKind ?? BasicAnalyzerHost.DefaultKind;
OwnsLogReaderState = state is null;
LogReaderState = state ?? new LogReaderState();
_leaveOpen = leaveOpen;
_lazyCompilerCalls = new(() =>
{
_stream.Position = 0;
return BinaryLogUtil.ReadAllCompilerCalls(_stream, ownerState: this);
});
_lazyMvidToCompilerCallIndexMap = new(() => BuildMvidToCompilerCallIndexMap());
}
public static BinaryLogReader Create(
Stream stream,
BasicAnalyzerKind? basicAnalyzerKind,
LogReaderState? state,
bool leaveOpen) =>
new BinaryLogReader(stream, leaveOpen, basicAnalyzerKind, state);
public static BinaryLogReader Create(
Stream stream,
BasicAnalyzerKind? basicAnalyzerKind = null,
bool leaveOpen = true) =>
Create(stream, basicAnalyzerKind, state: null, leaveOpen);
public static BinaryLogReader Create(
Stream stream,
LogReaderState? state,
bool leaveOpen = true) =>
Create(stream, basicAnalyzerKind: null, state: state, leaveOpen);
public static BinaryLogReader Create(
string filePath,
BasicAnalyzerKind? basicAnalyzerKind = null,
LogReaderState? state = null)
{
var stream = RoslynUtil.OpenBuildFileForRead(filePath);
return Create(stream, basicAnalyzerKind, state: state, leaveOpen: false);
}
public void Dispose()
{
if (IsDisposed)
{
return;
}
if (OwnsLogReaderState)
{
LogReaderState.Dispose();
}
if (!_leaveOpen)
{
_stream.Dispose();
}
_stream = null!;
}
public List<CompilerCall> ReadAllCompilerCalls(Func<CompilerCall, bool>? predicate = null)
{
predicate ??= static _ => true;
return _lazyCompilerCalls.Value.Where(predicate).ToList();
}
public List<CompilationData> ReadAllCompilationData(Func<CompilerCall, bool>? predicate = null)
{
var list = new List<CompilationData>();
foreach (var compilerCall in ReadAllCompilerCalls(predicate))
{
list.Add(ReadCompilationData(compilerCall));
}
return list;
}
public CompilerCall ReadCompilerCall(int index) =>
_lazyCompilerCalls.Value[index];
public CompilerCallData ReadCompilerCallData(CompilerCall compilerCall) =>
ReadCompilerCallDataCore(compilerCall).CompilerCallData;
private (CompilerCallData CompilerCallData, CommandLineArguments Arguments) ReadCompilerCallDataCore(CompilerCall compilerCall)
{
var args = ReadCommandLineArguments(compilerCall);
var assemblyFileName = RoslynUtil.GetAssemblyFileName(args);
var data = new CompilerCallData(
compilerCall,
assemblyFileName,
args.OutputDirectory,
args.ParseOptions,
args.CompilationOptions,
args.EmitOptions);
return (data, args);
}
/// <summary>
/// Reads the <see cref="CommandLineArguments"/> for the given <see cref="CompilerCall"/>.
/// </summary>
/// <remarks>
/// !!!WARNING!!!
///
/// This method is only valid when this instance represents a compilation on the disk of the
/// current machine. In any other scenario this will lead to mostly correct but potentially
/// incorrect results.
///
/// This method is on <see cref="BinaryLogReader"/> as its presence is a stronger indicator
/// that the necessary data is on disk.
/// </remarks>
public CommandLineArguments ReadCommandLineArguments(CompilerCall compilerCall)
{
CheckOwnership(compilerCall);
if (!_argumentsMap.TryGetValue(compilerCall, out var args))
{
args = BinaryLogUtil.ReadCommandLineArgumentsUnsafe(compilerCall);
_argumentsMap[compilerCall] = args;
}
return args;
}
public CompilationData ReadCompilationData(CompilerCall compilerCall)
{
CheckOwnership(compilerCall);
var (compilerCallData, args) = ReadCompilerCallDataCore(compilerCall);
var references = GetReferences();
var sourceTexts = GetSourceTexts();
var additionalTexts = GetAdditionalTexts();
var analyzerConfigs = GetAnalyzerConfigs();
var emitData = GetEmitData();
var basicAnalyzerHost = CreateBasicAnalyzerHost(compilerCall);
return compilerCall.IsCSharp ? GetCSharp() : GetVisualBasic();
CSharpCompilationData GetCSharp()
{
return RoslynUtil.CreateCSharpCompilationData(
compilerCall,
compilerCallData.AssemblyFileName,
(CSharpParseOptions)args.ParseOptions,
(CSharpCompilationOptions)args.CompilationOptions,
sourceTexts,
references,
analyzerConfigs,
additionalTexts,
args.EmitOptions,
emitData,
basicAnalyzerHost,
PathNormalizationUtil.Empty);
}
VisualBasicCompilationData GetVisualBasic()
{
return RoslynUtil.CreateVisualBasicCompilationData(
compilerCall,
args.CompilationName,
(VisualBasicParseOptions)args.ParseOptions,
(VisualBasicCompilationOptions)args.CompilationOptions,
sourceTexts,
references,
analyzerConfigs,
additionalTexts,
args.EmitOptions,
emitData,
basicAnalyzerHost,
PathNormalizationUtil.Empty);
}
List<(SourceText SourceText, string Path)> GetAnalyzerConfigs() =>
GetSourceTextsFromPaths(args.AnalyzerConfigPaths, args.AnalyzerConfigPaths.Length, args.ChecksumAlgorithm);
List<MetadataReference> GetReferences()
{
var list = new List<MetadataReference>(capacity: args.MetadataReferences.Length);
foreach (var reference in args.MetadataReferences)
{
var mdRef = ReadMetadataReference(reference.Reference).With(reference.Properties.Aliases, reference.Properties.EmbedInteropTypes);
list.Add(mdRef);
}
return list;
}
List<(SourceText SourceText, string Path)> GetSourceTexts() =>
GetSourceTextsFromPaths(args.SourceFiles.Select(x => x.Path), args.SourceFiles.Length, args.ChecksumAlgorithm);
static List<(SourceText SourceText, string Path)> GetSourceTextsFromPaths(IEnumerable<string> filePaths, int count, SourceHashAlgorithm checksumAlgorithm)
{
var list = new List<(SourceText, string)>(capacity: count);
foreach (var filePath in filePaths)
{
var sourceText = RoslynUtil.GetSourceText(filePath, checksumAlgorithm, canBeEmbedded: false);
list.Add((sourceText, filePath));
}
return list;
}
ImmutableArray<AdditionalText> GetAdditionalTexts()
{
var builder = ImmutableArray.CreateBuilder<AdditionalText>(args.AdditionalFiles.Length);
foreach (var additionalFile in args.AdditionalFiles)
{
var sourceText = RoslynUtil.GetSourceText(additionalFile.Path, args.ChecksumAlgorithm, canBeEmbedded: false);
var additionalText = new BasicAdditionalTextFile(
additionalFile.Path,
sourceText);
builder.Add(additionalText);
}
return builder.MoveToImmutable();
}
EmitData GetEmitData()
{
return new EmitData(
RoslynUtil.GetAssemblyFileName(args),
args.DocumentationPath,
win32ResourceStream: ReadFileAsMemoryStream(args.Win32ResourceFile),
sourceLinkStream: ReadFileAsMemoryStream(args.SourceLink),
resources: args.ManifestResources,
embeddedTexts: GetEmbeddedTexts());
}
MemoryStream? ReadFileAsMemoryStream(string? filePath)
{
if (filePath is null)
{
return null;
}
var bytes = File.ReadAllBytes(filePath);
return new MemoryStream(bytes);
}
IEnumerable<EmbeddedText>? GetEmbeddedTexts()
{
if (args.EmbeddedFiles.Length == 0)
{
return null;
}
var list = new List<EmbeddedText>(args.EmbeddedFiles.Length);
foreach (var item in args.EmbeddedFiles)
{
var sourceText = RoslynUtil.GetSourceText(item.Path, args.ChecksumAlgorithm, canBeEmbedded: true);
var embeddedText = EmbeddedText.FromSource(item.Path, sourceText);
list.Add(embeddedText);
}
return list;
}
}
public BasicAnalyzerHost CreateBasicAnalyzerHost(CompilerCall compilerCall)
{
var args = ReadCommandLineArguments(compilerCall);
var list = ReadAllAnalyzerData(compilerCall);
return LogReaderState.GetOrCreate(
BasicAnalyzerKind,
list,
(kind, analyzers) => kind switch
{
BasicAnalyzerKind.None => CreateNoneHost(),
BasicAnalyzerKind.OnDisk => new BasicAnalyzerHostOnDisk(this, analyzers),
BasicAnalyzerKind.InMemory => new BasicAnalyzerHostInMemory(this, analyzers),
_ => throw new ArgumentOutOfRangeException(nameof(kind)),
});
BasicAnalyzerHostNone CreateNoneHost()
{
if (!RoslynUtil.HasGeneratedFilesInPdb(args))
{
return new BasicAnalyzerHostNone("Compilation does not have a PDB compatible with generated files");
}
try
{
var generatedFiles = RoslynUtil.ReadGeneratedFiles(compilerCall, args);
var builder = ImmutableArray.CreateBuilder<(SourceText SourceText, string Path)>(generatedFiles.Count);
foreach (var tuple in generatedFiles)
{
var sourceText = RoslynUtil.GetSourceText(tuple.Stream, args.ChecksumAlgorithm, canBeEmbedded: false);
builder.Add((sourceText, tuple.FilePath));
}
return new BasicAnalyzerHostNone(builder.MoveToImmutable());
}
catch (Exception ex)
{
return new BasicAnalyzerHostNone(ex.Message);
}
}
}
public SourceText ReadSourceText(SourceTextData sourceTextData) =>
RoslynUtil.GetSourceText(sourceTextData.FilePath, sourceTextData.ChecksumAlgorithm, canBeEmbedded: false);
public List<ReferenceData> ReadAllReferenceData(CompilerCall compilerCall)
{
CheckOwnership(compilerCall);
var args = ReadCommandLineArguments(compilerCall);
return ReadAllReferenceDataCore(args.MetadataReferences, args.MetadataReferences.Length);
}
public List<CompilerAssemblyData> ReadAllCompilerAssemblies()
{
var list = new List<(string CompilerFilePath, AssemblyName AssemblyName)>();
var map = new Dictionary<string, (AssemblyName, string?)>(PathUtil.Comparer);
foreach (var compilerCall in ReadAllCompilerCalls())
{
if (compilerCall.CompilerFilePath is string compilerFilePath &&
!map.ContainsKey(compilerFilePath))
{
AssemblyName name;
string? commitHash;
try
{
name = AssemblyName.GetAssemblyName(compilerFilePath);
commitHash = RoslynUtil.ReadCompilerCommitHash(compilerFilePath);
}
catch
{
name = new AssemblyName(Path.GetFileName(compilerFilePath));
commitHash = null;
}
map[compilerCall.CompilerFilePath] = (name, commitHash);
}
}
return map
.OrderBy(x => x.Key, PathUtil.Comparer)
.Select(x => new CompilerAssemblyData(x.Key, x.Value.Item1, x.Value.Item2))
.ToList();
}
/// <inheritdoc cref="ICompilerCallReader.HasAllGeneratedFileContent(CompilerCall)"/>
public bool HasAllGeneratedFileContent(CompilerCall compilerCall) =>
RoslynUtil.HasGeneratedFilesInPdb(ReadCommandLineArguments(compilerCall));
/// <summary>
/// Attempt to add all the generated files from generators. When successful the generators
/// don't need to be run when re-hydrating the compilation.
/// </summary>
/// <remarks>
/// This method will throw if the compilation does not have a PDB compatible with generated files
/// available to read
/// </remarks>
public List<(string FilePath, MemoryStream Stream)> ReadAllGeneratedFiles(CompilerCall compilerCall)
{
var args = ReadCommandLineArguments(compilerCall);
return RoslynUtil.ReadGeneratedFiles(compilerCall, args);
}
public List<SourceTextData> ReadAllSourceTextData(CompilerCall compilerCall)
{
var args = ReadCommandLineArguments(compilerCall);
var list = new List<SourceTextData>(args.SourceFiles.Length + args.AnalyzerConfigPaths.Length + args.AdditionalFiles.Length);
list.AddRange(args.SourceFiles.Select(x => new SourceTextData(this, x.Path, args.ChecksumAlgorithm, SourceTextKind.SourceCode)));
list.AddRange(args.AnalyzerConfigPaths.Select(x => new SourceTextData(this, x, args.ChecksumAlgorithm, SourceTextKind.AnalyzerConfig)));
list.AddRange(args.AdditionalFiles.Select(x => new SourceTextData(this, x.Path, args.ChecksumAlgorithm, SourceTextKind.AnalyzerConfig)));
return list;
}
private AssemblyIdentityData GetOrReadAssemblyIdentityData(string filePath)
{
if (!_assemblyIdentityDataMap.TryGetValue(filePath, out var assemblyIdentityData))
{
assemblyIdentityData = RoslynUtil.ReadAssemblyIdentityData(filePath);
_assemblyIdentityDataMap[filePath] = assemblyIdentityData;
}
return assemblyIdentityData;
}
private List<ReferenceData> ReadAllReferenceDataCore(IEnumerable<CommandLineReference> commandLineReferences, int count)
{
var list = new List<ReferenceData>(capacity: count);
foreach (var commandLineReference in commandLineReferences)
{
var identityData = GetOrReadAssemblyIdentityData(commandLineReference.Reference);
var data = new ReferenceData(
identityData,
commandLineReference.Reference,
commandLineReference.Properties.Aliases,
commandLineReference.Properties.EmbedInteropTypes);
list.Add(data);
}
return list;
}
public List<AnalyzerData> ReadAllAnalyzerData(CompilerCall compilerCall)
{
var args = ReadCommandLineArguments(compilerCall);
var list = new List<AnalyzerData>(args.AnalyzerReferences.Length);
foreach (var analyzer in args.AnalyzerReferences)
{
var identityData = GetOrReadAssemblyIdentityData(analyzer.FilePath);
var data = new AnalyzerData(identityData, analyzer.FilePath);
list.Add(data);
}
return list;
}
private void CheckOwnership(CompilerCall compilerCall)
{
if (compilerCall.OwnerState is BinaryLogReader reader && object.ReferenceEquals(reader, this))
{
return;
}
throw new ArgumentException($"The provided {nameof(CompilerCall)} is not from this instance");
}
public MetadataReference ReadMetadataReference(ReferenceData referenceData)
{
var mdRef = ReadMetadataReference(referenceData.FilePath);
return mdRef.With(referenceData.Aliases, referenceData.EmbedInteropTypes);
}
public MetadataReference ReadMetadataReference(string filePath)
{
if (!_metadataReferenceMap.TryGetValue(filePath, out var mdRef))
{
mdRef = MetadataReference.CreateFromFile(filePath);
_metadataReferenceMap[filePath] = mdRef;
}
return mdRef;
}
public void CopyAssemblyBytes(AssemblyData data, Stream stream)
{
using var fileStream = RoslynUtil.OpenBuildFileForRead(data.FilePath);
fileStream.CopyTo(stream);
}
byte[] IBasicAnalyzerHostDataProvider.GetAssemblyBytes(AssemblyData data) =>
File.ReadAllBytes(data.FilePath);
public bool TryGetCompilerCallIndex(Guid mvid, out int compilerCallIndex)
{
var map = _lazyMvidToCompilerCallIndexMap.Value;
return map.TryGetValue(mvid, out compilerCallIndex);
}
private Dictionary<Guid, int> BuildMvidToCompilerCallIndexMap()
{
var map = new Dictionary<Guid, int>();
var compilerCalls = _lazyCompilerCalls.Value;
for (int i = 0; i < compilerCalls.Count; i++)
{
var compilerCall = compilerCalls[i];
var args = ReadCommandLineArguments(compilerCall);
var assemblyName = RoslynUtil.GetAssemblyFileName(args);
if (args.OutputDirectory is not null &&
RoslynUtil.TryReadMvid(Path.Combine(args.OutputDirectory, assemblyName)) is Guid assemblyMvid)
{
map[assemblyMvid] = i;
}
if (args.OutputRefFilePath is not null &&
RoslynUtil.TryReadMvid(args.OutputRefFilePath) is Guid refAssemblyMvid)
{
map[refAssemblyMvid] = i;
}
}
return map;
}
}