C#: Address review comments. Replace GetHashValue() with MetadataTokens.GetToken().

C#: Make path IDs consistent.
This commit is contained in:
calum 2018-12-19 14:50:43 +00:00
Родитель efe2fb502e
Коммит d687dd9deb
10 изменённых файлов: 38 добавлений и 26 удалений

Просмотреть файл

@ -4,6 +4,7 @@ using System.Linq;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using System.Reflection.Metadata; using System.Reflection.Metadata;
using System.Reflection; using System.Reflection;
using System.Reflection.Metadata.Ecma335;
namespace Semmle.Extraction.CIL.Entities namespace Semmle.Extraction.CIL.Entities
{ {
@ -88,7 +89,7 @@ namespace Semmle.Extraction.CIL.Entities
{ {
get get
{ {
yield return Tuples.metadata_handle(this, cx.assembly, handle.GetHashCode()); yield return Tuples.metadata_handle(this, cx.assembly, MetadataTokens.GetToken(handle));
foreach (var c in base.Contents) foreach (var c in base.Contents)
yield return c; yield return c;

Просмотреть файл

@ -16,8 +16,8 @@ namespace Semmle.Extraction.CIL.Entities
public File(Context cx, string path) : base(cx) public File(Context cx, string path) : base(cx)
{ {
this.path = path.Replace("\\", "/"); this.path = path;
ShortId = new StringId(path.Replace(":", "_")); ShortId = new StringId(Semmle.Extraction.Entities.File.PathAsDatabaseId(path));
} }
public override IEnumerable<IExtractionProduct> Contents public override IEnumerable<IExtractionProduct> Contents
@ -27,7 +27,7 @@ namespace Semmle.Extraction.CIL.Entities
var parent = cx.CreateFolder(System.IO.Path.GetDirectoryName(path)); var parent = cx.CreateFolder(System.IO.Path.GetDirectoryName(path));
yield return parent; yield return parent;
yield return Tuples.containerparent(parent, this); yield return Tuples.containerparent(parent, this);
yield return Tuples.files(this, path, System.IO.Path.GetFileNameWithoutExtension(path), System.IO.Path.GetExtension(path).Substring(1)); yield return Tuples.files(this, Semmle.Extraction.Entities.File.PathAsDatabaseString(path), System.IO.Path.GetFileNameWithoutExtension(path), System.IO.Path.GetExtension(path).Substring(1));
} }
} }

Просмотреть файл

@ -14,7 +14,7 @@ namespace Semmle.Extraction.CIL.Entities
public Folder(Context cx, string path) : base(cx) public Folder(Context cx, string path) : base(cx)
{ {
this.path = path; this.path = path;
ShortId = new StringId(path.Replace("\\", "/").Replace(":", "_")); ShortId = new StringId(Semmle.Extraction.Entities.File.PathAsDatabaseId(path));
} }
static readonly Id suffix = new StringId(";folder"); static readonly Id suffix = new StringId(";folder");
@ -33,7 +33,7 @@ namespace Semmle.Extraction.CIL.Entities
yield return parentFolder; yield return parentFolder;
yield return Tuples.containerparent(parentFolder, this); yield return Tuples.containerparent(parentFolder, this);
} }
yield return Tuples.folders(this, path, Path.GetFileName(path)); yield return Tuples.folders(this, Semmle.Extraction.Entities.File.PathAsDatabaseString(path), Path.GetFileName(path));
} }
} }

Просмотреть файл

