Skip to content

Commit 539fdf9

Browse files
committed
Extend base context in CIL project
1 parent 5fca946 commit 539fdf9

File tree

11 files changed

+28
-33
lines changed

11 files changed

+28
-33
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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, TrapWriter.Writer, Extractor);
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, TrapWriter.Writer, Extractor);
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+
public 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/Entities/Assembly.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public override IEnumerable<IExtractionProduct> Contents
7676
}
7777
catch (InternalError e)
7878
{
79-
Cx.Cx.ExtractionError("Error processing type definition", e.Message, GeneratedLocation.Create(Cx.Cx), e.StackTrace);
79+
Cx.ExtractionError("Error processing type definition", e.Message, GeneratedLocation.Create(Cx), e.StackTrace);
8080
}
8181

8282
// Limitation of C#: Cannot yield return inside a try-catch.
@@ -93,7 +93,7 @@ public override IEnumerable<IExtractionProduct> Contents
9393
}
9494
catch (InternalError e)
9595
{
96-
Cx.Cx.ExtractionError("Error processing bytecode", e.Message, GeneratedLocation.Create(Cx.Cx), e.StackTrace);
96+
Cx.ExtractionError("Error processing bytecode", e.Message, GeneratedLocation.Create(Cx), e.StackTrace);
9797
}
9898

9999
if (product != null)
@@ -102,11 +102,11 @@ public override IEnumerable<IExtractionProduct> Contents
102102
}
103103
}
104104

105-
private static void ExtractCIL(Extraction.Context cx, string assemblyPath, bool extractPdbs)
105+
private static void ExtractCIL(Extractor extractor, TrapWriter trapWriter, bool extractPdbs)
106106
{
107-
using var cilContext = new Context(cx, assemblyPath, extractPdbs);
107+
using var cilContext = new Context(extractor, trapWriter, extractor.OutputPath, extractPdbs);
108108
cilContext.Populate(new Assembly(cilContext));
109-
cilContext.Cx.PopulateAll();
109+
cilContext.PopulateAll();
110110
}
111111

112112
/// <summary>
@@ -135,8 +135,7 @@ public static void ExtractCIL(Layout layout, string assemblyPath, ILogger logger
135135
trapFile = trapWriter.TrapFile;
136136
if (nocache || !System.IO.File.Exists(trapFile))
137137
{
138-
var cx = new Extraction.Context(extractor, trapWriter);
139-
ExtractCIL(cx, assemblyPath, extractPdbs);
138+
ExtractCIL(extractor, trapWriter, extractPdbs);
140139
extracted = true;
141140
}
142141
}

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/LabelledEntity.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ namespace Semmle.Extraction.CIL
1111
public abstract class LabelledEntity : Extraction.LabelledEntity, IExtractedEntity
1212
{
1313
// todo: with .NET 5 this can override the base context, and change the return type.
14-
public Context Cx { get; }
14+
public Context Cx => (Context)base.Context;
1515

16-
protected LabelledEntity(Context cx) : base(cx.Cx)
16+
protected LabelledEntity(Context cx) : base(cx)
1717
{
18-
this.Cx = cx;
1918
}
2019

2120
public override Microsoft.CodeAnalysis.Location ReportingLocation => throw new NotImplementedException();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public Tuple(string name, params object[] args)
1414

1515
public void Extract(Context cx)
1616
{
17-
cx.Cx.TrapWriter.Emit(tuple);
17+
cx.TrapWriter.Emit(tuple);
1818
}
1919

2020
public override string ToString() => tuple.ToString();

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ namespace Semmle.Extraction.CIL
1010
public abstract class UnlabelledEntity : Extraction.UnlabelledEntity, IExtractedEntity
1111
{
1212
// todo: with .NET 5 this can override the base context, and change the return type.
13-
public Context Cx { get; }
13+
public Context Cx => (Context)base.Context;
1414

15-
protected UnlabelledEntity(Context cx) : base(cx.Cx)
15+
protected UnlabelledEntity(Context cx) : base(cx)
1616
{
17-
Cx = cx;
1817
}
1918

2019
public override Microsoft.CodeAnalysis.Location ReportingLocation => throw new NotImplementedException();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ public PrimitiveTypeCode GetUnderlyingEnumType(Type type)
4040

4141
if (wellKnownEnums.TryGetValue(name, out var code))
4242
{
43-
cx.Cx.Extractor.Logger.Log(Util.Logging.Severity.Debug, $"Using hard coded underlying enum type for {name}");
43+
cx.Extractor.Logger.Log(Util.Logging.Severity.Debug, $"Using hard coded underlying enum type for {name}");
4444
return code;
4545
}
4646

47-
cx.Cx.Extractor.Logger.Log(Util.Logging.Severity.Info, $"Couldn't get underlying enum type for {name}");
47+
cx.Extractor.Logger.Log(Util.Logging.Severity.Info, $"Couldn't get underlying enum type for {name}");
4848

4949
// We can't fall back to Int32, because the type returned here defines how many bytes are read from the
5050
// stream and how those bytes are interpreted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class File : LabelledEntity, IFileOrFolder
1111
public File(Context cx, string path) : base(cx)
1212
{
1313
this.OriginalPath = path;
14-
TransformedPath = cx.Cx.Extractor.PathTransformer.Transform(OriginalPath);
14+
TransformedPath = Cx.Extractor.PathTransformer.Transform(OriginalPath);
1515
}
1616

1717
public override void WriteId(TextWriter trapFile)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public IEnumerable<IExtractionProduct> JumpContents(Dictionary<int, Instruction>
476476
// TODO: Find a solution to this.
477477

478478
// For now, just log the error
479-
Cx.Cx.ExtractionError("A CIL instruction jumps outside the current method", null, Extraction.Entities.GeneratedLocation.Create(Cx.Cx), "", Util.Logging.Severity.Warning);
479+
Cx.ExtractionError("A CIL instruction jumps outside the current method", null, Extraction.Entities.GeneratedLocation.Create(Cx), "", Util.Logging.Severity.Warning);
480480
}
481481
}
482482
}

0 commit comments

Comments
 (0)