Skip to content

Commit

Permalink
Move more classes to the Semmle.Extraction.CSharp namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
tamasvajk committed Nov 13, 2024
1 parent 46da596 commit 02bd204
Show file tree
Hide file tree
Showing 32 changed files with 59 additions and 148 deletions.
2 changes: 2 additions & 0 deletions csharp/extractor/Semmle.Extraction.CSharp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ codeql_csharp_library(
name = "Semmle.Extraction.CSharp",
srcs = glob([
"_Base/**/*.cs",
"CodeAnalysisExtensions/**/*.cs",
"Comments/**/*.cs",
"Entities/**/*.cs",
"Extractor/**/*.cs",
"Kinds/**/*.cs",
"Populators/**/*.cs",
"Trap/**/*.cs",
"*.cs",
]),
allow_unsafe_blocks = True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using Microsoft.CodeAnalysis;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
public static class LocationExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ namespace Semmle.Extraction
/// <summary>
/// A factory for creating cached entities.
/// </summary>
public abstract class CachedEntityFactory<TInit, TEntity> where TEntity : CachedEntity
public abstract class CachedEntityFactory<TInit, TEntity> where TEntity : Semmle.Extraction.CachedEntity
{
/// <summary>
/// Initializes the entity, but does not generate any trap code.
/// </summary>
public abstract TEntity Create(Context cx, TInit init);
public abstract TEntity Create(Semmle.Extraction.Context cx, TInit init);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Semmle.Extraction
///
/// The <see cref="Entity.Id"/> property is used as label in caching.
/// </summary>
public abstract class CachedEntity : LabelledEntity
public abstract class CachedEntity : CSharp.LabelledEntity
{
protected CachedEntity(Context context) : base(context)
{
Expand Down Expand Up @@ -62,7 +62,7 @@ public override bool Equals(object? obj)
return other?.GetType() == GetType() && Equals(other.Symbol, Symbol);
}

public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel;
public override CSharp.TrapStackBehaviour TrapStackBehaviour => CSharp.TrapStackBehaviour.NoLabel;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using Microsoft.CodeAnalysis;
using Semmle.Extraction.CSharp;

namespace Semmle.Extraction
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Semmle.Extraction
/// <summary>
/// An entity which has a default "*" ID assigned to it.
/// </summary>
public abstract class FreshEntity : UnlabelledEntity
public abstract class FreshEntity : CSharp.UnlabelledEntity
{
protected FreshEntity(Context cx) : base(cx)
{
Expand Down Expand Up @@ -33,6 +33,6 @@ public string DebugContents

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

public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel;
public override CSharp.TrapStackBehaviour TrapStackBehaviour => CSharp.TrapStackBehaviour.NoLabel;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.IO;
using Microsoft.CodeAnalysis;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// Any program entity which has a corresponding label in the trap file.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
public abstract class LabelledEntity : Entity
public abstract class LabelledEntity : Semmle.Extraction.Entity
{
protected LabelledEntity(Context cx) : base(cx)
protected LabelledEntity(Semmle.Extraction.Context cx) : base(cx)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
public abstract class UnlabelledEntity : Entity
{
protected UnlabelledEntity(Context cx) : base(cx)
protected UnlabelledEntity(Extraction.Context cx) : base(cx)
{
cx.AddFreshLabel(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Threading;
using Semmle.Util;
using Semmle.Extraction.CSharp;

namespace Semmle.Extraction.Entities
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.CodeAnalysis;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// The scope of symbols in an assembly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Semmle.Util;
using Semmle.Util.Logging;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// Represents the parsed state of the command line arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Linq;
using System.Xml;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// Represents a .csproj file and reads information from it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Text.RegularExpressions;
using Semmle.Util;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
public sealed class InvalidFilePatternException : Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.CodeAnalysis;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// Defines which entities belong in the trap file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Semmle.Util;
using Semmle.Util.Logging;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// A class for interpreting path transformers specified using the environment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.CodeAnalysis;


namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Semmle.Util;
using Semmle.Util.Logging;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
public interface ITrapEmitter
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.Linq;
using Microsoft.CodeAnalysis;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// Exception thrown whenever extraction encounters something unexpected.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// A `TextWriter` object that wraps another `TextWriter` object, and which
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,13 @@
using System;
using System.IO;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// An ID. Either a fresh ID (`*`), a key, or a label (https://semmle.com/wiki/display/IN/TRAP+Files):
///
/// ```
/// id ::= '*' | key | label
/// ```
/// </summary>
public interface IId
{
/// <summary>
/// Appends this ID to the supplied trap builder.
/// </summary>
void AppendTo(TextWriter trapFile);
}

/// <summary>
/// A fresh ID (`*`).
/// </summary>
public class FreshId : IId
{
private FreshId() { }

/// <summary>
/// Gets the singleton <see cref="FreshId"/> instance.
/// </summary>
public static IId Instance { get; } = new FreshId();

public override string ToString() => "*";

public override bool Equals(object? obj) => obj?.GetType() == GetType();

public override int GetHashCode() => 0;

public void AppendTo(TextWriter trapFile)
{
trapFile.Write('*');
}
}

/// <summary>
/// A key. Either a simple key, e.g. `@"bool A.M();method"`, or a compound key, e.g.
/// `@"{0} {1}.M();method"` where `0` and `1` are both labels.
/// </summary>
public class Key : IId
public class Key
{
private readonly StringWriter trapBuilder = new StringWriter();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.IO;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
public static class TrapExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// How an entity behaves with respect to .push and .pop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,22 @@ namespace Semmle.Extraction.CSharp
/// </remarks>
internal static class Tuples
{
internal static void assemblies(this System.IO.TextWriter trapFile, Assembly assembly, Entities.File file, string identifier, string name, string version) =>
public static void containerparent(this TextWriter trapFile, Folder parent, IEntity child) =>
trapFile.WriteTuple("containerparent", parent, child);

internal static void extractor_messages(this TextWriter trapFile, ExtractionMessage error, Semmle.Util.Logging.Severity severity, string errorMessage, string entityText, Location location, string stackTrace) =>
trapFile.WriteTuple("extractor_messages", error, (int)severity, "C# extractor", errorMessage, entityText, location, stackTrace);

public static void files(this TextWriter trapFile, Entities.File file, string fullName) =>
trapFile.WriteTuple("files", file, fullName);

internal static void folders(this TextWriter trapFile, Folder folder, string path) =>
trapFile.WriteTuple("folders", folder, path);

public static void locations_default(this TextWriter trapFile, SourceLocation label, Entities.File file, int startLine, int startCol, int endLine, int endCol) =>
trapFile.WriteTuple("locations_default", label, file, startLine, startCol, endLine, endCol);

internal static void assemblies(this TextWriter trapFile, Assembly assembly, Entities.File file, string identifier, string name, string version) =>
trapFile.WriteTuple("assemblies", assembly, file, identifier, name, version);

internal static void accessor_location(this TextWriter trapFile, Accessor accessorKey, Location location) =>
Expand Down
9 changes: 5 additions & 4 deletions csharp/extractor/Semmle.Extraction.CSharp/_Base/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.CodeAnalysis;
using Semmle.Util.Logging;
using Semmle.Extraction.Entities;
using Semmle.Extraction.CSharp;

namespace Semmle.Extraction
{
Expand All @@ -18,7 +19,7 @@ public class Context
/// <summary>
/// Access various extraction functions, e.g. logger, trap writer.
/// </summary>
public CSharp.ExtractionContext ExtractionContext { get; }
public ExtractionContext ExtractionContext { get; }

/// <summary>
/// Access to the trap file.
Expand Down Expand Up @@ -190,7 +191,7 @@ public void PopulateAll()
}
}

protected Context(CSharp.ExtractionContext extractionContext, TrapWriter trapWriter, bool shouldAddAssemblyTrapPrefix = false)
protected Context(ExtractionContext extractionContext, TrapWriter trapWriter, bool shouldAddAssemblyTrapPrefix = false)
{
ExtractionContext = extractionContext;
TrapWriter = trapWriter;
Expand Down Expand Up @@ -274,7 +275,7 @@ private void Populate(ISymbol? optionalSymbol, CachedEntity entity)

bool duplicationGuard, deferred;

if (ExtractionContext.Mode is CSharp.ExtractorMode.Standalone)
if (ExtractionContext.Mode is ExtractorMode.Standalone)
{
duplicationGuard = false;
deferred = false;
Expand Down Expand Up @@ -408,7 +409,7 @@ private void ExtractionError(InternalError error)

private void ReportError(InternalError error)
{
if (!ExtractionContext.Mode.HasFlag(CSharp.ExtractorMode.Standalone))
if (!ExtractionContext.Mode.HasFlag(ExtractorMode.Standalone))
throw error;

ExtractionError(error);
Expand Down
9 changes: 4 additions & 5 deletions csharp/extractor/Semmle.Extraction.CSharp/_Base/Message.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Linq;
using System.Text;
using System.Text;
using Microsoft.CodeAnalysis;
using Semmle.Util.Logging;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// Encapsulates information for a log message.
Expand All @@ -25,12 +24,12 @@ public Message(string text, string? entityText, CSharp.Entities.Location? locati
Location = location;
}

public static Message Create(Context cx, string text, ISymbol symbol, string? stackTrace = null, Severity severity = Severity.Error)
public static Message Create(Extraction.Context cx, string text, ISymbol symbol, string? stackTrace = null, Severity severity = Severity.Error)
{
return new Message(text, symbol.ToString(), cx.CreateLocation(symbol.Locations.BestOrDefault()), stackTrace, severity);
}

public static Message Create(Context cx, string text, SyntaxNode node, string? stackTrace = null, Severity severity = Severity.Error)
public static Message Create(Extraction.Context cx, string text, SyntaxNode node, string? stackTrace = null, Severity severity = Severity.Error)
{
return new Message(text, node.ToString(), cx.CreateLocation(node.GetLocation()), stackTrace, severity);
}
Expand Down
Loading

0 comments on commit 02bd204

Please sign in to comment.