@ -5,6 +5,7 @@ using Microsoft.CodeAnalysis;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using System.Linq; using System.Linq;
using System.Reflection.Metadata.Ecma335;
namespace Semmle.Extraction.CIL.Entities namespace Semmle.Extraction.CIL.Entities
{ {
@ -207,7 +208,7 @@ namespace Semmle.Extraction.CIL.Entities
Attribute.Populate(cx, pe, p.GetCustomAttributes()); Attribute.Populate(cx, pe, p.GetCustomAttributes());
} }
yield return Tuples.metadata_handle(this, cx.assembly, handle.GetHashCode()); yield return Tuples.metadata_handle(this, cx.assembly, MetadataTokens.GetToken(handle));
yield return Tuples.cil_method(this, Name, declaringType, typeSignature.ReturnType); yield return Tuples.cil_method(this, Name, declaringType, typeSignature.ReturnType);
yield return Tuples.cil_method_source_declaration(this, this); yield return Tuples.cil_method_source_declaration(this, this);
yield return Tuples.cil_method_location(this, cx.assembly); yield return Tuples.cil_method_location(this, cx.assembly);

Просмотреть файл

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection.Metadata; using System.Reflection.Metadata;
using System.Linq; using System.Linq;
using System.Reflection.Metadata.Ecma335;
namespace Semmle.Extraction.CIL.Entities namespace Semmle.Extraction.CIL.Entities
{ {
@ -37,7 +38,7 @@ namespace Semmle.Extraction.CIL.Entities
{ {
get get
{ {
yield return Tuples.metadata_handle(this, cx.assembly, handle.GetHashCode()); yield return Tuples.metadata_handle(this, cx.assembly, MetadataTokens.GetToken(handle));
var sig = pd.DecodeSignature(cx.TypeSignatureDecoder, type); var sig = pd.DecodeSignature(cx.TypeSignatureDecoder, type);
yield return Tuples.cil_property(this, type, cx.ShortName(pd.Name), sig.ReturnType); yield return Tuples.cil_property(this, type, cx.ShortName(pd.Name), sig.ReturnType);

Просмотреть файл

@ -248,7 +248,7 @@ namespace Semmle.Extraction.CIL.Entities
/// </summary> /// </summary>
public sealed class TypeDefinitionType : Type public sealed class TypeDefinitionType : Type
{ {
Handle handle; readonly Handle handle;
readonly TypeDefinition td; readonly TypeDefinition td;
public TypeDefinitionType(Context cx, TypeDefinitionHandle handle) : base(cx) public TypeDefinitionType(Context cx, TypeDefinitionHandle handle) : base(cx)

Просмотреть файл

@ -4,6 +4,7 @@ using Semmle.Extraction.Entities;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection.Metadata; using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
namespace Semmle.Extraction.CSharp.Entities namespace Semmle.Extraction.CSharp.Entities
{ {
@ -103,11 +104,12 @@ namespace Semmle.Extraction.CSharp.Entities
protected void ExtractMetadataHandle() protected void ExtractMetadataHandle()
{ {
var handle = MetadataHandle; var handle = MetadataHandle;
if (!handle.IsNil)
Context.Emit(Tuples.metadata_handle(this, Location, handle.GetHashCode())); if (handle.HasValue)
Context.Emit(Tuples.metadata_handle(this, Location, MetadataTokens.GetToken(handle.Value)));
} }
public Handle MetadataHandle public Handle? MetadataHandle
{ {
get get
{ {
@ -122,21 +124,21 @@ namespace Semmle.Extraction.CSharp.Entities
{ {
return (MethodDefinitionHandle)value; return (MethodDefinitionHandle)value;
} }
else if(value is TypeDefinitionHandle) else if (value is TypeDefinitionHandle)
{ {
return (TypeDefinitionHandle)value; return (TypeDefinitionHandle)value;
} }
else if(value is PropertyDefinitionHandle) else if (value is PropertyDefinitionHandle)
{ {
return (PropertyDefinitionHandle)value; return (PropertyDefinitionHandle)value;
} }
else if(value is FieldDefinitionHandle) else if (value is FieldDefinitionHandle)
{ {
return (FieldDefinitionHandle)value; return (FieldDefinitionHandle)value;
} }
} }
return new Handle(); // A nil handle return null;
} }
} }
} }

Просмотреть файл

@ -19,7 +19,7 @@ namespace Semmle.Extraction.Entities
private set; private set;
} }
public string DatabasePath => FileAsDatabaseString(Path); public string DatabasePath => PathAsDatabaseId(Path);
public override bool NeedsPopulation => Context.DefinesFile(Path) || Path == Context.Extractor.OutputPath; public override bool NeedsPopulation => Context.DefinesFile(Path) || Path == Context.Extractor.OutputPath;
@ -41,7 +41,7 @@ namespace Semmle.Extraction.Entities
// remove the dot from the extension // remove the dot from the extension
if (extension.Length > 0) if (extension.Length > 0)
extension = extension.Substring(1); extension = extension.Substring(1);
Context.Emit(Tuples.files(this, DatabasePath, name, extension)); Context.Emit(Tuples.files(this, PathAsDatabaseString(Path), name, extension));
Context.Emit(Tuples.containerparent(Entities.Folder.Create(Context, fi.Directory), this)); Context.Emit(Tuples.containerparent(Entities.Folder.Create(Context, fi.Directory), this));
if (fromSource == 1) if (fromSource == 1)
@ -73,14 +73,21 @@ namespace Semmle.Extraction.Entities
} }
} }
internal static string FileAsDatabaseString(string fileName) /// <summary>
/// Converts a path string into a string to use as an ID
/// in the QL database.
/// </summary>
/// <param name="path">An absolute path.</param>
/// <returns>The database ID.</returns>
public static string PathAsDatabaseId(string path)
{ {
fileName = fileName.Replace('\\', '/'); if (path.Length >= 2 && path[1] == ':' && Char.IsLower(path[0]))
if (fileName.Length > 1 && fileName[1] == ':') path = Char.ToUpper(path[0]) + "_" + path.Substring(2);
fileName = Char.ToUpper(fileName[0]) + fileName.Substring(1); return path.Replace('\\', '/').Replace(":", "_");
return fileName;
} }
public static string PathAsDatabaseString(string path) => path.Replace('\\', '/');
public static File Create(Context cx, string path) => FileFactory.Instance.CreateEntity(cx, path); public static File Create(Context cx, string path) => FileFactory.Instance.CreateEntity(cx, path);
public static File CreateGenerated(Context cx) => GeneratedFile.Create(cx); public static File CreateGenerated(Context cx) => GeneratedFile.Create(cx);

