mirror of
https://github.com/github/codeql.git
synced 2026-04-30 19:26:02 +02:00
C#: Add extractor support for list- and slice patterns.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Semmle.Extraction.Kinds;
|
||||
using Semmle.Util;
|
||||
|
||||
namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
{
|
||||
internal class ListPattern : Expression
|
||||
{
|
||||
internal ListPattern(Context cx, ListPatternSyntax syntax, IExpressionParentEntity parent, int child) :
|
||||
base(new ExpressionInfo(cx, null, cx.CreateLocation(syntax.GetLocation()), ExprKind.LIST_PATTERN, parent, child, false, null))
|
||||
{
|
||||
syntax.Patterns.ForEach((p, i) => Pattern.Create(cx, p, this, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,6 +74,12 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
case DiscardPatternSyntax dp:
|
||||
return new Discard(cx, dp, parent, child);
|
||||
|
||||
case ListPatternSyntax listPattern:
|
||||
return new ListPattern(cx, listPattern, parent, child);
|
||||
|
||||
case SlicePatternSyntax slicePattern:
|
||||
return new SlicePattern(cx, slicePattern, parent, child);
|
||||
|
||||
default:
|
||||
throw new InternalError(syntax, "Pattern not handled");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Semmle.Extraction.Kinds;
|
||||
|
||||
namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
{
|
||||
internal class SlicePattern : Expression
|
||||
{
|
||||
public SlicePattern(Context cx, SlicePatternSyntax syntax, IExpressionParentEntity parent, int child) :
|
||||
base(new ExpressionInfo(cx, null, cx.CreateLocation(syntax.GetLocation()), ExprKind.SLICE_PATTERN, parent, child, false, null))
|
||||
{
|
||||
if (syntax.Pattern is not null)
|
||||
{
|
||||
Pattern.Create(cx, syntax.Pattern, this, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,6 +79,19 @@ namespace Semmle.Util
|
||||
a(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the action <paramref name="a"/> to each item and its index in this collection.
|
||||
/// </summary>
|
||||
public static void ForEach<T>(this IEnumerable<T> items, Action<T, int> a)
|
||||
{
|
||||
var i = 0;
|
||||
foreach (var item in items)
|
||||
{
|
||||
a(item, i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Forces enumeration of this collection and discards the result.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user