Skip to content

Enable nullability checks on Semmle.Extraction.CIL #4113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion csharp/extractor/Semmle.Extraction.CIL/CachedFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Semmle.Extraction.CIL
/// </summary>
/// <typeparam name="SrcType">The type of the source.</typeparam>
/// <typeparam name="TargetType">The type of the generated object.</typeparam>
public class CachedFunction<SrcType, TargetType>
public class CachedFunction<SrcType, TargetType> where SrcType : notnull
{
readonly Func<SrcType, TargetType> generator;
readonly Dictionary<SrcType, TargetType> cache;
Expand Down
23 changes: 15 additions & 8 deletions csharp/extractor/Semmle.Extraction.CIL/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ namespace Semmle.Extraction.CIL
/// </summary>
partial class Context : IDisposable
{
public Extraction.Context cx;
readonly FileStream stream;
public readonly MetadataReader mdReader;
public readonly PEReader peReader;
public readonly string assemblyPath;
public Entities.Assembly assembly;
public PDB.IPdb pdb;
Entities.Assembly? assemblyNull;

public Extraction.Context cx { get; }
public MetadataReader mdReader { get; }
public PEReader peReader { get; }
public string assemblyPath { get; }
public Entities.Assembly assembly
{
get { return assemblyNull!; }
set { assemblyNull = value; }
}
public PDB.IPdb? pdb { get; }

public Context(Extraction.Context cx, string assemblyPath, bool extractPdbs)
{
Expand Down Expand Up @@ -105,7 +111,7 @@ public Entities.Type ErrorType
/// </summary>
/// <param name="handle">The handle of the method.</param>
/// <returns>The debugging information, or null if the information could not be located.</returns>
public PDB.IMethod GetMethodDebugInformation(MethodDefinitionHandle handle)
public PDB.IMethod? GetMethodDebugInformation(MethodDefinitionHandle handle)
{
return pdb == null ? null : pdb.GetMethod(handle.ToDebugInformationHandle());
}
Expand All @@ -125,7 +131,8 @@ public GenericContext(Context cx)
}

/// <summary>
/// The list of generic type parameters.
/// The list of generic type parameters, including type parameters of
/// containing types.
/// </summary>
public abstract IEnumerable<Entities.Type> TypeParameters { get; }

Expand Down
10 changes: 5 additions & 5 deletions csharp/extractor/Semmle.Extraction.CIL/Entities/Assembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public override void WriteId(TextWriter trapFile)
trapFile.Write(cx.assemblyPath.Replace("\\", "/"));
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return GetType() == obj.GetType() && Equals(file, ((Assembly)obj).file);
return GetType() == obj?.GetType() && Equals(file, ((Assembly)obj).file);
}

public override int GetHashCode() => 7 * file.GetHashCode();
Expand All @@ -63,7 +63,7 @@ public override IEnumerable<IExtractionProduct> Contents
get
{
yield return file;
yield return Tuples.assemblies(this, file, FullName, assemblyName.Name, assemblyName.Version.ToString());
yield return Tuples.assemblies(this, file, FullName, assemblyName.Name ?? string.Empty, assemblyName.Version?.ToString() ?? string.Empty);

if (cx.pdb != null)
{
Expand All @@ -75,7 +75,7 @@ public override IEnumerable<IExtractionProduct> Contents

foreach (var handle in cx.mdReader.TypeDefinitions)
{
IExtractionProduct product = null;
IExtractionProduct? product = null;
try
{
product = cx.Create(handle);
Expand All @@ -92,7 +92,7 @@ public override IEnumerable<IExtractionProduct> Contents

foreach (var handle in cx.mdReader.MethodDefinitions)
{
IExtractionProduct product = null;
IExtractionProduct? product = null;
try
{
product = cx.Create(handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Attribute(Context cx, IEntity @object, CustomAttributeHandle handle) : ba
this.@object = @object;
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return obj is Attribute attribute && handle.Equals(attribute.handle);
}
Expand Down Expand Up @@ -58,13 +58,15 @@ public override IEnumerable<IExtractionProduct> Contents
for (int index = 0; index < decoded.FixedArguments.Length; ++index)
{
object value = decoded.FixedArguments[index].Value;
yield return Tuples.cil_attribute_positional_argument(this, index, value == null ? "null" : value.ToString());
var stringValue = value?.ToString();
yield return Tuples.cil_attribute_positional_argument(this, index, stringValue ?? "null");
}

foreach (var p in decoded.NamedArguments)
{
object value = p.Value;
yield return Tuples.cil_attribute_named_argument(this, p.Name, value == null ? "null" : value.ToString());
var stringValue = value?.ToString();
yield return Tuples.cil_attribute_named_argument(this, p.Name, stringValue ?? "null");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion csharp/extractor/Semmle.Extraction.CIL/Entities/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override void WriteId(TextWriter trapFile)

public override string IdSuffix => ";cil-event";

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return obj is Event e && handle.Equals(e.handle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override IEnumerable<IExtractionProduct> Contents
{
get
{
IInstruction try_start, try_end, handler_start;
IInstruction? try_start, try_end, handler_start;

if (!jump_table.TryGetValue(r.TryOffset, out try_start))
throw new InternalError("Failed to retrieve handler");
Expand All @@ -44,7 +44,7 @@ public override IEnumerable<IExtractionProduct> Contents

if (r.FilterOffset != -1)
{
IInstruction filter_start;
IInstruction? filter_start;
if (!jump_table.TryGetValue(r.FilterOffset, out filter_start))
throw new InternalError("ExceptionRegion filter clause");

Expand Down
4 changes: 2 additions & 2 deletions csharp/extractor/Semmle.Extraction.CIL/Entities/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public DefinitionField(GenericContext gc, FieldDefinitionHandle handle) : base(g
fd = cx.mdReader.GetFieldDefinition(handle);
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return obj is DefinitionField field && handle.Equals(field.handle);
}
Expand Down Expand Up @@ -153,7 +153,7 @@ public MemberReferenceField(GenericContext gc, MemberReferenceHandle handle) : b
declType = (Type)cx.CreateGeneric(gc, mr.Parent);
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return obj is MemberReferenceField field && Handle.Equals(field.Handle);
}
Expand Down
10 changes: 7 additions & 3 deletions csharp/extractor/Semmle.Extraction.CIL/Entities/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public override void WriteId(TextWriter trapFile)
trapFile.Write(Semmle.Extraction.Entities.File.PathAsDatabaseId(path));
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return GetType() == obj.GetType() && path == ((File)obj).path;
return GetType() == obj?.GetType() && path == ((File)obj).path;
}

public override int GetHashCode() => 11 * path.GetHashCode();
Expand All @@ -36,7 +36,11 @@ public override IEnumerable<IExtractionProduct> Contents
{
get
{
var parent = cx.CreateFolder(System.IO.Path.GetDirectoryName(path));
var directoryName = System.IO.Path.GetDirectoryName(path);
if (directoryName is null)
throw new InternalError($"Directory name for path '{path}' is null.");

var parent = cx.CreateFolder(directoryName);
yield return parent;
yield return Tuples.containerparent(parent, this);
yield return Tuples.files(this, path, System.IO.Path.GetFileNameWithoutExtension(path), System.IO.Path.GetExtension(path).Substring(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override IEnumerable<IExtractionProduct> Contents
}
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return obj is Folder folder && path == folder.path;
}
Expand Down
17 changes: 14 additions & 3 deletions csharp/extractor/Semmle.Extraction.CIL/Entities/Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ public override IEnumerable<IExtractionProduct> Contents
{
int offset = Offset;

if (Method.Implementation is null)
{
yield break;
}

yield return Tuples.cil_instruction(this, (int)OpCode, Index, Method.Implementation);

switch (PayloadType)
Expand Down Expand Up @@ -414,11 +419,17 @@ public override IEnumerable<IExtractionProduct> Contents
break;
case Payload.Arg8:
case Payload.Arg16:
yield return Tuples.cil_access(this, Method.Parameters[(int)UnsignedPayloadValue]);
if (Method.Parameters is object)
{
yield return Tuples.cil_access(this, Method.Parameters[(int)UnsignedPayloadValue]);
}
break;
case Payload.Local8:
case Payload.Local16:
yield return Tuples.cil_access(this, Method.LocalVariables[(int)UnsignedPayloadValue]);
if (Method.LocalVariables is object)
{
yield return Tuples.cil_access(this, Method.LocalVariables[(int)UnsignedPayloadValue]);
}
break;
case Payload.None:
case Payload.Target8:
Expand All @@ -439,7 +450,7 @@ public override IEnumerable<IExtractionProduct> Contents
public IEnumerable<IExtractionProduct> JumpContents(Dictionary<int, IInstruction> jump_table)
{
int target;
IInstruction inst;
IInstruction? inst;

switch (PayloadType)
{
Expand Down
Loading