Просмотреть файл

@ -16,7 +16,7 @@ namespace Semmle.Extraction.Entities
private set; private set;
} }
public string DatabasePath => File.FileAsDatabaseString(Path); public string DatabasePath => File.PathAsDatabaseId(Path);
public override void Populate() public override void Populate()
{ {
@ -27,7 +27,7 @@ namespace Semmle.Extraction.Entities
// On Windows: System.IO.DirectoryInfo.Name returns "L:\" // On Windows: System.IO.DirectoryInfo.Name returns "L:\"
string shortName = symbol.Parent == null ? "" : symbol.Name; string shortName = symbol.Parent == null ? "" : symbol.Name;
Context.Emit(Tuples.folders(this, DatabasePath, shortName)); Context.Emit(Tuples.folders(this, Semmle.Extraction.Entities.File.PathAsDatabaseString(Path), shortName));
if (symbol.Parent != null) if (symbol.Parent != null)
{ {
Context.Emit(Tuples.containerparent(Create(Context, symbol.Parent), this)); Context.Emit(Tuples.containerparent(Create(Context, symbol.Parent), this));

Просмотреть файл

@ -11,7 +11,7 @@ class MetadataEntity extends DotNet::NamedElement, @metadata_entity {
} }
query predicate tooManyMatchingHandles(MetadataEntity e) { query predicate tooManyMatchingHandles(MetadataEntity e) {
count(MetadataEntity e2 | e.matchesHandle(e2))>2 strictcount(MetadataEntity e2 | e.matchesHandle(e2)) > 2
} }
query predicate missingCil(Element e) { query predicate missingCil(Element e) {