C#: Handle VarPatternSyntax class introduced by Roslyn 3.0.0

This commit is contained in:
calum
2019-04-26 15:26:15 +01:00
parent 8a78c8f124
commit c28fa7ed3f
2 changed files with 52 additions and 33 deletions

View File

@@ -8,36 +8,43 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
{
class IsPattern : Expression<IsPatternExpressionSyntax>
{
IsPattern(ExpressionNodeInfo info) : base(info.SetKind(ExprKind.IS))
private IsPattern(ExpressionNodeInfo info) : base(info.SetKind(ExprKind.IS))
{
}
private void PopulatePattern(PatternSyntax pattern, TypeSyntax optionalType, SyntaxToken varKeyword, VariableDesignationSyntax designation)
{
bool isVar = optionalType is null;
if (!isVar)
Expressions.TypeAccess.Create(cx, optionalType, this, 1);
if (cx.Model(pattern).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
{
var type = Type.Create(cx, symbol.Type);
if (isVar)
new Expression(new ExpressionInfo(cx, type, cx.Create(varKeyword.GetLocation()), ExprKind.TYPE_ACCESS, this, 1, false, null));
VariableDeclaration.Create(cx, symbol, type, cx.Create(pattern.GetLocation()), cx.Create(designation.GetLocation()), isVar, this, 2);
}
}
protected override void Populate()
{
var constantPattern = Syntax.Pattern as ConstantPatternSyntax;
if (constantPattern != null)
{
Create(cx, Syntax.Expression, this, 0);
Create(cx, constantPattern.Expression, this, 3);
return;
}
var pattern = Syntax.Pattern as DeclarationPatternSyntax;
if (pattern == null)
{
throw new InternalError(Syntax, "Is-pattern not handled");
}
Create(cx, Syntax.Expression, this, 0);
TypeAccess.Create(cx, pattern.Type, this, 1);
var symbol = cx.Model(Syntax).GetDeclaredSymbol(pattern.Designation) as ILocalSymbol;
if (symbol != null)
switch (Syntax.Pattern)
{
var type = Type.Create(cx, symbol.Type);
var isVar = pattern.Type.IsVar;
VariableDeclaration.Create(cx, symbol, type, cx.Create(pattern.GetLocation()), cx.Create(pattern.Designation.GetLocation()), isVar, this, 2);
case ConstantPatternSyntax constantPattern:
Create(cx, constantPattern.Expression, this, 3);
return;
case VarPatternSyntax varPattern:
PopulatePattern(varPattern, null, varPattern.VarKeyword, varPattern.Designation);
return;
case DeclarationPatternSyntax declPattern:
PopulatePattern(declPattern, declPattern.Type, default(SyntaxToken), declPattern.Designation);
return;
default:
throw new InternalError(Syntax, "Is pattern not handled");
}
}

View File

@@ -64,23 +64,35 @@ namespace Semmle.Extraction.CSharp.Entities.Statements
class CasePattern : Case<CasePatternSwitchLabelSyntax>
{
CasePattern(Context cx, CasePatternSwitchLabelSyntax node, Switch parent, int child)
private CasePattern(Context cx, CasePatternSwitchLabelSyntax node, Switch parent, int child)
: base(cx, node, parent, child) { }
private void PopulatePattern(PatternSyntax pattern, TypeSyntax optionalType, SyntaxToken varKeyword, VariableDesignationSyntax designation)
{
var isVar = optionalType is null;
if(!isVar)
Expressions.TypeAccess.Create(cx, optionalType, this, 1);
if (cx.Model(pattern).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
{
var type = Type.Create(cx, symbol.Type);
if (isVar)
new Expression(new ExpressionInfo(cx, type, cx.Create(varKeyword.GetLocation()), ExprKind.TYPE_ACCESS, this, 1, false, null));
Expressions.VariableDeclaration.Create(cx, symbol, type, cx.Create(pattern.GetLocation()), cx.Create(designation.GetLocation()), isVar, this, 0);
}
}
protected override void Populate()
{
switch(Stmt.Pattern)
{
case VarPatternSyntax varPattern:
PopulatePattern(varPattern, null, varPattern.VarKeyword, varPattern.Designation);
break;
case DeclarationPatternSyntax declarationPattern:
var symbol = cx.Model(Stmt).GetDeclaredSymbol(declarationPattern.Designation) as ILocalSymbol;
if (symbol != null)
{
var type = Type.Create(cx, symbol.Type);
var isVar = declarationPattern.Type.IsVar;
Expressions.VariableDeclaration.Create(cx, symbol, type, cx.Create(declarationPattern.GetLocation()), cx.Create(declarationPattern.Designation.GetLocation()), isVar, this, 0);
}
Expressions.TypeAccess.Create(cx, declarationPattern.Type, this, 1);
PopulatePattern(declarationPattern, declarationPattern.Type, default(SyntaxToken), declarationPattern.Designation);
break;
case ConstantPatternSyntax pattern:
Expression.Create(cx, pattern.Expression, this, 0);