Skip to content

Commit 9ae22cb

Browse files
authored
Merge pull request github#5189 from tamasvajk/feature/refactor-3
C#: Split 'Context' class between CIL and source extraction
2 parents 5ecd231 + 4711856 commit 9ae22cb

File tree

89 files changed

+776
-724
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+776
-724
lines changed

csharp/extractor/Semmle.Extraction.CIL.Driver/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private static void ExtractAssembly(Layout layout, string assemblyPath, ILogger
2424
{
2525
var sw = new Stopwatch();
2626
sw.Start();
27-
Entities.Assembly.ExtractCIL(layout, assemblyPath, logger, nocache, extractPdbs, trapCompression, out _, out _);
27+
Analyser.ExtractCIL(layout, assemblyPath, logger, nocache, extractPdbs, trapCompression, out _, out _);
2828
sw.Stop();
2929
logger.Log(Severity.Info, " {0} ({1})", assemblyPath, sw.Elapsed);
3030
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Semmle.Util.Logging;
2+
using System;
3+
using Semmle.Util;
4+
using Semmle.Extraction.CIL.Entities;
5+
6+
namespace Semmle.Extraction.CIL
7+
{
8+
public static class Analyser
9+
{
10+
private static void ExtractCIL(Extractor extractor, TrapWriter trapWriter, bool extractPdbs)
11+
{
12+
using var cilContext = new Context(extractor, trapWriter, extractor.OutputPath, extractPdbs);
13+
cilContext.Populate(new Assembly(cilContext));
14+
cilContext.PopulateAll();
15+
}
16+
17+
/// <summary>
18+
/// Main entry point to the CIL extractor.
19+
/// Call this to extract a given assembly.
20+
/// </summary>
21+
/// <param name="layout">The trap layout.</param>
22+
/// <param name="assemblyPath">The full path of the assembly to extract.</param>
23+
/// <param name="logger">The logger.</param>
24+
/// <param name="nocache">True to overwrite existing trap file.</param>
25+
/// <param name="extractPdbs">Whether to extract PDBs.</param>
26+
/// <param name="trapFile">The path of the trap file.</param>
27+
/// <param name="extracted">Whether the file was extracted (false=cached).</param>
28+
public static void ExtractCIL(Layout layout, string assemblyPath, ILogger logger, bool nocache, bool extractPdbs, TrapWriter.CompressionMode trapCompression, out string trapFile, out bool extracted)
29+
{
30+
trapFile = "";
31+
extracted = false;
32+
try
33+
{
34+
var canonicalPathCache = CanonicalPathCache.Create(logger, 1000);
35+
var pathTransformer = new PathTransformer(canonicalPathCache);
36+
var extractor = new Extractor(false, assemblyPath, logger, pathTransformer);
37+
var transformedAssemblyPath = pathTransformer.Transform(assemblyPath);
38+
var project = layout.LookupProjectOrDefault(transformedAssemblyPath);
39+
using var trapWriter = project.CreateTrapWriter(logger, transformedAssemblyPath.WithSuffix(".cil"), trapCompression, discardDuplicates: true);
40+
trapFile = trapWriter.TrapFile;
41+
if (nocache || !System.IO.File.Exists(trapFile))
42+
{
43+
ExtractCIL(extractor, trapWriter, extractPdbs);
44+
extracted = true;
45+
}
46+
}
47+
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
48+
{
49+
logger.Log(Severity.Error, string.Format("Exception extracting {0}: {1}", assemblyPath, ex));
50+
}
51+
}
52+
}
53+
}

csharp/extractor/Semmle.Extraction.CIL/Context.Factories.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ namespace Semmle.Extraction.CIL
99
/// <summary>
1010
/// Provides methods for creating and caching various entities.
1111
/// </summary>
12-
public sealed partial class Context
12+
internal sealed partial class Context
1313
{
1414
private readonly Dictionary<object, Label> ids = new Dictionary<object, Label>();
1515

16-
public T Populate<T>(T e) where T : IExtractedEntity
16+
internal T Populate<T>(T e) where T : IExtractedEntity
1717
{
1818
if (e.Label.Valid)
1919
{
@@ -27,10 +27,10 @@ public T Populate<T>(T e) where T : IExtractedEntity
2727
}
2828
else
2929
{
30-
e.Label = Cx.GetNewLabel();
31-
Cx.DefineLabel(e, Cx.TrapWriter.Writer, Cx.Extractor);
30+
e.Label = GetNewLabel();
31+
DefineLabel(e);
3232
ids.Add(e, e.Label);
33-
Cx.PopulateLater(() =>
33+
PopulateLater(() =>
3434
{
3535
foreach (var c in e.Contents)
3636
c.Extract(this);
@@ -42,7 +42,7 @@ public T Populate<T>(T e) where T : IExtractedEntity
4242

4343
if (debugLabels.TryGetValue(id, out var previousEntity))
4444
{
45-
Cx.Extractor.Message(new Message("Duplicate trap ID", id, null, severity: Util.Logging.Severity.Warning));
45+
Extractor.Message(new Message("Duplicate trap ID", id, null, severity: Util.Logging.Severity.Warning));
4646
}
4747
else
4848
{
@@ -74,9 +74,9 @@ public PrimitiveType Create(PrimitiveTypeCode code)
7474
{
7575
e = new PrimitiveType(this, code)
7676
{
77-
Label = Cx.GetNewLabel()
77+
Label = GetNewLabel()
7878
};
79-
Cx.DefineLabel(e, Cx.TrapWriter.Writer, Cx.Extractor);
79+
DefineLabel(e);
8080
primitiveTypes[(int)code] = e;
8181
}
8282

csharp/extractor/Semmle.Extraction.CIL/Context.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ namespace Semmle.Extraction.CIL
1010
/// Adds additional context that is specific for CIL extraction.
1111
/// One context = one DLL/EXE.
1212
/// </summary>
13-
public sealed partial class Context : IDisposable
13+
internal sealed partial class Context : Extraction.Context, IDisposable
1414
{
1515
private readonly FileStream stream;
1616
private Entities.Assembly? assemblyNull;
17-
18-
public Extraction.Context Cx { get; }
1917
public MetadataReader MdReader { get; }
2018
public PEReader PeReader { get; }
2119
public string AssemblyPath { get; }
@@ -26,9 +24,9 @@ public Entities.Assembly Assembly
2624
}
2725
public PDB.IPdb? Pdb { get; }
2826

29-
public Context(Extraction.Context cx, string assemblyPath, bool extractPdbs)
27+
public Context(Extractor extractor, TrapWriter trapWriter, string assemblyPath, bool extractPdbs)
28+
: base(extractor, trapWriter)
3029
{
31-
this.Cx = cx;
3230
this.AssemblyPath = assemblyPath;
3331
stream = File.OpenRead(assemblyPath);
3432
PeReader = new PEReader(stream, PEStreamOptions.PrefetchEntireImage);
@@ -51,7 +49,7 @@ public Context(Extraction.Context cx, string assemblyPath, bool extractPdbs)
5149
Pdb = PDB.PdbReader.Create(assemblyPath, PeReader);
5250
if (Pdb != null)
5351
{
54-
cx.Extractor.Logger.Log(Util.Logging.Severity.Info, string.Format("Found PDB information for {0}", assemblyPath));
52+
Extractor.Logger.Log(Util.Logging.Severity.Info, string.Format("Found PDB information for {0}", assemblyPath));
5553
}
5654
}
5755
}

csharp/extractor/Semmle.Extraction.CIL/EmptyContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Semmle.Extraction.CIL
55
/// <summary>
66
/// A generic context which does not contain any type parameters.
77
/// </summary>
8-
public class EmptyContext : IGenericContext
8+
internal class EmptyContext : IGenericContext
99
{
1010
public EmptyContext(Context cx)
1111
{

csharp/extractor/Semmle.Extraction.CIL/Entities/Assembly.cs

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
using System.Reflection;
22
using System.Globalization;
33
using System.Collections.Generic;
4-
using Semmle.Util.Logging;
5-
using System;
64
using Semmle.Extraction.Entities;
75
using System.IO;
8-
using Semmle.Util;
96

107
namespace Semmle.Extraction.CIL.Entities
118
{
129
/// <summary>
1310
/// An assembly to extract.
1411
/// </summary>
15-
public class Assembly : LabelledEntity, ILocation
12+
internal class Assembly : LabelledEntity, ILocation
1613
{
1714
private readonly File file;
1815
private readonly AssemblyName assemblyName;
@@ -76,7 +73,7 @@ public override IEnumerable<IExtractionProduct> Contents
7673
}
7774
catch (InternalError e)
7875
{
79-
Cx.Cx.ExtractionError("Error processing type definition", e.Message, GeneratedLocation.Create(Cx.Cx), e.StackTrace);
76+
Cx.ExtractionError("Error processing type definition", e.Message, GeneratedLocation.Create(Cx), e.StackTrace);
8077
}
8178

8279
// Limitation of C#: Cannot yield return inside a try-catch.
@@ -93,57 +90,13 @@ public override IEnumerable<IExtractionProduct> Contents
9390
}
9491
catch (InternalError e)
9592
{
96-
Cx.Cx.ExtractionError("Error processing bytecode", e.Message, GeneratedLocation.Create(Cx.Cx), e.StackTrace);
93+
Cx.ExtractionError("Error processing bytecode", e.Message, GeneratedLocation.Create(Cx), e.StackTrace);
9794
}
9895

9996
if (product != null)
10097
yield return product;
10198
}
10299
}
103100
}
104-
105-
private static void ExtractCIL(Extraction.Context cx, string assemblyPath, bool extractPdbs)
106-
{
107-
using var cilContext = new Context(cx, assemblyPath, extractPdbs);
108-
cilContext.Populate(new Assembly(cilContext));
109-
cilContext.Cx.PopulateAll();
110-
}
111-
112-
/// <summary>
113-
/// Main entry point to the CIL extractor.
114-
/// Call this to extract a given assembly.
115-
/// </summary>
116-
/// <param name="layout">The trap layout.</param>
117-
/// <param name="assemblyPath">The full path of the assembly to extract.</param>
118-
/// <param name="logger">The logger.</param>
119-
/// <param name="nocache">True to overwrite existing trap file.</param>
120-
/// <param name="extractPdbs">Whether to extract PDBs.</param>
121-
/// <param name="trapFile">The path of the trap file.</param>
122-
/// <param name="extracted">Whether the file was extracted (false=cached).</param>
123-
public static void ExtractCIL(Layout layout, string assemblyPath, ILogger logger, bool nocache, bool extractPdbs, TrapWriter.CompressionMode trapCompression, out string trapFile, out bool extracted)
124-
{
125-
trapFile = "";
126-
extracted = false;
127-
try
128-
{
129-
var canonicalPathCache = CanonicalPathCache.Create(logger, 1000);
130-
var pathTransformer = new PathTransformer(canonicalPathCache);
131-
var extractor = new Extractor(false, assemblyPath, logger, pathTransformer);
132-
var transformedAssemblyPath = pathTransformer.Transform(assemblyPath);
133-
var project = layout.LookupProjectOrDefault(transformedAssemblyPath);
134-
using var trapWriter = project.CreateTrapWriter(logger, transformedAssemblyPath.WithSuffix(".cil"), trapCompression, discardDuplicates: true);
135-
trapFile = trapWriter.TrapFile;
136-
if (nocache || !System.IO.File.Exists(trapFile))
137-
{
138-
var cx = new Extraction.Context(extractor, trapWriter);
139-
ExtractCIL(cx, assemblyPath, extractPdbs);
140-
extracted = true;
141-
}
142-
}
143-
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
144-
{
145-
logger.Log(Severity.Error, string.Format("Exception extracting {0}: {1}", assemblyPath, ex));
146-
}
147-
}
148101
}
149102
}

csharp/extractor/Semmle.Extraction.CIL/Entities/Attribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public override IEnumerable<IExtractionProduct> Contents
4545
}
4646
catch
4747
{
48-
Cx.Cx.Extractor.Logger.Log(Util.Logging.Severity.Info,
48+
Cx.Extractor.Logger.Log(Util.Logging.Severity.Info,
4949
$"Attribute decoding is partial. Decoding attribute {constructor.DeclaringType.GetQualifiedName()} failed on {@object}.");
5050
yield break;
5151
}

csharp/extractor/Semmle.Extraction.CIL/Entities/Base/IExtractedEntity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Semmle.Extraction.CIL
55
/// <summary>
66
/// A CIL entity which has been extracted.
77
/// </summary>
8-
public interface IExtractedEntity : IExtractionProduct, IEntity
8+
internal interface IExtractedEntity : IExtractionProduct, IEntity
99
{
1010
/// <summary>
1111
/// The contents of the entity.

csharp/extractor/Semmle.Extraction.CIL/Entities/Base/IExtractionProduct.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Semmle.Extraction.CIL
1212
/// - Enumerate Contents to produce more extraction products
1313
/// - Extract these until there is nothing left to extract
1414
/// </remarks>
15-
public interface IExtractionProduct
15+
internal interface IExtractionProduct
1616
{
1717
/// <summary>
1818
/// Perform further extraction/population of this item as necessary.

csharp/extractor/Semmle.Extraction.CIL/Entities/Base/IGenericContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Semmle.Extraction.CIL
66
/// When we decode a type/method signature, we need access to
77
/// generic parameters.
88
/// </summary>
9-
public interface IGenericContext
9+
internal interface IGenericContext
1010
{
1111
Context Cx { get; }
1212

0 commit comments

Comments
 (0)