Move more classes to the Semmle.Extraction.CSharp namespace

This commit is contained in:
Tamas Vajk
2024-11-13 15:27:48 +01:00
parent 46da5960ee
commit 02bd204111
32 changed files with 59 additions and 148 deletions

View File

@@ -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,

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
public static class LocationExtensions
{

View File

@@ -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);
}
}

View File

@@ -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)
{
@@ -62,7 +62,7 @@ namespace Semmle.Extraction
return other?.GetType() == GetType() && Equals(other.Symbol, Symbol);
}
public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel;
public override CSharp.TrapStackBehaviour TrapStackBehaviour => CSharp.TrapStackBehaviour.NoLabel;
}
/// <summary>

View File

@@ -1,6 +1,7 @@
using System;
using System.IO;
using Microsoft.CodeAnalysis;
using Semmle.Extraction.CSharp;
namespace Semmle.Extraction
{

View File

@@ -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)
{
@@ -33,6 +33,6 @@ namespace Semmle.Extraction
public override Microsoft.CodeAnalysis.Location? ReportingLocation => null;
public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel;
public override CSharp.TrapStackBehaviour TrapStackBehaviour => CSharp.TrapStackBehaviour.NoLabel;
}
}

View File

@@ -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.

View File

@@ -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)
{
}
}

View File

@@ -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);
}

View File

@@ -2,6 +2,7 @@
using System.IO;
using System.Threading;
using Semmle.Util;
using Semmle.Extraction.CSharp;
namespace Semmle.Extraction.Entities
{

View File

@@ -1,6 +1,6 @@
using Microsoft.CodeAnalysis;
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// The scope of symbols in an assembly.

View File

@@ -2,7 +2,7 @@ using System;
using Semmle.Util;
using Semmle.Util.Logging;
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// Represents the parsed state of the command line arguments.

View File

@@ -4,7 +4,7 @@ using System.IO;
using System.Linq;
using System.Xml;
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// Represents a .csproj file and reads information from it.

View File

@@ -6,7 +6,7 @@ using System.Text;
using System.Text.RegularExpressions;
using Semmle.Util;
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
public sealed class InvalidFilePatternException : Exception
{

View File

@@ -1,6 +1,6 @@
using Microsoft.CodeAnalysis;
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// Defines which entities belong in the trap file

View File

@@ -5,7 +5,7 @@ using System.IO;
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

View File

@@ -2,7 +2,7 @@
using Microsoft.CodeAnalysis;
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>

View File

@@ -5,7 +5,7 @@ using System.Text;
using Semmle.Util;
using Semmle.Util.Logging;
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
public interface ITrapEmitter
{

View File

@@ -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.

View File

@@ -4,7 +4,7 @@ using System.Text;
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

View File

@@ -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();

View File

@@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.IO;
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
public static class TrapExtensions
{

View File

@@ -1,4 +1,4 @@
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// How an entity behaves with respect to .push and .pop

View File

@@ -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) =>

View File

@@ -6,6 +6,7 @@ using System.Linq;
using Microsoft.CodeAnalysis;
using Semmle.Util.Logging;
using Semmle.Extraction.Entities;
using Semmle.Extraction.CSharp;
namespace Semmle.Extraction
{
@@ -18,7 +19,7 @@ namespace Semmle.Extraction
/// <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.
@@ -190,7 +191,7 @@ namespace Semmle.Extraction
}
}
protected Context(CSharp.ExtractionContext extractionContext, TrapWriter trapWriter, bool shouldAddAssemblyTrapPrefix = false)
protected Context(ExtractionContext extractionContext, TrapWriter trapWriter, bool shouldAddAssemblyTrapPrefix = false)
{
ExtractionContext = extractionContext;
TrapWriter = trapWriter;
@@ -274,7 +275,7 @@ namespace Semmle.Extraction
bool duplicationGuard, deferred;
if (ExtractionContext.Mode is CSharp.ExtractorMode.Standalone)
if (ExtractionContext.Mode is ExtractorMode.Standalone)
{
duplicationGuard = false;
deferred = false;
@@ -408,7 +409,7 @@ namespace Semmle.Extraction
private void ReportError(InternalError error)
{
if (!ExtractionContext.Mode.HasFlag(CSharp.ExtractorMode.Standalone))
if (!ExtractionContext.Mode.HasFlag(ExtractorMode.Standalone))
throw error;
ExtractionError(error);

View File

@@ -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.
@@ -25,12 +24,12 @@ namespace Semmle.Extraction
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);
}

View File

@@ -1,36 +0,0 @@
using System.IO;
namespace Semmle.Extraction
{
/// <summary>
/// A tuple represents a string of the form "a(b,c,d)".
/// </summary>
public struct Tuple : ITrapEmitter
{
private readonly string name;
private readonly object[] args;
public Tuple(string name, params object[] args)
{
this.name = name;
this.args = args;
}
/// <summary>
/// Constructs a unique string for this tuple.
/// </summary>
/// <param name="trapFile">The trap file to write to.</param>
public void EmitTrap(TextWriter trapFile)
{
trapFile.WriteTuple(name, args);
}
public override string ToString()
{
// Only implemented for debugging purposes
using var writer = new StringWriter();
EmitTrap(writer);
return writer.ToString();
}
}
}

View File

@@ -1,35 +0,0 @@
using Semmle.Extraction.Entities;
namespace Semmle.Extraction
{
/// <summary>
/// Methods for creating DB tuples.
/// </summary>
public static class Tuples
{
public static void containerparent(this System.IO.TextWriter trapFile, CSharp.Entities.Folder parent, IEntity child)
{
trapFile.WriteTuple("containerparent", parent, child);
}
internal static void extractor_messages(this System.IO.TextWriter trapFile, ExtractionMessage error, Semmle.Util.Logging.Severity severity, string errorMessage, string entityText, CSharp.Entities.Location location, string stackTrace)
{
trapFile.WriteTuple("extractor_messages", error, (int)severity, "C# extractor", errorMessage, entityText, location, stackTrace);
}
public static void files(this System.IO.TextWriter trapFile, CSharp.Entities.File file, string fullName)
{
trapFile.WriteTuple("files", file, fullName);
}
internal static void folders(this System.IO.TextWriter trapFile, CSharp.Entities.Folder folder, string path)
{
trapFile.WriteTuple("folders", folder, path);
}
public static void locations_default(this System.IO.TextWriter trapFile, CSharp.Entities.SourceLocation label, CSharp.Entities.File file, int startLine, int startCol, int endLine, int endCol)
{
trapFile.WriteTuple("locations_default", label, file, startLine, startCol, endLine, endCol);
}
}
}

View File

@@ -1,4 +1,5 @@
using Xunit;
using Semmle.Extraction.CSharp;
namespace Semmle.Extraction.Tests
{

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Text.RegularExpressions;
using Semmle.Util;
using Semmle.Util.Logging;
using Semmle.Extraction.CSharp;
namespace Semmle.Extraction.Tests
{

View File

@@ -1,5 +1,6 @@
using Xunit;
using Semmle.Util;
using Semmle.Extraction.CSharp;
namespace Semmle.Extraction.Tests
{