Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b500a02

Browse files
committed
C#: Compare CIL entities directly by handle rather than by label.
C#: Remove IDs from the CIL extractor and make consistent with C# extractor. C#: Fix method collisions.
1 parent 685c494 commit b500a02

34 files changed

Lines changed: 871 additions & 635 deletions

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public Context(Extraction.Context cx, string assemblyPath, bool extractPdbs)
3131
mdReader = peReader.GetMetadataReader();
3232
TypeSignatureDecoder = new Entities.TypeSignatureDecoder(this);
3333

34-
globalNamespace = new Lazy<Entities.Namespace>(() => Populate(new Entities.Namespace(this, GetId(""), null)));
34+
globalNamespace = new Lazy<Entities.Namespace>(() => Populate(new Entities.Namespace(this, "", null)));
3535
systemNamespace = new Lazy<Entities.Namespace>(() => Populate(new Entities.Namespace(this, "System")));
3636
genericHandleFactory = new CachedFunction<GenericContext, Handle, ILabelledEntity>(CreateGenericHandle);
3737
namespaceFactory = new CachedFunction<StringHandle, Entities.Namespace>(n => CreateNamespace(mdReader.GetString(n)));
@@ -42,9 +42,6 @@ public Context(Extraction.Context cx, string assemblyPath, bool extractPdbs)
4242

4343
defaultGenericContext = new EmptyContext(this);
4444

45-
var def = mdReader.GetAssemblyDefinition();
46-
AssemblyPrefix = GetId(def.Name) + "_" + def.Version.ToString() + "::";
47-
4845
if (extractPdbs)
4946
{
5047
pdb = PDB.PdbReader.Create(assemblyPath, peReader);
@@ -75,7 +72,14 @@ public void Extract(IExtractedEntity entity)
7572
}
7673
}
7774

78-
public readonly Id AssemblyPrefix;
75+
public void WriteAssemblyPrefix(TextWriter trapFile)
76+
{
77+
var def = mdReader.GetAssemblyDefinition();
78+
trapFile.Write(GetString(def.Name));
79+
trapFile.Write('_');
80+
trapFile.Write(def.Version.ToString());
81+
trapFile.Write("::");
82+
}
7983

8084
public readonly Entities.TypeSignatureDecoder TypeSignatureDecoder;
8185

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Semmle.Util.Logging;
55
using System;
66
using Semmle.Extraction.Entities;
7+
using System.IO;
78

