mirror of
https://github.com/github/codeql.git
synced 2026-04-23 07:45:17 +02:00
C#: Introduce extractor support for collection expressions.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using System.IO;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Semmle.Util;
|
||||
using Semmle.Extraction.Kinds;
|
||||
|
||||
namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
{
|
||||
internal class Collection : Expression<CollectionExpressionSyntax>
|
||||
{
|
||||
private Collection(ExpressionNodeInfo info) : base(info.SetKind(ExprKind.COLLECTION)) { }
|
||||
|
||||
protected override void PopulateExpression(TextWriter trapFile) =>
|
||||
Syntax.Elements.ForEach((element, i) =>
|
||||
{
|
||||
switch (element.Kind())
|
||||
{
|
||||
case SyntaxKind.ExpressionElement:
|
||||
{
|
||||
Create(Context, ((ExpressionElementSyntax)element).Expression, this, i);
|
||||
return;
|
||||
}
|
||||
case SyntaxKind.SpreadElement:
|
||||
{
|
||||
Spread.Create(Context, (SpreadElementSyntax)element, this, i);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
{
|
||||
Context.ModelError(element, $"Unhandled collection element type {element.Kind()}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public static Expression Create(ExpressionNodeInfo info) => new Collection(info).TryPopulate();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Semmle.Extraction.Kinds;
|
||||
|
||||
namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
{
|
||||
internal class Spread : Expression
|
||||
{
|
||||
public Spread(Context cx, SpreadElementSyntax syntax, IExpressionParentEntity parent, int child) :
|
||||
base(new ExpressionInfo(cx, null, cx.CreateLocation(syntax.GetLocation()), ExprKind.SPREAD_ELEMENT, parent, child, false, null))
|
||||
{
|
||||
Create(cx, syntax.Expression, this, 0);
|
||||
}
|
||||
|
||||
public static Expression Create(Context cx, SpreadElementSyntax syntax, IExpressionParentEntity parent, int child) =>
|
||||
new Spread(cx, syntax, parent, child);
|
||||
}
|
||||
}
|
||||
@@ -256,6 +256,9 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
case SyntaxKind.WithExpression:
|
||||
return WithExpression.Create(info);
|
||||
|
||||
case SyntaxKind.CollectionExpression:
|
||||
return Collection.Create(info);
|
||||
|
||||
default:
|
||||
info.Context.ModelError(info.Node, $"Unhandled expression '{info.Node}' of kind '{info.Node.Kind()}'");
|
||||
return new Unknown(info);
|
||||
|
||||
Reference in New Issue
Block a user