Rename Extractor to ExtractionContext

This commit is contained in:
Tamas Vajk
2024-06-12 11:43:12 +02:00
parent af2a78ea4d
commit cdca607828
17 changed files with 40 additions and 40 deletions

View File

@@ -17,7 +17,7 @@ namespace Semmle.Extraction.CSharp.Entities
isOutputAssembly = init is null;
if (isOutputAssembly)
{
assemblyPath = cx.Extractor.OutputPath;
assemblyPath = cx.ExtractionContext.OutputPath;
assembly = cx.Compilation.Assembly;
}
else
@@ -25,7 +25,7 @@ namespace Semmle.Extraction.CSharp.Entities
assembly = init!.MetadataModule!.ContainingAssembly;
var identity = assembly.Identity;
var idString = identity.Name + " " + identity.Version;
assemblyPath = cx.Extractor.GetAssemblyFile(idString);
assemblyPath = cx.ExtractionContext.GetAssemblyFile(idString);
}
}
@@ -33,7 +33,7 @@ namespace Semmle.Extraction.CSharp.Entities
{
if (assemblyPath is not null)
{
var isBuildlessOutputAssembly = isOutputAssembly && Context.Extractor.Mode.HasFlag(ExtractorMode.Standalone);
var isBuildlessOutputAssembly = isOutputAssembly && Context.ExtractionContext.Mode.HasFlag(ExtractorMode.Standalone);
var identifier = isBuildlessOutputAssembly
? ""
: assembly.ToString() ?? "";
@@ -74,7 +74,7 @@ namespace Semmle.Extraction.CSharp.Entities
public override void WriteId(EscapingTextWriter trapFile)
{
if (isOutputAssembly && Context.Extractor.Mode.HasFlag(ExtractorMode.Standalone))
if (isOutputAssembly && Context.ExtractionContext.Mode.HasFlag(ExtractorMode.Standalone))
{
trapFile.Write("buildlessOutputAssembly");
}

View File

@@ -18,8 +18,8 @@ namespace Semmle.Extraction.CSharp.Entities
#nullable disable warnings
private Compilation(Context cx) : base(cx, null)
{
cwd = cx.Extractor.Cwd;
args = cx.Extractor.Args;
cwd = cx.ExtractionContext.Cwd;
args = cx.ExtractionContext.Args;
hashCode = cwd.GetHashCode();
for (var i = 0; i < args.Length; i++)
{

View File

@@ -28,7 +28,7 @@ namespace Semmle.Extraction.CSharp.Entities
{
if (messageCount == limit + 1)
{
Context.Extractor.Logger.LogWarning($"Stopped logging {key} compiler diagnostics for the current compilation after reaching {limit}");
Context.ExtractionContext.Logger.LogWarning($"Stopped logging {key} compiler diagnostics for the current compilation after reaching {limit}");
}
return;

View File

@@ -133,7 +133,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
.Where(method => method.Parameters.Length >= Syntax.ArgumentList.Arguments.Count)
.Where(method => method.Parameters.Count(p => !p.HasExplicitDefaultValue) <= Syntax.ArgumentList.Arguments.Count);
return Context.Extractor.Mode.HasFlag(ExtractorMode.Standalone) ?
return Context.ExtractionContext.Mode.HasFlag(ExtractorMode.Standalone) ?
candidates.FirstOrDefault() :
candidates.SingleOrDefault();
}

View File

@@ -61,7 +61,7 @@ namespace Semmle.Extraction.CSharp.Entities
}
}
trapFile.file_extraction_mode(this, Context.Extractor.Mode);
trapFile.file_extraction_mode(this, Context.ExtractionContext.Mode);
}
private bool IsPossiblyTextFile()

View File

@@ -63,7 +63,7 @@ namespace Semmle.Extraction.CSharp.Entities
{
if (method.MethodKind == MethodKind.ReducedExtension)
{
cx.Extractor.Logger.Log(Semmle.Util.Logging.Severity.Warning, "Reduced extension method symbols should not be directly extracted.");
cx.ExtractionContext.Logger.Log(Semmle.Util.Logging.Severity.Warning, "Reduced extension method symbols should not be directly extracted.");
}
return OrdinaryMethodFactory.Instance.CreateEntityFromSymbol(cx, method);

View File

@@ -36,7 +36,7 @@ namespace Semmle.Extraction.CSharp.Entities
if (Symbol.TypeKind == TypeKind.Error)
{
UnknownType.Create(Context); // make sure this exists so we can use it in `TypeRef::getReferencedType`
Context.Extractor.MissingType(Symbol.ToString()!, Context.FromSource);
Context.ExtractionContext.MissingType(Symbol.ToString()!, Context.FromSource);
return;
}

View File

@@ -36,7 +36,7 @@ namespace Semmle.Extraction.CSharp.Entities
}
else
{
Context.Extractor.MissingNamespace(name.ToFullString(), Context.FromSource);
Context.ExtractionContext.MissingNamespace(name.ToFullString(), Context.FromSource);
Context.ModelError(node, "Namespace not found");
return;
}

View File

@@ -19,7 +19,7 @@ namespace Semmle.Extraction.CSharp
/// </summary>
public class Analyser : IDisposable
{
protected Extraction.Extractor? extractor;
protected ExtractionContext? ExtractionContext;
protected CSharpCompilation? compilation;
protected CommonOptions? options;
private protected Entities.Compilation? compilationEntity;
@@ -108,12 +108,12 @@ namespace Semmle.Extraction.CSharp
var def = reader.GetAssemblyDefinition();
assemblyIdentity = reader.GetString(def.Name) + " " + def.Version;
}
extractor.SetAssemblyFile(assemblyIdentity, refPath);
ExtractionContext.SetAssemblyFile(assemblyIdentity, refPath);
}
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
extractor.Message(new Message("Exception reading reference file", reference.FilePath, null, ex.StackTrace));
ExtractionContext.Message(new Message("Exception reading reference file", reference.FilePath, null, ex.StackTrace));
}
}
}
@@ -158,7 +158,7 @@ namespace Semmle.Extraction.CSharp
if (compilation.GetAssemblyOrModuleSymbol(r) is IAssemblySymbol assembly)
{
var cx = new Context(extractor, compilation, trapWriter, new AssemblyScope(assembly, assemblyPath), addAssemblyTrapPrefix);
var cx = new Context(ExtractionContext, compilation, trapWriter, new AssemblyScope(assembly, assemblyPath), addAssemblyTrapPrefix);
foreach (var module in assembly.Modules)
{
@@ -201,7 +201,7 @@ namespace Semmle.Extraction.CSharp
if (!upToDate)
{
var cx = new Context(extractor, compilation, trapWriter, new SourceScope(tree), addAssemblyTrapPrefix);
var cx = new Context(ExtractionContext, compilation, trapWriter, new SourceScope(tree), addAssemblyTrapPrefix);
// Ensure that the file itself is populated in case the source file is totally empty
var root = tree.GetRoot();
Entities.File.Create(cx, root.SyntaxTree.FilePath);
@@ -223,7 +223,7 @@ namespace Semmle.Extraction.CSharp
}
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
extractor.Message(new Message($"Unhandled exception processing syntax tree. {ex.Message}", tree.FilePath, null, ex.StackTrace));
ExtractionContext.Message(new Message($"Unhandled exception processing syntax tree. {ex.Message}", tree.FilePath, null, ex.StackTrace));
}
}
@@ -231,7 +231,7 @@ namespace Semmle.Extraction.CSharp
{
try
{
var assemblyPath = extractor.OutputPath;
var assemblyPath = ExtractionContext.OutputPath;
var stopwatch = new Stopwatch();
stopwatch.Start();
var currentTaskId = IncrementTaskCount();
@@ -241,11 +241,11 @@ namespace Semmle.Extraction.CSharp
var assembly = compilation.Assembly;
var trapWriter = transformedAssemblyPath.CreateTrapWriter(Logger, options.TrapCompression, discardDuplicates: false);
compilationTrapFile = trapWriter; // Dispose later
var cx = new Context(extractor, compilation, trapWriter, new AssemblyScope(assembly, assemblyPath), addAssemblyTrapPrefix);
var cx = new Context(ExtractionContext, compilation, trapWriter, new AssemblyScope(assembly, assemblyPath), addAssemblyTrapPrefix);
compilationEntity = Entities.Compilation.Create(cx);
extractor.CompilationInfos.ForEach(ci => trapWriter.Writer.compilation_info(compilationEntity, ci.key, ci.value));
ExtractionContext.CompilationInfos.ForEach(ci => trapWriter.Writer.compilation_info(compilationEntity, ci.key, ci.value));
ReportProgressTaskDone(currentTaskId, assemblyPath, trapWriter.TrapFile, stopwatch.Elapsed, AnalysisAction.Extracted);
}
@@ -328,7 +328,7 @@ namespace Semmle.Extraction.CSharp
/// <summary>
/// Number of errors encountered during extraction.
/// </summary>
private int ExtractorErrors => extractor?.Errors ?? 0;
private int ExtractorErrors => ExtractionContext?.Errors ?? 0;
/// <summary>
/// Number of errors encountered by the compiler.

View File

@@ -77,7 +77,7 @@ namespace Semmle.Extraction.CSharp
internal CommentProcessor CommentGenerator { get; } = new CommentProcessor();
public Context(Extraction.Extractor e, Compilation c, TrapWriter trapWriter, IExtractionScope scope, bool addAssemblyTrapPrefix)
public Context(ExtractionContext e, Compilation c, TrapWriter trapWriter, IExtractionScope scope, bool addAssemblyTrapPrefix)
: base(e, trapWriter, addAssemblyTrapPrefix)
{
Compilation = c;
@@ -187,13 +187,13 @@ namespace Semmle.Extraction.CSharp
try
{
var fullPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(mappedFromPath)!, mappedToPath));
Extractor.Logger.LogDebug($"Found relative path in line mapping: '{mappedToPath}', interpreting it as '{fullPath}'");
ExtractionContext.Logger.LogDebug($"Found relative path in line mapping: '{mappedToPath}', interpreting it as '{fullPath}'");
mappedToPath = fullPath;
}
catch (Exception e)
{
Extractor.Logger.LogDebug($"Failed to compute absolute path for relative path in line mapping: '{mappedToPath}': {e}");
ExtractionContext.Logger.LogDebug($"Failed to compute absolute path for relative path in line mapping: '{mappedToPath}': {e}");
}
}

