Skip to content

Commit

Permalink
Capability for driver to read from a binary log
Browse files Browse the repository at this point in the history
This adds a new option to the driver, `--binlog`. When this is specified the driver will read all C# invocations from the binary log and process them.
  • Loading branch information
jaredpar committed Apr 26, 2024
1 parent 6147a38 commit 9a306c1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
51 changes: 49 additions & 2 deletions csharp/extractor/Semmle.Extraction.CSharp/Extractor/Extractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using Microsoft.CodeAnalysis.Text;
using Semmle.Util;
using Semmle.Util.Logging;
using Basic.CompilerLog;
using Basic.CompilerLog.Util;

namespace Semmle.Extraction.CSharp
{
Expand Down Expand Up @@ -92,11 +94,56 @@ public static ILogger MakeLogger(Verbosity verbosity, bool includeConsole)
/// <param name="args">Command line arguments as passed to csc.exe</param>
/// <returns><see cref="ExitCode"/></returns>
public static ExitCode Run(string[] args)
{
var options = Options.CreateWithEnvironment(args);
if (options.BinaryLogPath is string binlogPath)
{
using var fileStream = new FileStream(binlogPath, FileMode.Open, FileAccess.Read, FileShare.Read);

// Filter out compiler calls that aren't interesting for examination
var predicate = bool (CompilerCall compilerCall) =>
{
if (!compilerCall.IsCSharp)
{
return false;
}
return compilerCall.Kind switch
{
CompilerCallKind.XamlPreCompile => false,
CompilerCallKind.Satellite => false,
CompilerCallKind.WpfTemporaryCompile => false,
_ => true
};
};

var compilerCalls = BinaryLogUtil.ReadAllCompilerCalls(fileStream, predicate);
var exitCode = ExitCode.Ok;
foreach (var compilerCall in compilerCalls)
{
Console.WriteLine($"Processing {compilerCall.GetDiagnosticName()}");
var compilerCallOptions = Options.CreateWithEnvironment([]);
compilerCallOptions.CompilerName = compilerCall.CompilerFilePath;
compilerCallOptions.CompilerArguments.AddRange(compilerCall.GetArguments());
var ec = Run(compilerCallOptions);
if (ec != ExitCode.Ok)
{
exitCode = ec;
}
}

return exitCode;
}
else
{
return Run(options);
}
}

public static ExitCode Run(Options options)
{
var stopwatch = new Stopwatch();
stopwatch.Start();

var options = Options.CreateWithEnvironment(args);
Entities.Compilation.Settings = (Directory.GetCurrentDirectory(), options.CompilerArguments.ToArray());

using var logger = MakeLogger(options.Verbosity, options.Console);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ public sealed class Options : CommonOptions
/// <summary>
/// All other arguments passed to the compilation.
/// </summary>
public IList<string> CompilerArguments { get; } = new List<string>();
public List<string> CompilerArguments { get; } = new List<string>();

/// <summary>
/// Holds if assembly information should be prefixed to TRAP labels.
/// </summary>
public bool AssemblySensitiveTrap { get; private set; } = false;

public string? BinaryLogPath { get; set; }

public static Options CreateWithEnvironment(string[] arguments)
{
var options = new Options();
Expand Down Expand Up @@ -65,6 +67,9 @@ public override bool HandleOption(string key, string value)
case "load-sources-from-project":
ProjectsToLoad.Add(value);
return true;
case "binlog":
BinaryLogPath = value;
return true;
default:
return base.HandleOption(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.Build" Version="17.8.3" />
<PackageReference Include="Basic.CompilerLog.Util" Version="0.7.1" />
</ItemGroup>
</Project>

0 comments on commit 9a306c1

Please sign in to comment.