Skip to content

Commit c8509cc

Browse files
committed
C#: Introduce extractor mode to identify DBs created with codeql test run
1 parent 3df3054 commit c8509cc

File tree

24 files changed

+97
-80
lines changed

24 files changed

+97
-80
lines changed

csharp/extractor/Semmle.Extraction.CIL.Driver/ExtractorOptions.cs

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Reflection;
77
using System.Runtime.InteropServices;
88
using System.Globalization;
9+
using Semmle.Util;
910
using Semmle.Util.Logging;
1011

1112
namespace Semmle.Extraction.CIL.Driver
@@ -169,18 +170,14 @@ public void ResolveReferences()
169170
/// <summary>
170171
/// Parses the command line and collates a list of DLLs/EXEs to extract.
171172
/// </summary>
172-
internal class ExtractorOptions
173+
internal class ExtractorOptions : CommonOptions
173174
{
174175
private readonly AssemblyList assemblyList = new AssemblyList();
175176

176177
public ExtractorOptions(string[] args)
177178
{
178-
Verbosity = Verbosity.Info;
179-
Threads = System.Environment.ProcessorCount;
180179
PDB = true;
181-
TrapCompression = TrapWriter.CompressionMode.Gzip;
182-
183-
ParseArgs(args);
180+
this.ParseArguments(args);
184181

185182
AddFrameworkDirectories(false);
186183

@@ -203,12 +200,6 @@ private void AddFrameworkDirectories(bool extractAll)
203200
AddDirectory(RuntimeEnvironment.GetRuntimeDirectory(), extractAll);
204201
}
205202

206-
public Verbosity Verbosity { get; private set; }
207-
public bool NoCache { get; private set; }
208-
public int Threads { get; private set; }
209-
public bool PDB { get; private set; }
210-
public TrapWriter.CompressionMode TrapCompression { get; private set; }
211-
212203
private void AddFileOrDirectory(string path)
213204
{
214205
path = Path.GetFullPath(path);
@@ -237,43 +228,25 @@ private void AddFileOrDirectory(string path)
237228
/// </summary>
238229
public IEnumerable<AssemblyName> MissingReferences => assemblyList.MissingReferences;
239230

240-
private void ParseArgs(string[] args)
231+
public override bool HandleFlag(string flag, bool value)
241232
{
242-
foreach (var arg in args)
233+
switch (flag)
243234
{
244-
if (arg == "--verbose")
245-
{
246-
Verbosity = Verbosity.All;
247-
}
248-
else if (arg == "--silent")
249-
{
250-
Verbosity = Verbosity.Off;
251-
}
252-
else if (arg.StartsWith("--verbosity:"))
253-
{
254-
Verbosity = (Verbosity)int.Parse(arg.Substring(12));
255-
}
256-
else if (arg == "--dotnet")
257-
{
258-
AddFrameworkDirectories(true);
259-
}
260-
else if (arg == "--nocache")
261-
{
262-
NoCache = true;
263-
}
264-
else if (arg.StartsWith("--threads:"))
265-
{
266-
Threads = int.Parse(arg.Substring(10));
267-
}
268-
else if (arg == "--no-pdb")
269-
{
270-
PDB = false;
271-
}
272-
else
273-
{
274-
AddFileOrDirectory(arg);
275-
}
235+
case "dotnet":
236+
if (value)
237+
AddFrameworkDirectories(true);
238+
return true;
239+
default:
240+
return base.HandleFlag(flag, value);
276241
}
277242
}
243+
244+
public override bool HandleArgument(string argument)
245+
{
246+
AddFileOrDirectory(argument);
247+
return true;
248+
}
249+
250+
public override void InvalidArgument(string argument) { }
278251
}
279252
}

csharp/extractor/Semmle.Extraction.CIL.Driver/Program.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ private static void DisplayHelp()
2020
Console.WriteLine(" path A directory/dll/exe to analyze");
2121
}
2222

23-
private static void ExtractAssembly(Layout layout, string assemblyPath, ILogger logger, bool nocache, bool extractPdbs, TrapWriter.CompressionMode trapCompression)
23+
private static void ExtractAssembly(Layout layout, string assemblyPath, ILogger logger, CommonOptions options)
2424
{
2525
var sw = new Stopwatch();
2626
sw.Start();
27-
Analyser.ExtractCIL(layout, assemblyPath, logger, nocache, extractPdbs, trapCompression, out _, out _);
27+
Analyser.ExtractCIL(layout, assemblyPath, logger, options, out _, out _);
2828
sw.Stop();
2929
logger.Log(Severity.Info, " {0} ({1})", assemblyPath, sw.Elapsed);
3030
}
@@ -43,8 +43,7 @@ public static void Main(string[] args)
4343

4444
var actions = options.AssembliesToExtract
4545
.Select(asm => asm.Filename)
46-
.Select<string, Action>(filename =>
47-
() => ExtractAssembly(layout, filename, logger, options.NoCache, options.PDB, options.TrapCompression))
46+
.Select<string, Action>(filename => () => ExtractAssembly(layout, filename, logger, options))
4847
.ToArray();
4948

5049
foreach (var missingRef in options.MissingReferences)