89
namespace Semmle.Extraction.CIL.Entities
910
{
@@ -20,8 +21,6 @@ interface IAssembly : ILocation
2021
/// </summary>
2122
public class Assembly : LabelledEntity, IAssembly
2223
{
23-
public override Id IdSuffix => suffix;
24-
2524
readonly File file;
2625
readonly AssemblyName assemblyName;
2726

@@ -38,12 +37,24 @@ public Assembly(Context cx) : base(cx)
3837
if (!def.PublicKey.IsNil)
3938
assemblyName.SetPublicKey(cx.mdReader.GetBlobBytes(def.PublicKey));
4039

41-
ShortId = cx.GetId(FullName) + "#file:///" + cx.assemblyPath.Replace("\\", "/");
42-
4340
file = new File(cx, cx.assemblyPath);
4441
}
4542

46-
static readonly Id suffix = new StringId(";assembly");
43+
public override void WriteId(TextWriter trapFile)
44+
{
45+
trapFile.Write(FullName);
46+
trapFile.Write("#file:///");
47+
trapFile.Write(cx.assemblyPath.Replace("\\", "/"));
48+
}
49+
50+
public override bool Equals(object obj)
51+
{
52+
return GetType() == obj.GetType() && Equals(file, ((Assembly)obj).file);
53+
}
54+
55+
public override int GetHashCode() => 7 * file.GetHashCode();
56+
57+
public override string IdSuffix => ";assembly";
4758

4859
string FullName => assemblyName.GetPublicKey() is null ? assemblyName.FullName + ", PublicKeyToken=null" : assemblyName.FullName;
4960

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ interface IAttribute : IExtractedEntity
1414
/// <summary>
1515
/// Entity representing a CIL attribute.
1616
/// </summary>
17-
class Attribute : UnlabelledEntity, IAttribute
17+
sealed class Attribute : UnlabelledEntity, IAttribute
1818
{
19+
readonly CustomAttributeHandle handle;
1920
readonly CustomAttribute attrib;
2021
readonly IEntity @object;
2122

@@ -25,6 +26,13 @@ public Attribute(Context cx, IEntity @object, CustomAttributeHandle handle) : ba
2526
this.@object = @object;
2627
}
2728

29+
public override bool Equals(object obj)
30+
{
31+
return obj is Attribute attribute && handle.Equals(attribute.handle);
32+
}
33+
34+
public override int GetHashCode() => handle.GetHashCode();
35+
2836
public override IEnumerable<IExtractionProduct> Contents
2937
{
3038
get
@@ -78,7 +86,7 @@ class CustomAttributeDecoder : ICustomAttributeTypeProvider<Type>
7886
readonly Context cx;
7987
public CustomAttributeDecoder(Context cx) { this.cx = cx; }
8088

81-
public Type GetPrimitiveType(PrimitiveTypeCode typeCode) => cx.Populate(new PrimitiveType(cx, typeCode));
89+
public Type GetPrimitiveType(PrimitiveTypeCode typeCode) => cx.Create(typeCode);
8290

8391
public Type GetSystemType() => throw new NotImplementedException();
8492

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.IO;
23
using System.Reflection.Metadata;
34

45
namespace Semmle.Extraction.CIL.Entities
@@ -13,19 +14,35 @@ interface IEvent : ILabelledEntity
1314
/// <summary>
1415
/// An event entity.
1516
/// </summary>
16-
class Event : LabelledEntity, IEvent
17+
sealed class Event : LabelledEntity, IEvent
1718
{
19+
readonly EventDefinitionHandle handle;
1820
readonly Type parent;
1921
readonly EventDefinition ed;
20-
static readonly Id suffix = CIL.Id.Create(";cil-event");
2122

2223
public Event(Context cx, Type parent, EventDefinitionHandle handle) : base(cx)
2324
{
25+
this.handle = handle;
2426
this.parent = parent;
2527
ed = cx.mdReader.GetEventDefinition(handle);
26-
ShortId = parent.ShortId + cx.Dot + cx.ShortName(ed.Name) + suffix;
2728
}
2829

30+
public override void WriteId(TextWriter trapFile)
31+
{
32+
parent.WriteId(trapFile);
33+
trapFile.Write('.');
34+
trapFile.Write(cx.ShortName(ed.Name));
35+
}
36+
37+
public override string IdSuffix => ";cil-event";
38+
39+
public override bool Equals(object obj)
40+
{
41+
return obj is Event e && handle.Equals(e.handle);
42+
}
43+
44+
public override int GetHashCode() => handle.GetHashCode();
45+
2946
public override IEnumerable<IExtractionProduct> Contents
3047
{
3148
get
@@ -61,7 +78,5 @@ public override IEnumerable<IExtractionProduct> Contents
6178
yield return c;
6279
}
6380
}
64-
65-
public override Id IdSuffix => suffix;
6681
}
6782
}

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

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,24 @@ protected Field(Context cx) : base(cx)
3737

3838
public Label Label { get; set; }
3939

40-
public IId Id => ShortId + IdSuffix;
41-
4240
public void WriteId(TextWriter trapFile)
4341
{
44-
trapFile.WriteIId(Id);
42+
trapFile.WriteSubId(DeclaringType);
43+
trapFile.Write('.');
44+
trapFile.Write(Name);
4545
}
4646

4747
public void WriteQuotedId(TextWriter trapFile)
4848
{
49+
trapFile.Write("@\"");
4950
WriteId(trapFile);
51+
trapFile.Write(IdSuffix);
52+
trapFile.Write('\"');
5053
}
5154

52-
public Id IdSuffix => fieldSuffix;
53-
54-
static readonly StringId fieldSuffix = new StringId(";cil-field");
55-
56-
public Id ShortId
57-
{
58-
get; set;
59-
}
55+
public string IdSuffix => ";cil-field";
6056

61-
public abstract Id Name { get; }
57+
public abstract string Name { get; }
6258

6359
public abstract Type DeclaringType { get; }
6460

@@ -70,7 +66,7 @@ public virtual IEnumerable<IExtractionProduct> Contents
7066
{
7167
get
7268
{
73-
yield return Tuples.cil_field(this, DeclaringType, Name.Value, Type);
69+
yield return Tuples.cil_field(this, DeclaringType, Name, Type);
7470
}
7571
}
7672

@@ -93,9 +89,15 @@ public DefinitionField(GenericContext gc, FieldDefinitionHandle handle) : base(g
9389
this.handle = handle;
9490
this.gc = gc;
9591
fd = cx.mdReader.GetFieldDefinition(handle);
96-
ShortId = DeclaringType.ShortId + cx.Dot + Name;
9792
}
9893

94+
public override bool Equals(object obj)
95+
{
96+
return obj is DefinitionField field && handle.Equals(field.handle);
97+
}
98+
99+
public override int GetHashCode() => handle.GetHashCode();
100+
99101
public override IEnumerable<IExtractionProduct> Contents
100102
{
101103
get
@@ -125,7 +127,7 @@ public override IEnumerable<IExtractionProduct> Contents
125127
}
126128
}
127129

128-
public override Id Name => cx.GetId(fd.Name);
130+
public override string Name => cx.GetString(fd.Name);
129131