View File

@@ -17,7 +17,7 @@ namespace Semmle.Extraction.CSharp
public void Initialize(string outputPath, IEnumerable<(string, string)> compilationInfos, CSharpCompilation compilationIn, CommonOptions options)
{
compilation = compilationIn;
extractor = new Extraction.Extractor(Directory.GetCurrentDirectory(), [], outputPath, compilationInfos, Logger, PathTransformer, ExtractorMode.Standalone, options.QlTest);
ExtractionContext = new ExtractionContext(Directory.GetCurrentDirectory(), [], outputPath, compilationInfos, Logger, PathTransformer, ExtractorMode.Standalone, options.QlTest);
this.options = options;
LogExtractorInfo();
SetReferencePaths();
@@ -25,9 +25,9 @@ namespace Semmle.Extraction.CSharp
#nullable disable warnings
public IEnumerable<string> MissingTypes => extractor.MissingTypes;
public IEnumerable<string> MissingTypes => ExtractionContext.MissingTypes;
public IEnumerable<string> MissingNamespaces => extractor.MissingNamespaces;
public IEnumerable<string> MissingNamespaces => ExtractionContext.MissingNamespaces;
#nullable restore warnings
}

View File

@@ -47,7 +47,7 @@ namespace Semmle.Extraction.CSharp
throw new InternalError("EndInitialize called without BeginInitialize returning true");
this.options = options;
this.compilation = compilation;
this.extractor = new Extraction.Extractor(cwd, args, GetOutputName(compilation, commandLineArguments), [], Logger, PathTransformer, ExtractorMode.None, options.QlTest);
this.ExtractionContext = new ExtractionContext(cwd, args, GetOutputName(compilation, commandLineArguments), [], Logger, PathTransformer, ExtractorMode.None, options.QlTest);
var errorCount = LogDiagnostics(compilation);
SetReferencePaths();

