C#: Code tidy. Rename variables, delete dead code, format whitespace, improve comments.

This commit is contained in:
Calum Grant
2019-08-23 14:31:30 +01:00
parent b3d5e405a6
commit 40f56ff4b3
29 changed files with 359 additions and 306 deletions

View File

@@ -33,7 +33,7 @@ namespace Semmle.Extraction.CIL
globalNamespace = new Lazy<Entities.Namespace>(() => Populate(new Entities.Namespace(this, "", null)));
systemNamespace = new Lazy<Entities.Namespace>(() => Populate(new Entities.Namespace(this, "System")));
genericHandleFactory = new CachedFunction<GenericContext, Handle, ILabelledEntity>(CreateGenericHandle);
genericHandleFactory = new CachedFunction<GenericContext, Handle, IExtractedEntity>(CreateGenericHandle);
namespaceFactory = new CachedFunction<StringHandle, Entities.Namespace>(n => CreateNamespace(mdReader.GetString(n)));
namespaceDefinitionFactory = new CachedFunction<NamespaceDefinitionHandle, Entities.Namespace>(CreateNamespace);
sourceFiles = new CachedFunction<PDB.ISourceFile, Entities.PdbSourceFile>(path => new Entities.PdbSourceFile(this, path));

View File

@@ -7,7 +7,7 @@ namespace Semmle.Extraction.CIL.Entities
/// <summary>
/// An event.
/// </summary>
interface IEvent : ILabelledEntity
interface IEvent : IExtractedEntity
{
}

View File

@@ -13,7 +13,7 @@ namespace Semmle.Extraction.CIL.Entities
/// An entity represting a member.
/// Used to type tuples correctly.
/// </summary>
interface IMember : ILabelledEntity
interface IMember : IExtractedEntity
{
}

View File

