mirror of
https://github.com/github/codeql.git
synced 2026-04-30 03:05:15 +02:00
C#: Extract binary patterns
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Semmle.Extraction.Entities;
|
||||
using Semmle.Extraction.Kinds;
|
||||
|
||||
namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
{
|
||||
internal class BinaryPattern : Expression
|
||||
{
|
||||
public BinaryPattern(Context cx, BinaryPatternSyntax syntax, IExpressionParentEntity parent, int child) :
|
||||
base(new ExpressionInfo(cx, null, cx.Create(syntax.GetLocation()), GetKind(syntax.OperatorToken, syntax), parent, child, false, null))
|
||||
{
|
||||
Pattern.Create(cx, syntax.Left, this, 0);
|
||||
Pattern.Create(cx, syntax.Right, this, 1);
|
||||
}
|
||||
|
||||
private static ExprKind GetKind(SyntaxToken operatorToken, BinaryPatternSyntax syntax)
|
||||
{
|
||||
return operatorToken.Kind() switch
|
||||
{
|
||||
SyntaxKind.AndKeyword => ExprKind.AND_PATTERN,
|
||||
SyntaxKind.OrKeyword => ExprKind.OR_PATTERN,
|
||||
_ => throw new InternalError(syntax, $"Operator '{operatorToken.Kind()}' is not supported in binary patterns.")
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,9 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
case UnaryPatternSyntax unaryPattern:
|
||||
return new UnaryPattern(cx, unaryPattern, parent, child);
|
||||
|
||||
case BinaryPatternSyntax binaryPattern:
|
||||
return new BinaryPattern(cx, binaryPattern, parent, child);
|
||||
|
||||
case DeclarationPatternSyntax declPattern:
|
||||
// Creates a single local variable declaration.
|
||||
{
|
||||
|
||||
@@ -121,6 +121,8 @@ namespace Semmle.Extraction.Kinds
|
||||
LE_PATTERN = 124,
|
||||
GE_PATTERN = 125,
|
||||
NOT_PATTERN = 126,
|
||||
AND_PATTERN = 127,
|
||||
OR_PATTERN = 128,
|
||||
FUNCTION_POINTER_INVOCATION = 129,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user