Skip to content

Commit 67289a4

Browse files
committed
Share entity base classes between CIL and source extraction
1 parent e7853cc commit 67289a4

File tree

106 files changed

+704
-709
lines changed

Some content is hidden

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

106 files changed

+704
-709
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public override void WriteId(TextWriter trapFile)
4040
trapFile.Write(FullName);
4141
trapFile.Write("#file:///");
4242
trapFile.Write(Cx.AssemblyPath.Replace("\\", "/"));
43+
trapFile.Write(";assembly");
4344
}
4445

4546
public override bool Equals(object? obj)
@@ -49,8 +50,6 @@ public override bool Equals(object? obj)
4950

5051
public override int GetHashCode() => 7 * file.GetHashCode();
5152

52-
public override string IdSuffix => ";assembly";
53-
5453
private string FullName => assemblyName.GetPublicKey() is null ? assemblyName.FullName + ", PublicKeyToken=null" : assemblyName.FullName;
5554

5655
public override IEnumerable<IExtractionProduct> Contents

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ internal sealed class Attribute : UnlabelledEntity
1212
{
1313
private readonly CustomAttributeHandle handle;
1414
private readonly CustomAttribute attrib;
15-
private readonly IEntity @object;
15+
private readonly IExtractedEntity @object;
1616

17-
public Attribute(Context cx, IEntity @object, CustomAttributeHandle handle) : base(cx)
17+
public Attribute(Context cx, IExtractedEntity @object, CustomAttributeHandle handle) : base(cx)
1818
{
1919
attrib = cx.MdReader.GetCustomAttribute(handle);
2020
this.handle = handle;
@@ -80,7 +80,7 @@ private static string GetStringValue(Type type, object? value)
8080
return value?.ToString() ?? "null";
8181
}
8282

83-
public static IEnumerable<IExtractionProduct> Populate(Context cx, IEntity @object, CustomAttributeHandleCollection attributes)
83+
public static IEnumerable<IExtractionProduct> Populate(Context cx, IExtractedEntity @object, CustomAttributeHandleCollection attributes)
8484
{
8585
foreach (var attrib in attributes)
8686
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Collections.Generic;
2+
3+
namespace Semmle.Extraction.CIL
4+
{
5+
/// <summary>
6+
/// A CIL entity which has been extracted.
7+
/// </summary>
8+
public interface IExtractedEntity : IExtractionProduct, IEntity
9+
{
10+
/// <summary>
11+
/// The contents of the entity.
12+
/// </summary>
13+
14+
IEnumerable<IExtractionProduct> Contents { get; }
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace Semmle.Extraction.CIL
2+
{
3+
/// <summary>
4+
/// Something that is extracted from an entity.
5+
/// </summary>
6+
///
7+
/// <remarks>
8+
/// The extraction algorithm proceeds as follows:
9+
/// - Construct entity
10+
/// - Call Extract()
11+
/// - IExtractedEntity check if already extracted
12+
/// - Enumerate Contents to produce more extraction products
13+
/// - Extract these until there is nothing left to extract
14+
/// </remarks>
15+
public interface IExtractionProduct
16+
{
17+
/// <summary>
18+
/// Perform further extraction/population of this item as necessary.
19+
/// </summary>
20+
///
21+
/// <param name="cx">The extraction context.</param>
22+
void Extract(Context cx);
23+
}
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
namespace Semmle.Extraction.CIL
6+
{
7+
/// <summary>
8+
/// An entity that needs to be populated during extraction.
9+
/// This assigns a key and optionally extracts its contents.
10+
/// </summary>
11+
public abstract class LabelledEntity : Extraction.LabelledEntity, IExtractedEntity
12+
{
13+
// todo: with .NET 5 this can override the base context, and change the return type.
14+
public Context Cx { get; }
15+
16+
protected LabelledEntity(Context cx) : base(cx.Cx)
17+
{
18+
this.Cx = cx;
19+
}
20+
21+
public override Microsoft.CodeAnalysis.Location ReportingLocation => throw new NotImplementedException();
22+
23+
public void Extract(Context cx2)
24+
{
25+
cx2.Populate(this);
26+
}
27+
28+
public override string ToString()
29+
{
30+
using var writer = new StringWriter();
31+
WriteQuotedId(writer);
32+
return writer.ToString();
33+
}
34+
35+
public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel;
36+
37+
public abstract IEnumerable<IExtractionProduct> Contents { get; }
38+
}
39+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Semmle.Extraction.CIL
2+
{
3+
/// <summary>
4+
/// A tuple that is an extraction product.
5+
/// </summary>
6+
internal class Tuple : IExtractionProduct
7+
{
8+
private readonly Extraction.Tuple tuple;
9+
10+
public Tuple(string name, params object[] args)
11+
{
12+
tuple = new Extraction.Tuple(name, args);
13+
}
14+
15+
public void Extract(Context cx)
16+
{
17+
cx.Cx.Emit(tuple);
18+
}
19+
20+
public override string ToString() => tuple.ToString();
21+
}
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Semmle.Extraction.CIL
5+
{
6+
/// <summary>
7+
/// An entity that has contents to extract. There is no need to populate
8+
/// a key as it's done in the contructor.
9+
/// </summary>
10+
public abstract class UnlabelledEntity : Extraction.UnlabelledEntity, IExtractedEntity
11+
{
12+
// todo: with .NET 5 this can override the base context, and change the return type.
13+
public Context Cx { get; }
14+
15+
protected UnlabelledEntity(Context cx) : base(cx.Cx)
16+
{
17+
Cx = cx;
18+
}
19+
20+
public override Microsoft.CodeAnalysis.Location ReportingLocation => throw new NotImplementedException();
21+
22+
public void Extract(Context cx2)
23+
{
24+
cx2.Extract(this);
25+
}
26+
27+
public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel;
28+
29+
public abstract IEnumerable<IExtractionProduct> Contents { get; }
30+
}
31+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ public override void WriteId(TextWriter trapFile)
2525
parent.WriteId(trapFile);
2626
trapFile.Write('.');
2727
trapFile.Write(Cx.ShortName(ed.Name));
28+
trapFile.Write(";cil-event");
2829
}
2930

30-
public override string IdSuffix => ";cil-event";
31-
3231
public override bool Equals(object? obj)
3332
{
3433
return obj is Event e && handle.Equals(e.handle);

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

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,27 @@ namespace Semmle.Extraction.CIL.Entities
88
/// <summary>
99
/// An entity representing a field.
1010
/// </summary>
11-
internal abstract class Field : IGenericContext, IMember, ICustomModifierReceiver
11+
internal abstract class Field : LabelledEntity, IGenericContext, IMember, ICustomModifierReceiver
1212
{
13-
protected Field(Context cx)
13+
protected Field(Context cx) : base(cx)
1414
{
15-
Cx = cx;
1615
}
1716

18-
public Label Label { get; set; }
19-
20-
public void WriteId(TextWriter trapFile)
17+
public override void WriteId(TextWriter trapFile)
2118
{
2219
trapFile.WriteSubId(DeclaringType);
2320
trapFile.Write('.');
2421
trapFile.Write(Name);
22+
trapFile.Write(";cil-field");
2523
}
2624

27-
public void WriteQuotedId(TextWriter trapFile)
28-
{
29-
trapFile.Write("@\"");
30-
WriteId(trapFile);
31-
trapFile.Write(idSuffix);
32-
trapFile.Write('\"');
33-
}
34-
35-
private const string idSuffix = ";cil-field";
36-
3725
public abstract string Name { get; }
3826

3927
public abstract Type DeclaringType { get; }
4028

41-
public Location ReportingLocation => throw new NotImplementedException();
42-
4329
public abstract Type Type { get; }
4430

45-
public virtual IEnumerable<IExtractionProduct> Contents
31+
public override IEnumerable<IExtractionProduct> Contents
4632
{
4733
get
4834
{
@@ -56,17 +42,8 @@ public virtual IEnumerable<IExtractionProduct> Contents
5642
}
5743
}
5844

59-
public void Extract(Context cx2)
60-
{
61-
cx2.Populate(this);
62-
}
63-
64-
public TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel;
65-
6645
public abstract IEnumerable<Type> TypeParameters { get; }
6746

6847
public abstract IEnumerable<Type> MethodParameters { get; }
69-
70-
public Context Cx { get; }
7148
}
7249
}

0 commit comments

Comments
 (0)