View File

@@ -18,7 +18,7 @@ namespace Semmle.Extraction
/// <summary>
/// Access various extraction functions, e.g. logger, trap writer.
/// </summary>
public Extractor Extractor { get; }
public ExtractionContext ExtractionContext { get; }
/// <summary>
/// Access to the trap file.
@@ -190,9 +190,9 @@ namespace Semmle.Extraction
}
}
protected Context(Extractor extractor, TrapWriter trapWriter, bool shouldAddAssemblyTrapPrefix = false)
protected Context(ExtractionContext extractor, TrapWriter trapWriter, bool shouldAddAssemblyTrapPrefix = false)
{
Extractor = extractor;
ExtractionContext = extractor;
TrapWriter = trapWriter;
ShouldAddAssemblyTrapPrefix = shouldAddAssemblyTrapPrefix;
}
@@ -274,7 +274,7 @@ namespace Semmle.Extraction
bool duplicationGuard, deferred;
if (Extractor.Mode is ExtractorMode.Standalone)
if (ExtractionContext.Mode is ExtractorMode.Standalone)
{
duplicationGuard = false;
deferred = false;
@@ -398,7 +398,7 @@ namespace Semmle.Extraction
private void ExtractionError(Message msg)
{
new ExtractionMessage(this, msg);
Extractor.Message(msg);
ExtractionContext.Message(msg);
}
private void ExtractionError(InternalError error)
@@ -408,7 +408,7 @@ namespace Semmle.Extraction
private void ReportError(InternalError error)
{
if (!Extractor.Mode.HasFlag(ExtractorMode.Standalone))
if (!ExtractionContext.Mode.HasFlag(ExtractorMode.Standalone))
throw error;
ExtractionError(error);

View File

@@ -40,7 +40,7 @@ namespace Semmle.Extraction
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
{
trapFile.WriteLine("\"");
Context.Extractor.Message(new Message($"Unhandled exception generating id: {ex.Message}", ToString() ?? "", null, ex.StackTrace));
Context.ExtractionContext.Message(new Message($"Unhandled exception generating id: {ex.Message}", ToString() ?? "", null, ex.StackTrace));
}
trapFile.WriteLine();
}

View File

@@ -25,7 +25,7 @@ namespace Semmle.Extraction.Entities
{
if (messageCount == limit + 1)
{
Context.Extractor.Logger.LogWarning($"Stopped logging extractor messages after reaching {limit}");
Context.ExtractionContext.Logger.LogWarning($"Stopped logging extractor messages after reaching {limit}");
}
return;
}

View File

@@ -8,7 +8,7 @@ namespace Semmle.Extraction.Entities
: base(cx, path)
{
originalPath = path;
transformedPathLazy = new Lazy<PathTransformer.ITransformedPath>(() => Context.Extractor.PathTransformer.Transform(originalPath));
transformedPathLazy = new Lazy<PathTransformer.ITransformedPath>(() => Context.ExtractionContext.PathTransformer.Transform(originalPath));
}
protected readonly string originalPath;

View File

@@ -7,7 +7,7 @@ namespace Semmle.Extraction
/// <summary>
/// Implementation of the main extractor state.
/// </summary>
public class Extractor
public class ExtractionContext
{
public string Cwd { get; init; }
public string[] Args { get; init; }
@@ -18,7 +18,7 @@ namespace Semmle.Extraction
/// <summary>
/// Creates a new extractor instance for one compilation unit.
/// </summary>
public Extractor(string cwd, string[] args, string outputPath, IEnumerable<CompilationInfo> compilationInfos, ILogger logger, PathTransformer pathTransformer, ExtractorMode mode, bool isQlTest)
public ExtractionContext(string cwd, string[] args, string outputPath, IEnumerable<CompilationInfo> compilationInfos, ILogger logger, PathTransformer pathTransformer, ExtractorMode mode, bool isQlTest)
{
OutputPath = outputPath;
Logger = logger;