|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using System; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using Semmle.Util; |
| 6 | + |
| 7 | +namespace Semmle.Extraction.CSharp.Entities |
| 8 | +{ |
| 9 | + public class Compilation : CachedEntity<object> |
| 10 | + { |
| 11 | + private static (string Cwd, string[] Args) settings; |
| 12 | + private static int hashCode; |
| 13 | + |
| 14 | + public static (string Cwd, string[] Args) Settings |
| 15 | + { |
| 16 | + get { return settings; } |
| 17 | + set |
| 18 | + { |
| 19 | + settings = value; |
| 20 | + hashCode = settings.Cwd.GetHashCode(); |
| 21 | + for (var i = 0; i < settings.Args.Length; i++) |
| 22 | + { |
| 23 | + hashCode = HashCode.Combine(hashCode, settings.Args[i].GetHashCode()); |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + private Compilation(Context cx) : base(cx, null) |
| 29 | + { |
| 30 | + } |
| 31 | + |
| 32 | + public override void Populate(TextWriter trapFile) |
| 33 | + { |
| 34 | + var assembly = Extraction.Entities.Assembly.CreateOutputAssembly(Context); |
| 35 | + |
| 36 | + trapFile.compilations(this, FileUtils.ConvertToUnix(Compilation.Settings.Cwd)); |
| 37 | + trapFile.compilation_assembly(this, assembly); |
| 38 | + |
| 39 | + // Arguments |
| 40 | + var index = 0; |
| 41 | + foreach (var arg in Compilation.Settings.Args) |
| 42 | + { |
| 43 | + trapFile.compilation_args(this, index++, arg); |
| 44 | + } |
| 45 | + |
| 46 | + // Files |
| 47 | + index = 0; |
| 48 | + foreach (var file in Context.Compilation.SyntaxTrees.Select(tree => Extraction.Entities.File.Create(Context, tree.FilePath))) |
| 49 | + { |
| 50 | + trapFile.compilation_compiling_files(this, index++, file); |
| 51 | + } |
| 52 | + |
| 53 | + // References |
| 54 | + index = 0; |
| 55 | + foreach (var file in Context.Compilation.References |
| 56 | + .OfType<PortableExecutableReference>() |
| 57 | + .Select(r => Extraction.Entities.File.Create(Context, r.FilePath))) |
| 58 | + { |
| 59 | + trapFile.compilation_referencing_files(this, index++, file); |
| 60 | + } |
| 61 | + |
| 62 | + // Diagnostics |
| 63 | + index = 0; |
| 64 | + foreach (var diag in Context.Compilation.GetDiagnostics().Select(d => new Diagnostic(Context, d))) |
| 65 | + { |
| 66 | + trapFile.diagnostic_for(diag, this, 0, index++); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public void PopulatePerformance(PerformanceMetrics p) |
| 71 | + { |
| 72 | + var trapFile = Context.TrapWriter.Writer; |
| 73 | + var index = 0; |
| 74 | + foreach (var metric in p.Metrics) |
| 75 | + { |
| 76 | + trapFile.compilation_time(this, -1, index++, metric); |
| 77 | + } |
| 78 | + trapFile.compilation_finished(this, (float)p.Total.Cpu.TotalSeconds, (float)p.Total.Elapsed.TotalSeconds); |
| 79 | + } |
| 80 | + |
| 81 | + public override void WriteId(TextWriter trapFile) |
| 82 | + { |
| 83 | + trapFile.Write(hashCode); |
| 84 | + trapFile.Write(";compilation"); |
| 85 | + } |
| 86 | + |
| 87 | + public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel; |
| 88 | + |
| 89 | + public override Location ReportingLocation => throw new NotImplementedException(); |
| 90 | + |
| 91 | + public override bool NeedsPopulation => Context.IsAssemblyScope; |
| 92 | + |
| 93 | + private class CompilationFactory : ICachedEntityFactory<object, Compilation> |
| 94 | + { |
| 95 | + public static CompilationFactory Instance { get; } = new CompilationFactory(); |
| 96 | + |
| 97 | + public Compilation Create(Context cx, object init) => new Compilation(cx); |
| 98 | + } |
| 99 | + |
| 100 | + private static readonly object compilationCacheKey = new object(); |
| 101 | + |
| 102 | + public static Compilation Create(Context cx) |
| 103 | + => CompilationFactory.Instance.CreateEntity(cx, compilationCacheKey, null); |
| 104 | + } |
| 105 | +} |
0 commit comments