@@ -16,7 +16,6 @@ namespace Semmle.Extraction.CIL.Entities
this.path = path;
}
public override void WriteId(TextWriter trapFile)
{
trapFile.Write(Semmle.Extraction.Entities.File.PathAsDatabaseId(path));

View File

@@ -3,7 +3,7 @@ using System.IO;
namespace Semmle.Extraction.CIL.Entities
{
interface ILocal : ILabelledEntity
interface ILocal : IExtractedEntity
{
}

View File

@@ -64,7 +64,7 @@ namespace Semmle.Extraction.CIL.Entities
int index = 0;
foreach (var param in signature.ParameterTypes)
{
trapFile.WriteSeparator(",", index++);
trapFile.WriteSeparator(",", ref index);
param.WriteId(trapFile, this);
}
trapFile.Write(')');
@@ -503,7 +503,7 @@ namespace Semmle.Extraction.CIL.Entities
int index = 0;
foreach(var param in typeParams)
{
trapFile.WriteSeparator(",", index++);
trapFile.WriteSeparator(",", ref index);
trapFile.WriteSubId(param);
}
trapFile.Write('>');
@@ -516,10 +516,7 @@ namespace Semmle.Extraction.CIL.Entities
return obj is MethodSpecificationMethod method && handle.Equals(method.handle) && typeParams.SequenceEqual(method.typeParams);
}
public override int GetHashCode()
{
return handle.GetHashCode() * 11 + typeParams.SequenceHash();
}
public override int GetHashCode() => handle.GetHashCode() * 11 + typeParams.SequenceHash();
public override Method SourceDeclaration => unboundMethod;

View File

@@ -6,7 +6,7 @@ namespace Semmle.Extraction.CIL.Entities
/// <summary>
/// A parameter entity.
/// </summary>
interface IParameter : ILabelledEntity
interface IParameter : IExtractedEntity
{
}

View File

@@ -9,7 +9,7 @@ namespace Semmle.Extraction.CIL.Entities
/// <summary>
/// A property.
/// </summary>
interface IProperty : ILabelledEntity
interface IProperty : IExtractedEntity
{
}
@@ -42,11 +42,12 @@ namespace Semmle.Extraction.CIL.Entities
var signature = pd.DecodeSignature(new SignatureDecoder(), gc);
foreach (var param in signature.ParameterTypes)
{
trapFile.WriteSeparator(",", index++);
trapFile.WriteSeparator(",", ref index);
param.WriteId(trapFile, gc);
}
trapFile.Write(")");
}
public override bool Equals(object obj)
{
return obj is Property property && Equals(handle, property.handle);

View File

@@ -39,7 +39,7 @@ namespace Semmle.Extraction.CIL.Entities
/// <summary>
/// A type container (namespace/types/method).
/// </summary>
interface ITypeContainer : ILabelledEntity
interface ITypeContainer : IExtractedEntity
{
}
@@ -231,10 +231,9 @@ namespace Semmle.Extraction.CIL.Entities
/// <summary>
/// Gets the primitive type corresponding to this type, if possible.
/// </summary>
/// <param name="cx">Extraction context.</param>
/// <param name="t">The resulting primitive type.</param>
/// <param name="t">The resulting primitive type, or null.</param>
/// <returns>True if this type is a primitive type.</returns>
public bool TryGetPrimitiveType(Context cx, out PrimitiveType t)
public bool TryGetPrimitiveType(out PrimitiveType t)
{
if (TryGetPrimitiveTypeCode(out var code))
{
@@ -830,7 +829,7 @@ namespace Semmle.Extraction.CIL.Entities
int index = 0;
foreach (var t in thisTypeArguments)
{
trapFile.WriteSeparator(",", index++);
trapFile.WriteSeparator(",", ref index);
t.WriteId(trapFile);
}
trapFile.Write('>');
@@ -1253,7 +1252,7 @@ namespace Semmle.Extraction.CIL.Entities
int index = 0;
foreach(var arg in typeArguments)
{
trapFile.WriteSeparator(",", index++);
trapFile.WriteSeparator(",", ref index);
arg.WriteId(trapFile, gc);
}
trapFile.Write('>');

View File

@@ -12,7 +12,7 @@ namespace Semmle.Extraction.CIL
/// The extraction algorithm proceeds as follows:
/// - Construct entity
/// - Call Extract()
/// - ILabelledEntity check if already extracted
/// - IExtractedEntity check if already extracted
/// - Enumerate Contents to produce more extraction products
/// - Extract these until there is nothing left to extract
/// </remarks>
@@ -78,7 +78,7 @@ namespace Semmle.Extraction.CIL
/// An entity that needs to be populated during extraction.
/// This assigns a key and optionally extracts its contents.
/// </summary>
public abstract class LabelledEntity : ILabelledEntity
public abstract class LabelledEntity : IExtractedEntity
{
public abstract IEnumerable<IExtractionProduct> Contents { get; }
public Label Label { get; set; }
@@ -120,13 +120,6 @@ namespace Semmle.Extraction.CIL
TrapStackBehaviour IEntity.TrapStackBehaviour => TrapStackBehaviour.NoLabel;
}
/// <summary>
/// An entity with a defined ID.
/// </summary>
public interface ILabelledEntity : IExtractedEntity
{
}
/// <summary>
/// A tuple that is an extraction product.
/// </summary>

View File

@@ -13,7 +13,7 @@ namespace Semmle.Extraction.CIL
{
readonly Dictionary<object, Label> ids = new Dictionary<object, Label>();
public T Populate<T>(T e) where T : ILabelledEntity
public T Populate<T>(T e) where T : IExtractedEntity
{
if(e.Label.Valid)
{
@@ -41,7 +41,7 @@ namespace Semmle.Extraction.CIL
e.WriteId(writer);
var id = writer.ToString();
if (debugLabels.TryGetValue(id, out ILabelledEntity previousEntity))
if (debugLabels.TryGetValue(id, out IExtractedEntity previousEntity))
{
cx.Extractor.Message(new Message("Duplicate trap ID", id, null, severity: Util.Logging.Severity.Warning));
}
@@ -56,7 +56,7 @@ namespace Semmle.Extraction.CIL
}
#if DEBUG_LABELS
private readonly Dictionary<string, ILabelledEntity> debugLabels = new Dictionary<string, ILabelledEntity>();
private readonly Dictionary<string, IExtractedEntity> debugLabels = new Dictionary<string, IExtractedEntity>();
#endif
public IExtractedEntity Create(Handle h)
@@ -95,13 +95,13 @@ namespace Semmle.Extraction.CIL
/// <param name="h">The handle of the entity.</param>
/// <param name="genericContext">The generic context.</param>
/// <returns></returns>
public ILabelledEntity CreateGeneric(GenericContext genericContext, Handle h) => genericHandleFactory[genericContext, h];
public IExtractedEntity CreateGeneric(GenericContext genericContext, Handle h) => genericHandleFactory[genericContext, h];
readonly GenericContext defaultGenericContext;
ILabelledEntity CreateGenericHandle(GenericContext gc, Handle handle)
IExtractedEntity CreateGenericHandle(GenericContext gc, Handle handle)
{
ILabelledEntity entity;
IExtractedEntity entity;
switch (handle.Kind)
{
case HandleKind.MethodDefinition:
@@ -118,7 +118,8 @@ namespace Semmle.Extraction.CIL
break;
case HandleKind.TypeReference:
var tr = new TypeReferenceType(this, (TypeReferenceHandle)handle);
if (tr.TryGetPrimitiveType(gc.cx, out var pt))
if (tr.TryGetPrimitiveType(out var pt))
// Map special names like `System.Int32` to `int`
return pt;
entity = tr;
break;
@@ -135,7 +136,7 @@ namespace Semmle.Extraction.CIL
return entity;
}
ILabelledEntity Create(GenericContext gc, MemberReferenceHandle handle)
IExtractedEntity Create(GenericContext gc, MemberReferenceHandle handle)
{
var mr = mdReader.GetMemberReference(handle);
switch (mr.GetKind())
@@ -226,7 +227,7 @@ namespace Semmle.Extraction.CIL
#endregion
readonly CachedFunction<GenericContext, Handle, ILabelledEntity> genericHandleFactory;
readonly CachedFunction<GenericContext, Handle, IExtractedEntity> genericHandleFactory;
/// <summary>
/// Gets the short name of a member, without the preceding interface qualifier.