130132
public override Type DeclaringType => (Type)cx.Create(fd.GetDeclaringType());
131133

@@ -138,19 +140,30 @@ public override IEnumerable<IExtractionProduct> Contents
138140

139141
sealed class MemberReferenceField : Field
140142
{
143+
readonly MemberReferenceHandle Handle;
141144
readonly MemberReference mr;
142145
readonly GenericContext gc;
143146
readonly Type declType;
144147

145148
public MemberReferenceField(GenericContext gc, MemberReferenceHandle handle) : base(gc.cx)
146149
{
150+
Handle = handle;
147151
this.gc = gc;
148152
mr = cx.mdReader.GetMemberReference(handle);
149153
declType = (Type)cx.CreateGeneric(gc, mr.Parent);
150-
ShortId = declType.ShortId + cx.Dot + Name;
151154
}
152155

153-
public override Id Name => cx.GetId(mr.Name);
156+
public override bool Equals(object obj)
157+
{
158+
return obj is MemberReferenceField field && Handle.Equals(field.Handle);
159+
}
160+
161+
public override int GetHashCode()
162+
{
163+
return Handle.GetHashCode();
164+
}
165+
166+
public override string Name => cx.GetString(mr.Name);
154167

155168
public override Type DeclaringType => declType;
156169

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.IO;
23

34
namespace Semmle.Extraction.CIL.Entities
45
{
@@ -17,9 +18,20 @@ public class File : LabelledEntity, IFile
1718
public File(Context cx, string path) : base(cx)
1819
{
1920
this.path = Semmle.Extraction.Entities.File.PathAsDatabaseString(path);
20-
ShortId = new StringId(Semmle.Extraction.Entities.File.PathAsDatabaseId(path));
2121
}
2222

23+
public override void WriteId(TextWriter trapFile)
24+
{
25+
trapFile.Write(Semmle.Extraction.Entities.File.PathAsDatabaseId(path));
26+
}
27+
28+
public override bool Equals(object obj)
29+
{
30+
return GetType() == obj.GetType() && path == ((File)obj).path;
31+
}
32+
33+
public override int GetHashCode() => 11 * path.GetHashCode();
34+
2335
public override IEnumerable<IExtractionProduct> Contents
2436
{
2537
get
@@ -31,9 +43,7 @@ public override IEnumerable<IExtractionProduct> Contents
3143
}
3244
}
3345

34-
public override Id IdSuffix => suffix;
35-
36-
static readonly Id suffix = new StringId(";sourcefile");
46+
public override string IdSuffix => ";sourcefile";
3747
}
3848

3949
public class PdbSourceFile : File

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@ interface IFolder : IFileOrFolder
77
{
88
}
99

10-
public class Folder : LabelledEntity, IFolder
10+
public sealed class Folder : LabelledEntity, IFolder
1111
{
1212
readonly string path;
1313

1414
public Folder(Context cx, string path) : base(cx)
1515
{
1616
this.path = path;
17-
ShortId = new StringId(Semmle.Extraction.Entities.File.PathAsDatabaseId(path));
1817
}
1918

20-
static readonly Id suffix = new StringId(";folder");
19+
20+
public override void WriteId(TextWriter trapFile)
21+
{
22+
trapFile.Write(Semmle.Extraction.Entities.File.PathAsDatabaseId(path));
23+
}
24+
25+
public override string IdSuffix => ";folder";
2126

2227
public override IEnumerable<IExtractionProduct> Contents
2328
{
@@ -37,6 +42,11 @@ public override IEnumerable<IExtractionProduct> Contents
3742
}
3843
}
3944

40-
public override Id IdSuffix => suffix;
45+
public override bool Equals(object obj)
46+
{
47+
return obj is Folder folder && path == folder.path;
48+
}
49+
50+
public override int GetHashCode() => path.GetHashCode();
4151
}
4252
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.IO;
23

34
namespace Semmle.Extraction.CIL.Entities
45
{
@@ -17,12 +18,16 @@ public LocalVariable(Context cx, MethodImplementation m, int i, Type t) : base(c
1718
method = m;
1819
index = i;
1920
type = t;
20-
ShortId = CIL.Id.Create(method.Label) + underscore + index;
2121
}
2222

23-
static readonly Id underscore = CIL.Id.Create("_");
24-
static readonly Id suffix = CIL.Id.Create(";cil-local");
25-
public override Id IdSuffix => suffix;
23+
public override void WriteId(TextWriter trapFile)
24+
{
25+
trapFile.WriteSubId(method);
26+
trapFile.Write('_');
27+
trapFile.Write(index);
28+
}
29+
30+
public override string IdSuffix => ";cil-local";
2631

2732
public override IEnumerable<IExtractionProduct> Contents
2833
{

0 commit comments

Comments
 (0)