C#: Rename GeneratedLocation to EmptyLocation and make sure that we always create one such location.

This commit is contained in:
Michael Nebel
2025-09-25 16:26:50 +02:00
parent 5843fdbdd8
commit e9901305b2
3 changed files with 17 additions and 9 deletions

View File

@@ -2,11 +2,11 @@ using System.IO;
namespace Semmle.Extraction.CSharp.Entities
{
public class GeneratedLocation : SourceLocation
public class EmptyLocation : SourceLocation
{
private readonly File generatedFile;
private GeneratedLocation(Context cx)
private EmptyLocation(Context cx)
: base(cx, null)
{
generatedFile = GeneratedFile.Create(cx);
@@ -26,15 +26,16 @@ namespace Semmle.Extraction.CSharp.Entities
public override int GetHashCode() => 98732567;
public override bool Equals(object? obj) => obj is not null && obj.GetType() == typeof(GeneratedLocation);
public override bool Equals(object? obj) => obj is not null && obj.GetType() == typeof(EmptyLocation);
public static GeneratedLocation Create(Context cx) => GeneratedLocationFactory.Instance.CreateEntity(cx, typeof(GeneratedLocation), null);
public static EmptyLocation Create(Context cx)
=> EmptyLocationFactory.Instance.CreateEntity(cx, typeof(EmptyLocation), null);
private class GeneratedLocationFactory : CachedEntityFactory<string?, GeneratedLocation>
private class EmptyLocationFactory : CachedEntityFactory<string?, EmptyLocation>
{
public static GeneratedLocationFactory Instance { get; } = new GeneratedLocationFactory();
public static EmptyLocationFactory Instance { get; } = new EmptyLocationFactory();
public override GeneratedLocation Create(Context cx, string? init) => new GeneratedLocation(cx);
public override EmptyLocation Create(Context cx, string? init) => new EmptyLocation(cx);
}
}
}

View File

@@ -238,6 +238,9 @@ namespace Semmle.Extraction.CSharp
compilationEntity = Entities.Compilation.Create(cx);
// Ensure that the empty location is always created.
Entities.EmptyLocation.Create(cx);
ExtractionContext.CompilationInfos.ForEach(ci => trapWriter.Writer.compilation_info(compilationEntity, ci.key, ci.value));
ReportProgressTaskDone(currentTaskId, assemblyPath, trapWriter.TrapFile, stopwatch.Elapsed, AnalysisAction.Extracted);

View File

@@ -550,6 +550,10 @@ namespace Semmle.Extraction.CSharp
!SymbolEqualityComparer.Default.Equals(symbol, symbol.OriginalDefinition) ||
scope.InScope(symbol);
public bool ExtractLocation(ISymbol symbol) =>
SymbolEqualityComparer.Default.Equals(symbol, symbol.OriginalDefinition) &&
scope.InScope(symbol);
/// <summary>
/// Runs the given action <paramref name="a"/>, guarding for trap duplication
/// based on key <paramref name="key"/>.
@@ -582,14 +586,14 @@ namespace Semmle.Extraction.CSharp
public Entities.Location CreateLocation()
{
return SourceTree is null
? Entities.GeneratedLocation.Create(this)
? Entities.EmptyLocation.Create(this)
: CreateLocation(Microsoft.CodeAnalysis.Location.Create(SourceTree, Microsoft.CodeAnalysis.Text.TextSpan.FromBounds(0, 0)));
}
public Entities.Location CreateLocation(Microsoft.CodeAnalysis.Location? location)
{
return (location is null || location.Kind == LocationKind.None)
? Entities.GeneratedLocation.Create(this)
? Entities.EmptyLocation.Create(this)
: location.IsInSource
? Entities.NonGeneratedSourceLocation.Create(this, location)
: Entities.Assembly.Create(this, location);