C#: Adjust trap location, database ID and archiving of generated sources

This commit is contained in:
Tamas Vajk
2024-06-17 13:59:35 +02:00
parent dcd84f47a4
commit fb0520c74a
8 changed files with 124 additions and 42 deletions

View File

@@ -0,0 +1,60 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Semmle.Util.Logging;
namespace Semmle.Extraction
{
public class BinaryLogExtractionContext : ExtractionContext
{
private readonly IEnumerable<SyntaxTree> generatedSyntaxTrees;
private readonly string compilationIdentifier;
private readonly string generatedFolderName;
public BinaryLogExtractionContext(string cwd, string[] args, string outputPath,
IEnumerable<SyntaxTree> generatedSyntaxTrees, string compilationIdentifier,
ILogger logger, PathTransformer pathTransformer, bool isQlTest)
: base(cwd, args, outputPath, [], logger, pathTransformer, ExtractorMode.BinaryLog, isQlTest)
{
this.generatedSyntaxTrees = generatedSyntaxTrees;
this.compilationIdentifier = compilationIdentifier;
// Compute a unique folder name for the generated files:
generatedFolderName = "generated";
if (Directory.Exists(generatedFolderName))
{
var counter = 0;
do
{
generatedFolderName = $"generated{counter++}";
}
while (Directory.Exists(generatedFolderName));
}
}
private string? GetAdjustedPath(string path)
{
var syntaxTree = generatedSyntaxTrees.FirstOrDefault(t => t.FilePath == path);
if (syntaxTree is null)
{
return null;
}
return Path.Join(generatedFolderName, compilationIdentifier, path);
}
public static string? GetAdjustedPath(ExtractionContext extractionContext, string sourcePath)
{
if (extractionContext.Mode.HasFlag(ExtractorMode.BinaryLog)
&& extractionContext is BinaryLogExtractionContext binaryLogExtractionContext
&& binaryLogExtractionContext.GetAdjustedPath(sourcePath) is string adjustedPath)
{
return adjustedPath;
}
return null;
}
}
}