Skip to content

Commit

Permalink
Merge pull request #17984 from tamasvajk/fix/tech-debt-01
Browse files Browse the repository at this point in the history
Merge `Semmle.Extraction` into `Semmle.Extraction.CSharp`
  • Loading branch information
tamasvajk authored Nov 15, 2024
2 parents b1ac027 + e3b83bb commit e081b9a
Show file tree
Hide file tree
Showing 92 changed files with 629 additions and 900 deletions.
2 changes: 0 additions & 2 deletions csharp/CSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ VisualStudioVersion = 15.0.27130.2036
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Semmle.Util", "extractor\Semmle.Util\Semmle.Util.csproj", "{CDD7AD69-0FD8-40F0-A9DA-F1077A2A85D6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Semmle.Extraction", "extractor\Semmle.Extraction\Semmle.Extraction.csproj", "{81EAAD75-4BE1-44E4-91DF-20778216DB64}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Semmle.Extraction.CSharp", "extractor\Semmle.Extraction.CSharp\Semmle.Extraction.CSharp.csproj", "{C4D62DA0-B64B-440B-86DC-AB52318CB8BF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Semmle.Extraction.CSharp.DependencyFetching", "extractor\Semmle.Extraction.CSharp.DependencyFetching\Semmle.Extraction.CSharp.DependencyFetching.csproj", "{541D1AC5-E42C-4AB2-A1A4-C2355CE2A2EF}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ codeql_csharp_library(
nowarn = ["CA1822"],
visibility = ["//csharp:__subpackages__"],
deps = [
"//csharp/extractor/Semmle.Extraction",
"//csharp/extractor/Semmle.Extraction.CSharp",
"//csharp/extractor/Semmle.Util",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
internal class NugetExeWrapper : IDisposable
{
private readonly string? nugetExe;
private readonly Util.Logging.ILogger logger;
private readonly Semmle.Util.Logging.ILogger logger;

public int PackageCount => fileProvider.PackagesConfigs.Count;

Expand All @@ -33,7 +33,7 @@ internal class NugetExeWrapper : IDisposable
/// <summary>
/// Create the package manager for a specified source tree.
/// </summary>
public NugetExeWrapper(FileProvider fileProvider, TemporaryDirectory packageDirectory, Util.Logging.ILogger logger)
public NugetExeWrapper(FileProvider fileProvider, TemporaryDirectory packageDirectory, Semmle.Util.Logging.ILogger logger)
{
this.fileProvider = fileProvider;
this.packageDirectory = packageDirectory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Semmle.Util\Semmle.Util.csproj" />
<ProjectReference Include="..\Semmle.Extraction\Semmle.Extraction.csproj" />
<ProjectReference Include="..\Semmle.Extraction.CSharp\Semmle.Extraction.CSharp.csproj" />

<InternalsVisibleTo Include="Semmle.Extraction.Tests" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion csharp/extractor/Semmle.Extraction.CSharp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ load(
codeql_csharp_library(
name = "Semmle.Extraction.CSharp",
srcs = glob([
"CodeAnalysisExtensions/**/*.cs",
"Comments/**/*.cs",
"Entities/**/*.cs",
"Extractor/**/*.cs",
"Kinds/**/*.cs",
"Populators/**/*.cs",
"Trap/**/*.cs",
"*.cs",
]),
allow_unsafe_blocks = True,
visibility = ["//csharp:__subpackages__"],
deps = [
"//csharp/extractor/Semmle.Extraction",
"//csharp/extractor/Semmle.Extraction.CSharp.Util",
"//csharp/extractor/Semmle.Util",
"@paket.main//basic.compilerlog.util",
Expand Down
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
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 @@ -12,7 +12,7 @@ internal class CommentBlock

public IEnumerable<CommentLine> CommentLines => lines;

public Location Location { get; private set; }
public Microsoft.CodeAnalysis.Location Location { get; private set; }

public CommentBlock(CommentLine firstLine)
{
Expand Down Expand Up @@ -49,7 +49,7 @@ public void AddCommentLine(CommentLine line)
{
Location = !lines.Any()
? line.Location
: Location.Create(
: Microsoft.CodeAnalysis.Location.Create(
line.Location.SourceTree!,
new TextSpan(Location.SourceSpan.Start, line.Location.SourceSpan.End - Location.SourceSpan.Start));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public void AddComment(CommentLine comment)
}

// Comments sorted by location.
private readonly SortedDictionary<Location, CommentLine> comments = new SortedDictionary<Location, CommentLine>(new LocationComparer());
private readonly SortedDictionary<Microsoft.CodeAnalysis.Location, CommentLine> comments = new SortedDictionary<Microsoft.CodeAnalysis.Location, CommentLine>(new LocationComparer());

// Program elements sorted by location.
private readonly SortedDictionary<Location, Label> elements = new SortedDictionary<Location, Label>(new LocationComparer());
private readonly SortedDictionary<Microsoft.CodeAnalysis.Location, Label> elements = new SortedDictionary<Microsoft.CodeAnalysis.Location, Label>(new LocationComparer());

private readonly Dictionary<Label, Key> duplicationGuardKeys = new Dictionary<Label, Key>();

Expand All @@ -33,9 +33,9 @@ public void AddComment(CommentLine comment)
return null;
}

private class LocationComparer : IComparer<Location>
private class LocationComparer : IComparer<Microsoft.CodeAnalysis.Location>
{
public int Compare(Location? l1, Location? l2) => CommentProcessor.Compare(l1, l2);
public int Compare(Microsoft.CodeAnalysis.Location? l1, Microsoft.CodeAnalysis.Location? l2) => CommentProcessor.Compare(l1, l2);
}

/// <summary>
Expand All @@ -44,7 +44,7 @@ private class LocationComparer : IComparer<Location>
/// <param name="l1">First location</param>
/// <param name="l2">Second location</param>
/// <returns>&lt;0 if l1 before l2, &gt;0 if l1 after l2, else 0.</returns>
private static int Compare(Location? l1, Location? l2)
private static int Compare(Microsoft.CodeAnalysis.Location? l1, Microsoft.CodeAnalysis.Location? l2)
{
if (object.ReferenceEquals(l1, l2))
return 0;
Expand All @@ -68,7 +68,7 @@ private static int Compare(Location? l1, Location? l2)
/// <param name="elementLabel">The label of the element in the trap file.</param>
/// <param name="duplicationGuardKey">The duplication guard key of the element, if any.</param>
/// <param name="loc">The location of the element.</param>
public void AddElement(Label elementLabel, Key? duplicationGuardKey, Location? loc)
public void AddElement(Label elementLabel, Key? duplicationGuardKey, Microsoft.CodeAnalysis.Location? loc)
{
if (loc is not null && loc.IsInSource)
elements[loc] = elementLabel;
Expand All @@ -78,7 +78,7 @@ public void AddElement(Label elementLabel, Key? duplicationGuardKey, Location? l

// Ensure that commentBlock and element refer to the same file
// which can happen when processing multiple files.
private static void EnsureSameFile(Comments.CommentBlock commentBlock, ref KeyValuePair<Location, Label>? element)
private static void EnsureSameFile(Comments.CommentBlock commentBlock, ref KeyValuePair<Microsoft.CodeAnalysis.Location, Label>? element)
{
if (element is not null && element.Value.Key.SourceTree != commentBlock.Location.SourceTree)
element = null;
Expand All @@ -96,9 +96,9 @@ private static void EnsureSameFile(Comments.CommentBlock commentBlock, ref KeyVa
/// <param name="callback">Output binding information.</param>
private void GenerateBindings(
Comments.CommentBlock commentBlock,
KeyValuePair<Location, Label>? previousElement,
KeyValuePair<Location, Label>? nextElement,
KeyValuePair<Location, Label>? parentElement,
KeyValuePair<Microsoft.CodeAnalysis.Location, Label>? previousElement,
KeyValuePair<Microsoft.CodeAnalysis.Location, Label>? nextElement,
KeyValuePair<Microsoft.CodeAnalysis.Location, Label>? parentElement,
CommentBindingCallback callback
)
{
Expand All @@ -125,7 +125,7 @@ CommentBindingCallback callback
}

// Heuristic to decide which is the "best" element associated with the comment.
KeyValuePair<Location, Label>? bestElement;
KeyValuePair<Microsoft.CodeAnalysis.Location, Label>? bestElement;

if (previousElement is not null && previousElement.Value.Key.EndLine() == commentBlock.Location.StartLine())
{
Expand Down Expand Up @@ -180,14 +180,14 @@ CommentBindingCallback callback
private class ElementStack
{
// Invariant: the top of the stack must be contained by items below it.
private readonly Stack<KeyValuePair<Location, Label>> elementStack = new Stack<KeyValuePair<Location, Label>>();
private readonly Stack<KeyValuePair<Microsoft.CodeAnalysis.Location, Label>> elementStack = new();

/// <summary>
/// Add a new element to the stack.
/// </summary>
/// The stack is maintained.
/// <param name="value">The new element to push.</param>
public void Push(KeyValuePair<Location, Label> value)
public void Push(KeyValuePair<Microsoft.CodeAnalysis.Location, Label> value)
{
// Maintain the invariant by popping existing elements
while (elementStack.Count > 0 && !elementStack.Peek().Key.Contains(value.Key))
Expand All @@ -201,15 +201,15 @@ public void Push(KeyValuePair<Location, Label> value)
/// </summary>
/// <param name="l">The location of the comment.</param>
/// <returns>An element completely containing l, or null if none found.</returns>
public KeyValuePair<Location, Label>? FindParent(Location l) =>
public KeyValuePair<Microsoft.CodeAnalysis.Location, Label>? FindParent(Microsoft.CodeAnalysis.Location l) =>
elementStack.Where(v => v.Key.Contains(l)).FirstOrNull();

/// <summary>
/// Finds the element on the stack immediately preceding the comment at l.
/// </summary>
/// <param name="l">The location of the comment.</param>
/// <returns>The element before l, or null.</returns>
public KeyValuePair<Location, Label>? FindBefore(Location l)
public KeyValuePair<Microsoft.CodeAnalysis.Location, Label>? FindBefore(Microsoft.CodeAnalysis.Location l)
{
return elementStack
.Where(v => v.Key.SourceSpan.End < l.SourceSpan.Start)
Expand All @@ -222,7 +222,7 @@ public void Push(KeyValuePair<Location, Label> value)
/// <param name="comment">The location of the comment.</param>
/// <param name="next">The next element.</param>
/// <returns>The next element.</returns>
public KeyValuePair<Location, Label>? FindAfter(Location comment, KeyValuePair<Location, Label>? next)
public KeyValuePair<Microsoft.CodeAnalysis.Location, Label>? FindAfter(Microsoft.CodeAnalysis.Location comment, KeyValuePair<Microsoft.CodeAnalysis.Location, Label>? next)
{
var p = FindParent(comment);
return next.HasValue && p.HasValue && p.Value.Key.Before(next.Value.Key) ? null : next;
Expand All @@ -233,7 +233,7 @@ public void Push(KeyValuePair<Location, Label> value)
private void GenerateBindings(
Comments.CommentBlock block,
ElementStack elementStack,
KeyValuePair<Location, Label>? nextElement,
KeyValuePair<Microsoft.CodeAnalysis.Location, Label>? nextElement,
CommentBindingCallback cb
)
{
Expand All @@ -259,8 +259,8 @@ CommentBindingCallback cb
/// <param name="cb">Where to send the results.</param>
/// <returns>true if there are more comments to process, false otherwise.</returns>
private bool GenerateBindings(
IEnumerator<KeyValuePair<Location, CommentLine>> commentEnumerator,
KeyValuePair<Location, Label>? nextElement,
IEnumerator<KeyValuePair<Microsoft.CodeAnalysis.Location, CommentLine>> commentEnumerator,
KeyValuePair<Microsoft.CodeAnalysis.Location, Label>? nextElement,
ElementStack elementStack,
CommentBindingCallback cb
)
Expand Down Expand Up @@ -319,8 +319,8 @@ public void GenerateBindings(CommentBindingCallback cb)

var elementStack = new ElementStack();

using IEnumerator<KeyValuePair<Location, Label>> elementEnumerator = elements.GetEnumerator();
using IEnumerator<KeyValuePair<Location, CommentLine>> commentEnumerator = comments.GetEnumerator();
using IEnumerator<KeyValuePair<Microsoft.CodeAnalysis.Location, Label>> elementEnumerator = elements.GetEnumerator();
using IEnumerator<KeyValuePair<Microsoft.CodeAnalysis.Location, CommentLine>> commentEnumerator = comments.GetEnumerator();
if (!commentEnumerator.MoveNext())
{
// There are no comments to process.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

namespace Semmle.Extraction.CSharp.Entities
{
internal class Assembly : Extraction.Entities.Location
internal class Assembly : Location
{
public override Context Context => (Context)base.Context;

private readonly string assemblyPath;
private readonly IAssemblySymbol assembly;
private readonly bool isOutputAssembly;
Expand Down Expand Up @@ -56,7 +54,7 @@ public override bool Equals(object? obj)
return false;
}

public static Extraction.Entities.Location Create(Context cx, Microsoft.CodeAnalysis.Location loc) => AssemblyConstructorFactory.Instance.CreateEntity(cx, loc, loc);
public static Location Create(Context cx, Microsoft.CodeAnalysis.Location loc) => AssemblyConstructorFactory.Instance.CreateEntity(cx, loc, loc);

private class AssemblyConstructorFactory : CachedEntityFactory<Microsoft.CodeAnalysis.Location?, Assembly>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ private void ExtractArguments(TextWriter trapFile)

public override Microsoft.CodeAnalysis.Location? ReportingLocation => attributeSyntax?.Name.GetLocation();

private Semmle.Extraction.Entities.Location? location;
private Location? location;

private Semmle.Extraction.Entities.Location Location =>
private Location Location =>
location ??= Context.CreateLocation(attributeSyntax is null
? entity.ReportingLocation
: attributeSyntax.Name.GetLocation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.IO;
using Microsoft.CodeAnalysis;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp.Entities
{
/// <summary>
/// A cached entity.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp
{
/// <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 : Entities.CachedEntity
{
/// <summary>
/// Initializes the entity, but does not generate any trap code.
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
{
public static class CachedEntityFactoryExtensions
{
Expand All @@ -16,7 +16,7 @@ public static class CachedEntityFactoryExtensions
/// <param name="init">The initializer for the entity.</param>
/// <returns>The entity.</returns>
public static TEntity CreateEntity<TInit, TEntity>(this CachedEntityFactory<TInit, TEntity> factory, Context cx, object cacheKey, TInit init)
where TEntity : CachedEntity => cx.CreateEntity(factory, cacheKey, init);
where TEntity : Entities.CachedEntity => cx.CreateEntity(factory, cacheKey, init);

/// <summary>
/// Creates and populates a new entity from an `ISymbol`, or returns the existing one
Expand All @@ -30,6 +30,6 @@ public static TEntity CreateEntity<TInit, TEntity>(this CachedEntityFactory<TIni
/// <returns>The entity.</returns>
public static TEntity CreateEntityFromSymbol<TSymbol, TEntity>(this CachedEntityFactory<TSymbol, TEntity> factory, Context cx, TSymbol init)
where TSymbol : ISymbol
where TEntity : CachedEntity => cx.CreateEntityFromSymbol(factory, init);
where TEntity : Entities.CachedEntity => cx.CreateEntityFromSymbol(factory, init);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

Expand Down Expand Up @@ -89,7 +87,7 @@ protected void ExtractCompilerGenerated(TextWriter trapFile)
/// </summary>
public virtual Microsoft.CodeAnalysis.Location? FullLocation => Symbol.Locations.BestOrDefault();

public virtual IEnumerable<Extraction.Entities.Location> Locations
public virtual IEnumerable<Location> Locations
{
get
{
Expand Down Expand Up @@ -143,6 +141,6 @@ public ExpressionSyntax? ExpressionBody

public override bool NeedsPopulation => Context.Defines(Symbol);

public Extraction.Entities.Location Location => Context.CreateLocation(ReportingLocation);
public Location Location => Context.CreateLocation(ReportingLocation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
using System.IO;
using Microsoft.CodeAnalysis;

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

protected Entity(Context context)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.IO;

namespace Semmle.Extraction
namespace Semmle.Extraction.CSharp.Entities
{
/// <summary>
/// An entity which has a default "*" ID assigned to it.
Expand Down
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
Loading

0 comments on commit e081b9a

Please sign in to comment.