Skip to content

Commit 5452000

Browse files
authored
Merge pull request #590 from calumgrant/cs/metadata-handles2
C#: Extract the metadata handle
2 parents 6b459de + 6267946 commit 5452000

File tree

22 files changed

+473
-17
lines changed

22 files changed

+473
-17
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.CodeAnalysis;
55
using System.Reflection.Metadata;
66
using System.Reflection;
7+
using System.Reflection.Metadata.Ecma335;
78

89
namespace Semmle.Extraction.CIL.Entities
910
{
@@ -72,11 +73,13 @@ public void Extract(Context cx2)
7273

7374
sealed class DefinitionField : Field
7475
{
76+
readonly Handle handle;
7577
readonly FieldDefinition fd;
7678
readonly GenericContext gc;
7779

7880
public DefinitionField(GenericContext gc, FieldDefinitionHandle handle) : base(gc.cx)
7981
{
82+
this.handle = handle;
8083
this.gc = gc;
8184
fd = cx.mdReader.GetFieldDefinition(handle);
8285
ShortId = DeclaringType.ShortId + cx.Dot + Name;
@@ -86,6 +89,8 @@ public override IEnumerable<IExtractionProduct> Contents
8689
{
8790
get
8891
{
92+
yield return Tuples.metadata_handle(this, cx.assembly, MetadataTokens.GetToken(handle));
93+
8994
foreach (var c in base.Contents)
9095
yield return c;
9196

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public class File : LabelledEntity, IFile
1616

1717
public File(Context cx, string path) : base(cx)
1818
{
19-
this.path = path.Replace("\\", "/");
20-
ShortId = new StringId(path.Replace(":", "_"));
19+
this.path = Semmle.Extraction.Entities.File.PathAsDatabaseString(path);
20+
ShortId = new StringId(Semmle.Extraction.Entities.File.PathAsDatabaseId(path));
2121
}
2222

2323
public override IEnumerable<IExtractionProduct> Contents

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Folder : LabelledEntity, IFolder
1414
public Folder(Context cx, string path) : base(cx)
1515
{
1616
this.path = path;
17-
ShortId = new StringId(path.Replace("\\", "/").Replace(":", "_"));
17+
ShortId = new StringId(Semmle.Extraction.Entities.File.PathAsDatabaseId(path));
1818
}
1919

2020
static readonly Id suffix = new StringId(";folder");
@@ -33,7 +33,7 @@ public override IEnumerable<IExtractionProduct> Contents
3333
yield return parentFolder;
3434
yield return Tuples.containerparent(parentFolder, this);
3535
}
36-
yield return Tuples.folders(this, path, Path.GetFileName(path));
36+
yield return Tuples.folders(this, Semmle.Extraction.Entities.File.PathAsDatabaseString(path), Path.GetFileName(path));
3737
}
3838
}
3939

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Reflection;
77
using System.Linq;
8+
using System.Reflection.Metadata.Ecma335;
89

910
namespace Semmle.Extraction.CIL.Entities
1011
{
@@ -134,6 +135,7 @@ public override IEnumerable<IExtractionProduct> Contents
134135
/// </summary>
135136
class DefinitionMethod : Method, IMember
136137
{
138+
readonly Handle handle;
137139
readonly MethodDefinition md;
138140
readonly PDB.IMethod methodDebugInformation;
139141

@@ -147,6 +149,7 @@ public DefinitionMethod(GenericContext gc, MethodDefinitionHandle handle) : base
147149
{
148150
md = cx.mdReader.GetMethodDefinition(handle);
149151
this.gc = gc;
152+
this.handle = handle;
150153
name = cx.GetId(md.Name);
151154

152155
declaringType = (Type)cx.CreateGeneric(this, md.GetDeclaringType());
@@ -205,6 +208,7 @@ public override IEnumerable<IExtractionProduct> Contents
205208
Attribute.Populate(cx, pe, p.GetCustomAttributes());
206209
}
207210

211+
yield return Tuples.metadata_handle(this, cx.assembly, MetadataTokens.GetToken(handle));
208212
yield return Tuples.cil_method(this, Name, declaringType, typeSignature.ReturnType);
209213
yield return Tuples.cil_method_source_declaration(this, this);
210214
yield return Tuples.cil_method_location(this, cx.assembly);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Reflection.Metadata;
33
using System.Linq;
4+
using System.Reflection.Metadata.Ecma335;
45

56
namespace Semmle.Extraction.CIL.Entities
67
{
@@ -16,12 +17,14 @@ interface IProperty : ILabelledEntity
1617
/// </summary>
1718
class Property : LabelledEntity, IProperty
1819
{
20+
readonly Handle handle;
1921
readonly Type type;
2022
readonly PropertyDefinition pd;
2123
static readonly Id suffix = CIL.Id.Create(";cil-property");
2224

2325
public Property(GenericContext gc, Type type, PropertyDefinitionHandle handle) : base(gc.cx)
2426
{
27+
this.handle = handle;
2528
pd = cx.mdReader.GetPropertyDefinition(handle);
2629
this.type = type;
2730

@@ -35,6 +38,7 @@ public override IEnumerable<IExtractionProduct> Contents
3538
{
3639
get
3740
{
41+
yield return Tuples.metadata_handle(this, cx.assembly, MetadataTokens.GetToken(handle));
3842
var sig = pd.DecodeSignature(cx.TypeSignatureDecoder, type);
3943

4044
yield return Tuples.cil_property(this, type, cx.ShortName(pd.Name), sig.ReturnType);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,13 @@ public bool IsPrimitiveType
248248
/// </summary>
249249
public sealed class TypeDefinitionType : Type
250250
{
251+
readonly Handle handle;
251252
readonly TypeDefinition td;
252253

253254
public TypeDefinitionType(Context cx, TypeDefinitionHandle handle) : base(cx)
254255
{
255256
td = cx.mdReader.GetTypeDefinition(handle);
257+
this.handle = handle;
256258

257259
declType =
258260
td.GetDeclaringType().IsNil ? null :
@@ -373,6 +375,8 @@ public override IEnumerable<IExtractionProduct> Contents
373375
{
374376
get
375377
{
378+
yield return Tuples.metadata_handle(this, cx.assembly, handle.GetHashCode());
379+
376380
foreach (var c in base.Contents) yield return c;
377381

378382
MakeTypeParameters();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ internal static Tuple folders(IFolder folder, string path, string name) =>
200200
internal static Tuple locations_default(ISourceLocation label, IFile file, int startLine, int startCol, int endLine, int endCol) =>
201201
new Tuple("locations_default", label, file, startLine, startCol, endLine, endCol);
202202

203+
internal static Tuple metadata_handle(IEntity entity, Assembly assembly, int handleValue) =>
204+
new Tuple("metadata_handle", entity, assembly, handleValue);
205+
203206
internal static Tuple namespaces(INamespace ns, string name) =>
204207
new Tuple("namespaces", ns, name);
205208

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Field : CachedSymbol<IFieldSymbol>, IExpressionParentEntity
2424

2525
public override void Populate()
2626
{
27+
ExtractMetadataHandle();
2728
ExtractAttributes();
2829
ContainingType.ExtractGenerics();
2930

csharp/extractor/Semmle.Extraction.CSharp/Entities/Method.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ protected void PopulateMethod()
361361
ExtractParameters();
362362
ExtractMethodBody();
363363
ExtractGenerics();
364+
ExtractMetadataHandle();
364365
}
365366

366367
public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.PushesLabel;

csharp/extractor/Semmle.Extraction.CSharp/Entities/Property.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public override IId Id
2929

3030
public override void Populate()
3131
{
32+
ExtractMetadataHandle();
3233
ExtractAttributes();
3334
ExtractModifiers();
3435
BindComments();

0 commit comments

Comments
 (0)