Skip to content

Commit d687dd9

Browse files
committed
C#: Address review comments. Replace GetHashValue() with MetadataTokens.GetToken().
C#: Make path IDs consistent.
1 parent efe2fb5 commit d687dd9

File tree

10 files changed

+38
-26
lines changed

10 files changed

+38
-26
lines changed

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

Lines changed: 2 additions & 1 deletion
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
{
@@ -88,7 +89,7 @@ public override IEnumerable<IExtractionProduct> Contents
8889
{
8990
get
9091
{
91-
yield return Tuples.metadata_handle(this, cx.assembly, handle.GetHashCode());
92+
yield return Tuples.metadata_handle(this, cx.assembly, MetadataTokens.GetToken(handle));
9293

9394
foreach (var c in base.Contents)
9495
yield return c;

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

Lines changed: 3 additions & 3 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 = path;
20+
ShortId = new StringId(Semmle.Extraction.Entities.File.PathAsDatabaseId(path));
2121
}
2222

2323
public override IEnumerable<IExtractionProduct> Contents
@@ -27,7 +27,7 @@ public override IEnumerable<IExtractionProduct> Contents
2727
var parent = cx.CreateFolder(System.IO.Path.GetDirectoryName(path));
2828
yield return parent;
2929
yield return Tuples.containerparent(parent, this);
30-
yield return Tuples.files(this, path, System.IO.Path.GetFileNameWithoutExtension(path), System.IO.Path.GetExtension(path).Substring(1));
30+
yield return Tuples.files(this, Semmle.Extraction.Entities.File.PathAsDatabaseString(path), System.IO.Path.GetFileNameWithoutExtension(path), System.IO.Path.GetExtension(path).Substring(1));
3131
}
3232
}
3333

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: 2 additions & 1 deletion
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
{
@@ -207,7 +208,7 @@ public override IEnumerable<IExtractionProduct> Contents
207208
Attribute.Populate(cx, pe, p.GetCustomAttributes());
208209
}
209210

210-
yield return Tuples.metadata_handle(this, cx.assembly, handle.GetHashCode());
211+
yield return Tuples.metadata_handle(this, cx.assembly, MetadataTokens.GetToken(handle));
211212
yield return Tuples.cil_method(this, Name, declaringType, typeSignature.ReturnType);
212213
yield return Tuples.cil_method_source_declaration(this, this);
213214
yield return Tuples.cil_method_location(this, cx.assembly);

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

Lines changed: 2 additions & 1 deletion
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
{
@@ -37,7 +38,7 @@ public override IEnumerable<IExtractionProduct> Contents
3738
{
3839
get
3940
{
40-
yield return Tuples.metadata_handle(this, cx.assembly, handle.GetHashCode());
41+
yield return Tuples.metadata_handle(this, cx.assembly, MetadataTokens.GetToken(handle));
4142
var sig = pd.DecodeSignature(cx.TypeSignatureDecoder, type);
4243

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public bool IsPrimitiveType
248248
/// </summary>
249249
public sealed class TypeDefinitionType : Type
250250
{
251-
Handle handle;
251+
readonly Handle handle;
252252
readonly TypeDefinition td;
253253

254254
public TypeDefinitionType(Context cx, TypeDefinitionHandle handle) : base(cx)

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Linq;
66
using System.Reflection.Metadata;
7+
using System.Reflection.Metadata.Ecma335;
78

89
namespace Semmle.Extraction.CSharp.Entities
910
{
@@ -103,11 +104,12 @@ public ExpressionSyntax ExpressionBody
103104
protected void ExtractMetadataHandle()
104105
{
105106
var handle = MetadataHandle;
106-
if (!handle.IsNil)
107-
Context.Emit(Tuples.metadata_handle(this, Location, handle.GetHashCode()));
107+
108+
if (handle.HasValue)
109+
Context.Emit(Tuples.metadata_handle(this, Location, MetadataTokens.GetToken(handle.Value)));
108110
}
109111

110-
public Handle MetadataHandle
112+
public Handle? MetadataHandle
111113
{
112114
get
113115
{
@@ -122,21 +124,21 @@ public Handle MetadataHandle
122124
{
123125
return (MethodDefinitionHandle)value;
124126
}
125-
else if(value is TypeDefinitionHandle)
127+
else if (value is TypeDefinitionHandle)
126128
{
127129
return (TypeDefinitionHandle)value;
128130
}
129-
else if(value is PropertyDefinitionHandle)
131+
else if (value is PropertyDefinitionHandle)
130132
{
131133
return (PropertyDefinitionHandle)value;
132134
}
133-
else if(value is FieldDefinitionHandle)
135+
else if (value is FieldDefinitionHandle)
134136
{
135137
return (FieldDefinitionHandle)value;
136138
}
137139
}
138140

139-
return new Handle(); // A nil handle
141+
return null;
140142
}
141143
}
142144
}

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public string Path
1919
private set;
2020
}
2121

22-
public string DatabasePath => FileAsDatabaseString(Path);
22+
public string DatabasePath => PathAsDatabaseId(Path);
2323

2424
public override bool NeedsPopulation => Context.DefinesFile(Path) || Path == Context.Extractor.OutputPath;
2525

@@ -41,7 +41,7 @@ public override void Populate()
4141
// remove the dot from the extension
4242
if (extension.Length > 0)
4343
extension = extension.Substring(1);
44-
Context.Emit(Tuples.files(this, DatabasePath, name, extension));
44+
Context.Emit(Tuples.files(this, PathAsDatabaseString(Path), name, extension));
4545

4646
Context.Emit(Tuples.containerparent(Entities.Folder.Create(Context, fi.Directory), this));
4747
if (fromSource == 1)
@@ -73,14 +73,21 @@ public override IId Id
7373
}
7474
}
7575

