mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
C#: Enable nullability on Semmle.Extraction.CIL.Driver (#4114)
This commit is contained in:
@@ -50,6 +50,13 @@ namespace Semmle.Extraction.CIL.Driver
|
||||
return an;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AssemblyInfo"/> class.
|
||||
/// </summary>
|
||||
/// <param name="path">Path of the assembly.</param>
|
||||
/// <exception cref="Semmle.Extraction.CIL.Driver.InvalidAssemblyException">
|
||||
/// Thrown when the input file is not a valid assembly.
|
||||
/// </exception>
|
||||
public AssemblyInfo(string path)
|
||||
{
|
||||
filename = path;
|
||||
@@ -60,13 +67,11 @@ namespace Semmle.Extraction.CIL.Driver
|
||||
{
|
||||
try
|
||||
{
|
||||
isAssembly = peReader.HasMetadata;
|
||||
if (!isAssembly) return;
|
||||
if (!peReader.HasMetadata) throw new InvalidAssemblyException();
|
||||
|
||||
var mdReader = peReader.GetMetadataReader();
|
||||
|
||||
isAssembly = mdReader.IsAssembly;
|
||||
if (!mdReader.IsAssembly) return;
|
||||
if (!mdReader.IsAssembly) throw new InvalidAssemblyException();
|
||||
|
||||
// Get our own assembly name
|
||||
name = CreateAssemblyName(mdReader, mdReader.GetAssemblyDefinition());
|
||||
@@ -81,7 +86,7 @@ namespace Semmle.Extraction.CIL.Driver
|
||||
// This failed on one of the Roslyn tests that includes
|
||||
// a deliberately malformed assembly.
|
||||
// In this case, we just skip the extraction of this assembly.
|
||||
isAssembly = false;
|
||||
throw new InvalidAssemblyException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,7 +94,6 @@ namespace Semmle.Extraction.CIL.Driver
|
||||
public readonly AssemblyName name;
|
||||
public readonly string filename;
|
||||
public bool extract;
|
||||
public readonly bool isAssembly;
|
||||
public readonly AssemblyName[] references;
|
||||
}
|
||||
|
||||
@@ -102,11 +106,12 @@ namespace Semmle.Extraction.CIL.Driver
|
||||
{
|
||||
class AssemblyNameComparer : IEqualityComparer<AssemblyName>
|
||||
{
|
||||
bool IEqualityComparer<AssemblyName>.Equals(AssemblyName x, AssemblyName y) =>
|
||||
x.Name == y.Name && x.Version == y.Version;
|
||||
bool IEqualityComparer<AssemblyName>.Equals(AssemblyName? x, AssemblyName? y) =>
|
||||
object.ReferenceEquals(x, y) ||
|
||||
x?.Name == y?.Name && x?.Version == y?.Version;
|
||||
|
||||
int IEqualityComparer<AssemblyName>.GetHashCode(AssemblyName obj) =>
|
||||
obj.Name.GetHashCode() + 7 * obj.Version.GetHashCode();
|
||||
(obj.Name, obj.Version).GetHashCode();
|
||||
}
|
||||
|
||||
readonly Dictionary<AssemblyName, AssemblyInfo> assembliesRead = new Dictionary<AssemblyName, AssemblyInfo>(new AssemblyNameComparer());
|
||||
@@ -116,13 +121,15 @@ namespace Semmle.Extraction.CIL.Driver
|
||||
if (!filesAnalyzed.Contains(assemblyPath))
|
||||
{
|
||||
filesAnalyzed.Add(assemblyPath);
|
||||
var info = new AssemblyInfo(assemblyPath);
|
||||
if (info.isAssembly)
|
||||
try
|
||||
{
|
||||
var info = new AssemblyInfo(assemblyPath);
|
||||
info.extract = extractAll;
|
||||
if (!assembliesRead.ContainsKey(info.name))
|
||||
assembliesRead.Add(info.name, info);
|
||||
}
|
||||
catch (InvalidAssemblyException)
|
||||
{ }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,8 +144,7 @@ namespace Semmle.Extraction.CIL.Driver
|
||||
while (assembliesToReference.Any())
|
||||
{
|
||||
var item = assembliesToReference.Pop();
|
||||
AssemblyInfo info;
|
||||
if (assembliesRead.TryGetValue(item, out info))
|
||||
if (assembliesRead.TryGetValue(item, out AssemblyInfo? info))
|
||||
{
|
||||
if (!info.extract)
|
||||
{
|
||||
@@ -165,6 +171,21 @@ namespace Semmle.Extraction.CIL.Driver
|
||||
{
|
||||
readonly AssemblyList assemblyList = new AssemblyList();
|
||||
|
||||
public ExtractorOptions(string[] args)
|
||||
{
|
||||
Verbosity = Verbosity.Info;
|
||||
Threads = System.Environment.ProcessorCount;
|
||||
PDB = true;
|
||||
TrapCompression = TrapWriter.CompressionMode.Gzip;
|
||||
|
||||
ParseArgs(args);
|
||||
|
||||
AddFrameworkDirectories(false);
|
||||
|
||||
assemblyList.ResolveReferences();
|
||||
AssembliesToExtract = assemblyList.AssembliesToExtract.ToArray();
|
||||
}
|
||||
|
||||
public void AddDirectory(string directory, bool extractAll)
|
||||
{
|
||||
foreach (var file in
|
||||
@@ -192,7 +213,12 @@ namespace Semmle.Extraction.CIL.Driver
|
||||
if (File.Exists(path))
|
||||
{
|
||||
assemblyList.AddFile(path, true);
|
||||
AddDirectory(Path.GetDirectoryName(path), false);
|
||||
string? directory = Path.GetDirectoryName(path);
|
||||
if (directory is null)
|
||||
{
|
||||
throw new InternalError($"Directory of path '{path}' is null");
|
||||
}
|
||||
AddDirectory(directory, false);
|
||||
}
|
||||
else if (Directory.Exists(path))
|
||||
{
|
||||
@@ -200,13 +226,7 @@ namespace Semmle.Extraction.CIL.Driver
|
||||
}
|
||||
}
|
||||
|
||||
void ResolveReferences()
|
||||
{
|
||||
assemblyList.ResolveReferences();
|
||||
AssembliesToExtract = assemblyList.AssembliesToExtract.ToArray();
|
||||
}
|
||||
|
||||
public IEnumerable<AssemblyInfo> AssembliesToExtract { get; private set; }
|
||||
public IEnumerable<AssemblyInfo> AssembliesToExtract { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the assemblies that were referenced but were not available to be
|
||||
@@ -215,55 +235,43 @@ namespace Semmle.Extraction.CIL.Driver
|
||||
/// </summary>
|
||||
public IEnumerable<AssemblyName> MissingReferences => assemblyList.missingReferences;
|
||||
|
||||
public static ExtractorOptions ParseCommandLine(string[] args)
|
||||
private void ParseArgs(string[] args)
|
||||
{
|
||||
var options = new ExtractorOptions();
|
||||
options.Verbosity = Verbosity.Info;
|
||||
options.Threads = System.Environment.ProcessorCount;
|
||||
options.PDB = true;
|
||||
options.TrapCompression = TrapWriter.CompressionMode.Gzip;
|
||||
|
||||
foreach (var arg in args)
|
||||
{
|
||||
if (arg == "--verbose")
|
||||
{
|
||||
options.Verbosity = Verbosity.All;
|
||||
Verbosity = Verbosity.All;
|
||||
}
|
||||
else if (arg == "--silent")
|
||||
{
|
||||
options.Verbosity = Verbosity.Off;
|
||||
Verbosity = Verbosity.Off;
|
||||
}
|
||||
else if (arg.StartsWith("--verbosity:"))
|
||||
{
|
||||
options.Verbosity = (Verbosity)int.Parse(arg.Substring(12));
|
||||
Verbosity = (Verbosity)int.Parse(arg.Substring(12));
|
||||
}
|
||||
else if (arg == "--dotnet")
|
||||
{
|
||||
options.AddFrameworkDirectories(true);
|
||||
AddFrameworkDirectories(true);
|
||||
}
|
||||
else if (arg == "--nocache")
|
||||
{
|
||||
options.NoCache = true;
|
||||
NoCache = true;
|
||||
}
|
||||
else if (arg.StartsWith("--threads:"))
|
||||
{
|
||||
options.Threads = int.Parse(arg.Substring(10));
|
||||
Threads = int.Parse(arg.Substring(10));
|
||||
}
|
||||
else if (arg == "--no-pdb")
|
||||
{
|
||||
options.PDB = false;
|
||||
PDB = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
options.AddFileOrDirectory(arg);
|
||||
AddFileOrDirectory(arg);
|
||||
}
|
||||
}
|
||||
|
||||
options.AddFrameworkDirectories(false);
|
||||
options.ResolveReferences();
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
using System;
|
||||
|
||||
namespace Semmle.Extraction.CIL.Driver
|
||||
{
|
||||
class InvalidAssemblyException : Exception
|
||||
{ }
|
||||
}
|
||||
@@ -40,7 +40,7 @@ namespace Semmle.Extraction.CIL.Driver
|
||||
return;
|
||||
}
|
||||
|
||||
var options = ExtractorOptions.ParseCommandLine(args);
|
||||
var options = new ExtractorOptions(args);
|
||||
var layout = new Layout();
|
||||
var logger = new ConsoleLogger(options.Verbosity);
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
<RootNamespace>Semmle.Extraction.CIL.Driver</RootNamespace>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user