csharp/extractor/Semmle.Extraction.CIL/Analyser.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ private static void ExtractCIL(TracingExtractor extractor, TrapWriter trapWriter
2525
/// <param name="extractPdbs">Whether to extract PDBs.</param>
2626
/// <param name="trapFile">The path of the trap file.</param>
2727
/// <param name="extracted">Whether the file was extracted (false=cached).</param>
28-
public static void ExtractCIL(Layout layout, string assemblyPath, ILogger logger, bool nocache, bool extractPdbs, TrapWriter.CompressionMode trapCompression, out string trapFile, out bool extracted)
28+
public static void ExtractCIL(Layout layout, string assemblyPath, ILogger logger, CommonOptions options, out string trapFile, out bool extracted)
2929
{
3030
trapFile = "";
3131
extracted = false;
3232
try
3333
{
3434
var canonicalPathCache = CanonicalPathCache.Create(logger, 1000);
3535
var pathTransformer = new PathTransformer(canonicalPathCache);
36-
var extractor = new TracingExtractor(assemblyPath, logger, pathTransformer);
36+
var extractor = new TracingExtractor(assemblyPath, logger, pathTransformer, options);
3737
var transformedAssemblyPath = pathTransformer.Transform(assemblyPath);
3838
var project = layout.LookupProjectOrDefault(transformedAssemblyPath);
39-
using var trapWriter = project.CreateTrapWriter(logger, transformedAssemblyPath.WithSuffix(".cil"), trapCompression, discardDuplicates: true);
39+
using var trapWriter = project.CreateTrapWriter(logger, transformedAssemblyPath.WithSuffix(".cil"), options.TrapCompression, discardDuplicates: true);
4040
trapFile = trapWriter.TrapFile;
41-
if (nocache || !System.IO.File.Exists(trapFile))
41+
if (!options.Cache || !System.IO.File.Exists(trapFile))
4242
{
43-
ExtractCIL(extractor, trapWriter, extractPdbs);
43+
ExtractCIL(extractor, trapWriter, options.PDB);
4444
extracted = true;
4545
}
4646
}

csharp/extractor/Semmle.Extraction.CIL/Entities/PdbSourceFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public override IEnumerable<IExtractionProduct> Contents
2525
else
2626
Context.TrapWriter.Archive(TransformedPath, text);
2727

28-
yield return Tuples.file_extraction_mode(this, 2);
28+
yield return Tuples.file_extraction_mode(this, Context.Extractor.Mode | ExtractorMode.Pdb);
2929
}
3030
}
3131
}

csharp/extractor/Semmle.Extraction.CIL/Tuples.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ internal static Tuple containerparent(Folder parent, IFileOrFolder child) =>
206206
internal static Tuple files(File file, string fullName) =>
207207
new Tuple("files", file, fullName);
208208

209-
internal static Tuple file_extraction_mode(File file, int mode) =>
209+
internal static Tuple file_extraction_mode(File file, ExtractorMode mode) =>
210210
new Tuple("file_extraction_mode", file, mode);
211211

212212
internal static Tuple folders(Folder folder, string path) =>

csharp/extractor/Semmle.Extraction.CSharp/Entities/Assembly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private class AssemblyConstructorFactory : CachedEntityFactory<Microsoft.CodeAna
6464

6565
public static Assembly CreateOutputAssembly(Context cx)
6666
{
67-
if (cx.Extractor.Standalone)
67+
if (cx.Extractor.Mode.HasFlag(ExtractorMode.Standalone))
6868
throw new InternalError("Attempting to create the output assembly in standalone extraction mode");
6969
return AssemblyConstructorFactory.Instance.CreateEntity(cx, outputAssemblyCacheKey, null);
7070
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/Attribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override void Populate(TextWriter trapFile)
5353

5454
if (attributeSyntax is not null)
5555
{
56-
if (!Context.Extractor.Standalone)
56+
if (!Context.Extractor.Mode.HasFlag(ExtractorMode.Standalone))
5757
{
5858
trapFile.attribute_location(this, Assembly.CreateOutputAssembly(Context));
5959
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/CachedSymbol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public virtual IEnumerable<Extraction.Entities.Location> Locations
8585
{
8686
// Some built in operators lack locations, so loc is null.
8787
yield return Context.CreateLocation(ReportingLocation);
88-
if (!Context.Extractor.Standalone && loc.Kind == LocationKind.SourceFile)
88+
if (!Context.Extractor.Mode.HasFlag(ExtractorMode.Standalone) && loc.Kind == LocationKind.SourceFile)
8989
yield return Assembly.CreateOutputAssembly(Context);
9090
}
9191
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Invocation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public IMethodSymbol? TargetSymbol
129129
.Where(method => method.Parameters.Length >= Syntax.ArgumentList.Arguments.Count)
130130
.Where(method => method.Parameters.Count(p => !p.HasExplicitDefaultValue) <= Syntax.ArgumentList.Arguments.Count);
131131

132-
return Context.Extractor.Standalone ?
132+
return Context.Extractor.Mode.HasFlag(ExtractorMode.Standalone) ?
133133
candidates.FirstOrDefault() :
134134
candidates.SingleOrDefault();
135135
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/File.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public override void Populate(TextWriter trapFile)
6161
}
6262
}
6363

64-
trapFile.file_extraction_mode(this, Context.Extractor.Standalone ? 1 : 0);
64+
trapFile.file_extraction_mode(this, Context.Extractor.Mode);
6565
}
6666

6767
private bool IsPossiblyTextFile()

0 commit comments

Comments
 (0)