Skip to content

Commit

Permalink
Merge the two CachedEntityFactory classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tamasvajk committed Nov 13, 2024
1 parent 02bd204 commit a0cac46
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 40 deletions.
19 changes: 0 additions & 19 deletions csharp/extractor/Semmle.Extraction.CSharp/CachedEntityFactory.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// A factory for creating cached entities.
/// </summary>
public abstract class CachedEntityFactory<TInit, TEntity> where TEntity : Semmle.Extraction.CachedEntity
public abstract class CachedEntityFactory<TInit, TEntity> where TEntity : CachedEntity
{
/// <summary>
/// Initializes the entity, but does not generate any trap code.
/// </summary>
public abstract TEntity Create(Semmle.Extraction.Context cx, TInit init);
public abstract TEntity Create(Context cx, TInit init);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.CodeAnalysis;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
public static class CachedEntityFactoryExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.IO;
using Microsoft.CodeAnalysis;
using Semmle.Extraction.CSharp;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
public abstract class Entity : IEntity
{
public virtual Context Context { get; }
public virtual Semmle.Extraction.Context Context { get; }

protected Entity(Context context)
protected Entity(Semmle.Extraction.Context context)
{
this.Context = context;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Semmle.Extraction.CSharp
{
public abstract class LabelledEntity : Semmle.Extraction.Entity
public abstract class LabelledEntity : Entity
{
protected LabelledEntity(Semmle.Extraction.Context cx) : base(cx)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Semmle.Extraction.CSharp.Entities
{
internal abstract class CachedEntity<T> : Extraction.CachedEntity<T> where T : notnull
public abstract class CachedEntity<T> : Extraction.CachedEntity<T> where T : notnull
{
public override Context Context => (Context)base.Context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Semmle.Extraction.CSharp.Entities
{
public sealed class Folder : Extraction.CachedEntity<PathTransformer.ITransformedPath>
public sealed class Folder : CachedEntity<PathTransformer.ITransformedPath>
{
private Folder(Extraction.Context cx, PathTransformer.ITransformedPath init) : base(cx, init) { }
private Folder(Context cx, PathTransformer.ITransformedPath init) : base(cx, init) { }

public override void Populate(TextWriter trapFile)
{
Expand All @@ -21,16 +21,16 @@ public override void WriteId(EscapingTextWriter trapFile)
trapFile.Write(";folder");
}

public static Folder Create(Extraction.Context cx, PathTransformer.ITransformedPath folder) =>
public static Folder Create(Context cx, PathTransformer.ITransformedPath folder) =>
FolderFactory.Instance.CreateEntity(cx, folder, folder);

public override Microsoft.CodeAnalysis.Location? ReportingLocation => null;

private class FolderFactory : Extraction.CachedEntityFactory<PathTransformer.ITransformedPath, Folder>
private class FolderFactory : CachedEntityFactory<PathTransformer.ITransformedPath, Folder>
{
public static FolderFactory Instance { get; } = new FolderFactory();

public override Folder Create(Extraction.Context cx, PathTransformer.ITransformedPath init) => new Folder(cx, init);
public override Folder Create(Context cx, PathTransformer.ITransformedPath init) => new Folder(cx, init);
}

public override int GetHashCode() => Symbol.GetHashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override void WriteId(EscapingTextWriter trapFile)

public override bool Equals(object? obj) => obj is not null && obj.GetType() == typeof(GeneratedLocation);

public static GeneratedLocation Create(Extraction.Context cx) => GeneratedLocationFactory.Instance.CreateEntity(cx, typeof(GeneratedLocation), null);
public static GeneratedLocation Create(Context cx) => GeneratedLocationFactory.Instance.CreateEntity(cx, typeof(GeneratedLocation), null);

private class GeneratedLocationFactory : CachedEntityFactory<string?, GeneratedLocation>
{
Expand Down
4 changes: 2 additions & 2 deletions csharp/extractor/Semmle.Extraction.CSharp/_Base/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private TEntity CreateEntity<TInit, TCacheKey, TEntity>(CachedEntityFactory<TIni
using (StackGuard)
{
var label = GetNewLabel();
var entity = factory.Create(this, init);
var entity = factory.Create((CSharp.Context)this, init);
entity.Label = label;

dictionary[cacheKey] = entity;
Expand Down Expand Up @@ -493,7 +493,7 @@ public void Try(SyntaxNode? node, ISymbol? symbol, Action a)
}

public virtual CSharp.Entities.Location CreateLocation() =>
CSharp.Entities.GeneratedLocation.Create(this);
CSharp.Entities.GeneratedLocation.Create((CSharp.Context)this);

public virtual CSharp.Entities.Location CreateLocation(Microsoft.CodeAnalysis.Location? location) =>
CreateLocation();
Expand Down

0 comments on commit a0cac46

Please sign in to comment.