Skip to content

Commit

Permalink
Move location-like entities 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 e7844e2 commit b7098b7
Show file tree
Hide file tree
Showing 50 changed files with 122 additions and 138 deletions.
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,7 +3,7 @@

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

Expand Down Expand Up @@ -56,7 +56,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 @@ -89,7 +89,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 +143,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 @@ -116,7 +116,7 @@ protected override void ExtractInitializers(TextWriter trapFile)
}
}

private void ExtractSourceInitializer(TextWriter trapFile, ITypeSymbol? type, IMethodSymbol? symbol, ArgumentListSyntax arguments, Location location)
private void ExtractSourceInitializer(TextWriter trapFile, ITypeSymbol? type, IMethodSymbol? symbol, ArgumentListSyntax arguments, Microsoft.CodeAnalysis.Location location)
{
var initInfo = new ExpressionInfo(Context,
AnnotatedTypeSymbol.CreateNotAnnotated(type),
Expand Down
10 changes: 5 additions & 5 deletions csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class Expression : FreshEntity, IExpressionParentEntity
{
private readonly IExpressionInfo info;
public AnnotatedTypeSymbol? Type { get; private set; }
public Extraction.Entities.Location Location { get; }
public Location Location { get; }
public ExprKind Kind { get; }

internal Expression(IExpressionInfo info, bool shouldPopulate = true)
Expand Down Expand Up @@ -62,7 +62,7 @@ protected sealed override void Populate(TextWriter trapFile)
type.PopulateGenerics();
}

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

internal void SetType(ITypeSymbol? type)
{
Expand Down Expand Up @@ -138,7 +138,7 @@ private static bool ContainsPattern(SyntaxNode node) =>
/// Creates a generated expression from a typed constant.
/// </summary>
public static Expression? CreateGenerated(Context cx, TypedConstant constant, IExpressionParentEntity parent,
int childIndex, Extraction.Entities.Location location)
int childIndex, Location location)
{
if (constant.IsNull ||
constant.Type is null)
Expand Down Expand Up @@ -176,7 +176,7 @@ private static bool ContainsPattern(SyntaxNode node) =>
/// Creates a generated expression for a default argument value.
/// </summary>
public static Expression? CreateGenerated(Context cx, IParameterSymbol parameter, IExpressionParentEntity parent,
int childIndex, Extraction.Entities.Location location)
int childIndex, Location location)
{
if (!parameter.HasExplicitDefaultValue ||
parameter.Type is IErrorTypeSymbol)
Expand Down Expand Up @@ -315,7 +315,7 @@ public static bool IsDynamic(Context cx, ExpressionSyntax node)

/// <summary>
/// Given `b` in `a?.b.c`, return `(a?.b, a?.b)`.
///
///
/// Given `c` in `a?.b?.c.d`, return `(b?.c, a?.b?.c)`.
/// </summary>
/// <param name="node">A MemberBindingExpression.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ internal class ExpressionInfo : IExpressionInfo
{
public Context Context { get; }
public AnnotatedTypeSymbol? Type { get; }
public Extraction.Entities.Location Location { get; }
public Location Location { get; }
public ExprKind Kind { get; }
public IExpressionParentEntity Parent { get; }
public int Child { get; }
public bool IsCompilerGenerated { get; }
public string? ExprValue { get; }

public ExpressionInfo(Context cx, AnnotatedTypeSymbol? type, Extraction.Entities.Location location, ExprKind kind,
public ExpressionInfo(Context cx, AnnotatedTypeSymbol? type, Location location, ExprKind kind,
IExpressionParentEntity parent, int child, bool isCompilerGenerated, string? value)
{
Context = cx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ public string? ExprValue
}
}

private Extraction.Entities.Location? cachedLocation;
private Location? cachedLocation;

public Extraction.Entities.Location Location
public Location Location
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private NormalArrayCreation(ExpressionNodeInfo info) : base(info) { }

public static Expression Create(ExpressionNodeInfo info) => new NormalArrayCreation(info).TryPopulate();

public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, ITypeSymbol type, IEnumerable<TypedConstant> items, Semmle.Extraction.Entities.Location location)
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, ITypeSymbol type, IEnumerable<TypedConstant> items, Location location)
{
var info = new ExpressionInfo(
cx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override void PopulateExpression(TextWriter trapFile)

public override Microsoft.CodeAnalysis.Location ReportingLocation => Syntax.GetLocation();

public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, Microsoft.CodeAnalysis.ITypeSymbol type, object? value, Action<Expression, int> createChild, Extraction.Entities.Location location)
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, Microsoft.CodeAnalysis.ITypeSymbol type, object? value, Action<Expression, int> createChild, Location location)
{
var info = new ExpressionInfo(
cx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected override void PopulateExpression(TextWriter trapFile)
TypeAccess.Create(Context, Syntax.Type, this, 0);
}

public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, Extraction.Entities.Location location, string? value)
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, Location location, string? value)
{
var info = new ExpressionInfo(
cx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void AddOperatorCall(IMethodSymbol method)
/// Creates a new generated expression with an implicit conversion added.
/// </summary>
public static Expression CreateGeneratedConversion(Context cx, IExpressionParentEntity parent, int childIndex, ITypeSymbol type, object value,
Extraction.Entities.Location location)
Location location)
{
ExpressionInfo create(ExprKind kind, string? v) =>
new ExpressionInfo(
Expand Down Expand Up @@ -85,7 +85,7 @@ ExpressionInfo create(ExprKind kind, string? v) =>
/// Creates a new generated cast expression.
/// </summary>
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, ITypeSymbol type, object value,
Extraction.Entities.Location location)
Location location)
{
var info = new ExpressionInfo(cx,
AnnotatedTypeSymbol.CreateNotAnnotated(type),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected override void PopulateExpression(TextWriter trapFile)
}
}

public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int index, Extraction.Entities.Location location)
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int index, Location location)
{
var info = new ExpressionInfo(
cx,
Expand Down
Loading

0 comments on commit b7098b7

Please sign in to comment.