C#: Introduce extractor mode to identify DBs created with codeql test run

This commit is contained in:
Tom Hvitved
2022-01-05 11:54:38 +01:00
parent 3df30545d3
commit c8509cc382
24 changed files with 97 additions and 80 deletions

View File

@@ -6,6 +6,7 @@ using System.Reflection.Metadata;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Globalization;
using Semmle.Util;
using Semmle.Util.Logging;
namespace Semmle.Extraction.CIL.Driver
@@ -169,18 +170,14 @@ namespace Semmle.Extraction.CIL.Driver
/// <summary>
/// Parses the command line and collates a list of DLLs/EXEs to extract.
/// </summary>
internal class ExtractorOptions
internal class ExtractorOptions : CommonOptions
{
private readonly AssemblyList assemblyList = new AssemblyList();
public ExtractorOptions(string[] args)
{
Verbosity = Verbosity.Info;
Threads = System.Environment.ProcessorCount;
PDB = true;
TrapCompression = TrapWriter.CompressionMode.Gzip;
ParseArgs(args);
this.ParseArguments(args);
AddFrameworkDirectories(false);
@@ -203,12 +200,6 @@ namespace Semmle.Extraction.CIL.Driver
AddDirectory(RuntimeEnvironment.GetRuntimeDirectory(), extractAll);
}
public Verbosity Verbosity { get; private set; }
public bool NoCache { get; private set; }
public int Threads { get; private set; }
public bool PDB { get; private set; }
public TrapWriter.CompressionMode TrapCompression { get; private set; }
private void AddFileOrDirectory(string path)
{
path = Path.GetFullPath(path);
@@ -237,43 +228,25 @@ namespace Semmle.Extraction.CIL.Driver
/// </summary>
public IEnumerable<AssemblyName> MissingReferences => assemblyList.MissingReferences;
private void ParseArgs(string[] args)
public override bool HandleFlag(string flag, bool value)
{
foreach (var arg in args)
switch (flag)
{
if (arg == "--verbose")
{
Verbosity = Verbosity.All;
}
else if (arg == "--silent")
{
Verbosity = Verbosity.Off;
}
else if (arg.StartsWith("--verbosity:"))
{
Verbosity = (Verbosity)int.Parse(arg.Substring(12));
}
else if (arg == "--dotnet")
{
AddFrameworkDirectories(true);
}
else if (arg == "--nocache")
{
NoCache = true;
}
else if (arg.StartsWith("--threads:"))
{
Threads = int.Parse(arg.Substring(10));
}
else if (arg == "--no-pdb")
{
PDB = false;
}
else
{
AddFileOrDirectory(arg);
}
case "dotnet":
if (value)
AddFrameworkDirectories(true);
return true;
default:
return base.HandleFlag(flag, value);
}
}
public override bool HandleArgument(string argument)
{
AddFileOrDirectory(argument);
return true;
}
public override void InvalidArgument(string argument) { }
}
}

View File

@@ -20,11 +20,11 @@ namespace Semmle.Extraction.CIL.Driver
Console.WriteLine(" path A directory/dll/exe to analyze");
}
private static void ExtractAssembly(Layout layout, string assemblyPath, ILogger logger, bool nocache, bool extractPdbs, TrapWriter.CompressionMode trapCompression)
private static void ExtractAssembly(Layout layout, string assemblyPath, ILogger logger, CommonOptions options)
{
var sw = new Stopwatch();
sw.Start();
Analyser.ExtractCIL(layout, assemblyPath, logger, nocache, extractPdbs, trapCompression, out _, out _);
Analyser.ExtractCIL(layout, assemblyPath, logger, options, out _, out _);
sw.Stop();
logger.Log(Severity.Info, " {0} ({1})", assemblyPath, sw.Elapsed);
}
@@ -43,8 +43,7 @@ namespace Semmle.Extraction.CIL.Driver
var actions = options.AssembliesToExtract
.Select(asm => asm.Filename)
.Select<string, Action>(filename =>
() => ExtractAssembly(layout, filename, logger, options.NoCache, options.PDB, options.TrapCompression))
.Select<string, Action>(filename => () => ExtractAssembly(layout, filename, logger, options))
.ToArray();
foreach (var missingRef in options.MissingReferences)