76-
internal static string FileAsDatabaseString(string fileName)
76+
/// <summary>
77+
/// Converts a path string into a string to use as an ID
78+
/// in the QL database.
79+
/// </summary>
80+
/// <param name="path">An absolute path.</param>
81+
/// <returns>The database ID.</returns>
82+
public static string PathAsDatabaseId(string path)
7783
{
78-
fileName = fileName.Replace('\\', '/');
79-
if (fileName.Length > 1 && fileName[1] == ':')
80-
fileName = Char.ToUpper(fileName[0]) + fileName.Substring(1);
81-
return fileName;
84+
if (path.Length >= 2 && path[1] == ':' && Char.IsLower(path[0]))
85+
path = Char.ToUpper(path[0]) + "_" + path.Substring(2);
86+
return path.Replace('\\', '/').Replace(":", "_");
8287
}
8388

89+
public static string PathAsDatabaseString(string path) => path.Replace('\\', '/');
90+
8491
public static File Create(Context cx, string path) => FileFactory.Instance.CreateEntity(cx, path);
8592

8693
public static File CreateGenerated(Context cx) => GeneratedFile.Create(cx);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public string Path
1616
private set;
1717
}
1818

19-
public string DatabasePath => File.FileAsDatabaseString(Path);
19+
public string DatabasePath => File.PathAsDatabaseId(Path);
2020

2121
public override void Populate()
2222
{
@@ -27,7 +27,7 @@ public override void Populate()
2727
// On Windows: System.IO.DirectoryInfo.Name returns "L:\"
2828
string shortName = symbol.Parent == null ? "" : symbol.Name;
2929

30-
Context.Emit(Tuples.folders(this, DatabasePath, shortName));
30+
Context.Emit(Tuples.folders(this, Semmle.Extraction.Entities.File.PathAsDatabaseString(Path), shortName));
3131
if (symbol.Parent != null)
3232
{
3333
Context.Emit(Tuples.containerparent(Create(Context, symbol.Parent), this));

csharp/ql/test/library-tests/cil/consistency/Handles.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class MetadataEntity extends DotNet::NamedElement, @metadata_entity {
1111
}
1212

1313
query predicate tooManyMatchingHandles(MetadataEntity e) {
14-
count(MetadataEntity e2 | e.matchesHandle(e2))>2
14+
strictcount(MetadataEntity e2 | e.matchesHandle(e2)) > 2
1515
}
1616

1717
query predicate missingCil(Element e) {

0 commit comments

Comments
 (0)